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.io.ByteArrayInputStream;
37  import java.io.InputStream;
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
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.business.fastaction.IAction;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.DeploymentFactory;
51  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IActionService;
52  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IFtpService;
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.portal.service.spring.SpringContextService;
56  import fr.paris.lutece.portal.service.template.AppTemplateService;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  
59  public class InitMysqlDatabaseAction extends ExecuteSqlAction
60  {
61  
62      private static final String SCRIPT_INIT_NAME = "init_db";
63      private static final String DATABASE = "mysql";
64  
65      @Override
66      public String run( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance, CommandResult commandResult,
67              ActionParameter... parameter )
68      {
69  
70          HashMap model = new HashMap( );
71          model.put( ConstanteUtils.MARK_APPLICATION, data );
72  
73          IFtpService ftpService = DeploymentFactory.getInstance( ).getFtpService( data.getApplicationType( ) );
74  
75          HtmlTemplate templateInitDatabaseContext = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_INIT_DB, Locale.FRENCH, model );
76  
77          InputStream iTemplateDatabaseContext = new ByteArrayInputStream( templateInitDatabaseContext.getHtml( ).getBytes( ) );
78  
79          ftpService.uploadFile( SCRIPT_INIT_NAME + ".sql", iTemplateDatabaseContext, serverApplicationInstance.getFtpInfo( ),
80                  serverApplicationInstance.getFtpDirectoryScript( ), commandResult, false );
81  
82          if ( commandResult.getStatus( ) == CommandResult.STATUS_ERROR )
83          {
84  
85              return "false";
86          }
87  
88          return super.run( data, serverApplicationInstance, commandResult, parameter );
89  
90      }
91  
92      @Override
93      public <T extends AbstractWorkflowDeployContext> ActionParameter [ ] getActionParameters( T context )
94      {
95  
96          List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
97          ActionParameter actionParameter;
98  
99          actionParameter = new ActionParameter( );
100         actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
101         actionParameter.setValue( DATABASE );
102         listActionParameters.add( actionParameter );
103 
104         actionParameter = new ActionParameter( );
105         actionParameter.setName( ConstanteUtils.PARAM_SCRIPT_NAME );
106         actionParameter.setValue( SCRIPT_INIT_NAME );
107         listActionParameters.add( actionParameter );
108 
109         if ( context.getDiskRequiredFreespace( ) != null )
110         {
111             actionParameter = new ActionParameter( );
112             actionParameter.setName( ConstanteUtils.PARAM_DISK_REQUIRED_FREE_SPACE );
113             actionParameter.setValue( Integer.toString( context.getDiskRequiredFreespace( ) ) );
114             listActionParameters.add( actionParameter );
115         }
116 
117         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
118     }
119 
120 }