UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

72 lines 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DockerSpawner = exports.APP_DIR = void 0; const path_1 = require("path"); const os_1 = require("os"); const spawner_base_1 = require("./spawner-base"); const gnu_spawner_1 = require("./gnu-spawner"); const resolve_posix_path_1 = require("../resolve-posix-path"); const alwayscli_1 = require("@alwaysai/alwayscli"); const constants_1 = require("../../constants"); const environment_1 = require("../../environment"); const docker_1 = require("../docker"); exports.APP_DIR = '/app'; function DockerSpawner(opts) { const { dockerImageId, targetHardware } = opts; if (!dockerImageId) { throw new alwayscli_1.CliTerseError(constants_1.EMPTY_DOCKER_IMAGE_ID_MESSAGE); } const resolvePath = (0, resolve_posix_path_1.ResolvePosixPath)(exports.APP_DIR); const gnuSpawner = (0, gnu_spawner_1.GnuSpawner)(Object.assign({ resolvePath }, (0, spawner_base_1.SpawnerBase)(translate))); return Object.assign(Object.assign({}, gnuSpawner), { rimraf(path) { if (!path || resolvePath(path) === resolvePath()) { throw new Error('Refusing to delete whole directory because it is mirrored'); } return gnuSpawner.rimraf(path); } }); function translate(cmd) { if (environment_1.ALWAYSAI_OS_PLATFORM !== 'linux') { throw new alwayscli_1.CliTerseError('Docker is only supported on Linux'); } const containerTokenDir = cmd.superuser ? '/root/.config/alwaysai' : '/.config/alwaysai'; const volumes = [ `${process.cwd()}:${resolvePath()}`, '/dev:/dev', '/etc/timezone:/etc/timezone:ro', '/etc/localtime:/etc/localtime:ro', `${(0, path_1.join)((0, os_1.homedir)(), '.config', 'alwaysai')}:${containerTokenDir}` ]; if (opts.volumes) { volumes.push(...opts.volumes); } const env_vars = []; if (opts.env_vars) { env_vars.push(...opts.env_vars); } const uid = process.getuid ? process.getuid() : ''; const gid = process.getgid ? process.getgid() : ''; const args = (0, docker_1.getDockerRunCmd)({ dockerImageId, remove: true, interactive: true, tty: cmd.tty, volumes, env_vars, user: cmd.superuser ? undefined : `${uid}:${gid}`, targetHardware, workdir: resolvePath(cmd.cwd), exe: cmd.exe, exeArgs: cmd.args }); const translated = { exe: 'docker', args, input: cmd.input }; return translated; } } exports.DockerSpawner = DockerSpawner; //# sourceMappingURL=docker-spawner.js.map