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.service.ansible;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import org.apache.commons.lang.StringUtils;
41  
42  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
43  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FtpInfo;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.IAction;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.AbstractServerApplicationService;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.cloudmgr.CloudmgrUtils;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
49  import fr.paris.lutece.portal.service.datastore.DatastoreService;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.spring.SpringContextService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  
54  public class ServerApplicationService extends AbstractServerApplicationService
55  {
56  
57      public List<ServerApplicationInstance> getListServerApplicationInstanceByEnvironment( FastDeployApplicationData data, String strIdApplication,
58              String strCodeEnvironment, String strServerApplicationType, Locale locale, boolean withActions, boolean withStatus )
59      {
60  
61          List<AWXHost> listHosts = AWXService.getService( ).getHosts( data.getApplicationCode( ) );
62          List<ServerApplicationInstance> listServerApplicationInstance = new ArrayList<ServerApplicationInstance>( );
63  
64          listHosts.stream( ).filter( h -> getServerType( h.getCodeType( ) ).equals( strServerApplicationType ) && h.getCodeEnvironment( ).equals( strCodeEnvironment)).forEach( h -> listServerApplicationInstance.add( getServerApplicationInstance( data, strIdApplication, h.getCodeInstance( ),
65                  strCodeEnvironment, strServerApplicationType, locale, withActions, withStatus ) ));
66          
67          return listServerApplicationInstance;
68      }
69  
70      public ServerApplicationInstance getServerApplicationInstance( FastDeployApplicationData data, String strIdApplication, String strServerInstanceCode,
71              String strCodeEnvironment, String strServerType, Locale locale, boolean withActions, boolean withStatus )
72      {
73  
74          
75          List<AWXHost> listHosts = AWXService.getService( ).getHosts( data.getApplicationCode( ) );
76          
77          AWXHost host= listHosts.stream( ).filter( h -> getServerType( h.getCodeType( ) ).equals( strServerType ) && h.getCodeEnvironment( ).equals( strCodeEnvironment) && h.getCodeInstance( ).equals(strServerInstanceCode)).findFirst().orElseGet( null );
78          
79          
80          
81          
82          ServerApplicationInstance defaultServerApplicationInstance = _hashServerApplicationInstance
83                  .get( strServerType + ConstanteUtils.CONSTANTE_SEPARATOR_POINT + strServerInstanceCode );
84          ServerApplicationInstancetece/plugins/appcenter/modules/fastdeployapplication/business/ServerApplicationInstance.html#ServerApplicationInstance">ServerApplicationInstance serverApplicationInstance = (ServerApplicationInstance)defaultServerApplicationInstance.clone();
85          
86          serverApplicationInstance.setCodeEnvironment( strCodeEnvironment );
87  
88          String strServerNameSaved = getServerNameSaved( strIdApplication, strCodeEnvironment, strServerType, strServerInstanceCode );
89          if ( !StringUtils.isEmpty( strServerNameSaved ) )
90          {
91          	serverApplicationInstance.setName( strServerNameSaved );
92          }
93  
94          
95          serverApplicationInstance.setHostName( host.getName( ) );
96          setFtpInfo( serverApplicationInstance, data.getApplicationCode( ),host );
97  
98          if ( withActions )
99          {
100             serverApplicationInstance
101                     .setListServerApplicationAction( _actionService.getListActionByServerApplicationInstance( data, serverApplicationInstance, locale ) );
102         }
103 
104         if ( withStatus )
105         {
106             serverApplicationInstance.setStatus( getStatus( data, serverApplicationInstance ) );
107         }
108 
109         return serverApplicationInstance;
110     }
111 
112     private void setFtpInfo( ServerApplicationInstance serverApplicationInstance, String strCodeApplication ,AWXHost host)
113     {
114         serverApplicationInstance.setFtpDirectoryInstall( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_DIRECTORY_INSTALL_ANSIBLE ));
115         serverApplicationInstance.setFtpDirectoryScript( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_DIRECTORY_SCRIPT_ANSIBLE ) );
116         serverApplicationInstance.setFtpDirectoryDump( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_DIRECTORY_DUMP_ANSIBLE )  );
117         serverApplicationInstance.setFtpDirectoryContext( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_DIRECTORY_CONTEXT_ANSIBLE ));
118 
119         FtpInfo/plugins/appcenter/modules/fastdeployapplication/business/FtpInfo.html#FtpInfo">FtpInfo ftpInfo = new FtpInfo( );
120         ftpInfo.setHost( host.getVariables( ).getAnsibleHost() );
121         ftpInfo.setPort( AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_PORT_ANSIBLE, 21 ) );
122         ftpInfo.setUserLogin( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_USER_LOGIN_ANSIBLE ) );
123         ftpInfo.setUserPassword( AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOYMENT_SERVER_APPLICATION_FTP_USER_PASSWORD_ANSIBLE ) );
124 
125         serverApplicationInstance.setFtpInfo( ftpInfo );
126     }
127 
128    
129 
130     private Integer getStatus( FastDeployApplicationData data, ServerApplicationInstance serverApplicationInstance )
131     {
132         if ( ( serverApplicationInstance != null ) && ( serverApplicationInstance.getListServerApplicationAction( ) != null ) )
133         {
134             for ( IAction action : serverApplicationInstance.getListServerApplicationAction( ) )
135             {
136                 if ( action.isUsedForStatus( ) )
137                 {
138                     boolean bResultOk = _actionService.executeAction( data, action, serverApplicationInstance, null );
139 
140                     return new Integer( new Boolean( bResultOk ) ? 1 : 0 );
141                 }
142             }
143         }
144 
145         return 1;
146     }
147 
148     public void saveServerName( String strServerName, String strIdApplication, String strCodeEnvironment, String strServerType,
149             String strCodeServerApplicationInstance )
150     {
151         DatastoreService.setDataValue(
152                 strIdApplication + "_" + strCodeEnvironment + "_" + strServerType + "_" + strCodeServerApplicationInstance + "_" + SERVER_NAME, strServerName );
153     }
154 
155     public String getServerNameSaved( String strIdApplication, String strCodeEnvironment, String strServerType, String strCodeServerApplicationInstance )
156     {
157         return DatastoreService.getDataValue(
158                 strIdApplication + "_" + strCodeEnvironment + "_" + strServerType + "_" + strCodeServerApplicationInstance + "_" + SERVER_NAME, null );
159     }
160     
161     private static String getServerType( String strServerTypeCode)
162     {
163         
164         String strServerType=null;
165         
166         switch( strServerTypeCode )
167         {
168             case ConstanteUtils.CONSTANTE_SERVER_TOM:
169                 strServerType =ConstanteUtils.CONSTANTE_SERVER_TOMCAT;
170                 break ;
171             
172             case ConstanteUtils.CONSTANTE_SERVER_MYS:
173                 strServerType =ConstanteUtils.CONSTANTE_SERVER_MYSQL;
174                 break ;   
175             case ConstanteUtils.CONSTANTE_SERVER_MAR:
176                 strServerType =ConstanteUtils.CONSTANTE_SERVER_MYSQL;
177                 break ;   
178             default:
179                 strServerType =strServerTypeCode;
180                 break;
181         }
182         
183         
184         return strServerType;
185     }
186 
187     
188 }