From 76090ec8f9dee6490c8d8f14092326431e7c00f5 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 12 Dec 2023 19:42:40 +0200 Subject: [PATCH] Set should_be_managed_by__release & supervisor service_installs on device POST Change-type: minor --- .../supervisor-app/hooks/supervisor-app.ts | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/features/supervisor-app/hooks/supervisor-app.ts b/src/features/supervisor-app/hooks/supervisor-app.ts index 0a84a1a88a..001420f227 100644 --- a/src/features/supervisor-app/hooks/supervisor-app.ts +++ b/src/features/supervisor-app/hooks/supervisor-app.ts @@ -15,6 +15,40 @@ import type { const { BadRequestError } = pinejsErrors; +hooks.addPureHook('POST', 'resin', 'device', { + async POSTPARSE({ request, api }) { + if ( + request.values.supervisor_version != null && + request.values.is_of__device_type != null + ) { + const deviceType = (await api.get({ + resource: 'device_type', + id: request.values.is_of__device_type, + options: { + $select: 'is_of__cpu_architecture', + }, + })) as PickDeferred | null; + if (deviceType == null) { + return; + } + + const [supervisorRelease] = await getSupervisorReleaseResource( + api, + request.values.supervisor_version, + deviceType.is_of__cpu_architecture.__id, + ); + + if (supervisorRelease == null) { + return; + } + + // since this is a POST, we _know_ the device is being created and has no current/target state, so we can + // just append the target after determining which it is (like a preloaded app) + request.values.should_be_managed_by__release = supervisorRelease.id; + } + }, +}); + hooks.addPureHook('PATCH', 'resin', 'device', { /** * When a device checks in with it's initial supervisor version, set the corresponding should_be_managed_by__release resource @@ -134,7 +168,7 @@ async function checkSupervisorReleaseUpgrades( async function getSupervisorReleaseResource( api: sbvrUtils.PinejsClient, supervisorVersion: string, - archId: string, + archId: string | number, ) { return (await api.get({ resource: 'release',