balena-cli 17.2.4 → 17.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file
4
4
  automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
5
5
  This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## 17.3.0 - 2023-11-06
8
+
9
+ * Add device start-service and stop-service commands [Morgan Larsson]
10
+
7
11
  ## 17.2.4 - 2023-11-06
8
12
 
9
13
  * Dedupe shrinkwrap [myarmolinsky]
@@ -0,0 +1,17 @@
1
+ import Command from '../../command';
2
+ import type { BalenaSDK } from 'balena-sdk';
3
+ export default class DeviceStartServiceCmd extends Command {
4
+ static description: string;
5
+ static examples: string[];
6
+ static args: {
7
+ uuid: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
8
+ service: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
9
+ };
10
+ static usage: string;
11
+ static flags: {
12
+ help: import("@oclif/core/lib/interfaces").BooleanFlag<void>;
13
+ };
14
+ static authenticated: boolean;
15
+ run(): Promise<void>;
16
+ startServices(balena: BalenaSDK, deviceUuid: string, serviceNames: string[]): Promise<void>;
17
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@oclif/core");
4
+ const command_1 = require("../../command");
5
+ const cf = require("../../utils/common-flags");
6
+ const lazy_1 = require("../../utils/lazy");
7
+ class DeviceStartServiceCmd extends command_1.default {
8
+ async run() {
9
+ const { args: params } = await this.parse(DeviceStartServiceCmd);
10
+ const balena = (0, lazy_1.getBalenaSdk)();
11
+ const ux = (0, lazy_1.getCliUx)();
12
+ const deviceUuids = params.uuid.split(',');
13
+ const serviceNames = params.service.split(',');
14
+ for (const uuid of deviceUuids) {
15
+ ux.action.start(`Starting services on device ${uuid}`);
16
+ await this.startServices(balena, uuid, serviceNames);
17
+ ux.action.stop();
18
+ }
19
+ }
20
+ async startServices(balena, deviceUuid, serviceNames) {
21
+ const { ExpectedError } = await Promise.resolve().then(() => require('../../errors'));
22
+ const { getExpandedProp } = await Promise.resolve().then(() => require('../../utils/pine'));
23
+ const device = await balena.models.device.getWithServiceDetails(deviceUuid, {
24
+ $expand: {
25
+ is_running__release: { $select: 'commit' },
26
+ },
27
+ });
28
+ const activeReleaseCommit = getExpandedProp(device.is_running__release, 'commit');
29
+ serviceNames.forEach((service) => {
30
+ if (!device.current_services[service]) {
31
+ throw new ExpectedError(`Service ${service} not found on device ${deviceUuid}.`);
32
+ }
33
+ });
34
+ const startPromises = [];
35
+ for (const serviceName of serviceNames) {
36
+ const service = device.current_services[serviceName];
37
+ const serviceContainer = service.find((s) => {
38
+ return s.commit === activeReleaseCommit;
39
+ });
40
+ if (serviceContainer) {
41
+ startPromises.push(balena.models.device.startService(deviceUuid, serviceContainer.image_id));
42
+ }
43
+ }
44
+ try {
45
+ await Promise.all(startPromises);
46
+ }
47
+ catch (e) {
48
+ if (e.message.toLowerCase().includes('no online device')) {
49
+ throw new ExpectedError(`Device ${deviceUuid} is not online.`);
50
+ }
51
+ else {
52
+ throw e;
53
+ }
54
+ }
55
+ }
56
+ }
57
+ DeviceStartServiceCmd.description = (0, lazy_1.stripIndent) `
58
+ Start containers on a device.
59
+
60
+ Start containers on a device.
61
+
62
+ Multiple devices and services may be specified with a comma-separated list
63
+ of values (no spaces).
64
+ `;
65
+ DeviceStartServiceCmd.examples = [
66
+ '$ balena device start-service 23c73a1 myService',
67
+ '$ balena device start-service 23c73a1 myService1,myService2',
68
+ ];
69
+ DeviceStartServiceCmd.args = {
70
+ uuid: core_1.Args.string({
71
+ description: 'comma-separated list (no blank spaces) of device UUIDs',
72
+ required: true,
73
+ }),
74
+ service: core_1.Args.string({
75
+ description: 'comma-separated list (no blank spaces) of service names',
76
+ required: true,
77
+ }),
78
+ };
79
+ DeviceStartServiceCmd.usage = 'device start-service <uuid>';
80
+ DeviceStartServiceCmd.flags = {
81
+ help: cf.help,
82
+ };
83
+ DeviceStartServiceCmd.authenticated = true;
84
+ exports.default = DeviceStartServiceCmd;
85
+ //# sourceMappingURL=start-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-service.js","sourceRoot":"","sources":["../../../lib/commands/device/start-service.ts"],"names":[],"mappings":";;AAiBA,sCAAmC;AACnC,2CAAoC;AACpC,+CAA+C;AAC/C,2CAAuE;AAGvE,MAAqB,qBAAsB,SAAQ,iBAAO;IAiClD,KAAK,CAAC,GAAG;QACf,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;QAEjE,MAAM,MAAM,GAAG,IAAA,mBAAY,GAAE,CAAC;QAC9B,MAAM,EAAE,GAAG,IAAA,eAAQ,GAAE,CAAC;QAEtB,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAK/C,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE;YAC/B,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,IAAI,EAAE,CAAC,CAAC;YACvD,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC;YACrD,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;SACjB;IACF,CAAC;IAED,KAAK,CAAC,aAAa,CAClB,MAAiB,EACjB,UAAkB,EAClB,YAAsB;QAEtB,MAAM,EAAE,aAAa,EAAE,GAAG,2CAAa,cAAc,EAAC,CAAC;QACvD,MAAM,EAAE,eAAe,EAAE,GAAG,2CAAa,kBAAkB,EAAC,CAAC;QAG7D,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,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,GAAG,IAAA,kBAAW,EAAA;;;;;;;GAOrC,CAAC;AACW,8BAAQ,GAAG;IACxB,iDAAiD;IACjD,6DAA6D;CAC7D,CAAC;AAEY,0BAAI,GAAG;IACpB,IAAI,EAAE,WAAI,CAAC,MAAM,CAAC;QACjB,WAAW,EAAE,wDAAwD;QACrE,QAAQ,EAAE,IAAI;KACd,CAAC;IACF,OAAO,EAAE,WAAI,CAAC,MAAM,CAAC;QACpB,WAAW,EAAE,yDAAyD;QACtE,QAAQ,EAAE,IAAI;KACd,CAAC;CACF,CAAC;AAEY,2BAAK,GAAG,6BAA6B,CAAC;AAEtC,2BAAK,GAAG;IACrB,IAAI,EAAE,EAAE,CAAC,IAAI;CACb,CAAC;AAEY,mCAAa,GAAG,IAAI,CAAC;kBA/Bf,qBAAqB"}
@@ -0,0 +1,17 @@
1
+ import Command from '../../command';
2
+ import type { BalenaSDK } from 'balena-sdk';
3
+ export default class DeviceStopServiceCmd extends Command {
4
+ static description: string;
5
+ static examples: string[];
6
+ static args: {
7
+ uuid: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
8
+ service: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
9
+ };
10
+ static usage: string;
11
+ static flags: {
12
+ help: import("@oclif/core/lib/interfaces").BooleanFlag<void>;
13
+ };
14
+ static authenticated: boolean;
15
+ run(): Promise<void>;
16
+ stopServices(balena: BalenaSDK, deviceUuid: string, serviceNames: string[]): Promise<void>;
17
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@oclif/core");
4
+ const command_1 = require("../../command");
5
+ const cf = require("../../utils/common-flags");
6
+ const lazy_1 = require("../../utils/lazy");
7
+ class DeviceStopServiceCmd extends command_1.default {
8
+ async run() {
9
+ const { args: params } = await this.parse(DeviceStopServiceCmd);
10
+ const balena = (0, lazy_1.getBalenaSdk)();
11
+ const ux = (0, lazy_1.getCliUx)();
12
+ const deviceUuids = params.uuid.split(',');
13
+ const serviceNames = params.service.split(',');
14
+ for (const uuid of deviceUuids) {
15
+ ux.action.start(`Stopping services on device ${uuid}`);
16
+ await this.stopServices(balena, uuid, serviceNames);
17
+ ux.action.stop();
18
+ }
19
+ }
20
+ async stopServices(balena, deviceUuid, serviceNames) {
21
+ const { ExpectedError } = await Promise.resolve().then(() => require('../../errors'));
22
+ const { getExpandedProp } = await Promise.resolve().then(() => require('../../utils/pine'));
23
+ const device = await balena.models.device.getWithServiceDetails(deviceUuid, {
24
+ $expand: {
25
+ is_running__release: { $select: 'commit' },
26
+ },
27
+ });
28
+ const activeReleaseCommit = getExpandedProp(device.is_running__release, 'commit');
29
+ serviceNames.forEach((service) => {
30
+ if (!device.current_services[service]) {
31
+ throw new ExpectedError(`Service ${service} not found on device ${deviceUuid}.`);
32
+ }
33
+ });
34
+ const stopPromises = [];
35
+ for (const serviceName of serviceNames) {
36
+ const service = device.current_services[serviceName];
37
+ const serviceContainer = service.find((s) => {
38
+ return s.commit === activeReleaseCommit;
39
+ });
40
+ if (serviceContainer) {
41
+ stopPromises.push(balena.models.device.stopService(deviceUuid, serviceContainer.image_id));
42
+ }
43
+ }
44
+ try {
45
+ await Promise.all(stopPromises);
46
+ }
47
+ catch (e) {
48
+ if (e.message.toLowerCase().includes('no online device')) {
49
+ throw new ExpectedError(`Device ${deviceUuid} is not online.`);
50
+ }
51
+ else {
52
+ throw e;
53
+ }
54
+ }
55
+ }
56
+ }
57
+ DeviceStopServiceCmd.description = (0, lazy_1.stripIndent) `
58
+ Stop containers on a device.
59
+
60
+ Stop containers on a device.
61
+
62
+ Multiple devices and services may be specified with a comma-separated list
63
+ of values (no spaces).
64
+ `;
65
+ DeviceStopServiceCmd.examples = [
66
+ '$ balena device stop-service 23c73a1 myService',
67
+ '$ balena device stop-service 23c73a1 myService1,myService2',
68
+ ];
69
+ DeviceStopServiceCmd.args = {
70
+ uuid: core_1.Args.string({
71
+ description: 'comma-separated list (no blank spaces) of device UUIDs',
72
+ required: true,
73
+ }),
74
+ service: core_1.Args.string({
75
+ description: 'comma-separated list (no blank spaces) of service names',
76
+ required: true,
77
+ }),
78
+ };
79
+ DeviceStopServiceCmd.usage = 'device stop-service <uuid>';
80
+ DeviceStopServiceCmd.flags = {
81
+ help: cf.help,
82
+ };
83
+ DeviceStopServiceCmd.authenticated = true;
84
+ exports.default = DeviceStopServiceCmd;
85
+ //# sourceMappingURL=stop-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stop-service.js","sourceRoot":"","sources":["../../../lib/commands/device/stop-service.ts"],"names":[],"mappings":";;AAiBA,sCAAmC;AACnC,2CAAoC;AACpC,+CAA+C;AAC/C,2CAAuE;AAGvE,MAAqB,oBAAqB,SAAQ,iBAAO;IAiCjD,KAAK,CAAC,GAAG;QACf,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;QAEhE,MAAM,MAAM,GAAG,IAAA,mBAAY,GAAE,CAAC;QAC9B,MAAM,EAAE,GAAG,IAAA,eAAQ,GAAE,CAAC;QAEtB,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC3C,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAK/C,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE;YAC/B,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,IAAI,EAAE,CAAC,CAAC;YACvD,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC;YACpD,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;SACjB;IACF,CAAC;IAED,KAAK,CAAC,YAAY,CACjB,MAAiB,EACjB,UAAkB,EAClB,YAAsB;QAEtB,MAAM,EAAE,aAAa,EAAE,GAAG,2CAAa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AM,IAAI,aAAa,CAAC,UAAU,UAAU,iBAAiB,CAAC,CAAC;aAC/D;iBAAM;gBACN,MAAM,CAAC,CAAC;aACR;SACD;IACF,CAAC;;AAjHa,gCAAW,GAAG,IAAA,kBAAW,EAAA;;;;;;;GAOrC,CAAC;AACW,6BAAQ,GAAG;IACxB,gDAAgD;IAChD,4DAA4D;CAC5D,CAAC;AAEY,yBAAI,GAAG;IACpB,IAAI,EAAE,WAAI,CAAC,MAAM,CAAC;QACjB,WAAW,EAAE,wDAAwD;QACrE,QAAQ,EAAE,IAAI;KACd,CAAC;IACF,OAAO,EAAE,WAAI,CAAC,MAAM,CAAC;QACpB,WAAW,EAAE,yDAAyD;QACtE,QAAQ,EAAE,IAAI;KACd,CAAC;CACF,CAAC;AAEY,0BAAK,GAAG,4BAA4B,CAAC;AAErC,0BAAK,GAAG;IACrB,IAAI,EAAE,EAAE,CAAC,IAAI;CACb,CAAC;AAEY,kCAAa,GAAG,IAAI,CAAC;kBA/Bf,oBAAoB"}
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2016-2020 Balena Ltd.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { Args } from '@oclif/core';
19
+ import Command from '../../command';
20
+ import * as cf from '../../utils/common-flags';
21
+ import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';
22
+ import type { BalenaSDK } from 'balena-sdk';
23
+
24
+ export default class DeviceStartServiceCmd extends Command {
25
+ public static description = stripIndent`
26
+ Start containers on a device.
27
+
28
+ Start containers on a device.
29
+
30
+ Multiple devices and services may be specified with a comma-separated list
31
+ of values (no spaces).
32
+ `;
33
+ public static examples = [
34
+ '$ balena device start-service 23c73a1 myService',
35
+ '$ balena device start-service 23c73a1 myService1,myService2',
36
+ ];
37
+
38
+ public static args = {
39
+ uuid: Args.string({
40
+ description: 'comma-separated list (no blank spaces) of device UUIDs',
41
+ required: true,
42
+ }),
43
+ service: Args.string({
44
+ description: 'comma-separated list (no blank spaces) of service names',
45
+ required: true,
46
+ }),
47
+ };
48
+
49
+ public static usage = 'device start-service <uuid>';
50
+
51
+ public static flags = {
52
+ help: cf.help,
53
+ };
54
+
55
+ public static authenticated = true;
56
+
57
+ public async run() {
58
+ const { args: params } = await this.parse(DeviceStartServiceCmd);
59
+
60
+ const balena = getBalenaSdk();
61
+ const ux = getCliUx();
62
+
63
+ const deviceUuids = params.uuid.split(',');
64
+ const serviceNames = params.service.split(',');
65
+
66
+ // Iterate sequentially through deviceUuids.
67
+ // We may later want to add a batching feature,
68
+ // so that n devices are processed in parallel
69
+ for (const uuid of deviceUuids) {
70
+ ux.action.start(`Starting services on device ${uuid}`);
71
+ await this.startServices(balena, uuid, serviceNames);
72
+ ux.action.stop();
73
+ }
74
+ }
75
+
76
+ async startServices(
77
+ balena: BalenaSDK,
78
+ deviceUuid: string,
79
+ serviceNames: string[],
80
+ ) {
81
+ const { ExpectedError } = await import('../../errors');
82
+ const { getExpandedProp } = await import('../../utils/pine');
83
+
84
+ // Get device
85
+ const device = await balena.models.device.getWithServiceDetails(
86
+ deviceUuid,
87
+ {
88
+ $expand: {
89
+ is_running__release: { $select: 'commit' },
90
+ },
91
+ },
92
+ );
93
+
94
+ const activeReleaseCommit = getExpandedProp(
95
+ device.is_running__release,
96
+ 'commit',
97
+ );
98
+
99
+ // Check specified services exist on this device before startinganything
100
+ serviceNames.forEach((service) => {
101
+ if (!device.current_services[service]) {
102
+ throw new ExpectedError(
103
+ `Service ${service} not found on device ${deviceUuid}.`,
104
+ );
105
+ }
106
+ });
107
+
108
+ // Start services
109
+ const startPromises: Array<Promise<void>> = [];
110
+ for (const serviceName of serviceNames) {
111
+ const service = device.current_services[serviceName];
112
+ // Each service is an array of `CurrentServiceWithCommit`
113
+ // because when service is updating, it will actually hold 2 services
114
+ // Target commit matching `device.is_running__release`
115
+ const serviceContainer = service.find((s) => {
116
+ return s.commit === activeReleaseCommit;
117
+ });
118
+
119
+ if (serviceContainer) {
120
+ startPromises.push(
121
+ balena.models.device.startService(
122
+ deviceUuid,
123
+ serviceContainer.image_id,
124
+ ),
125
+ );
126
+ }
127
+ }
128
+
129
+ try {
130
+ await Promise.all(startPromises);
131
+ } catch (e) {
132
+ if (e.message.toLowerCase().includes('no online device')) {
133
+ throw new ExpectedError(`Device ${deviceUuid} is not online.`);
134
+ } else {
135
+ throw e;
136
+ }
137
+ }
138
+ }
139
+ }
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2016-2020 Balena Ltd.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { Args } from '@oclif/core';
19
+ import Command from '../../command';
20
+ import * as cf from '../../utils/common-flags';
21
+ import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';
22
+ import type { BalenaSDK } from 'balena-sdk';
23
+
24
+ export default class DeviceStopServiceCmd extends Command {
25
+ public static description = stripIndent`
26
+ Stop containers on a device.
27
+
28
+ Stop containers on a device.
29
+
30
+ Multiple devices and services may be specified with a comma-separated list
31
+ of values (no spaces).
32
+ `;
33
+ public static examples = [
34
+ '$ balena device stop-service 23c73a1 myService',
35
+ '$ balena device stop-service 23c73a1 myService1,myService2',
36
+ ];
37
+
38
+ public static args = {
39
+ uuid: Args.string({
40
+ description: 'comma-separated list (no blank spaces) of device UUIDs',
41
+ required: true,
42
+ }),
43
+ service: Args.string({
44
+ description: 'comma-separated list (no blank spaces) of service names',
45
+ required: true,
46
+ }),
47
+ };
48
+
49
+ public static usage = 'device stop-service <uuid>';
50
+
51
+ public static flags = {
52
+ help: cf.help,
53
+ };
54
+
55
+ public static authenticated = true;
56
+
57
+ public async run() {
58
+ const { args: params } = await this.parse(DeviceStopServiceCmd);
59
+
60
+ const balena = getBalenaSdk();
61
+ const ux = getCliUx();
62
+
63
+ const deviceUuids = params.uuid.split(',');
64
+ const serviceNames = params.service.split(',');
65
+
66
+ // Iterate sequentially through deviceUuids.
67
+ // We may later want to add a batching feature,
68
+ // so that n devices are processed in parallel
69
+ for (const uuid of deviceUuids) {
70
+ ux.action.start(`Stopping services on device ${uuid}`);
71
+ await this.stopServices(balena, uuid, serviceNames);
72
+ ux.action.stop();
73
+ }
74
+ }
75
+
76
+ async stopServices(
77
+ balena: BalenaSDK,
78
+ deviceUuid: string,
79
+ serviceNames: string[],
80
+ ) {
81
+ const { ExpectedError } = await import('../../errors');
82
+ const { getExpandedProp } = await import('../../utils/pine');
83
+
84
+ // Get device
85
+ const device = await balena.models.device.getWithServiceDetails(
86
+ deviceUuid,
87
+ {
88
+ $expand: {
89
+ is_running__release: { $select: 'commit' },
90
+ },
91
+ },
92
+ );
93
+
94
+ const activeReleaseCommit = getExpandedProp(
95
+ device.is_running__release,
96
+ 'commit',
97
+ );
98
+
99
+ // Check specified services exist on this device before stoppinganything
100
+ serviceNames.forEach((service) => {
101
+ if (!device.current_services[service]) {
102
+ throw new ExpectedError(
103
+ `Service ${service} not found on device ${deviceUuid}.`,
104
+ );
105
+ }
106
+ });
107
+
108
+ // Stop services
109
+ const stopPromises: Array<Promise<void>> = [];
110
+ for (const serviceName of serviceNames) {
111
+ const service = device.current_services[serviceName];
112
+ // Each service is an array of `CurrentServiceWithCommit`
113
+ // because when service is updating, it will actually hold 2 services
114
+ // Target commit matching `device.is_running__release`
115
+ const serviceContainer = service.find((s) => {
116
+ return s.commit === activeReleaseCommit;
117
+ });
118
+
119
+ if (serviceContainer) {
120
+ stopPromises.push(
121
+ balena.models.device.stopService(
122
+ deviceUuid,
123
+ serviceContainer.image_id,
124
+ ),
125
+ );
126
+ }
127
+ }
128
+
129
+ try {
130
+ await Promise.all(stopPromises);
131
+ } catch (e) {
132
+ if (e.message.toLowerCase().includes('no online device')) {
133
+ throw new ExpectedError(`Device ${deviceUuid} is not online.`);
134
+ } else {
135
+ throw e;
136
+ }
137
+ }
138
+ }
139
+ }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "balena-cli",
3
- "version": "17.2.4",
3
+ "version": "17.3.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "balena-cli",
9
- "version": "17.2.4",
9
+ "version": "17.3.0",
10
10
  "hasInstallScript": true,
11
11
  "license": "Apache-2.0",
12
12
  "dependencies": {
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "17.2.4",
2
+ "version": "17.3.0",
3
3
  "commands": {
4
4
  "build": {
5
5
  "id": "build",
@@ -2815,6 +2815,78 @@
2815
2815
  },
2816
2816
  "authenticated": true
2817
2817
  },
2818
+ "device:start-service": {
2819
+ "id": "device:start-service",
2820
+ "description": "Start containers on a device.\n\nStart containers on a device.\n\nMultiple devices and services may be specified with a comma-separated list\nof values (no spaces).",
2821
+ "strict": true,
2822
+ "usage": "device start-service <uuid>",
2823
+ "pluginName": "balena-cli",
2824
+ "pluginAlias": "balena-cli",
2825
+ "pluginType": "core",
2826
+ "aliases": [],
2827
+ "examples": [
2828
+ "$ balena device start-service 23c73a1 myService",
2829
+ "$ balena device start-service 23c73a1 myService1,myService2"
2830
+ ],
2831
+ "flags": {
2832
+ "help": {
2833
+ "name": "help",
2834
+ "type": "boolean",
2835
+ "char": "h",
2836
+ "description": "Show CLI help.",
2837
+ "allowNo": false
2838
+ }
2839
+ },
2840
+ "args": {
2841
+ "uuid": {
2842
+ "name": "uuid",
2843
+ "description": "comma-separated list (no blank spaces) of device UUIDs",
2844
+ "required": true
2845
+ },
2846
+ "service": {
2847
+ "name": "service",
2848
+ "description": "comma-separated list (no blank spaces) of service names",
2849
+ "required": true
2850
+ }
2851
+ },
2852
+ "authenticated": true
2853
+ },
2854
+ "device:stop-service": {
2855
+ "id": "device:stop-service",
2856
+ "description": "Stop containers on a device.\n\nStop containers on a device.\n\nMultiple devices and services may be specified with a comma-separated list\nof values (no spaces).",
2857
+ "strict": true,
2858
+ "usage": "device stop-service <uuid>",
2859
+ "pluginName": "balena-cli",
2860
+ "pluginAlias": "balena-cli",
2861
+ "pluginType": "core",
2862
+ "aliases": [],
2863
+ "examples": [
2864
+ "$ balena device stop-service 23c73a1 myService",
2865
+ "$ balena device stop-service 23c73a1 myService1,myService2"
2866
+ ],
2867
+ "flags": {
2868
+ "help": {
2869
+ "name": "help",
2870
+ "type": "boolean",
2871
+ "char": "h",
2872
+ "description": "Show CLI help.",
2873
+ "allowNo": false
2874
+ }
2875
+ },
2876
+ "args": {
2877
+ "uuid": {
2878
+ "name": "uuid",
2879
+ "description": "comma-separated list (no blank spaces) of device UUIDs",
2880
+ "required": true
2881
+ },
2882
+ "service": {
2883
+ "name": "service",
2884
+ "description": "comma-separated list (no blank spaces) of service names",
2885
+ "required": true
2886
+ }
2887
+ },
2888
+ "authenticated": true
2889
+ },
2818
2890
  "device:track-fleet": {
2819
2891
  "id": "device:track-fleet",
2820
2892
  "description": "Make a device track the fleet's pinned release.\n\nMake a device track the fleet's pinned release.",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "balena-cli",
3
- "version": "17.2.4",
3
+ "version": "17.3.0",
4
4
  "description": "The official balena Command Line Interface",
5
5
  "main": "./build/app.js",
6
6
  "homepage": "https://github.com/balena-io/balena-cli",
@@ -282,6 +282,6 @@
282
282
  "windosu": "^0.3.0"
283
283
  },
284
284
  "versionist": {
285
- "publishedAt": "2023-11-06T17:12:45.031Z"
285
+ "publishedAt": "2023-11-06T19:14:55.820Z"
286
286
  }
287
287
  }