package automately.core;
import automately.core.file.VirtualFileService;
import automately.core.services.api.ApiServer;
import automately.core.services.container.ContainerService;
import automately.core.services.core.BaseService;
import automately.core.services.http.ClusteredHttpServer;
import automately.core.services.job.JobServer;
import automately.core.services.sdk.SdkDeploymentService;
import automately.core.services.sdk.SdkSockJSServer;
import automately.core.services.sdk.eventbus.SdkAuthManager;
import automately.core.services.sdk.eventbus.SdkEventManager;
import automately.core.services.ssh.SSHDaemonService;
import io.jsync.app.ClusterApp;
import io.jsync.app.core.Cluster;
import io.jsync.app.core.Config;
import io.jsync.json.JsonObject;
import io.jsync.logging.impl.LoggerFactory;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Properties;
/**
* Automately is a {@link io.jsync.app.ClusterApp}
*/
public class Automately extends ClusterApp {
static {
// Let's set some properties..
if(System.getProperty("java.util.logging.config.file") == null){
System.setProperty("java.util.logging.config.file", "default_logging.properties");
System.setProperty(LoggerFactory.LOGGER_PROPERTIES_FILE, "default_logging.properties");
} else {
System.setProperty(LoggerFactory.LOGGER_PROPERTIES_FILE, System.getProperty("java.util.logging.config.file"));
}
// By default we don't want hazelcast to phone home
System.setProperty("hazelcast.phone.home.enabled", "false");
System.setProperty("hazelcast.socket.bind.any", "false");
System.setProperty("async.pool.eventloop.size", String.valueOf(Runtime.getRuntime().availableProcessors() * 2));
System.setProperty("async.pool.worker.size", String.valueOf(Runtime.getRuntime().availableProcessors() * 10));
}
/**
* Running this will result in the initialization of the default Automately instance
* and will join the cluster by default.
*
* @param args the jcluster arguments that you want to use
* @throws Exception
*/
public static void main(final String[] args) {
System.out.println("Automately Core Version: " + getVersion());
try {
// We must initialize the jsync.io cluster app properly
ClusterApp.initialize(new Automately(), ArrayUtils.add(args, "--join"));
} catch (Exception e){
e.printStackTrace();
System.err.println("Error initializing Automately Core Version: " + getVersion());
System.exit(1);
}
}
/**
* getVersion() is a useful method to return the of the Automately Backend.
* @return
*/
public static String getVersion() {
try {
Properties properties = new Properties();
properties.load(Automately.class.getResourceAsStream("application.properties"));
return String.valueOf(properties.get("automately.core.version"));
} catch (Exception ignored) {
}
return "debugBuild";
}
@Override
public void prepareConfig(Config config) {
if (config.isDefault()) {
config.setRole("all");
}
if (!config.rawConfig().containsField("automately")) {
config.rawConfig().putObject("automately", new JsonObject());
}
if (!config.rawConfig().getObject("automately").containsField("core")) {
config.rawConfig().getObject("automately")
.putObject("core", new JsonObject());
}
if(!config.rawConfig().getObject("cluster").containsField("async_pool_size")){
config.rawConfig().getObject("cluster").putNumber("async_pool_size", Runtime.getRuntime().availableProcessors() * 2);
}
}
@Override
public void prepareCluster(Cluster cluster) {
try {
Config config = cluster.config();
cluster.addService(new BaseService());
/**
* Add VirtualFileSupport. This needs to be on every member of the cluster and loaded before any other member
*/
cluster.addService(new VirtualFileService());
cluster.addService(new ClusteredHttpServer());
// JCluster Roles: default, all, client, data
// Automately Roles: all, api, job, screenshot
// All members of the cluster have the JobServer service
// We don't need to add special services for the job
cluster.addService(new JobServer());
if(config.isRole("job") || config.isAll()){
cluster.addService(new ContainerService());
}
if(config.isRole("hz")){
cluster.logger().info("Running as hazelcast only cluster.");
} else if (config.isRole("api") || config.isAll()) {
cluster.addService(new SSHDaemonService());
cluster.addService(new SdkDeploymentService());
cluster.addService(new ApiServer());
cluster.addService(new SdkAuthManager());
cluster.addService(new SdkEventManager());
cluster.addService(new SdkSockJSServer());
} else if (config.isRole("sdk")) {
cluster.addService(new SdkAuthManager());
cluster.addService(new SdkEventManager());
cluster.addService(new SdkSockJSServer());
}
} catch (Exception e) {
throw new RuntimeException("There was an issue while attempting to prepare the cluster.", e);
}
}
/**
* This is a helper method to return the core automately configuration
*
* @return returns a JsonObject that contains all the data in {automately:{core:{}}}
*/
private JsonObject coreConfig() {
return cluster().config().rawConfig().getObject("automately").getObject("core");
}
}