UNPKG

jamsocket

Version:

A CLI for the Jamsocket platform

131 lines (130 loc) 6.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const jamsocket_1 = require("../../jamsocket"); const customFlags = tslib_1.__importStar(require("../../flags")); const formatting_1 = require("../../lib/formatting"); class Spawn extends core_1.Command { async run() { const { args, flags } = await this.parse(Spawn); const env = flags.env ? Object.fromEntries(flags.env) : undefined; const parts = args.service.split('/'); if (parts.length > 2 || parts[0] === '' || parts[1] === '') { this.error(`Invalid service name: ${args.service}`); } const service = parts[0]; const environment = parts[1]; let auth; if (flags.auth) { try { auth = JSON.parse(flags.auth); } catch { this.error('Failed to parse JSON provided to --auth flag.'); } } const connectReqBody = { key: flags.key, user: flags.user, auth, }; if (flags.spawn === false) { if (connectReqBody.key === undefined) { this.error('The --no-spawn flag requires a key to be provided.'); } if (flags.tag) { this.warn('Ignoring --tag flag because --no-spawn flag was provided.'); } if (flags.cluster) { this.warn('Ignoring --cluster flag because --no-spawn flag was provided.'); } if (flags['max-idle-seconds']) { this.warn('Ignoring --max-idle-seconds flag because --no-spawn flag was provided.'); } if (flags['lifetime-limit-seconds']) { this.warn('Ignoring --lifetime-limit-seconds flag because --no-spawn flag was provided.'); } if (flags.env) { this.warn('Ignoring --env flag because --no-spawn flag was provided.'); } connectReqBody.spawn = false; } else { connectReqBody.spawn = { tag: flags.tag, cluster: flags.cluster, executable: { env, }, lifetime_limit_seconds: flags['lifetime-limit-seconds'], max_idle_seconds: flags['max-idle-seconds'], }; } const jamsocket = jamsocket_1.Jamsocket.fromEnvironment(); const responseBody = await jamsocket.connect(service, environment, connectReqBody); const appBaseUrl = jamsocket.api.getAppBaseUrl(); this.log((0, formatting_1.lightBlue)('Backend spawned!')); this.log(chalk_1.default.bold `backend id: `, (0, formatting_1.blue)(responseBody.backend_id)); this.log(chalk_1.default.bold `backend status: `, (0, formatting_1.blue)(responseBody.status ?? '-')); this.log(chalk_1.default.bold `backend url: `, (0, formatting_1.blue)(responseBody.url)); this.log(chalk_1.default.bold `status url: `, (0, formatting_1.blue)(responseBody.status_url)); this.log(chalk_1.default.bold `ready url: `, (0, formatting_1.blue)(responseBody.ready_url)); if (flags.key) { this.log(chalk_1.default.bold `spawned: `, (0, formatting_1.blue)(responseBody.spawned.toString())); } this.log(chalk_1.default.bold `dashboard: `, (0, formatting_1.lightGreen)(`${appBaseUrl}/backend/${responseBody.backend_id}`)); } } Spawn.aliases = ['connect']; Spawn.description = 'Gets a URL that can be used to connect to a session backend. Will spawn a new session backend if no key (aka lock) is provided or if no session backend is currently holding the provided key.'; Spawn.examples = [ '<%= config.bin %> <%= command.id %> my-service', '<%= config.bin %> <%= command.id %> my-service -k my-key', '<%= config.bin %> <%= command.id %> my-service -t my-tag', '<%= config.bin %> <%= command.id %> my-service -e SOME_ENV_VAR=foo -e ANOTHER_ENV_VAR=bar', '<%= config.bin %> <%= command.id %> my-service -i 60', '<%= config.bin %> <%= command.id %> my-service -l 300', '<%= config.bin %> <%= command.id %> my-service --no-spawn', '<%= config.bin %> <%= command.id %> my-service -u my-user -a \'{"foo":"my-json-data"}\'', ]; Spawn.flags = { // passing { multiple: true } here due to a bug: https://github.com/oclif/core/pull/414 env: customFlags.env({ multiple: true }), tag: core_1.Flags.string({ char: 't', description: 'An optional image tag or image digest to use when spawning a backend.', }), cluster: core_1.Flags.string({ char: 'c', description: 'The cluster to spawn the backend in (only relevant if you are running multiple clusters with Jamsocket).', }), 'max-idle-seconds': core_1.Flags.integer({ char: 'i', description: 'The max time in seconds a session backend should wait after last connection is closed before shutting down container (default is 300)', }), 'lifetime-limit-seconds': core_1.Flags.integer({ char: 'l', description: 'The max time in seconds the session backend should be allowed to run.', }), key: core_1.Flags.string({ char: 'k', description: 'If provided, fetches the session backend currently holding the given key (formerly known as a "lock"). If no session backend holds the key, or if a key is not provided, a new session backend will be spawned.', }), user: core_1.Flags.string({ char: 'u', description: 'Optional username to be associated with the URL/connection string returned by the connect command.', }), auth: core_1.Flags.string({ char: 'a', description: 'Optional serialized JSON to be passed to a session backend when connecting with the returned URL/connection string.', }), spawn: core_1.Flags.boolean({ default: true, allowNo: true, description: 'Whether to spawn a new session backend if no session backend is currently holding the provided key.', }), }; Spawn.args = [{ name: 'service', required: true, description: 'Name of service to spawn.' }]; exports.default = Spawn;