@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
122 lines • 5.94 kB
JavaScript
;
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const sdm_1 = require("@atomist/sdm");
const sdm_core_1 = require("@atomist/sdm-core");
const spawn = require("cross-spawn");
/**
* Start up an executable Jar on the same node as the automation client.
* Not intended as a Paas, but for use during demos and development.
* Always uses the same URL, whatever the branch and sha.
* @param opts options
*/
function executableJarDeployer(opts) {
if (!exports.managedExecutableJarDeployments) {
automation_client_1.logger.debug("Created new deployments record");
exports.managedExecutableJarDeployments = new sdm_core_1.ManagedDeployments(opts.lowerPort);
}
return new ExecutableJarDeployer(Object.assign(Object.assign({}, sdm_core_1.DefaultLocalDeployerOptions), opts));
}
exports.executableJarDeployer = executableJarDeployer;
class ExecutableJarDeployer {
constructor(opts) {
this.opts = opts;
this.logInterpreter = sdm_1.lastLinesLogInterpreter("Executable jar deployment");
}
findDeployments(id, ti, creds) {
return __awaiter(this, void 0, void 0, function* () {
const thisDeployment = this.deploymentFor(ti);
return thisDeployment ? [thisDeployment] : [];
});
}
undeploy(id, deployment, log) {
return __awaiter(this, void 0, void 0, function* () {
return exports.managedExecutableJarDeployments.terminateIfRunning(id.managedDeploymentKey, sdm_core_1.LookupStrategy.service);
});
}
deploy(da, ti, log, credentials, atomistTeam) {
return __awaiter(this, void 0, void 0, function* () {
if (!da.filename) {
throw new Error("No filename in deployable artifact!");
}
const port = yield exports.managedExecutableJarDeployments.findPort(ti.managedDeploymentKey, sdm_core_1.LookupStrategy.service, this.opts.baseUrl);
automation_client_1.logger.info("Deploying app [%j] on port [%d] for team %s", da, port, atomistTeam);
const startupInfo = {
port,
atomistTeam,
contextRoot: this.contextRoot(da.id),
};
yield exports.managedExecutableJarDeployments.terminateIfRunning(ti.managedDeploymentKey, sdm_core_1.LookupStrategy.service);
const childProcess = spawn("java", [
"-jar",
da.filename,
].concat(this.opts.commandLineArgumentsFor(startupInfo)), {
cwd: da.cwd,
});
const newLineDelimitedLog = new sdm_1.DelimitedWriteProgressLogDecorator(log, "\n");
childProcess.stdout.on("data", what => newLineDelimitedLog.write(what.toString()));
childProcess.stderr.on("data", what => newLineDelimitedLog.write(what.toString()));
const deployment = {
childProcess,
endpoint: `${this.opts.baseUrl}:${port}/${this.contextRoot(ti.managedDeploymentKey)}`,
};
exports.managedExecutableJarDeployments.recordDeployment({
id: ti.managedDeploymentKey,
port,
childProcess,
deployment,
lookupStrategy: sdm_core_1.LookupStrategy.service,
});
return [yield new Promise((resolve, reject) => {
childProcess.stdout.addListener("data", (what) => __awaiter(this, void 0, void 0, function* () {
if (!!what && this.opts.successPatterns.some(successPattern => successPattern.test(what.toString()))) {
resolve(deployment);
}
}));
childProcess.addListener("exit", () => {
reject(new Error("ExecutableJarDeployer: We should have found success message pattern by now!!"));
});
childProcess.addListener("error", reject);
})];
});
}
contextRoot(id) {
return `/${id.owner}/${id.repo}/staging`;
}
deploymentFor(ti) {
const managed = exports.managedExecutableJarDeployments.findDeployment(ti.managedDeploymentKey, sdm_core_1.LookupStrategy.service);
if (!managed) {
return undefined;
}
const port = managed.port;
const baseUrl = this.opts.baseUrl;
return {
endpoint: `${baseUrl}:${port}/${this.contextRoot(ti.managedDeploymentKey)}`,
};
}
}
//# sourceMappingURL=executableJarDeployer.js.map