alwaysai
Version:
The alwaysAI command-line interface (CLI)
83 lines • 4.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deviceCheckAndUpdateComponent = void 0;
const alwayscli_1 = require("@alwaysai/alwayscli");
const path_1 = require("path");
const constants_1 = require("../../constants");
const app_1 = require("../../core/app");
const device_1 = require("../../core/device");
const infrastructure_1 = require("../../infrastructure");
const device_paths_1 = require("../../infrastructure/device-paths");
const paths_1 = require("../../paths");
const util_1 = require("../../util");
async function deviceCheckAndUpdateComponent(props) {
const { targetHostname, device, isNewDevice } = props;
const spawner = (0, util_1.SshSpawner)({
targetHostname,
targetPath: (0, path_1.join)(paths_1.REMOTE_AAI_CFG_DIR_LINUX, (0, device_paths_1.getDeviceConfigPath)())
});
const CLEAR_OLD_CONF_MESSAGE = "To remove old configurations from the device run 'aai device clean', and then run 'aai app configure' again.";
if (isNewDevice) {
await (0, util_1.runWithSpinner)(async () => {
try {
const { accessToken, refreshToken, idToken } = await (0, infrastructure_1.refreshDevice)(device.uuid);
// Write systemId to {HOME}/.config/alwaysai/alwaysai.config.json
const aaiConfigFile = new infrastructure_1.RemoteAaiCfg(targetHostname);
util_1.logger.debug('Remote alwaysai configuration instance created, attempting to write aai configuration file.');
await aaiConfigFile.writeAaiCfgFile();
// Then write tokens, device UUID and systemId to {HOME}/.config/alwaysai/device{getSystemId()/alwaysai.device.json
await (0, device_1.writeDeviceCfgFile)({
spawner,
deviceUuid: device.uuid,
accessToken,
refreshToken,
idToken
});
}
catch (e) {
util_1.logger.error((0, util_1.stringifyError)(e));
if (e.code === device_1.DeviceCfgErrors.TARGET_DEVICE_CONFIGURED) {
throw new alwayscli_1.CliTerseError(`The target device is already configured as an alwaysAI device! ${CLEAR_OLD_CONF_MESSAGE}`);
}
else {
util_1.logger.error((0, util_1.stringifyError)(e));
throw new alwayscli_1.CliTerseError(`There was an error with configuring the device. ${constants_1.PLEASE_REPORT_THIS_ERROR_MESSAGE}`);
}
}
}, [], 'Configure device');
}
else {
await (0, util_1.runWithSpinner)(async () => {
// NOTE: if hardwareId passes, write the new configuration files (assume it is the correct device)
const hardwareId = await (0, app_1.getTargetHardwareUuid)(spawner);
if (hardwareId !== device.hardware_ids) {
throw new alwayscli_1.CliTerseError(`The target device does not match the selected device! ${CLEAR_OLD_CONF_MESSAGE}`);
}
const { accessToken, refreshToken, idToken } = await (0, infrastructure_1.refreshDevice)(device.uuid);
try {
// Write systemId to {HOME}/.config/alwaysai/alwaysai.config.json
const aaiConfigFile = new infrastructure_1.RemoteAaiCfg(targetHostname);
await aaiConfigFile.writeAaiCfgFile();
// Then write tokens, device UUID and systemId to {HOME}/.config/alwaysai/{device folder}/alwaysai.device.json
await (0, device_1.writeOrValidateDeviceCfgFile)({
spawner,
deviceUuid: device.uuid,
accessToken,
idToken,
refreshToken
});
}
catch (e) {
if (e.code === device_1.DeviceCfgErrors.TARGET_DEVICE_MISMATCH) {
throw new alwayscli_1.CliTerseError(`The target device does not match the selected device! ${CLEAR_OLD_CONF_MESSAGE}`);
}
else {
util_1.logger.error((0, util_1.stringifyError)(e));
throw new alwayscli_1.CliTerseError(`There was an error with updating the device configuration. ${constants_1.PLEASE_REPORT_THIS_ERROR_MESSAGE}`);
}
}
}, [], 'Check and update device configuration and tokens');
}
}
exports.deviceCheckAndUpdateComponent = deviceCheckAndUpdateComponent;
//# sourceMappingURL=device-check-and-update-component.js.map