View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ansible;
35  
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Locale;
40  
41  import org.apache.commons.lang.BooleanUtils;
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.AbstractWorkflowDeployContext;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployScriptContext;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ActionParameter;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXExecuteResult;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXJobResult;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXParameters;
53  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXService;
54  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr.CloudmgrUtils;
55  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
56  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
57  import fr.paris.lutece.portal.service.i18n.I18nService;
58  import fr.paris.lutece.portal.service.util.AppLogService;
59  import fr.paris.lutece.portal.service.util.AppPropertiesService;
60  import net.sf.json.JSONArray;
61  import net.sf.json.JSONObject;
62  
63  /**
64   *
65   * ExecuteAction
66   *
67   */
68  public class ExecuteSqlAction extends DefaultAction
69  {
70      private static final String MESSAGE_STD_OUT   = "module.appcenter.fastdeployapplication.deploy_action.stdout.filetoolong";
71      private static final String MESSAGE_STD_ERROR = "module.appcenter.fastdeployapplication.deploy_action.stderr.filetoolong";
72      public  static final String PARAM_ID_JOBS_AWX = "idJobsAWX";
73      @Override
74      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult, ActionParameter... parameter )
75      {
76  
77          String strResult = null;
78  
79          String strDataBase = null;
80          String strScriptName = null;
81          String strDiskRequiredFreeSpace=null;
82     
83          if(parameter!=null && parameter.length>0)
84          {
85          	
86  	        for ( int i = 0; i < parameter.length; i++ )
87  	        {
88  	
89  	            if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
90  	            {
91  	
92  	                strDataBase = parameter [i].getValue( );
93  	            }
94  	            if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_SCRIPT_NAME ) )
95  	            {
96  	
97  	                strScriptName = parameter [i].getValue( );
98  	            }
99  	            else if(parameter [i].getName( ).equals( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE ) )
100                 {
101                     
102                     strDiskRequiredFreeSpace=parameter [i].getValue( );
103                 }
104 	        }
105         }
106 
107         if ( !StringUtils.isEmpty( strDataBase ) && !StringUtils.isEmpty( strScriptName ) )
108         {
109         	 
110             if(strScriptName.endsWith( ".sql" ))
111             {
112                 strScriptName=strScriptName.substring( 0,strScriptName.length( )-4 );
113             }
114             ExecuteActionParameters/fastdeployapplication/business/fastaction/ansible/ExecuteActionParameters.html#ExecuteActionParameters">ExecuteActionParameters executeActionParameter=new ExecuteActionParameters(strDataBase,strScriptName,strDiskRequiredFreeSpace!=null?new Integer( strDiskRequiredFreeSpace ):null);
115              
116              
117              AWXJobResult jobResult= AWXService.getService( ).launchJob( this.getTechnicalCode( ),new AWXParameters( serverApplicationInstance.getHostName( ), executeActionParameter) );
118              
119              if(jobResult==null && commandResult != null)
120              {
121                  
122                  DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult,  serverApplicationInstance.getHostName( )));     
123              }
124              else
125              {
126                 if(jobResult.isRunning( ))
127                 {
128                     
129                     jobResult= refreshJobInformations(jobResult.getUrl( ));
130                     
131                 }
132                 
133                 if(jobResult.isFailed( ))
134                 {
135                     
136                   
137                     DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult,  serverApplicationInstance.getHostName( )));
138                 }
139                 else 
140                 {
141                   if(jobResult.getResult()!=null)
142                 	  {
143                 	  if( jobResult.getId() != 0 )
144                 	  {
145                 		  commandResult.getResultInformations().put(PARAM_ID_JOBS_AWX, String.valueOf( jobResult.getId() ));
146                 	  }
147                       int nMaxLogSize = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_MAX_LOG_SIZE, 500000);
148                       if( StringUtils.isNotBlank(jobResult.getResult().getStdout( )) && jobResult.getResult().getStdout( ).length() > nMaxLogSize )
149                       {
150                           commandResult.getLog( ).append( "**************stdout******************* \n" );
151                           String strMessageOutToLog = I18nService.getLocalizedString( MESSAGE_STD_OUT, Locale.getDefault());
152                           commandResult.getLog( ).append(strMessageOutToLog + "\n");
153                           commandResult.getLog( ).append( "*************************************** \n" );
154                       } else if ( StringUtils.isNotBlank(jobResult.getResult().getStdout( )) )
155                       {
156                           commandResult.getLog( ).append( "**************stdout******************* \n" );
157                           commandResult.getLog( ).append(jobResult.getResult().getStdout( ));
158                           commandResult.getLog( ).append( "*************************************** \n" );
159                       }
160                       
161                       if( StringUtils.isNotBlank( jobResult.getResult().getStderr( ) ) && jobResult.getResult().getStderr( ).length() > nMaxLogSize )
162                       {
163                           commandResult.getLog( ).append( "**************stdError******************* \n" );
164                           String strMessageErrToLog = I18nService.getLocalizedString( MESSAGE_STD_ERROR , Locale.getDefault());
165                           commandResult.getLog( ).append(strMessageErrToLog + "\n");
166                           commandResult.getLog( ).append( "*************************************** \n" );
167                       } else if ( StringUtils.isNotBlank( jobResult.getResult().getStderr( ) ) )
168                       {
169                           commandResult.getLog( ).append( "**************stdError******************* \n" );
170                           commandResult.getLog( ).append(jobResult.getResult().getStderr( ));
171                           commandResult.getLog( ).append( "*************************************** \n" );
172                       }
173 
174                       strResult= BooleanUtils.toStringTrueFalse(!jobResult.getResult().isExecutionFailed( ));
175                 	  }
176                 	  else
177                 	  {
178                 	      DeploymentUtils.addTechnicalError( commandResult, "No result found after execution script" );
179                 	  }
180 
181                 }
182                     
183                 
184                
185  
186         
187         
188         }
189         }
190 
191         return strResult;
192     }
193     
194     @Override
195     public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
196     {
197 
198         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
199         ActionParameter actionParameter;
200        
201         actionParameter = new ActionParameter( );
202         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
203         actionParameter.setValue( ((WorkflowDeployScriptContext)context).getDatabaseName( ) );
204         listActionParameters.add( actionParameter );
205         
206         actionParameter = new ActionParameter( );
207         actionParameter.setName( ConstanteUtils.PARAM_SCRIPT_NAME );
208         actionParameter.setValue( ((WorkflowDeployScriptContext)context).getScriptFileItemName( ) );
209         listActionParameters.add( actionParameter );
210 
211         
212         if(context.getDiskRequiredFreespace( )!=null)
213         {
214             actionParameter = new ActionParameter( );
215             actionParameter.setName( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE );
216             actionParameter.setValue(Integer.toString( context.getDiskRequiredFreespace( ) ));
217             listActionParameters.add( actionParameter );
218         }
219         
220         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
221     }
222     
223    
224 }