Skip to content

Commit

Permalink
Add option for detached HUP on startOsUpdate
Browse files Browse the repository at this point in the history
This will call the v2 actions endpoint for resinhup
which runs a detached version of HUP that increases
HUP reliability on slow networks but will offer
no status updates such as in_progress.
  • Loading branch information
jaomaloy committed Oct 6, 2024
1 parent 9f62435 commit 4f63a6d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ const getDeviceModel = function (
}),
);

const getOsUpdateHelper = once(async () => {
const getOsUpdateHelper = once(async (deviceActionsApiVersion: string) => {
const $deviceUrlsBase = await getDeviceUrlsBase();
const { getOsUpdateHelper: _getOsUpdateHelper } =
require('../util/device-actions/os-update') as typeof import('../util/device-actions/os-update');
return _getOsUpdateHelper($deviceUrlsBase, request);
return _getOsUpdateHelper($deviceUrlsBase, deviceActionsApiVersion, request);
});
/* eslint-enable @typescript-eslint/no-require-imports */

Expand Down Expand Up @@ -334,14 +334,17 @@ const getDeviceModel = function (
async function startOsUpdate(
uuidOrUuids: string,
targetOsVersion: string,
options: { runDetached?: boolean },
): Promise<OsUpdateActionResult>;
async function startOsUpdate(
uuidOrUuids: string[],
targetOsVersion: string,
options: { runDetached?: boolean },
): Promise<Dictionary<OsUpdateActionResult>>;
async function startOsUpdate(
uuidOrUuids: string | string[],
targetOsVersion: string,
options: { runDetached?: boolean } = { runDetached: false},
): Promise<OsUpdateActionResult | Dictionary<OsUpdateActionResult>> {
if (!targetOsVersion) {
throw new errors.BalenaInvalidParameterError(
Expand All @@ -368,7 +371,10 @@ const getDeviceModel = function (
{ primitive: true, promise: true },
);

const osUpdateHelper = await getOsUpdateHelper();
const osUpdateHelper = options.runDetached === true
? await getOsUpdateHelper('v2')
: await getOsUpdateHelper('v1');

const results: Dictionary<
ResolvableReturnType<typeof osUpdateHelper.startOsUpdate>
> = {};
Expand Down
5 changes: 2 additions & 3 deletions src/util/device-actions/device-actions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ interface GetActionStatusParams {
extraOptions?: any;
}

const DEVICE_ACTIONS_API_VERSION = 'v1';

export class DeviceActionsService {
private actionsEndpoint: string;

constructor(
deviceUrlsBase: string,
deviceActionsApiVersion: string,
private request: BalenaRequest,
) {
this.actionsEndpoint = `https://actions.${deviceUrlsBase}/${DEVICE_ACTIONS_API_VERSION}`;
this.actionsEndpoint = `https://actions.${deviceUrlsBase}/${deviceActionsApiVersion}`;
}

public startAction = <T>({
Expand Down
17 changes: 12 additions & 5 deletions src/util/device-actions/os-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const OS_UPDATE_ACTION_NAME = 'resinhup';

// See: https://github.com/balena-io/resin-proxy/issues/51#issuecomment-274251469
export interface OsUpdateActionResult {
status: 'idle' | 'in_progress' | 'done' | 'error' | 'configuring';
status: 'idle' | 'in_progress' | 'done' | 'error' | 'configuring' | 'triggered';
parameters?: {
target_version: string;
};
Expand All @@ -15,10 +15,12 @@ export interface OsUpdateActionResult {

export const getOsUpdateHelper = function (
deviceUrlsBase: string,
deviceActionsApiVersion: string,
request: BalenaRequest,
) {
const deviceActionsService = new DeviceActionsService(
deviceUrlsBase,
deviceActionsApiVersion,
request,
);

Expand All @@ -40,8 +42,13 @@ export const getOsUpdateHelper = function (
});
};

return {
startOsUpdate,
getOsUpdateStatus,
};
if (deviceActionsApiVersion === 'v2')
{
return { startOsUpdate };
} else {
return {
startOsUpdate,
getOsUpdateStatus,
};
}
};

0 comments on commit 4f63a6d

Please sign in to comment.