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.cloudmgr;
35  
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  import org.apache.commons.lang.StringUtils;
41  
42  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.AbstractWorkflowDeployContext;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployScriptContext;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ActionParameter;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr.CloudmgrUtils;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  import net.sf.json.JSONArray;
54  import net.sf.json.JSONObject;
55  
56  /**
57   *
58   * ExecuteAction
59   *
60   */
61  public class ExecuteSqlAction extends DefaultAction
62  {
63      @Override
64      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult, ActionParameter... parameter )
65      {
66  
67          String strResult = null;
68  
69          String strDataBase = null;
70          String strScriptName = null;
71          for ( int i = 0; i < parameter.length; i++ )
72          {
73  
74              if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
75              {
76  
77                  strDataBase = parameter [i].getValue( );
78              }
79              if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_SCRIPT_NAME ) )
80              {
81  
82                  strScriptName = parameter [i].getValue( );
83              }
84          }
85  
86          if ( !StringUtils.isEmpty( strDataBase ) && !StringUtils.isEmpty( strScriptName ) )
87          {
88              String strPlateformEnvironmentBaseUrl = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL );
89              String strWebserviceActionJsonPropery = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_WEBSERVICE_ACTION_RESULT_JSON_PROPERTY_RESULT );
90              String strJSONAction = null;
91  
92              try
93              {
94                  strJSONAction = CloudmgrUtils.callPlateformEnvironmentWs( strPlateformEnvironmentBaseUrl + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
95                          + CloudmgrUtils.getPlateformUrlServerApplicationAction(data.getApplicationCode( ), serverApplicationInstance, this.getCode( ) )
96                          + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strDataBase + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strScriptName );
97              }
98              catch( Exception e )
99              {
100                 AppLogService.error( e );
101             }
102 
103             if ( strJSONAction != null )
104             {
105                 JSONObject jo = DeploymentUtils.getJSONOBject( strJSONAction );
106                 JSONObject joStep;
107 
108                 if ( jo != null )
109                 {
110                     strResult = jo.getString( strWebserviceActionJsonPropery );
111 
112                     JSONArray steps = jo.getJSONObject( "execution" ).getJSONArray( "steps" );
113                     Iterator<JSONObject> iterator = steps.iterator( );
114 
115                     while ( iterator.hasNext( ) )
116                     {
117                         joStep = iterator.next( );
118                         if ( joStep.getString( "step_name" ).equals( "[ STEP 3 ] : execute" ) )
119                         {
120 
121                             commandResult.getLog( ).append( "**************stdout******************* \n" );
122                             Iterator<String> iteratStdOut = joStep.getJSONArray( "stdout" ).iterator( );
123                             while ( iteratStdOut.hasNext( ) )
124                             {
125                                 commandResult.getLog( ).append( iteratStdOut.next( ) + " \n" );
126                             }
127                             commandResult.getLog( ).append( "*************************************** \n" );
128                             commandResult.getLog( ).append( "**************Errors******************* \n" );
129                             Iterator<String> iteratStdError = joStep.getJSONArray( "stderr" ).iterator( );
130 
131                             while ( iteratStdError.hasNext( ) )
132                             {
133                                 commandResult.getLog( ).append( iteratStdError.next( ) + " \n" );
134                             }
135                             commandResult.getLog( ).append( "*************************************** \n" );
136                         }
137 
138                     }
139 
140                 }
141             }
142         }
143 
144         return strResult;
145     }
146     
147     @Override
148     public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
149     {
150 
151         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
152         ActionParameter actionParameter;
153        
154         actionParameter = new ActionParameter( );
155         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
156         actionParameter.setValue( ((WorkflowDeployScriptContext)context).getDatabaseName( ) );
157         listActionParameters.add( actionParameter );
158         
159         actionParameter = new ActionParameter( );
160         actionParameter.setName( ConstanteUtils.PARAM_SCRIPT_NAME );
161         actionParameter.setValue( ((WorkflowDeployScriptContext)context).getScriptFileItemName( ) );
162         listActionParameters.add( actionParameter );
163 
164         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
165     }
166     
167    
168 }