View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.HashMap;
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.DeploymentFactory;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IDatabaseService;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXJobResult;
53  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXParameters;
54  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.ansible.AWXService;
55  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr.CloudmgrUtils;
56  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
57  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
58  import fr.paris.lutece.portal.service.spring.SpringContextService;
59  import fr.paris.lutece.portal.service.template.AppTemplateService;
60  import fr.paris.lutece.portal.service.util.AppLogService;
61  import fr.paris.lutece.portal.service.util.AppPropertiesService;
62  import fr.paris.lutece.util.html.HtmlTemplate;
63  import net.sf.json.JSONObject;
64  
65  /**
66   *
67   * WarInstallerAction
68   *
69   */
70  public class DumpWithOptionsAction extends DefaultAction
71  {
72  
73      private static final String CONSTANTE_DUMP_FILE_NAME_PREFIX = "FROM_Z00-";
74      private static final String CONSTANTE_DUMP_FILE_NAME_SUFIX = "-ALL_TABLES";
75      private static final String CONSTANTE_COMMAND_MYSQL_OPTIONS = "--opt --skip-comments --extended-insert --no-create-db --debug-info";
76  
77      @Override
78      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
79              ActionParameter... parameter )
80      {
81  
82          String strResult = null;
83  
84          String strDataBase = null;
85          String strDiskRequiredFreeSpace = null;
86          if ( parameter != null && parameter.length > 0 )
87          {
88              for ( int i = 0; i < parameter.length; i++ )
89              {
90  
91                  if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
92                  {
93  
94                      strDataBase = parameter [i].getValue( );
95                  }
96                  else
97                      if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE ) )
98                      {
99  
100                         strDiskRequiredFreeSpace = parameter [i].getValue( );
101                     }
102             }
103         }
104 
105         if ( !StringUtils.isEmpty( strDataBase ) )
106         {
107 
108             // String strDumpFileName=CONSTANTE_DUMP_FILE_NAME_PREFIX+strDataBase+CONSTANTE_DUMP_FILE_NAME_SUFIX;
109             String strDumpFileName = strDataBase + ".sql";
110             DumpWithOptionsParametersastdeployapplication/business/fastaction/ansible/DumpWithOptionsParameters.html#DumpWithOptionsParameters">DumpWithOptionsParameters dumpActionParameters = new DumpWithOptionsParameters( CONSTANTE_COMMAND_MYSQL_OPTIONS, strDataBase, strDataBase,
111                     strDiskRequiredFreeSpace != null ? new Integer( strDiskRequiredFreeSpace ) : null );
112 
113             AWXJobResult jobResult = AWXService.getService( ).launchJob( this.getTechnicalCode( ),
114                     new AWXParameters( serverApplicationInstance.getHostName( ), dumpActionParameters ) );
115 
116             if ( jobResult == null && commandResult != null )
117             {
118 
119                 DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult, serverApplicationInstance.getHostName( ) ) );
120 
121             }
122             else
123             {
124                 if ( jobResult.isRunning( ) )
125                 {
126 
127                     jobResult = refreshJobInformations( jobResult.getUrl( ) );
128 
129                 }
130 
131                 if ( jobResult.isFailed( ) )
132                 {
133 
134                     DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult, serverApplicationInstance.getHostName( ) ) );
135                 }
136                 else
137                 {
138 
139                     strResult = BooleanUtils.toStringTrueFalse( !jobResult.isFailed( ) );
140                     commandResult.getResultInformations( ).put( ConstanteUtils.MARK_DUMP_FILE_URL,
141                             DeploymentUtils.getDownloadUrl( serverApplicationInstance, data, strDataBase, strDumpFileName ) );
142                 }
143             }
144         }
145 
146         return strResult;
147     }
148 
149     @Override
150     public boolean canRunAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
151             ActionParameter... parameter )
152     {
153         String strDataBase = null;
154         String strDoDump = null;
155         for ( int i = 0; i < parameter.length; i++ )
156         {
157 
158             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
159             {
160 
161                 strDataBase = parameter [i].getValue( );
162             }
163 
164             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_DO_DUMP ) )
165             {
166 
167                 strDoDump = parameter [i].getValue( );
168             }
169         }
170 
171         return !StringUtils.isEmpty( strDataBase ) && !StringUtils.equals( strDoDump, "false" );
172 
173     }
174 
175     @Override
176     public String getTemplateFormAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, Locale locale )
177     {
178 
179         HashMap model = new HashMap( );
180 
181         model.put( ConstanteUtils.MARK_DATABASE_LIST,
182                 DeploymentUtils
183                         .getSimpleReferenceList( DeploymentFactory.getInstance( ).getDatabaseService( FastDeployApplicationData.APPLICATION_TYPE_ANSIBLE )
184                                 .getDatabases( data.getApplicationCode( ), serverApplicationInstance, locale ) ) );
185         model.put( ConstanteUtils.MARK_DISK_REQUIRED_FREESPACE,
186                 AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOY_SCRIT_WITH_DUMP_DISK_REQUIRED_FREESPACE ) );
187 
188         HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_FORM_ACTION_DUMP, locale, model );
189 
190         return template.getHtml( );
191     }
192 
193     @Override
194     public boolean isDisplayFormAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance )
195     {
196 
197         return true;
198     }
199 
200     @Override
201     public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
202     {
203 
204         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
205         ActionParameter actionParameter;
206 
207         actionParameter = new ActionParameter( );
208         actionParameter.setName( ConstanteUtils.PARAM_DO_DUMP );
209         actionParameter.setValue( Boolean.toString( ( (WorkflowDeployScriptContext) context ).getDoDump( ) ) );
210         listActionParameters.add( actionParameter );
211 
212         actionParameter = new ActionParameter( );
213         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
214         actionParameter.setValue( ( (WorkflowDeployScriptContext) context ).getDatabaseName( ) );
215         listActionParameters.add( actionParameter );
216 
217         if ( context.getDiskRequiredFreespace( ) != null )
218         {
219             actionParameter = new ActionParameter( );
220             actionParameter.setName( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE );
221             actionParameter.setValue( Integer.toString( context.getDiskRequiredFreespace( ) ) );
222             listActionParameters.add( actionParameter );
223         }
224 
225         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
226     }
227 
228 }