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 DumpAction extends DefaultAction
71  {
72  
73      @Override
74      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
75              ActionParameter... parameter )
76      {
77  
78          String strResult = null;
79  
80          String strDataBase = null;
81          String strDiskRequiredFreeSpace = null;
82          if ( parameter != null && parameter.length > 0 )
83          {
84              for ( int i = 0; i < parameter.length; i++ )
85              {
86  
87                  if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
88                  {
89  
90                      strDataBase = parameter [i].getValue( );
91                  }
92                  else
93                      if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE ) )
94                      {
95  
96                          strDiskRequiredFreeSpace = parameter [i].getValue( );
97                      }
98              }
99          }
100 
101         if ( !StringUtils.isEmpty( strDataBase ) )
102         {
103 
104             String strDumpFileName = strDataBase + ".sql";
105 
106             DumpParametersr/modules/fastdeployapplication/business/fastaction/ansible/DumpParameters.html#DumpParameters">DumpParameters dumpActionParameters = new DumpParameters( strDataBase,
107                     strDiskRequiredFreeSpace != null ? new Integer( strDiskRequiredFreeSpace ) : null );
108 
109             AWXJobResult jobResult = AWXService.getService( ).launchJob( this.getTechnicalCode( ),
110                     new AWXParameters( serverApplicationInstance.getHostName( ), dumpActionParameters ) );
111 
112             if ( jobResult == null && commandResult != null )
113             {
114 
115                 DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult, serverApplicationInstance.getHostName( ) ) );
116 
117             }
118             else
119             {
120                 if ( jobResult.isRunning( ) )
121                 {
122 
123                     jobResult = refreshJobInformations( jobResult.getUrl( ) );
124 
125                 }
126 
127                 if ( jobResult.isFailed( ) )
128                 {
129 
130                     DeploymentUtils.addTechnicalError( commandResult, getDefaultErrorMessage( jobResult, serverApplicationInstance.getHostName( ) ) );
131 
132                 }
133                 else
134                 {
135 
136                     strResult = BooleanUtils.toStringTrueFalse( !jobResult.isFailed( ) );
137                     commandResult.getResultInformations( ).put( ConstanteUtils.MARK_DUMP_FILE_URL,
138                             DeploymentUtils.getDownloadUrl( serverApplicationInstance, data, strDataBase, strDumpFileName ) );
139 
140                 }
141             }
142         }
143 
144         return strResult;
145     }
146 
147     @Override
148     public boolean canRunAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
149             ActionParameter... parameter )
150     {
151         String strDataBase = null;
152         String strDoDump = null;
153         for ( int i = 0; i < parameter.length; i++ )
154         {
155 
156             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_CODE_DATABASE ) )
157             {
158 
159                 strDataBase = parameter [i].getValue( );
160             }
161 
162             if ( parameter [i].getName( ).equals( ConstanteUtils.PARAM_DO_DUMP ) )
163             {
164 
165                 strDoDump = parameter [i].getValue( );
166             }
167         }
168 
169         return !StringUtils.isEmpty( strDataBase ) && !StringUtils.equals( strDoDump, "false" );
170 
171     }
172 
173     @Override
174     public String getTemplateFormAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, Locale locale )
175     {
176 
177         HashMap model = new HashMap( );
178 
179         model.put( ConstanteUtils.MARK_DATABASE_LIST,
180                 DeploymentUtils
181                         .getSimpleReferenceList( DeploymentFactory.getInstance( ).getDatabaseService( FastDeployApplicationData.APPLICATION_TYPE_ANSIBLE )
182                                 .getDatabases( data.getApplicationCode( ), serverApplicationInstance, locale, true ) ) );
183 
184         model.put( ConstanteUtils.MARK_DISK_REQUIRED_FREESPACE,
185                 AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOY_SCRIT_WITH_DUMP_DISK_REQUIRED_FREESPACE ) );
186 
187         HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_FORM_ACTION_DUMP, locale, model );
188 
189         return template.getHtml( );
190     }
191 
192     @Override
193     public boolean isDisplayFormAction( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance )
194     {
195 
196         return true;
197     }
198 
199     @Override
200     public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
201     {
202 
203         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
204         ActionParameter actionParameter;
205 
206         actionParameter = new ActionParameter( );
207         actionParameter.setName( ConstanteUtils.PARAM_DO_DUMP );
208         actionParameter.setValue( Boolean.toString( ( (WorkflowDeployScriptContext) context ).getDoDump( ) ) );
209         listActionParameters.add( actionParameter );
210 
211         actionParameter = new ActionParameter( );
212         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
213         actionParameter.setValue( ( (WorkflowDeployScriptContext) context ).getDatabaseName( ) );
214         listActionParameters.add( actionParameter );
215 
216         if ( context.getDiskRequiredFreespace( ) != null )
217         {
218             actionParameter = new ActionParameter( );
219             actionParameter.setName( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE );
220             actionParameter.setValue( Integer.toString( context.getDiskRequiredFreespace( ) ) );
221             listActionParameters.add( actionParameter );
222         }
223 
224         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
225     }
226 
227 }