From cb26a736fc77da7f3571b998277f49956b961785 Mon Sep 17 00:00:00 2001 From: Matthew Yarmolinsky Date: Fri, 17 Jun 2022 14:46:46 +0000 Subject: [PATCH] Add device track command for pinning a device to the latest release or a specific release Change-type: minor Signed-off-by: Matthew Yarmolinsky --- completion/_balena | 2 +- completion/balena-completion.bash | 2 +- lib/commands/device/pin.ts | 103 +++++++++++++++++++++++++++++ lib/commands/device/track-fleet.ts | 64 ++++++++++++++++++ npm-shrinkwrap.json | 6 +- 5 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 lib/commands/device/pin.ts create mode 100644 lib/commands/device/track-fleet.ts diff --git a/completion/_balena b/completion/_balena index ac9252be..1959735a 100644 --- a/completion/_balena +++ b/completion/_balena @@ -12,7 +12,7 @@ _balena() { # Sub-completions api_key_cmds=( generate ) config_cmds=( generate inject read reconfigure write ) - device_cmds=( deactivate identify init local-mode move os-update public-url purge reboot register rename restart rm shutdown ) + device_cmds=( deactivate identify init local-mode move os-update pin public-url purge reboot register rename restart rm shutdown track-fleet ) devices_cmds=( supported ) env_cmds=( add rename rm ) fleet_cmds=( create purge rename restart rm ) diff --git a/completion/balena-completion.bash b/completion/balena-completion.bash index 5bdf0860..0f0a86e8 100644 --- a/completion/balena-completion.bash +++ b/completion/balena-completion.bash @@ -11,7 +11,7 @@ _balena_complete() # Sub-completions api_key_cmds="generate" config_cmds="generate inject read reconfigure write" - device_cmds="deactivate identify init local-mode move os-update public-url purge reboot register rename restart rm shutdown" + device_cmds="deactivate identify init local-mode move os-update pin public-url purge reboot register rename restart rm shutdown track-fleet" devices_cmds="supported" env_cmds="add rename rm" fleet_cmds="create purge rename restart rm" diff --git a/lib/commands/device/pin.ts b/lib/commands/device/pin.ts new file mode 100644 index 00000000..281364c4 --- /dev/null +++ b/lib/commands/device/pin.ts @@ -0,0 +1,103 @@ +/** + * @license + * Copyright 2016-2020 Balena Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { flags } from '@oclif/command'; +import type { IArg } from '@oclif/parser/lib/args'; +import Command from '../../command'; +import * as cf from '../../utils/common-flags'; +import { getBalenaSdk, stripIndent } from '../../utils/lazy'; +import { getExpandedProp } from '../../utils/pine'; + +interface FlagsDef { + help: void; +} + +interface ArgsDef { + uuid: string; + releaseToPinTo?: string; +} + +export default class DevicePinCmd extends Command { + public static description = stripIndent` + Pin a device to a release. + + Pin a device to a release. + + Note, if the commit is omitted, the currently pinned release will be printed, with instructions for how to see a list of releases + `; + public static examples = [ + '$ balena device pin 7cf02a6', + '$ balena device pin 7cf02a6 91165e5', + ]; + + public static args: Array> = [ + { + name: 'uuid', + description: 'the uuid of the device to pin to a release', + required: true, + }, + { + name: 'releaseToPinTo', + description: 'the commit of the release for the device to get pinned to', + }, + ]; + + public static usage = 'device pin [releaseToPinTo]'; + + public static flags: flags.Input = { + help: cf.help, + }; + + public static authenticated = true; + + public async run() { + const { args: params } = this.parse(DevicePinCmd); + + const balena = getBalenaSdk(); + + const device = await balena.models.device.get(params.uuid, { + $expand: { + should_be_running__release: { + $select: 'commit', + }, + belongs_to__application: { + $select: 'slug', + }, + }, + }); + + const pinnedRelease = getExpandedProp( + device.should_be_running__release, + 'commit', + ); + const appSlug = getExpandedProp(device.belongs_to__application, 'slug'); + + const releaseToPinTo = params.releaseToPinTo; + + if (!releaseToPinTo) { + console.log( + `${ + pinnedRelease + ? `This device is currently pinned to ${pinnedRelease}.` + : 'This device is not currently pinned to any release.' + } \n\nTo see a list of all releases this device can be pinned to, run \`balena releases ${appSlug}\`.`, + ); + } else { + await balena.models.device.pinToRelease(params.uuid, releaseToPinTo); + } + } +} diff --git a/lib/commands/device/track-fleet.ts b/lib/commands/device/track-fleet.ts new file mode 100644 index 00000000..8dd20a76 --- /dev/null +++ b/lib/commands/device/track-fleet.ts @@ -0,0 +1,64 @@ +/** + * @license + * Copyright 2016-2020 Balena Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { flags } from '@oclif/command'; +import type { IArg } from '@oclif/parser/lib/args'; +import Command from '../../command'; +import * as cf from '../../utils/common-flags'; +import { getBalenaSdk, stripIndent } from '../../utils/lazy'; + +interface FlagsDef { + help: void; +} + +interface ArgsDef { + uuid: string; + releaseToPinTo?: string; +} + +export default class DeviceTrackFleetCmd extends Command { + public static description = stripIndent` + Make a device track the fleet's pinned release. + + Make a device track the fleet's pinned release. + `; + public static examples = ['$ balena device track-fleet 7cf02a6']; + + public static args: Array> = [ + { + name: 'uuid', + description: "the uuid of the device to make track the fleet's release", + required: true, + }, + ]; + + public static usage = 'device track-fleet '; + + public static flags: flags.Input = { + help: cf.help, + }; + + public static authenticated = true; + + public async run() { + const { args: params } = this.parse(DeviceTrackFleetCmd); + + const balena = getBalenaSdk(); + + await balena.models.device.trackApplicationRelease(params.uuid); + } +} diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index fd651266..202af3f1 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -4595,7 +4595,7 @@ "buffer-shims": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/buffer-shims/-/buffer-shims-1.0.0.tgz", - "integrity": "sha512-Zy8ZXMyxIT6RMTeY7OP/bDndfj6bwCan7SS98CEndS6deHwWPpseeHlwarNcBim+etXnF9HBc1non5JgDaJU1g==" + "integrity": "sha1-mXjOMXOIxkmth5MCjDR37wRKi1E=" }, "buffers": { "version": "0.1.1", @@ -11433,7 +11433,7 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "multicast-dns": { - "version": "git+https://github.com/resin-io-modules/multicast-dns.git#db98d68b79bbefc936b6799de9de1038ba49f85d", + "version": "git+https://github.com/resin-io-modules/multicast-dns.git#a15c63464eb43e8925b187ed5cb9de6892e8aacc", "from": "git+https://github.com/resin-io-modules/multicast-dns.git#listen-on-all-interfaces", "requires": { "dns-packet": "^1.0.1", @@ -16888,7 +16888,7 @@ "bluebird": { "version": "3.4.7", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.4.7.tgz", - "integrity": "sha512-iD3898SR7sWVRHbiQv+sHUtHnMvC1o3nW5rAcqnq3uOn07DSAppZYUkIGslDz6gXC7HfunPe7YVBgoEJASPcHA==" + "integrity": "sha1-9y12C+Cbf3bQjtj66Ysomo0F+rM=" }, "process-nextick-args": { "version": "1.0.7",