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  
40  import org.apache.commons.lang.BooleanUtils;
41  import org.apache.commons.lang.StringUtils;
42  
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.AbstractWorkflowDeployContext;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployScriptContext;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ActionParameter;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXExecuteResult;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXJobResult;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXParameters;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXService;
53  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr.CloudmgrUtils;
54  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
55  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
56  import fr.paris.lutece.portal.service.util.AppLogService;
57  import fr.paris.lutece.portal.service.util.AppPropertiesService;
58  import net.sf.json.JSONArray;
59  import net.sf.json.JSONObject;
60  
61  /**
62   *
63   * ExecuteAction
64   *
65   */
66  public class ExecuteSqlAction extends DefaultAction
67  {
68      @Override
69      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult, ActionParameter... parameter )
70      {
71  
72          String strResult = null;
73  
74          String strDataBase = null;
75          String strScriptName = null;
76          String strDiskRequiredFreeSpace=null;
77     
78          if(parameter!=null && parameter.length>0)
79          {
80          	
81  	        for ( int i = 0; i < parameter.length; i++ )
82  	        {
83  	
84  	            if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
85  	            {
86  	
87  	                strDataBase = parameter [i].getValue( );
88  	            }
89  	            if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_SCRIPT_NAME ) )
90  	            {
91  	
92  	                strScriptName = parameter [i].getValue( );
93  	            }
94  	            else if(parameter [i].getName( ).equals( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE ) )
95                  {
96                      
97                      strDiskRequiredFreeSpace=parameter [i].getValue( );
98                  }
99  	        }
100         }
101 
102         if ( !StringUtils.isEmpty( strDataBase ) && !StringUtils.isEmpty( strScriptName ) )
103         {
104         	 
105             if(strScriptName.endsWith( ".sql" ))
106             {
107                 strScriptName=strScriptName.substring( 0,strScriptName.length( )-4 );
108             }
109             ExecuteActionParameters/fastdeployapplication/business/fastaction/ansible/ExecuteActionParameters.html#ExecuteActionParameters">ExecuteActionParameters executeActionParameter=new ExecuteActionParameters(strDataBase,strScriptName,strDiskRequiredFreeSpace!=null?new Integer( strDiskRequiredFreeSpace ):null);
110              
111              
112              AWXJobResult jobResult= AWXService.getService( ).launchJob( this.getTechnicalCode( ),new AWXParameters( serverApplicationInstance.getHostName( ), executeActionParameter) );
113              
114              if(jobResult==null && commandResult != null)
115              {
116                  
117                  DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult,  serverApplicationInstance.getHostName( )));     
118              }
119              else
120              {
121                 if(jobResult.isRunning( ))
122                 {
123                     
124                     jobResult= refreshJobInformations(jobResult.getUrl( ));
125                     
126                 }
127                 
128                 if(jobResult.isFailed( ))
129                 {
130                     
131                   
132                     DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult,  serverApplicationInstance.getHostName( )));
133                 }
134                 else 
135                 {
136                   if(jobResult.getResult()!=null)
137                 	  {
138                 	  
139                 	      commandResult.getLog( ).append( "**************stdout******************* \n" );
140                 	      commandResult.getLog( ).append(jobResult.getResult().getStdout( ));
141                 	      commandResult.getLog( ).append( "*************************************** \n" );
142                 	      commandResult.getLog( ).append( "**************stdError******************* \n" );
143                 	      commandResult.getLog( ).append(jobResult.getResult().getStderr( ));
144                 	      commandResult.getLog( ).append( "*************************************** \n" );
145                 	      strResult= BooleanUtils.toStringTrueFalse(!jobResult.getResult().isExecutionFailed( ));
146                 	  }
147                 	  else
148                 	  {
149                 	      DeploymentUtils.addTechnicalError( commandResult, "No result found after execution script" );
150                 	  }
151 
152                 }
153                     
154                 
155                
156  
157         
158         
159         }
160         }
161 
162         return strResult;
163     }
164     
165     @Override
166     public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
167     {
168 
169         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
170         ActionParameter actionParameter;
171        
172         actionParameter = new ActionParameter( );
173         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
174         actionParameter.setValue( ((WorkflowDeployScriptContext)context).getDatabaseName( ) );
175         listActionParameters.add( actionParameter );
176         
177         actionParameter = new ActionParameter( );
178         actionParameter.setName( ConstanteUtils.PARAM_SCRIPT_NAME );
179         actionParameter.setValue( ((WorkflowDeployScriptContext)context).getScriptFileItemName( ) );
180         listActionParameters.add( actionParameter );
181 
182         
183         if(context.getDiskRequiredFreespace( )!=null)
184         {
185             actionParameter = new ActionParameter( );
186             actionParameter.setName( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE );
187             actionParameter.setValue(Integer.toString( context.getDiskRequiredFreespace( ) ));
188             listActionParameters.add( actionParameter );
189         }
190         
191         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
192     }
193     
194    
195 }