Allow local target state to be applied in unmanaged mode

An unmanaged Supervisor will not have a balenaApi defined, so any
attempts to POST /v2/local/target-state will cause the Supervisor to
crash loop. This fix allows for the Supervisor to apply local target
state, which is useful for integration testing.

Change-type: patch
Signed-off-by: Christina Ying Wang <christina@balena.io>
This commit is contained in:
Christina Ying Wang 2022-11-07 12:16:24 -08:00
parent fc15ad2554
commit fd320f85b5

View File

@ -6,7 +6,6 @@ import * as config from '../config';
import * as db from '../db'; import * as db from '../db';
import * as volumeManager from '../compose/volume-manager'; import * as volumeManager from '../compose/volume-manager';
import * as serviceManager from '../compose/service-manager'; import * as serviceManager from '../compose/service-manager';
import * as deviceState from '../device-state';
import * as applicationManager from '../compose/application-manager'; import * as applicationManager from '../compose/application-manager';
import { import {
StatusError, StatusError,
@ -62,7 +61,6 @@ async function createVolumeFromLegacyData(
*/ */
export async function normaliseLegacyDatabase() { export async function normaliseLegacyDatabase() {
await apiBinder.initialized(); await apiBinder.initialized();
await deviceState.initialized();
if (apiBinder.balenaApi == null) { if (apiBinder.balenaApi == null) {
throw new InternalInconsistencyError( throw new InternalInconsistencyError(
@ -268,15 +266,11 @@ export async function fromV2TargetState(
} }
/** /**
* Convert v2 to v3 target apps. If local is false * Retrieves the appUuid for an app from balena API
* it will query the API to get the app uuid * @param appId
*/ */
export async function fromV2TargetApps( const getUUIDFromAPI = async (appId: number) => {
apps: TargetAppsV2,
local = false,
): Promise<TargetApps> {
await apiBinder.initialized(); await apiBinder.initialized();
await deviceState.initialized();
if (apiBinder.balenaApi == null) { if (apiBinder.balenaApi == null) {
throw new InternalInconsistencyError( throw new InternalInconsistencyError(
@ -285,22 +279,30 @@ export async function fromV2TargetApps(
} }
const { balenaApi } = apiBinder; const { balenaApi } = apiBinder;
const getUUIDFromAPI = async (appId: number) => {
const appDetails = await balenaApi.get({
resource: 'application',
id: appId,
options: {
$select: ['uuid'],
},
});
if (!appDetails || !appDetails.uuid) { const appDetails = await balenaApi.get({
throw new StatusError(404, `No app with id ${appId} found on the API.`); resource: 'application',
} id: appId,
options: {
$select: ['uuid'],
},
});
return appDetails.uuid; if (!appDetails || !appDetails.uuid) {
}; throw new StatusError(404, `No app with id ${appId} found on the API.`);
}
return appDetails.uuid;
};
/**
* Convert v2 to v3 target apps. If local is false
* it will query the API to get the app uuid
*/
export async function fromV2TargetApps(
apps: TargetAppsV2,
local = false,
): Promise<TargetApps> {
return ( return (
( (
await Promise.all( await Promise.all(