@webda/shell
Version:
Deploy a Webda app or configure it
122 lines • 3.5 kB
JavaScript
import { AbstractDeployer, Logger } from "@webda/core";
import { spawn } from "child_process";
/**
* **Deployer** represent one type of deploy like: *S3* or *Docker* or *Lambda+API Gateway* or *Fargate*
*
* This is an abstract class that should be extended to implement new one
* @module DeploymentSystem
*/
export class Deployer extends AbstractDeployer {
constructor(manager, resources = undefined) {
super();
this._defaulted = false;
this.now = Date.now();
this.manager = manager;
this.logger = new Logger(this.manager.getApplication().getWorkerOutput(), `deployers.${this.constructor.name}`);
this.app = this.manager.getApplication();
this.resources = resources;
this.parameters = {};
}
/**
* Set the deployer name
* @param name
*/
setName(name) {
this.name = name;
}
/**
* Set the deployer name
* @param name
*/
setType(type) {
this.type = type;
}
/**
* Return the Webda Application
*/
getApplication() {
return this.app;
}
/**
* Initiate the default value for resources
*/
async defaultResources() {
await this.loadDefaults();
}
/**
* Load default resources
*/
async loadDefaults() {
// Do nothing
}
/**
* Replace variables in resources
*
* @param obj to replace variables from
*/
replaceVariables(obj) {
return this.getApplication().replaceVariables(obj, {
resources: this.resources,
deployer: {
name: this.name,
type: this.type
},
...this.parameters,
// Allow environment access in case of secrets from CI
env: process.env
});
}
/**
* Replace the resources variables
*
* ```
* this.resources = this.replaceVariables(this.resources);
* ```
*/
replaceResourcesVariables() {
this.resources = this.replaceVariables(this.resources);
}
/**
*
* @param script command to execute
* @param stdin
*/
async execute(command, stdin = undefined, resolveOnError = false, logLevel = "TRACE") {
this.logger.log("DEBUG", "Command", command, stdin ? "with stdin <<EOF\n" + stdin + "\nEOF\n" : undefined);
return new Promise((resolve, reject) => {
let res = {
status: 0,
error: "",
output: ""
};
const ls = spawn(command, { shell: true });
ls.stdout.on("data", data => {
this.logger.log(logLevel, data.toString());
res.output += data.toString();
});
ls.stderr.on("data", data => {
this.logger.log("ERROR", data.toString());
res.error += data.toString();
});
ls.on("close", code => {
if (code == 0) {
resolve(res);
}
else {
res.status = code;
if (resolveOnError) {
resolve(res);
}
else {
reject(res);
}
}
});
if (stdin) {
ls.stdin.write(stdin);
ls.stdin.end();
}
});
}
}
//# sourceMappingURL=deployer.js.map