Skip to content

Commit

Permalink
state-get-v3: Unify the user,SV&hostApp app state generation helpers
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi committed Jan 22, 2024
1 parent af6a5eb commit 792d33a
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions src/features/device-state/routes/state-get-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,24 @@ const getStateV3 = async (req: Request, uuid: string): Promise<StateV3> => {
storedDeviceFields: _.pick(device, getStateEventAdditionalFields),
});

// We use an empty config for the supervisor & hostApp as we don't want any labels applied to them due to user app config
const svAndHostAppConfig = {};
const apps = {
...getSystemAppState(device, 'should_be_managed_by__release'),
...getSystemAppState(device, 'should_be_operated_by__release', {
// This label is necessary for older supervisors to properly detect the hostApp
// and ignore it, sinc `is_host: true` wasn't enough. W/o this the device would
// try to install the hostApp container like a normal user app and restart it
// constantly b/c the image doesn't have a CMD specified.
// See: https://github.com/balena-os/balena-supervisor/blob/v15.2.0/src/compose/app.ts#L839
'io.balena.image.store': 'root',
}),
...getUserAppState(device, config),
...getAppState(device, 'should_be_managed_by__release', svAndHostAppConfig),
...getAppState(
device,
'should_be_operated_by__release',
svAndHostAppConfig,
{
// This label is necessary for older supervisors to properly detect the hostApp
// and ignore it, sinc `is_host: true` wasn't enough. W/o this the device would
// try to install the hostApp container like a normal user app and restart it
// constantly b/c the image doesn't have a CMD specified.
// See: https://github.com/balena-os/balena-supervisor/blob/v15.2.0/src/compose/app.ts#L839
'io.balena.image.store': 'root',
},
),
...getAppState(device, 'should_be_running__release', config),
};

const state: StateV3 = {
Expand Down Expand Up @@ -360,11 +367,26 @@ const getDevice = getStateDelayingEmpty(

const getAppState = (
device: AnyObject,
application: AnyObject,
release: AnyObject | undefined,
targetReleaseField:
| 'should_be_running__release'
| 'should_be_managed_by__release'
| 'should_be_operated_by__release',
config: Dictionary<string>,
defaultLabels?: Dictionary<string>,
): StateV3[string]['apps'] => {
): StateV3[string]['apps'] | null => {
let application: AnyObject;
let release: AnyObject | undefined;
if (targetReleaseField === 'should_be_running__release') {
application = device.belongs_to__application[0];
release = getReleaseForDevice(device);
} else {
release = device[targetReleaseField][0];
if (!release) {
return null;
}
application = release.belongs_to__application[0];
}

return {
[application.uuid]: {
id: application.id,
Expand All @@ -383,27 +405,3 @@ const getAppState = (
},
};
};

const getUserAppState = (
device: AnyObject,
config: Dictionary<string>,
): StateV3[string]['apps'] => {
const userApp = device.belongs_to__application[0];
const userAppRelease = getReleaseForDevice(device);
return getAppState(device, userApp, userAppRelease, config);
};
const getSystemAppState = (
device: AnyObject,
targetReleaseField:
| 'should_be_managed_by__release'
| 'should_be_operated_by__release',
defaultLabels?: Dictionary<string>,
): StateV3[string]['apps'] | null => {
const release = device[targetReleaseField][0];
if (!release) {
return null;
}
const app = release.belongs_to__application[0];
// We use an empty config as we don't want any labels applied to the supervisor/hostApp due to user app config
return getAppState(device, app, release, {}, defaultLabels);
};

0 comments on commit 792d33a

Please sign in to comment.