UNPKG

alwaysai

Version:

The alwaysAI command-line interface (CLI)

77 lines 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateProductionDeviceFunc = void 0; const alwayscli_1 = require("@alwaysai/alwayscli"); const path_1 = require("path"); const constants_1 = require("../../constants"); const device_1 = require("../../core/device"); const write_or_validate_device_cfg_file_1 = require("../../core/device/write-or-validate-device-cfg-file"); const project_1 = require("../../core/project"); const infrastructure_1 = require("../../infrastructure"); const certificate_paths_1 = require("../../infrastructure/certificate-paths"); const device_paths_1 = require("../../infrastructure/device-paths"); const paths_1 = require("../../paths"); const util_1 = require("../../util"); const user_1 = require("../user"); async function generateProductionDeviceFunc(props) { const { name, description } = props; await (0, user_1.checkUserIsLoggedInComponent)({ yes: false }); await (0, project_1.requirePaidPlan)(); const { username } = await (0, infrastructure_1.CliAuthenticationClient)().getInfo(); const deviceToAdd = { owner: username, friendly_name: name, host_name: '', device_user_name: '', description: description !== null && description !== void 0 ? description : '', hardware_ids: '', device_hash: '', iotKeys: '' }; try { const response = await generateDevice(deviceToAdd); const outDir = (0, path_1.join)(process.cwd(), response.deviceUUID); const aaiDir = (0, path_1.join)(outDir, paths_1.AAI_CONFIG_DIRECTORY_NAME); const cfgDir = (0, path_1.join)(aaiDir, (0, device_paths_1.getDeviceConfigPath)()); await (0, util_1.runWithSpinner)(async () => { // Write tokens, deviceId and systemId to // {cwd}/{UUID}/.config/alwaysai/device{getSystemId()}/alwaysai.device.json // and write systemId to // {cwd}/{UUID}/.config/alwaysai/alwaysai.config.json const tokenSpawner = (0, util_1.JsSpawner)({ path: cfgDir }); const certSpawner = (0, util_1.JsSpawner)({ path: (0, path_1.join)(cfgDir, certificate_paths_1.DEVICE_CERTIFICATES_DIRECTORY_NAME) }); const aaiConfigFile = new infrastructure_1.LocalAaiCfg(aaiDir); await aaiConfigFile.writeAaiCfgFile(); await (0, write_or_validate_device_cfg_file_1.writeDeviceCfgFile)({ spawner: tokenSpawner, deviceUuid: response.deviceUUID, accessToken: response.accessToken, refreshToken: response.refreshToken, idToken: response.idToken }); await (0, device_1.writeCertificate)({ spawner: certSpawner, certificate: response.iotKeys.certificatePem, privateKey: response.iotKeys.keyPair.PrivateKey, rootCa: response.iotKeys.rootCA }); }, [], `Write device credentials to ${outDir}`); (0, util_1.echo)(`Created ${name}: device ID=${response.deviceUUID}`); return response.deviceUUID; } catch (error) { util_1.logger.error((0, util_1.stringifyError)(error)); throw new alwayscli_1.CliTerseError(`There was an error with creating the device. ${constants_1.PLEASE_REPORT_THIS_ERROR_MESSAGE}`); } } exports.generateProductionDeviceFunc = generateProductionDeviceFunc; async function generateDevice(deviceToAdd) { let response; await (0, util_1.runWithSpinner)(async () => { response = await (0, infrastructure_1.addDevice)(deviceToAdd, 'production'); }, [], 'Create new production device'); return response; } //# sourceMappingURL=generate-production-device.js.map