[os-update] Use detached HUP for os updates

This will not track HUP progress when we call
OS update in the CLI but will allow for more
reliable OS updates by default.

Change-type: major
This commit is contained in:
jaomaloy 2024-10-21 21:06:02 +08:00
parent 0336eed004
commit 3946df2c99
No known key found for this signature in database
GPG Key ID: 21EED739A5B948D5
2 changed files with 14 additions and 49 deletions

View File

@ -146,7 +146,18 @@ export default class DeviceOsUpdateCmd extends Command {
'Host OS updates require a device restart when they complete. Are you sure you want to proceed?',
);
await sdk.models.device.startOsUpdate(uuid, targetOsVersion);
await patterns.awaitDeviceOsUpdate(uuid, targetOsVersion);
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);
});
}
}

View File

@ -30,9 +30,8 @@ import {
ExpectedError,
NotAvailableInOfflineModeError,
} from '../errors';
import { getBalenaSdk, getVisuals, stripIndent, getCliForm } from './lazy';
import { getBalenaSdk, stripIndent, getCliForm } from './lazy';
import validation = require('./validation');
import { delay } from './helpers';
export function authenticate(options: object): Promise<void> {
const balena = getBalenaSdk();
@ -286,51 +285,6 @@ export async function getAndSelectOrganization() {
}
}
export async function awaitDeviceOsUpdate(
uuid: string,
targetOsVersion: string,
) {
const balena = getBalenaSdk();
const deviceName = await balena.models.device.getName(uuid);
const visuals = getVisuals();
const progressBar = new visuals.Progress(
`Updating the OS of ${deviceName} to v${targetOsVersion}`,
);
progressBar.update({ percentage: 0 });
const poll = async (): Promise<void> => {
const [osUpdateStatus, { overall_progress: osUpdateProgress }] =
await Promise.all([
balena.models.device.getOsUpdateStatus(uuid),
balena.models.device.get(uuid, { $select: 'overall_progress' }),
]);
if (osUpdateStatus.status === 'done') {
console.info(
`The device ${deviceName} has been updated to v${targetOsVersion} and will restart shortly!`,
);
return;
}
if (osUpdateStatus.error) {
throw new ExpectedError(
`Failed to complete Host OS update on device ${deviceName}\n${osUpdateStatus.error}`,
);
}
if (osUpdateProgress !== null) {
// Avoid resetting to 0% at end of process when device goes disconnected.
progressBar.update({ percentage: osUpdateProgress });
}
await delay(3000);
await poll();
};
await poll();
return uuid;
}
/*
* Given fleetOrDevice, which may be
* - a fleet name