Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

device os-update: Add support for updates that require takeover #2890

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"balena-config-json": "^4.2.0",
"balena-device-init": "^7.0.1",
"balena-errors": "^4.7.3",
"balena-hup-action-utils": "^6.2.0-build-ui-takeover-test-570b5b071b6ce6fe12ffdf4194a616f1203147aa-1",
"balena-image-fs": "^7.0.6",
"balena-preload": "^16.0.0",
"balena-sdk": "^20.3.0",
Expand Down
88 changes: 60 additions & 28 deletions src/commands/device/os-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';
import type { Device } from 'balena-sdk';
import { ExpectedError } from '../../errors';
import { getExpandedProp } from '../../utils/pine';

export default class DeviceOsUpdateCmd extends Command {
public static description = stripIndent`
Expand Down Expand Up @@ -68,15 +69,19 @@ export default class DeviceOsUpdateCmd extends Command {
const sdk = getBalenaSdk();

// Get device info
const { uuid, is_of__device_type, os_version, os_variant } =
(await sdk.models.device.get(params.uuid, {
$select: ['uuid', 'os_version', 'os_variant'],
$expand: {
is_of__device_type: {
$select: 'slug',
},
const {
// uuid,
is_of__device_type,
os_version,
os_variant,
} = (await sdk.models.device.get(params.uuid, {
$select: ['uuid', 'os_version', 'os_variant'],
$expand: {
is_of__device_type: {
$select: 'slug',
},
})) as DeviceWithDeviceType;
},
})) as DeviceWithDeviceType;

// Get current device OS version
const currentOsVersion = sdk.models.device.getOsVersion({
Expand Down Expand Up @@ -115,6 +120,10 @@ export default class DeviceOsUpdateCmd extends Command {
);
}

const { HUPActionHelper, actionsConfig } = await import(
'balena-hup-action-utils'
);
const hupActionHelper = new HUPActionHelper(actionsConfig);
// Get target OS version
let targetOsVersion = options.version;
if (targetOsVersion != null) {
Expand All @@ -129,35 +138,58 @@ export default class DeviceOsUpdateCmd extends Command {
targetOsVersion = await getCliForm().ask({
message: 'Target OS version',
type: 'list',
choices: hupVersionInfo.versions.map((version) => ({
name:
hupVersionInfo.recommended === version
? `${version} (recommended)`
: version,
value: version,
})),
choices: hupVersionInfo.versions.map((version) => {
const takeoverRequired =
hupActionHelper.getHUPActionType(
getExpandedProp(is_of__device_type, 'slug')!,
currentOsVersion,
version,
) === 'takeover';

return {
name: `${
hupVersionInfo.recommended === version
? `${version} (recommended)`
: version
}${takeoverRequired ? ' WARNING: No rollback mechanism' : ''}`,
value: version,
};
}),
});
}

const takeoverRequired =
hupActionHelper.getHUPActionType(
getExpandedProp(is_of__device_type, 'slug')!,
currentOsVersion,
targetOsVersion,
) === 'takeover';
const patterns = await import('../../utils/patterns');
// Warn the user if the update requires a takeover
if (takeoverRequired) {
await patterns.confirm(
options.yes || false,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
);
}
// Confirm and start update
await patterns.confirm(
options.yes || false,
'Host OS updates require a device restart when they complete. Are you sure you want to proceed?',
);

await sdk.models.device
.startOsUpdate(uuid, targetOsVersion, {
runDetached: true,
})
.then(() => {
console.log(
`The balena OS update has started. You can keep track of the progress via the dashboard.\n` +
`To open the dashboard page related to a device via the CLI, you can use \`balena device UUID --view\``,
);
})
.catch((error) => {
console.error(`Failed to start OS update for device ${uuid}:`, error);
});
// await sdk.models.device
// .startOsUpdate(uuid, targetOsVersion, {
// runDetached: true,
// })
// .then(() => {
// console.log(
// `The balena OS update has started. You can keep track of the progress via the dashboard.\n` +
// `To open the dashboard page related to a device via the CLI, you can use \`balena device UUID --view\``,
// );
// })
// .catch((error) => {
// console.error(`Failed to start OS update for device ${uuid}:`, error);
// });
}
}
Loading