alwaysai
Version:
The alwaysAI command-line interface (CLI)
170 lines • 5.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runDockerContainerForeground = exports.runDockerContainer = exports.getDockerRunCmd = exports.pullDockerImage = exports.buildDockerImage = void 0;
const alwayscli_1 = require("@alwaysai/alwayscli");
const environment_1 = require("../../environment");
const logger_1 = require("../logger");
const stringify_error_1 = require("../stringify-error");
async function buildDockerImage(props) {
const { targetHostSpawner, targetHardware, dockerImageTag, dockerfilePath, pullBaseImage, runInForeground, logger } = props;
let args = ['build', '--network=host'];
if (targetHardware) {
args = args.concat(['--build-arg', `ALWAYSAI_HW=${targetHardware}`]);
}
if (dockerImageTag) {
args = args.concat(['-t', dockerImageTag]);
}
if (dockerfilePath) {
args = args.concat(['-f', dockerfilePath]);
}
if (pullBaseImage) {
args = args.concat(['--pull']);
}
// first run to see build logs
if (runInForeground) {
// On Windows, stream to stdout to prevent garbled log output
if (process.platform === 'win32') {
const dockerLogs = await targetHostSpawner.runStreaming({
exe: 'docker',
args: args.concat(['.']),
cwd: '.'
});
dockerLogs.pipe(process.stdout);
}
else {
targetHostSpawner.runForegroundSync({
exe: 'docker',
args: args.concat(['.']),
cwd: '.'
});
}
}
else {
// NOTE: Seeing errors with streaming logs to readable stream so sending all-at-once for now.
// See:
// * https://github.com/winstonjs/winston/issues/1339
// * https://github.com/sponja23/winston-stream-wrapper/blob/main/src/index.ts
const dockerLogs = await targetHostSpawner.run({
exe: 'docker',
args: args.concat(['.']),
cwd: '.'
});
logger === null || logger === void 0 ? void 0 : logger.debug(dockerLogs);
}
// second run returns an output - docker does not rebuild it but it returns the success message
const output = await targetHostSpawner.run({
exe: 'docker',
args: args.concat(['--quiet', '.']),
cwd: '.'
});
const dockerImageId = output.trim();
logger === null || logger === void 0 ? void 0 : logger.info(`Built ${dockerImageId}`);
return dockerImageId;
}
exports.buildDockerImage = buildDockerImage;
async function pullDockerImage(props) {
const { targetHostSpawner, dockerImageId } = props;
const dockerArgs = ['pull', dockerImageId];
try {
await targetHostSpawner.run({
exe: 'docker',
args: dockerArgs,
cwd: '.'
});
}
catch (err) {
logger_1.logger.error((0, stringify_error_1.stringifyError)(err));
throw new alwayscli_1.CliTerseError(`Pull access denied for ${dockerImageId}, repository does not exist or may require 'docker login'.`);
}
}
exports.pullDockerImage = pullDockerImage;
function getDockerRunCmd(cmd) {
var _a, _b, _c;
const args = ['run', '--privileged'];
if (environment_1.ALWAYSAI_SYSTEM_ID) {
args.push('--env', `ALWAYSAI_SYSTEM_ID=${environment_1.ALWAYSAI_SYSTEM_ID}`);
}
if (cmd.remove) {
args.push('--rm');
}
if (cmd.pullImage) {
args.push('--pull', 'always');
}
if (cmd.interactive) {
args.push('--interactive');
}
if (cmd.tty) {
args.push('--tty');
}
if (cmd.volumes) {
for (const v of cmd.volumes) {
args.push('--volume', v);
}
}
if (cmd.env_vars) {
for (const e of cmd.env_vars) {
args.push('--env', e);
}
}
if (cmd.user) {
args.push('--user', cmd.user);
}
if (cmd.ports) {
for (const p of cmd.ports) {
args.push('--publish', `127.0.0.1:${p}:${p}/tcp`);
}
}
else {
args.push('--network=host');
}
if (((_a = cmd.targetHardware) === null || _a === void 0 ? void 0 : _a.includes('jetson')) ||
((_b = cmd.targetHardware) === null || _b === void 0 ? void 0 : _b.includes('x86-trt'))) {
args.push('--runtime=nvidia');
}
if ((_c = cmd.targetHardware) === null || _c === void 0 ? void 0 : _c.includes('jetson')) {
args.push('--ipc=host');
args.push('--volume');
args.push('/tmp/argus_socket:/tmp/argus_socket');
}
if (cmd.workdir) {
args.push('--workdir', cmd.workdir);
}
if (cmd.detach) {
args.push('--detach');
}
if (cmd.restart) {
args.push('--restart', cmd.restart);
}
args.push(cmd.dockerImageId);
if (cmd.exe) {
args.push(cmd.exe);
if (cmd.exeArgs) {
args.push(...cmd.exeArgs);
}
}
return args;
}
exports.getDockerRunCmd = getDockerRunCmd;
async function runDockerContainer(props) {
const { targetHostSpawner, cmd } = props;
const dockerArgs = getDockerRunCmd(cmd);
const output = await targetHostSpawner.run({
exe: 'docker',
args: dockerArgs,
cwd: '.'
});
const containerId = output.trim();
return containerId;
}
exports.runDockerContainer = runDockerContainer;
async function runDockerContainerForeground(props) {
const { targetHostSpawner, cmd } = props;
const dockerArgs = getDockerRunCmd(cmd);
await targetHostSpawner.runForeground({
exe: 'docker',
args: dockerArgs,
cwd: '.'
});
}
exports.runDockerContainerForeground = runDockerContainerForeground;
//# sourceMappingURL=docker-cmd.js.map