diff --git a/src/compose/application-manager.ts b/src/compose/application-manager.ts index 212bed89..e052bb10 100644 --- a/src/compose/application-manager.ts +++ b/src/compose/application-manager.ts @@ -35,6 +35,7 @@ import { TargetApplications, DeviceStatus, DeviceReportFields, + TargetState, } from '../types/state'; import { checkTruthy, checkInt } from '../lib/validation'; import { Proxyvisor } from '../proxyvisor'; @@ -446,7 +447,7 @@ export async function executeStep( // FIXME: This shouldn't be in this module export async function setTarget( apps: TargetApplications, - dependent: any, + dependent: TargetState['dependent'], source: string, maybeTrx?: Transaction, ) { diff --git a/src/device-state/preload.ts b/src/device-state/preload.ts index e9922c12..0e0161de 100644 --- a/src/device-state/preload.ts +++ b/src/device-state/preload.ts @@ -83,7 +83,7 @@ export async function loadTargetFromFile( preloadState.config = { ...formattedConf, ...deviceConf }; const localState = { local: { name: '', ...preloadState }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; await deviceState.setTarget(localState); diff --git a/src/device-state/target-state-cache.ts b/src/device-state/target-state-cache.ts index 9eb00a55..597420f2 100644 --- a/src/device-state/target-state-cache.ts +++ b/src/device-state/target-state-cache.ts @@ -7,8 +7,8 @@ import * as db from '../db'; // at all, and we can use the below type for both insertion and retrieval. export interface DatabaseApp { name: string; - releaseId: number; - commit: string; + releaseId?: number; + commit?: string; appId: number; services: string; networks: string; diff --git a/src/lib/validation.ts b/src/lib/validation.ts index e5e11dd1..c208d3ed 100644 --- a/src/lib/validation.ts +++ b/src/lib/validation.ts @@ -243,6 +243,16 @@ export function isValidDependentAppsObject(apps: unknown): boolean { } return _.conformsTo(val, { + parentApp: (n: any) => { + if (!checkInt(n)) { + log.debug( + 'Invalid parentApp passed to validation.isValidDependentAppsObject\nName:', + inspect(n), + ); + return false; + } + return true; + }, name: (n: any) => { if (!isValidShortText(n)) { log.debug( @@ -283,16 +293,6 @@ export function isValidDependentAppsObject(apps: unknown): boolean { } return true; }, - environment: (e: any) => { - if (!undefinedOrValidEnv(e)) { - log.debug( - 'Invalid environment passed to validation.isValidDependentAppsObject\nEnvironment:', - inspect(e), - ); - return false; - } - return true; - }, }); }); } diff --git a/src/types/state.ts b/src/types/state.ts index 831ee2eb..d131c02d 100644 --- a/src/types/state.ts +++ b/src/types/state.ts @@ -55,8 +55,8 @@ export interface TargetState { apps: { [appId: string]: { name: string; - commit: string; - releaseId: number; + commit?: string; + releaseId?: number; services: { [serviceId: string]: { labels: LabelObject; @@ -65,6 +65,7 @@ export interface TargetState { image: string; running?: boolean; environment: Dictionary; + contract?: Dictionary; } & ServiceComposeConfig; }; volumes: Dictionary>; @@ -72,23 +73,29 @@ export interface TargetState { }; }; }; - // TODO: Correctly type this once dependent devices are - // actually properly supported dependent: { - apps: Array<{ - name?: string; - image?: string; - commit?: string; - config?: EnvVarObject; - environment?: EnvVarObject; - }>; - devices: Array<{ - name?: string; - apps?: Dictionary<{ - config?: EnvVarObject; - environment?: EnvVarObject; - }>; - }>; + apps: { + [appId: string]: { + name: string; + parentApp: number; + config: EnvVarObject; + releaseId?: number; + imageId?: number; + commit?: string; + image?: string; + }; + }; + devices: { + [uuid: string]: { + name: string; + apps: { + [id: string]: { + config: EnvVarObject; + environment: EnvVarObject; + }; + }; + }; + }; }; } @@ -98,6 +105,10 @@ export type TargetApplication = LocalTargetState['apps'][0]; export type TargetApplicationService = TargetApplication['services'][0]; export type AppsJsonFormat = Omit & { pinDevice?: boolean; + apps: { + // The releaseId/commit are required for preloading + [id: string]: Required; + }; }; export type InstancedAppState = { [appId: number]: App }; diff --git a/test/05-device-state.spec.ts b/test/05-device-state.spec.ts index 079262b9..dc3f9d9a 100644 --- a/test/05-device-state.spec.ts +++ b/test/05-device-state.spec.ts @@ -66,7 +66,7 @@ const testTarget2 = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; const testTargetWithDefaults2 = { @@ -112,7 +112,6 @@ const testTargetWithDefaults2 = { }, }, }, - dependent: { apps: [], devices: [] }, }; const testTargetInvalid = { @@ -155,7 +154,7 @@ const testTargetInvalid = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; describe('deviceState', () => { diff --git a/test/32-compose-app-manager.spec.ts b/test/32-compose-app-manager.spec.ts index 69687ea6..1571b3fa 100644 --- a/test/32-compose-app-manager.spec.ts +++ b/test/32-compose-app-manager.spec.ts @@ -205,7 +205,7 @@ describe('compose/application-manager', () => { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; before(async () => { diff --git a/test/lib/application-manager-test-states.ts b/test/lib/application-manager-test-states.ts index bebe2140..7d0a45f6 100644 --- a/test/lib/application-manager-test-states.ts +++ b/test/lib/application-manager-test-states.ts @@ -54,7 +54,7 @@ targetState[0] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; targetState[1] = { @@ -92,7 +92,7 @@ targetState[1] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; targetState[2] = { @@ -146,7 +146,7 @@ targetState[2] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; targetState[3] = { @@ -201,7 +201,7 @@ targetState[3] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; targetState[4] = { @@ -255,7 +255,7 @@ targetState[4] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; targetState[5] = { @@ -308,7 +308,7 @@ targetState[5] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; targetState[6] = { @@ -327,7 +327,7 @@ targetState[6] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState = []; @@ -415,7 +415,7 @@ currentState[0] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState[1] = { @@ -437,7 +437,7 @@ currentState[1] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState[2] = { @@ -494,7 +494,7 @@ currentState[2] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState[3] = { @@ -585,7 +585,7 @@ currentState[3] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState[4] = { @@ -640,7 +640,7 @@ currentState[4] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState[5] = { @@ -670,7 +670,7 @@ currentState[5] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; currentState[6] = { @@ -757,7 +757,7 @@ currentState[6] = { }, }, }, - dependent: { apps: [], devices: [] }, + dependent: { apps: {}, devices: {} }, }; availableImages = [];