DeployAppArgs deployArgs = new DeployAppArgs();
Tools.parseArgs(deployArgs, args);
try {
ZkClient zkClient = new ZkClient(deployArgs.zkConnectionString, deployArgs.timeout);
zkClient.setZkSerializer(new ZNRecordSerializer());
tmpAppsDir = Files.createTempDir();
if (!Strings.isNullOrEmpty(deployArgs.s4rPath) && !Strings.isNullOrEmpty(deployArgs.generatedS4R)) {
logger.error("-s4r and -generatedS4R options are mutually exclusive");
System.exit(1);
}
File s4rToDeploy;
if (deployArgs.s4rPath != null) {
s4rToDeploy = new File(deployArgs.s4rPath);
if (!s4rToDeploy.exists()) {
logger.error("Specified S4R file does not exist in {}", s4rToDeploy.getAbsolutePath());
System.exit(1);
} else {
logger.info(
"Using specified S4R [{}], the S4R archive will not be built from source (and corresponding parameters are ignored)",
s4rToDeploy.getAbsolutePath());
}
} else {
List<String> params = new ArrayList<String>();
// prepare gradle -P parameters, including passed gradle opts
params.addAll(deployArgs.gradleOpts);
params.add("appClass=" + deployArgs.appClass);
params.add("appsDir=" + tmpAppsDir.getAbsolutePath());
params.add("appName=" + deployArgs.appName);
ExecGradle.exec(deployArgs.gradleBuildFile, "installS4R", params.toArray(new String[] {}));
File tmpS4R = new File(tmpAppsDir.getAbsolutePath() + "/" + deployArgs.appName + ".s4r");
if (!Strings.isNullOrEmpty(deployArgs.generatedS4R)) {
logger.info("Copying generated S4R to [{}]", deployArgs.generatedS4R);
s4rToDeploy = new File(deployArgs.generatedS4R);
if (!(ByteStreams.copy(Files.newInputStreamSupplier(tmpS4R),
Files.newOutputStreamSupplier(s4rToDeploy)) > 0)) {
logger.error("Cannot copy generated s4r from {} to {}", tmpS4R.getAbsolutePath(),
s4rToDeploy.getAbsolutePath());
System.exit(1);
}
} else {
s4rToDeploy = tmpS4R;
}
}
final String uri = s4rToDeploy.toURI().toString();
ZNRecord record = new ZNRecord(String.valueOf(System.currentTimeMillis()));
record.putSimpleField(DistributedDeploymentManager.S4R_URI, uri);
record.putSimpleField("name", deployArgs.appName);
String deployedAppPath = "/s4/clusters/" + deployArgs.clusterName + "/app/s4App";
if (zkClient.exists(deployedAppPath)) {
ZNRecord readData = zkClient.readData(deployedAppPath);
logger.error("Cannot deploy app [{}], because app [{}] is already deployed", deployArgs.appName,
readData.getSimpleField("name"));
System.exit(1);
}
zkClient.create("/s4/clusters/" + deployArgs.clusterName + "/app/s4App", record, CreateMode.PERSISTENT);
logger.info(
"uploaded application [{}] to cluster [{}], using zookeeper znode [{}], and s4r file [{}]",
new String[] { deployArgs.appName, deployArgs.clusterName,
"/s4/clusters/" + deployArgs.clusterName + "/app/" + deployArgs.appName,
s4rToDeploy.getAbsolutePath() });