zcatalyst-cli
Version:
Command Line Tool for CATALYST
180 lines (179 loc) • 10.6 kB
JavaScript
"use strict";
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlaveManager = void 0;
const error_1 = __importDefault(require("../../../error"));
const runtime_store_1 = __importDefault(require("../../../runtime-store"));
const path_1 = require("path");
const fs_1 = require("../../../util_modules/fs");
const constants_1 = require("../../../util_modules/constants");
const logger_1 = require("../../../util_modules/logger");
const credential_1 = __importDefault(require("../../../authentication/credential"));
const project_1 = require("../../../util_modules/project");
const port_resolver_1 = __importDefault(require("../../../port-resolver"));
const fn_utils_1 = require("../../../fn-utils");
const container_1 = require("../../../util_modules/container");
const fn_execution_handler_1 = require("./fn-execution-handler");
class SlaveManager extends fn_execution_handler_1.FnExecutionHandler {
constructor(fn, repl, localFnEvents) {
var _a, _b, _c, _d, _e;
super(fn, localFnEvents);
this.killed = false;
this.isContainer = (0, container_1.isContainer)();
this.call = (shell = true) => (rawData = {}) => {
var _a;
let data = {};
try {
if (shell) {
if (typeof rawData === 'string') {
const content = fs_1.SYNC.readJSONFile((0, path_1.resolve)(runtime_store_1.default.get('cwd'), rawData));
if (content === undefined) {
return new error_1.default('content is undefined for provided file path');
}
data = content;
}
else {
if (typeof rawData !== 'object') {
return new error_1.default('content is not of type object');
}
data = Object.assign({}, rawData);
}
switch ((_a = this.fn.target) === null || _a === void 0 ? void 0 : _a.type) {
case constants_1.FN_TYPE.cron: {
data = { data };
break;
}
case constants_1.FN_TYPE.event: {
if (!('event_bus_details' in data)) {
if (!('events' in data)) {
(0, logger_1.info)('For event function kindly use signals:generate command to generate the input json.');
(0, logger_1.info)('You can also provide event details as per documentation.');
throw new error_1.default('Event details missing', {
exit: 1,
skipHelp: true
});
}
}
else {
(0, logger_1.warning)('Since the event:generate command is being deprecated and being sunsetted in future releases. Please use signals:generate command here after to get the event details.');
}
break;
}
}
}
else {
if (typeof rawData === 'string') {
return new error_1.default('tunnel data cannot be of type string', {
exit: 1
});
}
data = Object.assign({}, rawData);
}
}
catch (e) {
(0, logger_1.info)('the input must be in the form of json or a path to a file containing json data.');
return new error_1.default('Invalid input: ' + e);
}
return (() => __awaiter(this, void 0, void 0, function* () {
var _b, _c, _d, _e, _f, _g, _h, _j, _k;
if (!this.fn.target) {
throw new error_1.default('Invalid function target: ' + this.fn.target, {
exit: 2
});
}
const accessToken = yield credential_1.default.getAccessToken();
yield fs_1.ASYNC.ensureFile(this.responseFile, true);
yield fs_1.ASYNC.ensureFile(this.metaFile, true);
if (((_b = this.fn.target) === null || _b === void 0 ? void 0 : _b.type) === constants_1.FN_TYPE.job) {
if ('params' in data) {
data.job_details = Object.assign({
job_meta_details: {
params: data.params
}
}, (data.job_details || {}));
delete data.params;
}
const jobData = data;
const jobMetaDetails = {
headers: Object.assign(Object.assign({}, (_d = (_c = jobData === null || jobData === void 0 ? void 0 : jobData.job_details) === null || _c === void 0 ? void 0 : _c.job_meta_details) === null || _d === void 0 ? void 0 : _d.headers), {
'x-zc-projectid': (0, project_1.getProjectId)(),
'x-zc-project-domain': (0, project_1.getDomainPrefix)() + '.' + constants_1.ORIGIN.app.replace('https://', ''),
'x-zc-project-key': (0, project_1.getDomainKey)(),
'x-zc-environment': (0, project_1.getEnvName)(),
'x-zc-user-cred-type': 'token',
'x-zc-user-cred-token': accessToken,
'x-zc-admin-cred-type': 'token',
'x-zc-admin-cred-token': accessToken,
'x-zc-user-type': 'admin'
}),
params: ((_f = (_e = jobData === null || jobData === void 0 ? void 0 : jobData.job_details) === null || _e === void 0 ? void 0 : _e.job_meta_details) === null || _f === void 0 ? void 0 : _f.params) || {},
jobpool_details: Object.assign({ type: 'Function', project_details: {
project_name: (0, project_1.getProjectName)(),
id: (0, project_1.getProjectId)()
} }, (_h = (_g = jobData === null || jobData === void 0 ? void 0 : jobData.job_details) === null || _g === void 0 ? void 0 : _g.job_meta_details) === null || _h === void 0 ? void 0 : _h.jobpool_details)
};
jobData.job_details = Object.assign(Object.assign({}, jobData.job_details), { capacity: jobData.capacity || {
memory: '256'
}, job_meta_details: jobMetaDetails });
jobData.capacity = jobData.capacity || {
memory: '256'
};
data = jobData;
}
if (this.isContainer) {
yield this.executeWithContainer(data, accessToken);
}
else {
yield this.execute(data, accessToken);
}
if (!this.slave) {
throw new error_1.default('Slave listening started before initializing', {
exit: 2
});
}
(_j = this.slave.stdout) === null || _j === void 0 ? void 0 : _j.on('data', (message) => {
process.stdout.write(message.toString());
});
(_k = this.slave.stderr) === null || _k === void 0 ? void 0 : _k.on('data', (message) => {
process.stderr.write(message.toString());
});
this.slave.on('error', (err) => {
this.localFnEvents.listenerCount('error') > 0 &&
this.localFnEvents.emit('error', err);
});
this.slave.once('exit', (code, sig) => __awaiter(this, void 0, void 0, function* () {
(0, logger_1.debug)(`local fn slave exit: ${code}, ${sig}`);
if (this.isContainer && [143, 130].includes(code)) {
code = 0;
}
yield this.fnResponseHandler(code, sig);
if (this.fn.debugPort !== -1) {
port_resolver_1.default.freePort(this.fn.debugPort);
}
this.repl.resume();
this.repl.showPrompt();
}));
return this.slave;
}))();
};
this.repl = repl;
const projectRoot = (0, project_1.getProjectRoot)();
this.watcher = (_a = fn.target) === null || _a === void 0 ? void 0 : _a.watcher;
if ((_c = (_b = this.fn.target) === null || _b === void 0 ? void 0 : _b.stack) === null || _c === void 0 ? void 0 : _c.startsWith(constants_1.RUNTIME.language.java.value)) {
this.javaInvoker = (0, path_1.join)(projectRoot, constants_1.FOLDERNAME.build, '.catalyst', (_d = this.fn.target) === null || _d === void 0 ? void 0 : _d.stack, 'Java' + ((_e = this.fn.target) === null || _e === void 0 ? void 0 : _e.type) + 'Invoker');
fn_utils_1.fnUtils.java.ensureJavaInvoker(this.javaInvoker, (0, path_1.normalize)((0, path_1.join)(__dirname, '../invoker', this.fn.target.type, 'java', 'Java' + this.fn.target.type + 'Invoker.java')), this.fn.target);
}
}
}
exports.SlaveManager = SlaveManager;