@webda/shell
Version:
Deploy a Webda app or configure it
145 lines • 5.63 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Cache, Core, FileUtils, Logger, WebdaError } from "@webda/core";
import * as fs from "fs";
import * as merge from "merge";
import * as path from "path";
import WebdaConsole from "../console/webda.js";
export class DeploymentManager {
constructor(app, deploymentName, streams = undefined) {
this.deployersDefinition = {};
this.deployers = {};
this.application = app;
this.application.compile();
this.application.setCurrentDeployment(deploymentName);
let deployment = this.application.getDeployment(deploymentName);
this.webda = new Core(this.application);
this.webda.initStatics();
this.deployersDefinition = this.application.getDeployers();
this.output = app.getWorkerOutput();
this.logger = new Logger(this.output, `deploymentManager`);
if (streams) {
this.streams = streams;
}
else {
this.streams = { out: console.log, err: console.error };
}
deployment.units.forEach(d => {
if (!this.deployersDefinition[this.application.completeNamespace(d.type)]) {
this.logger.log("ERROR", "Cannot find deployer", d.type, "known types:", Object.keys(this.deployersDefinition).join(","));
}
else {
this.deployers[d.name] = merge.recursive(true, deployment.resources, d); // Load deployer
}
});
}
static async newDeployment(argv) {
let name = argv.name;
let application = WebdaConsole.app;
let output = application.getWorkerOutput();
await application.load();
let deployment = {
$schema: "../.webda-deployment-schema.json",
units: [],
resources: {},
services: {},
parameters: {}
};
name ?? (name = await output.requestInput("Name"));
let deploymentPath = application.getAppPath(path.join("deployments", name + ".json"));
while (fs.existsSync(deploymentPath)) {
output.log("ERROR", "Deployment already exists");
name = await output.requestInput("Name");
deploymentPath = application.getAppPath(path.join("deployments", name + ".json"));
}
FileUtils.save({ ...deployment, name: undefined }, deploymentPath);
output.log("INFO", "You can customize the parameters,resources and services objects within the deployment", deploymentPath);
}
/**
*
* @param output
*/
setOutput(output) {
this.output = output;
}
getOutput() {
return this.output;
}
/**
* Command line executor
* @param argv
*/
async commandLine(argv) {
const [deployerName, command = "deploy"] = argv._;
let deployers = [deployerName];
if (deployerName === undefined) {
deployers = Object.keys(this.deployers);
}
else if (!this.deployers[deployerName]) {
this.logger.log("ERROR", "Unknown deployer", deployerName);
return 1;
}
let args = argv._.slice(2);
for (let i in deployers) {
let deployer = await this.getDeployer(deployers[i]);
this.logger.logTitle(`Deploying ${deployers[i]} (${this.getApplication().getCurrentDeployment()})`);
await deployer[command](...args);
}
return 0;
}
/**
* Return instantiated Webda application
*
* Not initialization performed on it
*/
getWebda() {
return this.webda;
}
/**
* Return the Webda Application
*/
getApplication() {
return this.application;
}
async getDeployer(name) {
if (!this.deployers[name]) {
throw new WebdaError.CodeError("DEPLOYER_UNKNOWN", `Unknown deployer ${name} (${Object.keys(this.deployers).join(",")})`);
}
let deployer = new this.deployersDefinition[this.deployers[name].type](this, this.deployers[name]);
deployer.setName(name);
deployer.setType(this.deployers[name].type);
await deployer.defaultResources();
deployer.replaceResourcesVariables();
return deployer;
}
getDeploymentName() {
return this.application.getDeployment().name;
}
/**
*
* @param type of deployer to run
* @param resources parameters
*/
async run(type, resources) {
if (!this.deployersDefinition[type.toLowerCase()]) {
throw new WebdaError.CodeError("DEPLOYER_TYPE_UNKNOWN", "Unknown deployer type " + type);
}
let deployer = new this.deployersDefinition[type.toLowerCase()](this, resources);
await deployer.defaultResources();
return deployer.deploy();
}
/**
* Get package.json information
*/
getPackageDescription() {
return this.application.getPackageDescription();
}
}
__decorate([
Cache()
], DeploymentManager.prototype, "getPackageDescription", null);
//# sourceMappingURL=deploymentmanager.js.map