View Javadoc
1   package org.kuali.common.deploy;
2   
3   import org.kuali.common.deploy.appserver.ApplicationServer;
4   import org.kuali.common.deploy.monitoring.Monitoring;
5   import org.kuali.common.util.Assert;
6   import org.kuali.common.util.FormatUtils;
7   import org.kuali.common.util.execute.Executable;
8   import org.kuali.common.util.execute.impl.NoOpExecutable;
9   import org.kuali.common.util.maven.RepositoryUtils;
10  import org.kuali.common.util.nullify.NullUtils;
11  import org.kuali.common.util.secure.channel.SSHUtils;
12  import org.kuali.common.util.secure.channel.SecureChannel;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  public class DefaultDeployService implements DeployService {
17  
18  	private static final Logger logger = LoggerFactory.getLogger(DefaultDeployService.class);
19  
20  	private static final Executable DEFAULT_SYS_ADMIN_EXEC = NoOpExecutable.INSTANCE;
21  	private static final Executable DEFAULT_DB_RESET_EXEC = NoOpExecutable.INSTANCE;
22  
23  	public DefaultDeployService(DeployContext context, Monitoring monitoring, ApplicationServer appServer) {
24  		this(context, DEFAULT_SYS_ADMIN_EXEC, monitoring, appServer, DEFAULT_DB_RESET_EXEC);
25  	}
26  
27  	public DefaultDeployService(DeployContext context, Executable sysAdmin, Monitoring monitoring, ApplicationServer appServer, Executable dbReset) {
28  		Assert.noNulls(context, sysAdmin, monitoring, appServer, dbReset);
29  		this.context = context;
30  		this.sysAdminExecutable = sysAdmin;
31  		this.monitoring = monitoring;
32  		this.appServer = appServer;
33  		this.databaseResetExecutable = dbReset;
34  	}
35  
36  	private final DeployContext context;
37  	private final Executable sysAdminExecutable;
38  	private final Monitoring monitoring;
39  	private final ApplicationServer appServer;
40  	private final Executable databaseResetExecutable;
41  
42  	@Override
43  	public void deploy() {
44  		SecureChannel channel = context.getChannel();
45  		long start = System.currentTimeMillis();
46  		logger.info("[deploy:starting]");
47  		try {
48  			logger.info("---------------- Deploy Application ----------------");
49  			if (channel.getPort() == SSHUtils.DEFAULT_PORT) {
50  				logger.info("Secure Channel - {}@{}", channel.getUsername(), channel.getHostname());
51  			} else {
52  				Object[] args = { channel.getUsername(), channel.getHostname(), channel.getPort() };
53  				logger.info("Secure Channel - {}@{}:{}", args);
54  			}
55  			logger.info("Environment - {}", context.getEnvironment().getName());
56  			logger.info("Application - {}", RepositoryUtils.toString(context.getApplication()));
57  			if (context.getJdbcDriver().isPresent()) {
58  				logger.info("Jdbc Driver - {}", RepositoryUtils.toString(context.getJdbcDriver().get()));
59  			} else {
60  				logger.info("Jdbc Driver - {}", NullUtils.NONE);
61  			}
62  			for (Deployable deployable : context.getConfigFiles()) {
63  				logger.info("Config - [{}]", deployable.getLocal());
64  			}
65  			logger.info("----------------------------------------------------");
66  			channel.open();
67  			monitoring.stop();
68  			appServer.stop();
69  			sysAdminExecutable.execute();
70  			databaseResetExecutable.execute();
71  			monitoring.prepare();
72  			monitoring.start();
73  			appServer.prepare();
74  			appServer.start();
75  		} catch (Exception e) {
76  			throw new IllegalStateException(e);
77  		} finally {
78  			channel.close();
79  		}
80  		logger.info("[deploy:complete] - {}", FormatUtils.getTime(System.currentTimeMillis() - start));
81  	}
82  
83  	public static Logger getLogger() {
84  		return logger;
85  	}
86  
87  	public DeployContext getContext() {
88  		return context;
89  	}
90  
91  	public Executable getSysAdminExecutable() {
92  		return sysAdminExecutable;
93  	}
94  
95  	public Monitoring getMonitoring() {
96  		return monitoring;
97  	}
98  
99  	public ApplicationServer getAppServer() {
100 		return appServer;
101 	}
102 
103 	public Executable getDatabaseResetExecutable() {
104 		return databaseResetExecutable;
105 	}
106 
107 }