Skip to content

Commit

Permalink
[os-update] Use detached HUP for os updates
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jaomaloy committed Oct 25, 2024
1 parent 0336eed commit 3d97cba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 49 deletions.
15 changes: 13 additions & 2 deletions src/commands/device/os-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 deviceUUID --view`,
);
})
.catch((error) => {
console.error(`Failed to start OS update for device ${uuid}:`, error);
});
}
}
48 changes: 1 addition & 47 deletions src/utils/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3d97cba

Please sign in to comment.