View Javadoc

1   package org.apache.continuum.buildagent.manager;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
23  import org.apache.continuum.buildagent.installation.BuildAgentInstallationService;
24  import org.apache.continuum.buildagent.model.Installation;
25  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
26  import org.apache.continuum.model.repository.LocalRepository;
27  import org.apache.continuum.release.config.ContinuumReleaseDescriptor;
28  import org.apache.maven.continuum.model.project.Project;
29  import org.apache.maven.continuum.model.project.ProjectGroup;
30  import org.apache.maven.continuum.release.ContinuumReleaseException;
31  import org.apache.maven.continuum.release.ContinuumReleaseManager;
32  import org.apache.maven.continuum.release.ContinuumReleaseManagerListener;
33  import org.apache.maven.continuum.release.DefaultReleaseManagerListener;
34  import org.apache.maven.shared.release.ReleaseResult;
35  import org.apache.maven.shared.release.config.ReleaseDescriptor;
36  import org.codehaus.plexus.util.StringUtils;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import java.io.File;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Properties;
45  
46  /**
47   * @plexus.component role="org.apache.continuum.buildagent.manager.BuildAgentReleaseManager" role-hint="default"
48   */
49  public class DefaultBuildAgentReleaseManager
50      implements BuildAgentReleaseManager
51  {
52      private static final Logger log = LoggerFactory.getLogger( DefaultBuildAgentReleaseManager.class );
53  
54      /**
55       * @plexus.requirement
56       */
57      ContinuumReleaseManager releaseManager;
58  
59      /**
60       * @plexus.requirement
61       */
62      BuildAgentConfigurationService buildAgentConfigurationService;
63  
64      /**
65       * @plexus.requirement
66       */
67      BuildAgentInstallationService buildAgentInstallationService;
68  
69      public String releasePrepare( Map<String, Object> projectMap, Properties releaseProperties,
70                                    Map<String, String> releaseVersion, Map<String, String> developmentVersion,
71                                    Map<String, String> environments, String username )
72          throws ContinuumReleaseException
73      {
74          Project project = getProject( projectMap );
75  
76          ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
77  
78          listener.setUsername( username );
79  
80          String workingDirectory = buildAgentConfigurationService.getWorkingDirectory( project.getId() ).getPath();
81  
82          String executable = buildAgentInstallationService.getExecutorConfigurator(
83              BuildAgentInstallationService.MAVEN2_TYPE ).getExecutable();
84  
85          if ( environments == null )
86          {
87              environments = new HashMap<String, String>();
88          }
89  
90          // get environments from Slave (Build Agent)
91          List<Installation> installations = buildAgentConfigurationService.getAvailableInstallations();
92  
93          if ( installations != null )
94          {
95              for ( Installation installation : installations )
96              {
97                  // combine environments (Master and Slave); Slave's environments overwrite Master's environments
98                  environments.put( installation.getVarName(), installation.getVarValue() );
99              }
100         }
101 
102         if ( environments != null )
103         {
104             String m2Home = environments.get( buildAgentInstallationService.getEnvVar(
105                 BuildAgentInstallationService.MAVEN2_TYPE ) );
106             if ( StringUtils.isNotEmpty( m2Home ) )
107             {
108                 executable = m2Home + File.separator + "bin" + File.separator + executable;
109             }
110         }
111 
112         try
113         {
114             return releaseManager.prepare( project, releaseProperties, releaseVersion, developmentVersion, listener,
115                                            workingDirectory, environments, executable );
116         }
117         catch ( ContinuumReleaseException e )
118         {
119             log.error( "Error while preparing release", e );
120             throw e;
121         }
122     }
123 
124     public ReleaseResult getReleaseResult( String releaseId )
125     {
126         return (ReleaseResult) releaseManager.getReleaseResults().get( releaseId );
127     }
128 
129     public Map<String, Object> getListener( String releaseId )
130     {
131         ContinuumReleaseManagerListener listener = (ContinuumReleaseManagerListener) releaseManager.getListeners().get(
132             releaseId );
133 
134         Map<String, Object> map = new HashMap<String, Object>();
135 
136         if ( listener != null )
137         {
138             map.put( ContinuumBuildAgentUtil.KEY_RELEASE_STATE, listener.getState() );
139 
140             map.put( ContinuumBuildAgentUtil.KEY_USERNAME, listener.getUsername() );
141 
142             if ( listener.getPhases() != null )
143             {
144                 map.put( ContinuumBuildAgentUtil.KEY_RELEASE_PHASES, listener.getPhases() );
145             }
146             if ( listener.getCompletedPhases() != null )
147             {
148                 map.put( ContinuumBuildAgentUtil.KEY_COMPLETED_RELEASE_PHASES, listener.getCompletedPhases() );
149             }
150             if ( listener.getInProgress() != null )
151             {
152                 map.put( ContinuumBuildAgentUtil.KEY_RELEASE_IN_PROGRESS, listener.getInProgress() );
153             }
154             if ( listener.getError() != null )
155             {
156                 map.put( ContinuumBuildAgentUtil.KEY_RELEASE_ERROR, listener.getError() );
157             }
158         }
159 
160         return map;
161     }
162 
163     public void removeListener( String releaseId )
164     {
165         releaseManager.getListeners().remove( releaseId );
166     }
167 
168     public String getPreparedReleaseName( String releaseId )
169     {
170         Map preparedReleases = releaseManager.getPreparedReleases();
171 
172         if ( preparedReleases.containsKey( releaseId ) )
173         {
174             ReleaseDescriptor descriptor = (ReleaseDescriptor) preparedReleases.get( releaseId );
175             return descriptor.getReleaseVersions().get( releaseId ).toString();
176         }
177 
178         return "";
179     }
180 
181     @SuppressWarnings( "unchecked" )
182     public void releasePerform( String releaseId, String goals, String arguments, boolean useReleaseProfile,
183                                 Map repository, String username )
184         throws ContinuumReleaseException
185     {
186         ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
187 
188         listener.setUsername( username );
189 
190         LocalRepository repo = null;
191 
192         if ( !repository.isEmpty() )
193         {
194             List<org.apache.continuum.buildagent.model.LocalRepository> localRepos =
195                 buildAgentConfigurationService.getLocalRepositories();
196             for ( org.apache.continuum.buildagent.model.LocalRepository localRepo : localRepos )
197             {
198                 if ( localRepo.getName().equalsIgnoreCase( ContinuumBuildAgentUtil.getLocalRepositoryName(
199                     repository ) ) )
200                 {
201                     repo = new LocalRepository();
202                     repo.setLayout( localRepo.getLayout() );
203                     repo.setName( localRepo.getName() );
204                     repo.setLocation( localRepo.getLocation() );
205 
206                     break;
207                 }
208             }
209         }
210 
211         File performDirectory = new File( buildAgentConfigurationService.getWorkingDirectory(),
212                                           "releases-" + System.currentTimeMillis() );
213         performDirectory.mkdirs();
214 
215         releaseManager.perform( releaseId, performDirectory, goals, arguments, useReleaseProfile, listener, repo );
216     }
217 
218     public String releasePerformFromScm( String goals, String arguments, boolean useReleaseProfile, Map repository,
219                                          String scmUrl, String scmUsername, String scmPassword, String scmTag,
220                                          String scmTagBase, Map<String, String> environments, String username )
221         throws ContinuumReleaseException
222     {
223         ContinuumReleaseDescriptor descriptor = new ContinuumReleaseDescriptor();
224         descriptor.setScmSourceUrl( scmUrl );
225         descriptor.setScmUsername( scmUsername );
226         descriptor.setScmPassword( scmPassword );
227         descriptor.setScmReleaseLabel( scmTag );
228         descriptor.setScmTagBase( scmTagBase );
229         descriptor.setEnvironments( environments );
230 
231         String releaseId = "";
232 
233         do
234         {
235             releaseId = String.valueOf( System.currentTimeMillis() );
236         }
237         while ( releaseManager.getPreparedReleases().containsKey( releaseId ) );
238 
239         releaseManager.getPreparedReleases().put( releaseId, descriptor );
240 
241         releasePerform( releaseId, goals, arguments, useReleaseProfile, repository, username );
242 
243         return releaseId;
244     }
245 
246     public String releaseCleanup( String releaseId )
247     {
248         releaseManager.getReleaseResults().remove( releaseId );
249 
250         ContinuumReleaseManagerListener listener =
251             (ContinuumReleaseManagerListener) releaseManager.getListeners().remove( releaseId );
252 
253         if ( listener != null )
254         {
255             return listener.getGoalName() + "Finished";
256         }
257         else
258         {
259             return "";
260         }
261     }
262 
263     public void releaseRollback( String releaseId, int projectId )
264         throws ContinuumReleaseException
265     {
266         ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
267 
268         releaseManager.rollback( releaseId, buildAgentConfigurationService.getWorkingDirectory( projectId ).getPath(),
269                                  listener );
270 
271         //recurse until rollback is finished
272         while ( listener.getState() != ContinuumReleaseManagerListener.FINISHED )
273         {
274             try
275             {
276                 Thread.sleep( 1000 );
277             }
278             catch ( InterruptedException e )
279             {
280                 //do nothing
281             }
282         }
283 
284         releaseManager.getPreparedReleases().remove( releaseId );
285 
286         if ( StringUtils.isNotBlank( listener.getError() ) )
287         {
288             throw new ContinuumReleaseException( "Failed to rollback release: " + listener.getError() );
289         }
290     }
291 
292     private Project getProject( Map<String, Object> context )
293     {
294         Project project = new Project();
295 
296         project.setId( ContinuumBuildAgentUtil.getProjectId( context ) );
297         project.setGroupId( ContinuumBuildAgentUtil.getGroupId( context ) );
298         project.setArtifactId( ContinuumBuildAgentUtil.getArtifactId( context ) );
299         project.setScmUrl( ContinuumBuildAgentUtil.getScmUrl( context ) );
300 
301         ProjectGroup group = new ProjectGroup();
302 
303         String localRepo = ContinuumBuildAgentUtil.getLocalRepositoryName( context );
304 
305         if ( StringUtils.isBlank( localRepo ) )
306         {
307             group.setLocalRepository( null );
308         }
309         else
310         {
311             LocalRepository localRepository = new LocalRepository();
312             List<org.apache.continuum.buildagent.model.LocalRepository> localRepos =
313                 buildAgentConfigurationService.getLocalRepositories();
314             for ( org.apache.continuum.buildagent.model.LocalRepository localRepoBA : localRepos )
315             {
316                 if ( localRepoBA.getName().equalsIgnoreCase( localRepo ) )
317                 {
318                     localRepository.setLocation( localRepoBA.getLocation() );
319                     group.setLocalRepository( localRepository );
320                     break;
321                 }
322             }
323         }
324 
325         project.setProjectGroup( group );
326 
327         return project;
328     }
329 
330     public void setBuildAgentConfigurationService( BuildAgentConfigurationService buildAgentConfigurationService )
331     {
332         this.buildAgentConfigurationService = buildAgentConfigurationService;
333     }
334 
335     public ContinuumReleaseManager getReleaseManager()
336     {
337         return releaseManager;
338     }
339 }