alwaysai
Version:
The alwaysAI command-line interface (CLI)
78 lines • 3.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deviceAddComponent = void 0;
const alwayscli_1 = require("@alwaysai/alwayscli");
const constants_1 = require("../../constants");
const Sentry = require("@sentry/node");
const util_1 = require("../../util");
const app_1 = require("../../core/app");
const infrastructure_1 = require("../../infrastructure");
const target_hostname_check_component_1 = require("./target-hostname-check-component");
async function deviceAddComponent(props) {
const { yes } = props;
if (yes) {
throw new alwayscli_1.CliTerseError('Adding a new device is not supported with --yes');
}
const { username } = await (0, infrastructure_1.CliAuthenticationClient)().getInfo();
await enforceDevModeLimit({ username });
const deviceMode = 'development';
const friendlyName = await (0, util_1.promptForInput)({
purpose: 'to choose a device friendly name',
questions: [
{
type: 'text',
name: 'name',
message: 'Enter a device friendly name:'
}
]
});
const hostname = await (0, target_hostname_check_component_1.targetHostnameCheckComponent)({
targetHostname: props.hostname,
prompt: true
});
const hardwareId = await (0, app_1.getTargetHardwareUuid)((0, util_1.SshSpawner)({ targetHostname: hostname }));
const deviceUserName = await (0, util_1.SshSpawner)({ targetHostname: hostname }).run({
exe: 'whoami'
});
const deviceToAdd = {
owner: username,
friendly_name: friendlyName.name,
host_name: hostname,
device_user_name: deviceUserName,
description: '',
hardware_ids: hardwareId,
device_hash: '',
iotKeys: ''
};
let response;
try {
response = await (0, infrastructure_1.addDevice)(deviceToAdd, deviceMode);
}
catch (error) {
util_1.logger.error((0, util_1.stringifyError)(error));
Sentry.captureException(error);
throw new alwayscli_1.CliTerseError(`There was an error with adding a device. ${constants_1.PLEASE_REPORT_THIS_ERROR_MESSAGE}`);
}
const device = Object.assign(Object.assign({}, deviceToAdd), { id: response.deviceId, uuid: response.deviceUUID, mode: deviceMode, deleted: false, created_at: '', updated_at: '', cognito_device_key: '' });
return device;
}
exports.deviceAddComponent = deviceAddComponent;
async function enforceDevModeLimit(props) {
const client = (0, infrastructure_1.CliRpcClient)();
const teams = await client.listTeamsUserIsMember({
user_name: props.username
});
const teamLimits = await client.checkTeamLimits({
team_id: teams[0].id
});
if (teamLimits.enforce_limits) {
const { dev_devices } = teamLimits;
const available = dev_devices.limit !== null
? Math.max(dev_devices.limit - dev_devices.used, 0)
: null;
if (!available) {
throw new alwayscli_1.CliTerseError(`No available development devices left.`);
}
}
}
//# sourceMappingURL=device-add-component.js.map