UNPKG

balena-cli

Version:

The official balena Command Line Interface

126 lines (123 loc) 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const lazy_1 = require("../../utils/lazy"); class DeviceRestartCmd extends core_1.Command { async run() { var _a; const { args: params, flags: options } = await this.parse(DeviceRestartCmd); const balena = (0, lazy_1.getBalenaSdk)(); const ux = (0, lazy_1.getCliUx)(); const deviceUuids = params.uuid.split(','); const serviceNames = (_a = options.service) === null || _a === void 0 ? void 0 : _a.split(','); for (const uuid of deviceUuids) { ux.action.start(`Restarting services on device ${uuid}`); if (serviceNames) { await this.restartServices(balena, uuid, serviceNames); } else { await this.restartAllServices(balena, uuid); } ux.action.stop(); } } async restartServices(balena, deviceUuid, serviceNames) { const { ExpectedError, instanceOf } = await Promise.resolve().then(() => require('../../errors')); const { getExpandedProp } = await Promise.resolve().then(() => require('../../utils/pine')); let device; try { device = await balena.models.device.getWithServiceDetails(deviceUuid, { $expand: { is_running__release: { $select: 'commit' }, }, }); } catch (e) { const { BalenaDeviceNotFound } = await Promise.resolve().then(() => require('balena-errors')); if (instanceOf(e, BalenaDeviceNotFound)) { throw new ExpectedError(`Device ${deviceUuid} not found.`); } else { throw e; } } const activeRelease = getExpandedProp(device.is_running__release, 'commit'); serviceNames.forEach((service) => { if (!device.current_services[service]) { throw new ExpectedError(`Service ${service} not found on device ${deviceUuid}.`); } }); const restartPromises = []; for (const serviceName of serviceNames) { const service = device.current_services[serviceName]; const serviceContainer = service.find((s) => { return s.commit === activeRelease; }); if (serviceContainer) { restartPromises.push(balena.models.device.restartService(deviceUuid, serviceContainer.image_id)); } } try { await Promise.all(restartPromises); } catch (e) { if (e.message.toLowerCase().includes('no online device')) { throw new ExpectedError(`Device ${deviceUuid} is not online.`); } else { throw e; } } } async restartAllServices(balena, deviceUuid) { const { instanceOf, ExpectedError } = await Promise.resolve().then(() => require('../../errors')); try { const device = await balena.models.device.get(deviceUuid, { $select: 'is_connected_to_vpn', }); if (!device.is_connected_to_vpn) { throw new ExpectedError(`Device ${deviceUuid} is not online.`); } } catch (e) { const { BalenaDeviceNotFound } = await Promise.resolve().then(() => require('balena-errors')); if (instanceOf(e, BalenaDeviceNotFound)) { throw new ExpectedError(`Device ${deviceUuid} not found.`); } else { throw e; } } await balena.models.device.restartApplication(deviceUuid); } } DeviceRestartCmd.description = (0, lazy_1.stripIndent) ` Restart containers on a device. Restart containers on a device. If the --service flag is provided, then only those services' containers will be restarted, otherwise all containers on the device will be restarted. Multiple devices and services may be specified with a comma-separated list of values (no spaces). Note this does not reboot the device, to do so use instead \`balena device reboot\`. `; DeviceRestartCmd.examples = [ '$ balena device restart 23c73a1', '$ balena device restart 55d43b3,23c73a1', '$ balena device restart 23c73a1 --service myService', '$ balena device restart 23c73a1 -s myService1,myService2', ]; DeviceRestartCmd.args = { uuid: core_1.Args.string({ description: 'comma-separated list (no blank spaces) of device UUIDs to restart', required: true, }), }; DeviceRestartCmd.flags = { service: core_1.Flags.string({ description: 'comma-separated list (no blank spaces) of service names to restart', char: 's', }), }; DeviceRestartCmd.authenticated = true; exports.default = DeviceRestartCmd; //# sourceMappingURL=restart.js.map