1 package org.apache.maven.continuum.web.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.continuum.configuration.BuildAgentConfigurationException;
23 import org.apache.continuum.model.repository.LocalRepository;
24 import org.apache.continuum.release.config.ContinuumReleaseDescriptor;
25 import org.apache.continuum.release.distributed.DistributedReleaseUtil;
26 import org.apache.continuum.release.distributed.manager.DistributedReleaseManager;
27 import org.apache.continuum.utils.release.ReleaseUtil;
28 import org.apache.continuum.web.action.AbstractReleaseAction;
29 import org.apache.continuum.web.util.AuditLog;
30 import org.apache.continuum.web.util.AuditLogConstants;
31 import org.apache.maven.continuum.ContinuumException;
32 import org.apache.maven.continuum.model.project.Project;
33 import org.apache.maven.continuum.model.system.Profile;
34 import org.apache.maven.continuum.release.ContinuumReleaseManager;
35 import org.apache.maven.continuum.release.ContinuumReleaseManagerListener;
36 import org.apache.maven.continuum.release.DefaultReleaseManagerListener;
37 import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
38 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
39 import org.apache.maven.shared.release.ReleaseResult;
40 import org.codehaus.plexus.util.StringUtils;
41
42 import java.io.File;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48
49
50
51
52
53 public class ReleasePerformAction
54 extends AbstractReleaseAction
55 {
56 private int projectId;
57
58 private String releaseId;
59
60 private String scmUrl;
61
62 private String scmUsername;
63
64 private String scmPassword;
65
66 private String scmTag;
67
68 private String scmTagBase;
69
70 private String goals = "clean deploy";
71
72 private String arguments;
73
74 private boolean useReleaseProfile = true;
75
76 private ContinuumReleaseManagerListener listener;
77
78 private ReleaseResult result;
79
80 private String projectGroupName = "";
81
82 private List<Profile> profiles;
83
84 private int profileId;
85
86 private void init()
87 throws Exception
88 {
89 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
90 {
91 DistributedReleaseManager distributedReleaseManager = getContinuum().getDistributedReleaseManager();
92
93 getReleasePluginParameters( distributedReleaseManager.getReleasePluginParameters( projectId, "pom.xml" ) );
94 }
95 else
96 {
97 Project project = getContinuum().getProject( projectId );
98
99 String workingDirectory = getContinuum().getWorkingDirectory( project.getId() ).getPath();
100
101 getReleasePluginParameters( workingDirectory, "pom.xml" );
102 }
103 }
104
105 public String inputFromScm()
106 throws Exception
107 {
108 try
109 {
110 checkBuildProjectInGroupAuthorization( getProjectGroupName() );
111 }
112 catch ( AuthorizationRequiredException e )
113 {
114 return REQUIRES_AUTHORIZATION;
115 }
116
117 try
118 {
119 init();
120 }
121 catch ( BuildAgentConfigurationException e )
122 {
123 List<Object> args = new ArrayList<Object>();
124 args.add( e.getMessage() );
125
126 addActionError( getText( "distributedBuild.releasePerform.input.error", args ) );
127 return RELEASE_ERROR;
128 }
129
130 populateFromProject();
131
132 releaseId = "";
133
134 profiles = this.getContinuum().getProfileService().getAllProfiles();
135
136 return SUCCESS;
137 }
138
139 public String input()
140 throws Exception
141 {
142 try
143 {
144 checkBuildProjectInGroupAuthorization( getProjectGroupName() );
145 }
146 catch ( AuthorizationRequiredException e )
147 {
148 return REQUIRES_AUTHORIZATION;
149 }
150
151 try
152 {
153 init();
154 }
155 catch ( BuildAgentConfigurationException e )
156 {
157 List<Object> args = new ArrayList<Object>();
158 args.add( e.getMessage() );
159
160 addActionError( getText( "distributedBuild.releasePerform.input.error", args ) );
161 return RELEASE_ERROR;
162 }
163
164 return SUCCESS;
165 }
166
167
168
169
170
171 private void getReleasePluginParameters( String workingDirectory, String pomFilename )
172 throws Exception
173 {
174 Map<String, Object> params = ReleaseUtil.getReleasePluginParameters( workingDirectory, pomFilename );
175
176 if ( params.get( "use-release-profile" ) != null )
177 {
178 useReleaseProfile = (Boolean) params.get( "use-release-profile" );
179 }
180
181 if ( params.get( "perform-goals" ) != null )
182 {
183 goals = (String) params.get( "perform-goals" );
184 }
185
186 if ( params.get( "arguments" ) != null )
187 {
188 arguments = (String) params.get( "arguments" );
189 }
190 }
191
192 public String execute()
193 throws Exception
194 {
195 try
196 {
197 checkBuildProjectInGroupAuthorization( getProjectGroupName() );
198 }
199 catch ( AuthorizationRequiredException e )
200 {
201 return REQUIRES_AUTHORIZATION;
202 }
203
204 Project project = getContinuum().getProject( projectId );
205
206 LocalRepository repository = project.getProjectGroup().getLocalRepository();
207
208 String username = getPrincipal();
209
210 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
211 {
212 DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager();
213
214 try
215 {
216 releaseManager.releasePerform( projectId, releaseId, goals, arguments, useReleaseProfile, repository,
217 username );
218 }
219 catch ( BuildAgentConfigurationException e )
220 {
221 List<Object> args = new ArrayList<Object>();
222 args.add( e.getMessage() );
223
224 addActionError( getText( "distributedBuild.releasePerform.release.error", args ) );
225 return RELEASE_ERROR;
226 }
227 }
228 else
229 {
230 listener = new DefaultReleaseManagerListener();
231
232 listener.setUsername( username );
233
234 ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager();
235
236
237 File performDirectory = new File( getContinuum().getConfiguration().getWorkingDirectory(),
238 "releases-" + System.currentTimeMillis() );
239 performDirectory.mkdirs();
240
241 releaseManager.perform( releaseId, performDirectory, goals, arguments, useReleaseProfile, listener,
242 repository );
243 }
244
245 AuditLog event = new AuditLog( "ReleaseId=" + releaseId, AuditLogConstants.PERFORM_RELEASE );
246 event.setCategory( AuditLogConstants.PROJECT );
247 event.setCurrentUser( username );
248 event.log();
249
250 return SUCCESS;
251 }
252
253 public String executeFromScm()
254 throws Exception
255 {
256 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
257 {
258 Project project = getContinuum().getProject( projectId );
259
260 LocalRepository repository = project.getProjectGroup().getLocalRepository();
261
262 DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager();
263
264 Profile profile = null;
265 if ( profileId != -1 )
266 {
267 profile = getContinuum().getProfileService().getProfile( profileId );
268 }
269 Map<String, String> environments =
270 getEnvironments( profile, releaseManager.getDefaultBuildagent( projectId ) );
271
272 try
273 {
274 releaseId = releaseManager.releasePerformFromScm( projectId, goals, arguments, useReleaseProfile,
275 repository, scmUrl, scmUsername, scmPassword, scmTag,
276 scmTagBase, environments, getPrincipal() );
277 }
278 catch ( BuildAgentConfigurationException e )
279 {
280 List<Object> args = new ArrayList<Object>();
281 args.add( e.getMessage() );
282
283 addActionError( getText( "distributedBuild.releasePerform.release.error", args ) );
284 return RELEASE_ERROR;
285 }
286
287 return SUCCESS;
288 }
289 else
290 {
291 ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager();
292
293 ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
294 descriptor.setScmSourceUrl( scmUrl );
295 descriptor.setScmUsername( scmUsername );
296 descriptor.setScmPassword( scmPassword );
297 descriptor.setScmReleaseLabel( scmTag );
298 descriptor.setScmTagBase( scmTagBase );
299
300 if ( profileId != -1 )
301 {
302 Profile profile = getContinuum().getProfileService().getProfile( profileId );
303 descriptor.setEnvironments( getEnvironments( profile, null ) );
304 }
305
306 do
307 {
308 releaseId = String.valueOf( System.currentTimeMillis() );
309 }
310 while ( releaseManager.getPreparedReleases().containsKey( releaseId ) );
311
312 releaseManager.getPreparedReleases().put( releaseId, descriptor );
313
314 return execute();
315 }
316 }
317
318 private void populateFromProject()
319 throws Exception
320 {
321 Project project = getContinuum().getProjectWithAllDetails( projectId );
322
323 scmUrl = project.getScmUrl();
324 scmUsername = project.getScmUsername();
325 scmPassword = project.getScmPassword();
326
327 if ( scmUrl.startsWith( "scm:svn:" ) )
328 {
329 scmTagBase = new SvnScmProviderRepository( scmUrl, scmUsername, scmPassword ).getTagBase();
330 }
331 else
332 {
333 scmTagBase = "";
334 }
335
336 releaseId = "";
337 }
338
339 private void getReleasePluginParameters( Map context )
340 {
341 useReleaseProfile = DistributedReleaseUtil.getUseReleaseProfile( context, useReleaseProfile );
342
343 if ( StringUtils.isNotEmpty( DistributedReleaseUtil.getPerformGoals( context, goals ) ) )
344 {
345 goals = DistributedReleaseUtil.getPerformGoals( context, goals );
346 }
347
348 if ( StringUtils.isNotEmpty( DistributedReleaseUtil.getArguments( context, "" ) ) )
349 {
350 arguments = DistributedReleaseUtil.getArguments( context, "" );
351 }
352 }
353
354 public String getReleaseId()
355 {
356 return releaseId;
357 }
358
359 public void setReleaseId( String releaseId )
360 {
361 this.releaseId = releaseId;
362 }
363
364 public String getScmUrl()
365 {
366 return scmUrl;
367 }
368
369 public void setScmUrl( String scmUrl )
370 {
371 this.scmUrl = scmUrl;
372 }
373
374 public String getScmUsername()
375 {
376 return scmUsername;
377 }
378
379 public void setScmUsername( String scmUsername )
380 {
381 this.scmUsername = scmUsername;
382 }
383
384 public String getScmPassword()
385 {
386 return scmPassword;
387 }
388
389 public void setScmPassword( String scmPassword )
390 {
391 this.scmPassword = scmPassword;
392 }
393
394 public String getScmTag()
395 {
396 return scmTag;
397 }
398
399 public void setScmTag( String scmTag )
400 {
401 this.scmTag = scmTag;
402 }
403
404 public String getScmTagBase()
405 {
406 return scmTagBase;
407 }
408
409 public void setScmTagBase( String scmTagBase )
410 {
411 this.scmTagBase = scmTagBase;
412 }
413
414 public String getGoals()
415 {
416 return goals;
417 }
418
419 public void setGoals( String goals )
420 {
421 this.goals = goals;
422 }
423
424 public boolean isUseReleaseProfile()
425 {
426 return useReleaseProfile;
427 }
428
429 public void setUseReleaseProfile( boolean useReleaseProfile )
430 {
431 this.useReleaseProfile = useReleaseProfile;
432 }
433
434 public ContinuumReleaseManagerListener getListener()
435 {
436 return listener;
437 }
438
439 public void setListener( ContinuumReleaseManagerListener listener )
440 {
441 this.listener = listener;
442 }
443
444 public ReleaseResult getResult()
445 {
446 return result;
447 }
448
449 public void setResult( ReleaseResult result )
450 {
451 this.result = result;
452 }
453
454 public int getProjectId()
455 {
456 return projectId;
457 }
458
459 public void setProjectId( int projectId )
460 {
461 this.projectId = projectId;
462 }
463
464 public String getProjectGroupName()
465 throws ContinuumException
466 {
467 if ( projectGroupName == null || "".equals( projectGroupName ) )
468 {
469 projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
470 }
471
472 return projectGroupName;
473 }
474
475 public List<Profile> getProfiles()
476 {
477 return profiles;
478 }
479
480 public void setProfiles( List<Profile> profiles )
481 {
482 this.profiles = profiles;
483 }
484
485 public int getProfileId()
486 {
487 return profileId;
488 }
489
490 public void setProfileId( int profileId )
491 {
492 this.profileId = profileId;
493 }
494
495 public String getArguments()
496 {
497 return arguments;
498 }
499
500 public void setArguments( String arguments )
501 {
502 this.arguments = arguments;
503 }
504 }