UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

240 lines 8.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TargetJsonFile = exports.targetConfigSchema = void 0; const path_1 = require("path"); const ajv_1 = require("ajv"); const chalk = require("chalk"); const alwayscli_1 = require("@alwaysai/alwayscli"); const target_protocol_1 = require("./target-protocol"); const constants_1 = require("../../constants"); const util_1 = require("../../util"); const get_target_hardware_type_1 = require("./get-target-hardware-type"); const config_nodejs_1 = require("@alwaysai/config-nodejs"); const paths_1 = require("../../paths"); exports.targetConfigSchema = { anyOf: [ { type: 'object', properties: { targetProtocol: { type: 'string', const: 'ssh+docker:' }, targetHostname: { type: 'string' }, targetPath: { type: 'string' }, dockerImageId: { type: 'string' }, targetHardware: { type: 'string', enum: get_target_hardware_type_1.TARGET_HARDWARE_OPTIONS }, deviceId: { type: 'string' } }, required: [ 'targetProtocol', 'targetHostname', 'targetPath', 'dockerImageId', 'targetHardware', 'deviceId' ] }, { type: 'object', properties: { targetProtocol: { type: 'string', const: 'docker:' }, dockerImageId: { type: 'string' }, targetHardware: { type: 'string', enum: get_target_hardware_type_1.TARGET_HARDWARE_OPTIONS } }, required: ['targetProtocol', 'dockerImageId', 'targetHardware'] }, { type: 'object', properties: { targetProtocol: { type: 'string', const: 'native:' } }, required: ['targetProtocol'] } ] }; const ajv = new ajv_1.default(); const validateFunction = ajv.compile(exports.targetConfigSchema); const DID_YOU_RUN_APP_CONFIGURE = `Did you run "${constants_1.ALWAYSAI_CLI_EXECUTABLE_NAME} app configure"?`; const ENOENT = { message: `${paths_1.TARGET_JSON_FILE_NAME} not found. ${DID_YOU_RUN_APP_CONFIGURE}`, code: alwayscli_1.CLI_TERSE_ERROR }; function TargetJsonFile(cwd = process.cwd()) { const filePath = (0, path_1.join)(cwd, paths_1.TARGET_JSON_FILE_NAME); const configFile = (0, config_nodejs_1.ConfigFileSchema)({ path: filePath, validateFunction, ENOENT }); return Object.assign(Object.assign({}, configFile), { name: paths_1.TARGET_JSON_FILE_NAME, readContainerSpawner, readHostSpawner, readTargetProtocolSafe, readFieldSafe, describe }); function describe() { const config = configFile.readIfExists(); if (!config) { return `Target configuration file "${paths_1.TARGET_JSON_FILE_NAME}" not found`; } const docker = chalk.bold('docker'); switch (config.targetProtocol) { case 'native:': { return `Target: Native alwaysAI Python on this host`; } case 'docker:': { return `Target: ${docker} container on this host`; } case 'ssh+docker:': { const hostname = chalk.bold(config.targetHostname); const path = chalk.bold(config.targetPath); return `Target: ${docker} container on ${hostname}, path ${path}`; } default: throw new Error('Unsupported protocol'); } } function readContainerSpawner(opts) { const targetJson = configFile.read(); const volumes = opts === null || opts === void 0 ? void 0 : opts.volumes; const env_vars = opts === null || opts === void 0 ? void 0 : opts.env_vars; switch (targetJson.targetProtocol) { case 'ssh+docker:': { const { targetHostname, targetPath, dockerImageId, targetHardware } = targetJson; return (0, util_1.SshDockerSpawner)({ dockerImageId, targetPath, targetHostname, targetHardware, volumes, env_vars }); } case 'docker:': { const { dockerImageId } = targetJson; const targetHardware = opts && opts.ignoreTargetHardware ? undefined : targetJson.targetHardware; return (0, util_1.DockerSpawner)({ dockerImageId, targetHardware, volumes, env_vars }); } case 'native:': default: throw new alwayscli_1.CliTerseError('Unsupported protocol'); } } function readHostSpawner() { const targetJson = configFile.read(); switch (targetJson.targetProtocol) { case 'ssh+docker:': { const { targetPath, targetHostname } = targetJson; return (0, util_1.SshSpawner)({ targetHostname, targetPath }); } case 'docker:': case 'native:': { return (0, util_1.JsSpawner)(); } default: throw new alwayscli_1.CliTerseError('Unsupported protocol'); } } function readTargetProtocolSafe() { try { const targetJson = configFile.readIfExists(); if (!targetJson) { return undefined; } return targetJson.targetProtocol; } catch (err) { configFile.remove(); return undefined; } } function readFieldSafe(props) { try { const targetJson = configFile.readIfExists(); if (!targetJson) { return undefined; } switch (targetJson.targetProtocol) { case 'ssh+docker:': { switch (props.name) { case 'targetProtocol': return target_protocol_1.TargetProtocol[targetJson.targetProtocol]; case 'targetHostname': return targetJson.targetHostname; case 'targetPath': return targetJson.targetPath; case 'dockerImageId': return targetJson.dockerImageId; case 'targetHardware': return targetJson.targetHardware; case 'deviceId': return targetJson.deviceId; default: return undefined; } } case 'docker:': { switch (props.name) { case 'targetProtocol': return target_protocol_1.TargetProtocol[targetJson.targetProtocol]; case 'dockerImageId': return targetJson.dockerImageId; case 'targetHardware': return targetJson.targetHardware; default: return undefined; } } case 'native:': { switch (props.name) { case 'targetProtocol': return target_protocol_1.TargetProtocol[targetJson.targetProtocol]; default: return undefined; } } default: throw new alwayscli_1.CliTerseError('Unsupported protocol'); } } catch (err) { util_1.logger.error((0, util_1.stringifyError)(err)); configFile.remove(); return undefined; } } } exports.TargetJsonFile = TargetJsonFile; //# sourceMappingURL=target-json-file.js.map