mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-05-31 23:00:48 +00:00
device-state: Convert to a singleton
Change-type: patch Signed-off-by: Rich Bayliss <rich@balena.io> Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
d50f7791e1
commit
e3864915bc
@ -23,7 +23,7 @@ import * as request from './lib/request';
|
||||
|
||||
import log from './lib/supervisor-console';
|
||||
|
||||
import DeviceState from './device-state';
|
||||
import * as deviceState from './device-state';
|
||||
import * as globalEventBus from './event-bus';
|
||||
import * as TargetState from './device-state/target-state';
|
||||
import * as logger from './logger';
|
||||
@ -53,7 +53,6 @@ interface DeviceTag {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export let deviceState: DeviceState;
|
||||
const lastReportedState: DeviceStatus = {
|
||||
local: {},
|
||||
dependent: {},
|
||||
@ -66,10 +65,6 @@ let reportPending = false;
|
||||
export let stateReportErrors = 0;
|
||||
let readyForUpdates = false;
|
||||
|
||||
export function setDeviceState(newState: DeviceState) {
|
||||
deviceState = newState;
|
||||
}
|
||||
|
||||
export async function healthcheck() {
|
||||
const {
|
||||
appUpdatePollInterval,
|
||||
@ -179,7 +174,7 @@ export async function start() {
|
||||
// must wait for the provisioning because we need a
|
||||
// target state on which to apply the backup
|
||||
globalEventBus.getInstance().once('targetStateChanged', async (state) => {
|
||||
await loadBackupFromMigration(deviceState, state, bootstrapRetryDelay);
|
||||
await loadBackupFromMigration(state, bootstrapRetryDelay);
|
||||
});
|
||||
|
||||
readyForUpdates = true;
|
||||
@ -718,6 +713,7 @@ export let balenaApi: PinejsClientRequest | null = null;
|
||||
export const initialized = (async () => {
|
||||
await config.initialized;
|
||||
await eventTracker.initialized;
|
||||
await deviceState.initialized;
|
||||
|
||||
const { unmanaged, apiEndpoint, currentApiKey } = await config.getMany([
|
||||
'unmanaged',
|
||||
|
8
src/application-manager.d.ts
vendored
8
src/application-manager.d.ts
vendored
@ -7,9 +7,9 @@ import { ServiceAction } from './device-api/common';
|
||||
import { DeviceStatus, InstancedAppState } from './types/state';
|
||||
|
||||
import type { Image } from './compose/images';
|
||||
import DeviceState from './device-state';
|
||||
import * as deviceState from './device-state';
|
||||
import * as apiBinder from './api-binder';
|
||||
|
||||
import { APIBinder } from './api-binder';
|
||||
import * as config from './config';
|
||||
|
||||
import {
|
||||
@ -41,8 +41,6 @@ class ApplicationManager extends EventEmitter {
|
||||
// TODO: When the module which is/declares these fields is converted to
|
||||
// typecript, type the following
|
||||
public _lockingIfNecessary: any;
|
||||
public deviceState: DeviceState;
|
||||
public apiBinder: APIBinder;
|
||||
|
||||
public proxyvisor: any;
|
||||
public timeSpentFetching: number;
|
||||
@ -52,7 +50,7 @@ class ApplicationManager extends EventEmitter {
|
||||
|
||||
public router: Router;
|
||||
|
||||
public constructor({ deviceState: DeviceState, apiBinder: APIBinder });
|
||||
public constructor();
|
||||
|
||||
public init(): Promise<void>;
|
||||
|
||||
|
@ -34,6 +34,9 @@ import { createV1Api } from './device-api/v1';
|
||||
import { createV2Api } from './device-api/v2';
|
||||
import { serviceAction } from './device-api/common';
|
||||
|
||||
import * as deviceState from './device-state';
|
||||
import * as apiBinder from './api-binder';
|
||||
|
||||
import * as db from './db';
|
||||
|
||||
// TODO: move this to an Image class?
|
||||
@ -70,7 +73,7 @@ const createApplicationManagerRouter = function (applications) {
|
||||
};
|
||||
|
||||
export class ApplicationManager extends EventEmitter {
|
||||
constructor({ deviceState, apiBinder }) {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.serviceAction = serviceAction;
|
||||
@ -123,7 +126,6 @@ export class ApplicationManager extends EventEmitter {
|
||||
};
|
||||
|
||||
this.reportCurrentState = this.reportCurrentState.bind(this);
|
||||
this.init = this.init.bind(this);
|
||||
this.getStatus = this.getStatus.bind(this);
|
||||
this.getDependentState = this.getDependentState.bind(this);
|
||||
this.getCurrentForComparison = this.getCurrentForComparison.bind(this);
|
||||
|
@ -3,6 +3,8 @@ import { NextFunction, Request, Response, Router } from 'express';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { ApplicationManager } from '../application-manager';
|
||||
import * as deviceState from '../device-state';
|
||||
import * as apiBinder from '../api-binder';
|
||||
import { Service } from '../compose/service';
|
||||
import Volume from '../compose/volume';
|
||||
import * as config from '../config';
|
||||
@ -25,7 +27,7 @@ import { isVPNActive } from '../network';
|
||||
import { doPurge, doRestart, safeStateClone, serviceAction } from './common';
|
||||
|
||||
export function createV2Api(router: Router, applications: ApplicationManager) {
|
||||
const { _lockingIfNecessary, deviceState } = applications;
|
||||
const { _lockingIfNecessary } = applications;
|
||||
|
||||
const handleServiceAction = (
|
||||
req: Request,
|
||||
@ -394,7 +396,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
|
||||
router.get('/v2/state/status', async (_req, res) => {
|
||||
const currentRelease = await config.get('currentCommit');
|
||||
|
||||
const pending = applications.deviceState.applyInProgress;
|
||||
const pending = deviceState.isApplyInProgress();
|
||||
const containerStates = (await serviceManager.getAll()).map((svc) =>
|
||||
_.pick(
|
||||
svc,
|
||||
@ -452,7 +454,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
|
||||
|
||||
router.get('/v2/device/tags', async (_req, res) => {
|
||||
try {
|
||||
const tags = await applications.apiBinder.fetchDeviceTags();
|
||||
const tags = await apiBinder.fetchDeviceTags();
|
||||
return res.json({
|
||||
status: 'success',
|
||||
tags,
|
||||
|
@ -26,7 +26,6 @@ import * as updateLock from './lib/update-lock';
|
||||
import * as validation from './lib/validation';
|
||||
import * as network from './network';
|
||||
|
||||
import * as APIBinder from './api-binder';
|
||||
import { ApplicationManager } from './application-manager';
|
||||
import * as deviceConfig from './device-config';
|
||||
import { ConfigStep } from './device-config';
|
||||
@ -88,8 +87,8 @@ function validateState(state: any): asserts state is TargetState {
|
||||
|
||||
// TODO (refactor): This shouldn't be here, and instead should be part of the other
|
||||
// device api stuff in ./device-api
|
||||
function createDeviceStateRouter(deviceState: DeviceState) {
|
||||
const router = express.Router();
|
||||
function createDeviceStateRouter() {
|
||||
router = express.Router();
|
||||
router.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
|
||||
router.use(bodyParser.json({ limit: '10mb' }));
|
||||
|
||||
@ -101,10 +100,7 @@ function createDeviceStateRouter(deviceState: DeviceState) {
|
||||
const override = await config.get('lockOverride');
|
||||
const force = validation.checkTruthy(req.body.force) || override;
|
||||
try {
|
||||
const response = await deviceState.executeStepAction(
|
||||
{ action },
|
||||
{ force },
|
||||
);
|
||||
const response = await executeStepAction({ action }, { force });
|
||||
res.status(202).json(response);
|
||||
} catch (e) {
|
||||
const status = e instanceof UpdatesLockedError ? 423 : 500;
|
||||
@ -139,7 +135,7 @@ function createDeviceStateRouter(deviceState: DeviceState) {
|
||||
|
||||
router.get('/v1/device', async (_req, res) => {
|
||||
try {
|
||||
const state = await deviceState.getStatus();
|
||||
const state = await getStatus();
|
||||
const stateToSend = _.pick(state.local, [
|
||||
'api_port',
|
||||
'ip_address',
|
||||
@ -173,14 +169,10 @@ function createDeviceStateRouter(deviceState: DeviceState) {
|
||||
}
|
||||
});
|
||||
|
||||
router.use(deviceState.applications.router);
|
||||
router.use(applications.router);
|
||||
return router;
|
||||
}
|
||||
|
||||
interface DeviceStateConstructOpts {
|
||||
apiBinder: typeof APIBinder;
|
||||
}
|
||||
|
||||
interface DeviceStateEvents {
|
||||
error: Error;
|
||||
change: void;
|
||||
@ -203,6 +195,15 @@ type DeviceStateEventEmitter = StrictEventEmitter<
|
||||
EventEmitter,
|
||||
DeviceStateEvents
|
||||
>;
|
||||
const events = new EventEmitter() as DeviceStateEventEmitter;
|
||||
export const on: typeof events['on'] = events.on.bind(events);
|
||||
export const once: typeof events['once'] = events.once.bind(events);
|
||||
export const removeListener: typeof events['removeListener'] = events.removeListener.bind(
|
||||
events,
|
||||
);
|
||||
export const removeAllListeners: typeof events['removeAllListeners'] = events.removeAllListeners.bind(
|
||||
events,
|
||||
);
|
||||
|
||||
type DeviceStateStepTarget = 'reboot' | 'shutdown' | 'noop';
|
||||
|
||||
@ -216,36 +217,32 @@ type DeviceStateStep<T extends PossibleStepTargets> =
|
||||
| CompositionStep<T extends CompositionStepAction ? T : never>
|
||||
| ConfigStep;
|
||||
|
||||
export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmitter) {
|
||||
public applications: ApplicationManager;
|
||||
// export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmitter) {
|
||||
export const applications = new ApplicationManager();
|
||||
|
||||
private currentVolatile: DeviceReportFields = {};
|
||||
private writeLock = updateLock.writeLock;
|
||||
private readLock = updateLock.readLock;
|
||||
private cancelDelay: null | (() => void) = null;
|
||||
private maxPollTime: number;
|
||||
private intermediateTarget: TargetState | null = null;
|
||||
private applyBlocker: Nullable<Promise<void>>;
|
||||
let currentVolatile: DeviceReportFields = {};
|
||||
const writeLock = updateLock.writeLock;
|
||||
const readLock = updateLock.readLock;
|
||||
let maxPollTime: number;
|
||||
let intermediateTarget: TargetState | null = null;
|
||||
let applyBlocker: Nullable<Promise<void>>;
|
||||
let cancelDelay: null | (() => void) = null;
|
||||
|
||||
public lastSuccessfulUpdate: number | null = null;
|
||||
public failedUpdates: number = 0;
|
||||
public applyInProgress = false;
|
||||
public applyCancelled = false;
|
||||
public lastApplyStart = process.hrtime();
|
||||
public scheduledApply: { force?: boolean; delay?: number } | null = null;
|
||||
public shuttingDown = false;
|
||||
public connected: boolean;
|
||||
public router: express.Router;
|
||||
let failedUpdates: number = 0;
|
||||
let applyCancelled = false;
|
||||
let lastApplyStart = process.hrtime();
|
||||
let scheduledApply: { force?: boolean; delay?: number } | null = null;
|
||||
let shuttingDown = false;
|
||||
|
||||
constructor({ apiBinder }: DeviceStateConstructOpts) {
|
||||
super();
|
||||
this.applications = new ApplicationManager({
|
||||
deviceState: this,
|
||||
apiBinder,
|
||||
});
|
||||
let applyInProgress = false;
|
||||
export let connected: boolean;
|
||||
export let lastSuccessfulUpdate: number | null = null;
|
||||
|
||||
this.on('error', (err) => log.error('deviceState error: ', err));
|
||||
this.on('apply-target-state-end', function (err) {
|
||||
export let router: express.Router;
|
||||
createDeviceStateRouter();
|
||||
|
||||
events.on('error', (err) => log.error('deviceState error: ', err));
|
||||
events.on('apply-target-state-end', function (err) {
|
||||
if (err != null) {
|
||||
if (!(err instanceof UpdatesLockedError)) {
|
||||
return log.error('Device state apply error', err);
|
||||
@ -257,12 +254,30 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
// should clear any rate limiting it's applied
|
||||
return deviceConfig.resetRateLimits();
|
||||
}
|
||||
});
|
||||
this.applications.on('change', (d) => this.reportCurrentState(d));
|
||||
this.router = createDeviceStateRouter(this);
|
||||
}
|
||||
});
|
||||
applications.on('change', (d) => reportCurrentState(d));
|
||||
|
||||
public async healthcheck() {
|
||||
export const initialized = (async () => {
|
||||
await config.initialized;
|
||||
|
||||
config.on('change', (changedConfig) => {
|
||||
if (changedConfig.loggingEnabled != null) {
|
||||
logger.enable(changedConfig.loggingEnabled);
|
||||
}
|
||||
if (changedConfig.apiSecret != null) {
|
||||
reportCurrentState({ api_secret: changedConfig.apiSecret });
|
||||
}
|
||||
if (changedConfig.appUpdatePollInterval != null) {
|
||||
maxPollTime = changedConfig.appUpdatePollInterval;
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
export function isApplyInProgress() {
|
||||
return applyInProgress;
|
||||
}
|
||||
|
||||
export async function healthcheck() {
|
||||
const unmanaged = await config.get('unmanaged');
|
||||
|
||||
// Don't have to perform checks for unmanaged
|
||||
@ -270,44 +285,66 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
return true;
|
||||
}
|
||||
|
||||
const cycleTime = process.hrtime(this.lastApplyStart);
|
||||
const cycleTime = process.hrtime(lastApplyStart);
|
||||
const cycleTimeMs = cycleTime[0] * 1000 + cycleTime[1] / 1e6;
|
||||
const cycleTimeWithinInterval =
|
||||
cycleTimeMs - this.applications.timeSpentFetching < 2 * this.maxPollTime;
|
||||
cycleTimeMs - applications.timeSpentFetching < 2 * maxPollTime;
|
||||
|
||||
// Check if target is healthy
|
||||
const applyTargetHealthy =
|
||||
!this.applyInProgress ||
|
||||
this.applications.fetchesInProgress > 0 ||
|
||||
!applyInProgress ||
|
||||
applications.fetchesInProgress > 0 ||
|
||||
cycleTimeWithinInterval;
|
||||
|
||||
if (!applyTargetHealthy) {
|
||||
log.info(
|
||||
stripIndent`
|
||||
Healthcheck failure - At least ONE of the following conditions must be true:
|
||||
- No applyInProgress ? ${!(this.applyInProgress === true)}
|
||||
- fetchesInProgress ? ${this.applications.fetchesInProgress > 0}
|
||||
Healthcheck failure - Atleast ONE of the following conditions must be true:
|
||||
- No applyInProgress ? ${!(applyInProgress === true)}
|
||||
- fetchesInProgress ? ${applications.fetchesInProgress > 0}
|
||||
- cycleTimeWithinInterval ? ${cycleTimeWithinInterval}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
// All tests pass!
|
||||
return true;
|
||||
}
|
||||
return applyTargetHealthy;
|
||||
}
|
||||
|
||||
public async init() {
|
||||
config.on('change', (changedConfig) => {
|
||||
if (changedConfig.loggingEnabled != null) {
|
||||
logger.enable(changedConfig.loggingEnabled);
|
||||
}
|
||||
if (changedConfig.apiSecret != null) {
|
||||
this.reportCurrentState({ api_secret: changedConfig.apiSecret });
|
||||
}
|
||||
if (changedConfig.appUpdatePollInterval != null) {
|
||||
this.maxPollTime = changedConfig.appUpdatePollInterval;
|
||||
export async function initNetworkChecks({
|
||||
apiEndpoint,
|
||||
connectivityCheckEnabled,
|
||||
}: {
|
||||
apiEndpoint: config.ConfigType<'apiEndpoint'>;
|
||||
connectivityCheckEnabled: config.ConfigType<'connectivityCheckEnabled'>;
|
||||
}) {
|
||||
network.startConnectivityCheck(apiEndpoint, connectivityCheckEnabled, (c) => {
|
||||
connected = c;
|
||||
});
|
||||
config.on('change', function (changedConfig) {
|
||||
if (changedConfig.connectivityCheckEnabled != null) {
|
||||
network.enableConnectivityCheck(changedConfig.connectivityCheckEnabled);
|
||||
}
|
||||
});
|
||||
log.debug('Starting periodic check for IP addresses');
|
||||
|
||||
await network.startIPAddressUpdate()(async (addresses) => {
|
||||
const macAddress = await config.get('macAddress');
|
||||
reportCurrentState({
|
||||
ip_address: addresses.join(' '),
|
||||
mac_address: macAddress,
|
||||
});
|
||||
}, constants.ipAddressUpdateInterval);
|
||||
}
|
||||
|
||||
async function saveInitialConfig() {
|
||||
const devConf = await deviceConfig.getCurrent();
|
||||
|
||||
await deviceConfig.setTarget(devConf);
|
||||
await config.set({ initialConfigSaved: true });
|
||||
}
|
||||
|
||||
export async function loadInitialState() {
|
||||
await applications.init();
|
||||
|
||||
const conf = await config.getMany([
|
||||
'initialConfigSaved',
|
||||
@ -325,17 +362,16 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
'unmanaged',
|
||||
'appUpdatePollInterval',
|
||||
]);
|
||||
this.maxPollTime = conf.appUpdatePollInterval;
|
||||
maxPollTime = conf.appUpdatePollInterval;
|
||||
|
||||
initNetworkChecks(conf);
|
||||
|
||||
await this.applications.init();
|
||||
if (!conf.initialConfigSaved) {
|
||||
await this.saveInitialConfig();
|
||||
await saveInitialConfig();
|
||||
}
|
||||
|
||||
this.initNetworkChecks(conf);
|
||||
|
||||
log.info('Reporting initial state, supervisor version and API info');
|
||||
await this.reportCurrentState({
|
||||
reportCurrentState({
|
||||
api_port: conf.listenPort,
|
||||
api_secret: conf.apiSecret,
|
||||
os_version: conf.osVersion,
|
||||
@ -351,10 +387,10 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
update_downloaded: false,
|
||||
});
|
||||
|
||||
const targetApps = await this.applications.getTargetApps();
|
||||
const targetApps = await applications.getTargetApps();
|
||||
if (!conf.provisioned || (_.isEmpty(targetApps) && !conf.targetStateSet)) {
|
||||
try {
|
||||
await loadTargetFromFile(null, this);
|
||||
await loadTargetFromFile(null);
|
||||
} finally {
|
||||
await config.set({ targetStateSet: true });
|
||||
}
|
||||
@ -368,87 +404,51 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
await config.set({ targetStateSet: true });
|
||||
}
|
||||
}
|
||||
await this.triggerApplyTarget({ initial: true });
|
||||
}
|
||||
triggerApplyTarget({ initial: true });
|
||||
}
|
||||
|
||||
public async initNetworkChecks({
|
||||
apiEndpoint,
|
||||
connectivityCheckEnabled,
|
||||
}: {
|
||||
apiEndpoint: config.ConfigType<'apiEndpoint'>;
|
||||
connectivityCheckEnabled: config.ConfigType<'connectivityCheckEnabled'>;
|
||||
}) {
|
||||
network.startConnectivityCheck(
|
||||
apiEndpoint,
|
||||
connectivityCheckEnabled,
|
||||
(connected) => {
|
||||
return (this.connected = connected);
|
||||
},
|
||||
);
|
||||
config.on('change', function (changedConfig) {
|
||||
if (changedConfig.connectivityCheckEnabled != null) {
|
||||
return network.enableConnectivityCheck(
|
||||
changedConfig.connectivityCheckEnabled,
|
||||
);
|
||||
}
|
||||
});
|
||||
log.debug('Starting periodic check for IP addresses');
|
||||
|
||||
await network.startIPAddressUpdate()(async (addresses) => {
|
||||
const macAddress = await config.get('macAddress');
|
||||
await this.reportCurrentState({
|
||||
ip_address: addresses.join(' '),
|
||||
mac_address: macAddress,
|
||||
});
|
||||
}, constants.ipAddressUpdateInterval);
|
||||
}
|
||||
|
||||
private async saveInitialConfig() {
|
||||
const devConf = await deviceConfig.getCurrent();
|
||||
|
||||
await deviceConfig.setTarget(devConf);
|
||||
await config.set({ initialConfigSaved: true });
|
||||
}
|
||||
|
||||
// We keep compatibility with the StrictEventEmitter types
|
||||
// from the outside, but within this function all hells
|
||||
// breaks loose due to the liberal any casting
|
||||
private emitAsync<T extends keyof DeviceStateEvents>(
|
||||
// We keep compatibility with the StrictEventEmitter types
|
||||
// from the outside, but within this function all hells
|
||||
// breaks loose due to the liberal any casting
|
||||
function emitAsync<T extends keyof DeviceStateEvents>(
|
||||
ev: T,
|
||||
...args: DeviceStateEvents[T] extends (...args: any) => void
|
||||
? Parameters<DeviceStateEvents[T]>
|
||||
: Array<DeviceStateEvents[T]>
|
||||
) {
|
||||
) {
|
||||
if (_.isArray(args)) {
|
||||
return setImmediate(() => this.emit(ev as any, ...args));
|
||||
return setImmediate(() => events.emit(ev as any, ...args));
|
||||
} else {
|
||||
return setImmediate(() => this.emit(ev as any, args));
|
||||
}
|
||||
return setImmediate(() => events.emit(ev as any, args));
|
||||
}
|
||||
}
|
||||
|
||||
private readLockTarget = () =>
|
||||
this.readLock('target').disposer((release) => release());
|
||||
private writeLockTarget = () =>
|
||||
this.writeLock('target').disposer((release) => release());
|
||||
private inferStepsLock = () =>
|
||||
this.writeLock('inferSteps').disposer((release) => release());
|
||||
private usingReadLockTarget(fn: () => any) {
|
||||
return Bluebird.using(this.readLockTarget, () => fn());
|
||||
}
|
||||
private usingWriteLockTarget(fn: () => any) {
|
||||
return Bluebird.using(this.writeLockTarget, () => fn());
|
||||
}
|
||||
private usingInferStepsLock(fn: () => any) {
|
||||
return Bluebird.using(this.inferStepsLock, () => fn());
|
||||
}
|
||||
const readLockTarget = () =>
|
||||
readLock('target').disposer((release) => release());
|
||||
const writeLockTarget = () =>
|
||||
writeLock('target').disposer((release) => release());
|
||||
const inferStepsLock = () =>
|
||||
writeLock('inferSteps').disposer((release) => release());
|
||||
function usingReadLockTarget(fn: () => any) {
|
||||
return Bluebird.using(readLockTarget, () => fn());
|
||||
}
|
||||
function usingWriteLockTarget(fn: () => any) {
|
||||
return Bluebird.using(writeLockTarget, () => fn());
|
||||
}
|
||||
function usingInferStepsLock(fn: () => any) {
|
||||
return Bluebird.using(inferStepsLock, () => fn());
|
||||
}
|
||||
|
||||
export async function setTarget(target: TargetState, localSource?: boolean) {
|
||||
await db.initialized;
|
||||
await config.initialized;
|
||||
|
||||
public async setTarget(target: TargetState, localSource?: boolean) {
|
||||
// When we get a new target state, clear any built up apply errors
|
||||
// This means that we can attempt to apply the new state instantly
|
||||
if (localSource == null) {
|
||||
localSource = false;
|
||||
}
|
||||
this.failedUpdates = 0;
|
||||
failedUpdates = 0;
|
||||
|
||||
validateState(target);
|
||||
|
||||
@ -456,20 +456,20 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
|
||||
const apiEndpoint = await config.get('apiEndpoint');
|
||||
|
||||
await this.usingWriteLockTarget(async () => {
|
||||
await usingWriteLockTarget(async () => {
|
||||
await db.transaction(async (trx) => {
|
||||
await config.set({ name: target.local.name }, trx);
|
||||
await deviceConfig.setTarget(target.local.config, trx);
|
||||
|
||||
if (localSource || apiEndpoint == null) {
|
||||
await this.applications.setTarget(
|
||||
await applications.setTarget(
|
||||
target.local.apps,
|
||||
target.dependent,
|
||||
'local',
|
||||
trx,
|
||||
);
|
||||
} else {
|
||||
await this.applications.setTarget(
|
||||
await applications.setTarget(
|
||||
target.local.apps,
|
||||
target.dependent,
|
||||
apiEndpoint,
|
||||
@ -478,54 +478,54 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public getTarget({
|
||||
export function getTarget({
|
||||
initial = false,
|
||||
intermediate = false,
|
||||
}: { initial?: boolean; intermediate?: boolean } = {}): Bluebird<
|
||||
}: { initial?: boolean; intermediate?: boolean } = {}): Bluebird<
|
||||
InstancedDeviceState
|
||||
> {
|
||||
return this.usingReadLockTarget(async () => {
|
||||
> {
|
||||
return usingReadLockTarget(async () => {
|
||||
if (intermediate) {
|
||||
return this.intermediateTarget;
|
||||
return intermediateTarget;
|
||||
}
|
||||
|
||||
return {
|
||||
local: {
|
||||
name: await config.get('name'),
|
||||
config: await deviceConfig.getTarget({ initial }),
|
||||
apps: await this.applications.getTargetApps(),
|
||||
apps: await applications.getTargetApps(),
|
||||
},
|
||||
dependent: await this.applications.getDependentTargets(),
|
||||
dependent: await applications.getDependentTargets(),
|
||||
};
|
||||
}) as Bluebird<InstancedDeviceState>;
|
||||
}
|
||||
}
|
||||
|
||||
public async getStatus(): Promise<DeviceStatus> {
|
||||
const appsStatus = await this.applications.getStatus();
|
||||
export async function getStatus(): Promise<DeviceStatus> {
|
||||
const appsStatus = await applications.getStatus();
|
||||
const theState: DeepPartial<DeviceStatus> = {
|
||||
local: {},
|
||||
dependent: {},
|
||||
};
|
||||
theState.local = { ...theState.local, ...this.currentVolatile };
|
||||
theState.local.apps = appsStatus.local;
|
||||
theState.local = { ...theState.local, ...currentVolatile };
|
||||
theState.local!.apps = appsStatus.local;
|
||||
theState.dependent!.apps = appsStatus.dependent;
|
||||
if (appsStatus.commit && !this.applyInProgress) {
|
||||
theState.local.is_on__commit = appsStatus.commit;
|
||||
if (appsStatus.commit && !applyInProgress) {
|
||||
theState.local!.is_on__commit = appsStatus.commit;
|
||||
}
|
||||
|
||||
return theState as DeviceStatus;
|
||||
}
|
||||
}
|
||||
|
||||
public async getCurrentForComparison(): Promise<
|
||||
export async function getCurrentForComparison(): Promise<
|
||||
DeviceStatus & { local: { name: string } }
|
||||
> {
|
||||
> {
|
||||
const [name, devConfig, apps, dependent] = await Promise.all([
|
||||
config.get('name'),
|
||||
deviceConfig.getCurrent(),
|
||||
this.applications.getCurrentForComparison(),
|
||||
this.applications.getDependentState(),
|
||||
applications.getCurrentForComparison(),
|
||||
applications.getDependentState(),
|
||||
]);
|
||||
return {
|
||||
local: {
|
||||
@ -536,48 +536,48 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
|
||||
dependent,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public reportCurrentState(newState: DeviceReportFields = {}) {
|
||||
export function reportCurrentState(newState: DeviceReportFields = {}) {
|
||||
if (newState == null) {
|
||||
newState = {};
|
||||
}
|
||||
this.currentVolatile = { ...this.currentVolatile, ...newState };
|
||||
return this.emitAsync('change', undefined);
|
||||
}
|
||||
currentVolatile = { ...currentVolatile, ...newState };
|
||||
emitAsync('change', undefined);
|
||||
}
|
||||
|
||||
private async reboot(force?: boolean, skipLock?: boolean) {
|
||||
await this.applications.stopAll({ force, skipLock });
|
||||
export async function reboot(force?: boolean, skipLock?: boolean) {
|
||||
await applications.stopAll({ force, skipLock });
|
||||
logger.logSystemMessage('Rebooting', {}, 'Reboot');
|
||||
const reboot = await dbus.reboot();
|
||||
this.shuttingDown = true;
|
||||
this.emitAsync('shutdown', undefined);
|
||||
return reboot;
|
||||
}
|
||||
const $reboot = await dbus.reboot();
|
||||
shuttingDown = true;
|
||||
emitAsync('shutdown', undefined);
|
||||
return await $reboot;
|
||||
}
|
||||
|
||||
private async shutdown(force?: boolean, skipLock?: boolean) {
|
||||
await this.applications.stopAll({ force, skipLock });
|
||||
export async function shutdown(force?: boolean, skipLock?: boolean) {
|
||||
await applications.stopAll({ force, skipLock });
|
||||
logger.logSystemMessage('Shutting down', {}, 'Shutdown');
|
||||
const shutdown = await dbus.shutdown();
|
||||
this.shuttingDown = true;
|
||||
this.emitAsync('shutdown', undefined);
|
||||
return shutdown;
|
||||
}
|
||||
const $shutdown = await dbus.shutdown();
|
||||
shuttingDown = true;
|
||||
emitAsync('shutdown', undefined);
|
||||
return $shutdown;
|
||||
}
|
||||
|
||||
public async executeStepAction<T extends PossibleStepTargets>(
|
||||
export async function executeStepAction<T extends PossibleStepTargets>(
|
||||
step: DeviceStateStep<T>,
|
||||
{
|
||||
force,
|
||||
initial,
|
||||
skipLock,
|
||||
}: { force?: boolean; initial?: boolean; skipLock?: boolean },
|
||||
) {
|
||||
) {
|
||||
if (deviceConfig.isValidAction(step.action)) {
|
||||
await deviceConfig.executeStepAction(step as ConfigStep, {
|
||||
initial,
|
||||
});
|
||||
} else if (_.includes(this.applications.validActions, step.action)) {
|
||||
return this.applications.executeStepAction(step as any, {
|
||||
} else if (_.includes(applications.validActions, step.action)) {
|
||||
return applications.executeStepAction(step as any, {
|
||||
force,
|
||||
skipLock,
|
||||
});
|
||||
@ -588,13 +588,13 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
// and if they do, we wouldn't know about it until after
|
||||
// the response has been sent back to the API. Just return
|
||||
// "OK" for this and the below action
|
||||
await this.reboot(force, skipLock);
|
||||
await reboot(force, skipLock);
|
||||
return {
|
||||
Data: 'OK',
|
||||
Error: null,
|
||||
};
|
||||
case 'shutdown':
|
||||
await this.shutdown(force, skipLock);
|
||||
await shutdown(force, skipLock);
|
||||
return {
|
||||
Data: 'OK',
|
||||
Error: null,
|
||||
@ -605,9 +605,9 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
throw new Error(`Invalid action ${step.action}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async applyStep<T extends PossibleStepTargets>(
|
||||
export async function applyStep<T extends PossibleStepTargets>(
|
||||
step: DeviceStateStep<T>,
|
||||
{
|
||||
force,
|
||||
@ -618,39 +618,39 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
initial?: boolean;
|
||||
skipLock?: boolean;
|
||||
},
|
||||
) {
|
||||
if (this.shuttingDown) {
|
||||
) {
|
||||
if (shuttingDown) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const stepResult = await this.executeStepAction(step, {
|
||||
const stepResult = await executeStepAction(step, {
|
||||
force,
|
||||
initial,
|
||||
skipLock,
|
||||
});
|
||||
this.emitAsync('step-completed', null, step, stepResult || undefined);
|
||||
emitAsync('step-completed', null, step, stepResult || undefined);
|
||||
} catch (e) {
|
||||
this.emitAsync('step-error', e, step);
|
||||
emitAsync('step-error', e, step);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private applyError(
|
||||
function applyError(
|
||||
err: Error,
|
||||
{
|
||||
force,
|
||||
initial,
|
||||
intermediate,
|
||||
}: { force?: boolean; initial?: boolean; intermediate?: boolean },
|
||||
) {
|
||||
this.emitAsync('apply-target-state-error', err);
|
||||
this.emitAsync('apply-target-state-end', err);
|
||||
) {
|
||||
emitAsync('apply-target-state-error', err);
|
||||
emitAsync('apply-target-state-end', err);
|
||||
if (intermediate) {
|
||||
throw err;
|
||||
}
|
||||
this.failedUpdates += 1;
|
||||
this.reportCurrentState({ update_failed: true });
|
||||
if (this.scheduledApply != null) {
|
||||
failedUpdates += 1;
|
||||
reportCurrentState({ update_failed: true });
|
||||
if (scheduledApply != null) {
|
||||
if (!(err instanceof UpdatesLockedError)) {
|
||||
log.error(
|
||||
"Updating failed, but there's another update scheduled immediately: ",
|
||||
@ -659,8 +659,8 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
}
|
||||
} else {
|
||||
const delay = Math.min(
|
||||
Math.pow(2, this.failedUpdates) * constants.backoffIncrement,
|
||||
this.maxPollTime,
|
||||
Math.pow(2, failedUpdates) * constants.backoffIncrement,
|
||||
maxPollTime,
|
||||
);
|
||||
// If there was an error then schedule another attempt briefly in the future.
|
||||
if (err instanceof UpdatesLockedError) {
|
||||
@ -675,29 +675,30 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
err,
|
||||
);
|
||||
}
|
||||
return this.triggerApplyTarget({ force, delay, initial });
|
||||
}
|
||||
return triggerApplyTarget({ force, delay, initial });
|
||||
}
|
||||
}
|
||||
|
||||
public async applyTarget({
|
||||
// We define this function this way so we can mock it in the tests
|
||||
export const applyTarget = async ({
|
||||
force = false,
|
||||
initial = false,
|
||||
intermediate = false,
|
||||
skipLock = false,
|
||||
nextDelay = 200,
|
||||
retryCount = 0,
|
||||
} = {}) {
|
||||
} = {}) => {
|
||||
if (!intermediate) {
|
||||
await this.applyBlocker;
|
||||
await applyBlocker;
|
||||
}
|
||||
await this.applications.localModeSwitchCompletion();
|
||||
await applications.localModeSwitchCompletion();
|
||||
|
||||
return this.usingInferStepsLock(async () => {
|
||||
return usingInferStepsLock(async () => {
|
||||
const [currentState, targetState] = await Promise.all([
|
||||
this.getCurrentForComparison(),
|
||||
this.getTarget({ initial, intermediate }),
|
||||
getCurrentForComparison(),
|
||||
getTarget({ initial, intermediate }),
|
||||
]);
|
||||
const extraState = await this.applications.getExtraStateForComparison(
|
||||
const extraState = await applications.getExtraStateForComparison(
|
||||
currentState,
|
||||
targetState,
|
||||
);
|
||||
@ -717,7 +718,7 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
backoff = false;
|
||||
steps = deviceConfigSteps;
|
||||
} else {
|
||||
const appSteps = await this.applications.getRequiredSteps(
|
||||
const appSteps = await applications.getRequiredSteps(
|
||||
currentState,
|
||||
targetState,
|
||||
extraState,
|
||||
@ -738,13 +739,13 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
}
|
||||
|
||||
if (_.isEmpty(steps)) {
|
||||
this.emitAsync('apply-target-state-end', null);
|
||||
emitAsync('apply-target-state-end', null);
|
||||
if (!intermediate) {
|
||||
log.debug('Finished applying target state');
|
||||
this.applications.timeSpentFetching = 0;
|
||||
this.failedUpdates = 0;
|
||||
this.lastSuccessfulUpdate = Date.now();
|
||||
this.reportCurrentState({
|
||||
applications.timeSpentFetching = 0;
|
||||
failedUpdates = 0;
|
||||
lastSuccessfulUpdate = Date.now();
|
||||
reportCurrentState({
|
||||
update_failed: false,
|
||||
update_pending: false,
|
||||
update_downloaded: false,
|
||||
@ -754,7 +755,7 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
}
|
||||
|
||||
if (!intermediate) {
|
||||
this.reportCurrentState({ update_pending: true });
|
||||
reportCurrentState({ update_pending: true });
|
||||
}
|
||||
if (_.every(steps, (step) => step.action === 'noop')) {
|
||||
if (backoff) {
|
||||
@ -768,11 +769,11 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
|
||||
try {
|
||||
await Promise.all(
|
||||
steps.map((s) => this.applyStep(s, { force, initial, skipLock })),
|
||||
steps.map((s) => applyStep(s, { force, initial, skipLock })),
|
||||
);
|
||||
|
||||
await Bluebird.delay(nextDelay);
|
||||
await this.applyTarget({
|
||||
await applyTarget({
|
||||
force,
|
||||
initial,
|
||||
intermediate,
|
||||
@ -793,19 +794,19 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
);
|
||||
}
|
||||
}).catch((err) => {
|
||||
return this.applyError(err, { force, initial, intermediate });
|
||||
return applyError(err, { force, initial, intermediate });
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
public pausingApply(fn: () => any) {
|
||||
export function pausingApply(fn: () => any) {
|
||||
const lock = () => {
|
||||
return this.writeLock('pause').disposer((release) => release());
|
||||
return writeLock('pause').disposer((release) => release());
|
||||
};
|
||||
// TODO: This function is a bit of a mess
|
||||
const pause = () => {
|
||||
return Bluebird.try(() => {
|
||||
let res;
|
||||
this.applyBlocker = new Promise((resolve) => {
|
||||
applyBlocker = new Promise((resolve) => {
|
||||
res = resolve;
|
||||
});
|
||||
return res;
|
||||
@ -813,80 +814,75 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
|
||||
};
|
||||
|
||||
return Bluebird.using(lock(), () => Bluebird.using(pause(), () => fn()));
|
||||
}
|
||||
}
|
||||
|
||||
public triggerApplyTarget({
|
||||
export function triggerApplyTarget({
|
||||
force = false,
|
||||
delay = 0,
|
||||
initial = false,
|
||||
isFromApi = false,
|
||||
} = {}) {
|
||||
if (this.applyInProgress) {
|
||||
if (this.scheduledApply == null || (isFromApi && this.cancelDelay)) {
|
||||
this.scheduledApply = { force, delay };
|
||||
} = {}) {
|
||||
if (applyInProgress) {
|
||||
if (scheduledApply == null || (isFromApi && cancelDelay)) {
|
||||
scheduledApply = { force, delay };
|
||||
if (isFromApi) {
|
||||
// Cancel promise delay if call came from api to
|
||||
// prevent waiting due to backoff (and if we've
|
||||
// previously setup a delay)
|
||||
this.cancelDelay?.();
|
||||
cancelDelay?.();
|
||||
}
|
||||
} else {
|
||||
// If a delay has been set it's because we need to hold off before applying again,
|
||||
// so we need to respect the maximum delay that has
|
||||
// been passed
|
||||
if (!this.scheduledApply.delay) {
|
||||
if (!scheduledApply.delay) {
|
||||
throw new InternalInconsistencyError(
|
||||
'No delay specified in scheduledApply',
|
||||
);
|
||||
}
|
||||
this.scheduledApply.delay = Math.max(delay, this.scheduledApply.delay);
|
||||
if (!this.scheduledApply.force) {
|
||||
this.scheduledApply.force = force;
|
||||
scheduledApply.delay = Math.max(delay, scheduledApply.delay);
|
||||
if (!scheduledApply.force) {
|
||||
scheduledApply.force = force;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.applyCancelled = false;
|
||||
this.applyInProgress = true;
|
||||
applyCancelled = false;
|
||||
applyInProgress = true;
|
||||
new Bluebird((resolve, reject) => {
|
||||
setTimeout(resolve, delay);
|
||||
this.cancelDelay = reject;
|
||||
cancelDelay = reject;
|
||||
})
|
||||
.catch(() => {
|
||||
this.applyCancelled = true;
|
||||
applyCancelled = true;
|
||||
})
|
||||
.then(() => {
|
||||
this.cancelDelay = null;
|
||||
if (this.applyCancelled) {
|
||||
cancelDelay = null;
|
||||
if (applyCancelled) {
|
||||
log.info('Skipping applyTarget because of a cancellation');
|
||||
return;
|
||||
}
|
||||
this.lastApplyStart = process.hrtime();
|
||||
lastApplyStart = process.hrtime();
|
||||
log.info('Applying target state');
|
||||
return this.applyTarget({ force, initial });
|
||||
return applyTarget({ force, initial });
|
||||
})
|
||||
.finally(() => {
|
||||
this.applyInProgress = false;
|
||||
this.reportCurrentState();
|
||||
if (this.scheduledApply != null) {
|
||||
this.triggerApplyTarget(this.scheduledApply);
|
||||
this.scheduledApply = null;
|
||||
applyInProgress = false;
|
||||
reportCurrentState();
|
||||
if (scheduledApply != null) {
|
||||
triggerApplyTarget(scheduledApply);
|
||||
scheduledApply = null;
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
public applyIntermediateTarget(
|
||||
intermediateTarget: TargetState,
|
||||
{ force = false, skipLock = false } = {},
|
||||
) {
|
||||
this.intermediateTarget = _.cloneDeep(intermediateTarget);
|
||||
return this.applyTarget({ intermediate: true, force, skipLock }).then(
|
||||
() => {
|
||||
this.intermediateTarget = null;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default DeviceState;
|
||||
export function applyIntermediateTarget(
|
||||
intermediate: TargetState,
|
||||
{ force = false, skipLock = false } = {},
|
||||
) {
|
||||
intermediateTarget = _.cloneDeep(intermediate);
|
||||
return applyTarget({ intermediate: true, force, skipLock }).then(() => {
|
||||
intermediateTarget = null;
|
||||
});
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import { DeviceStatus } from '../types/state';
|
||||
import { getRequestInstance } from '../lib/request';
|
||||
import * as config from '../config';
|
||||
import * as eventTracker from '../event-tracker';
|
||||
import DeviceState from '../device-state';
|
||||
import * as deviceState from '../device-state';
|
||||
import { CoreOptions } from 'request';
|
||||
import * as url from 'url';
|
||||
|
||||
@ -22,7 +22,6 @@ const INTERNAL_STATE_KEYS = [
|
||||
'update_failed',
|
||||
];
|
||||
|
||||
let deviceState: DeviceState;
|
||||
export let stateReportErrors = 0;
|
||||
const lastReportedState: DeviceStatus = {
|
||||
local: {},
|
||||
@ -243,9 +242,7 @@ const reportCurrentState = (): null => {
|
||||
return null;
|
||||
};
|
||||
|
||||
// TODO: Remove the passing in of deviceState once it's a singleton
|
||||
export const startReporting = ($deviceState: typeof deviceState) => {
|
||||
deviceState = $deviceState;
|
||||
export const startReporting = () => {
|
||||
deviceState.on('change', () => {
|
||||
if (!reportPending) {
|
||||
// A latency of 100ms should be acceptable and
|
||||
|
@ -2,7 +2,7 @@ import * as _ from 'lodash';
|
||||
import { fs } from 'mz';
|
||||
|
||||
import { Image } from '../compose/images';
|
||||
import DeviceState from '../device-state';
|
||||
import * as deviceState from '../device-state';
|
||||
import * as config from '../config';
|
||||
import * as deviceConfig from '../device-config';
|
||||
import * as eventTracker from '../event-tracker';
|
||||
@ -17,7 +17,6 @@ import { AppsJsonFormat } from '../types/state';
|
||||
|
||||
export async function loadTargetFromFile(
|
||||
appsPath: Nullable<string>,
|
||||
deviceState: DeviceState,
|
||||
): Promise<void> {
|
||||
log.info('Attempting to load any preloaded applications');
|
||||
if (!appsPath) {
|
||||
@ -88,7 +87,6 @@ export async function loadTargetFromFile(
|
||||
};
|
||||
|
||||
await deviceState.setTarget(localState);
|
||||
|
||||
log.success('Preloading complete');
|
||||
if (preloadState.pinDevice) {
|
||||
// Multi-app warning!
|
||||
|
@ -3,20 +3,24 @@ import * as _ from 'lodash';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
import { child_process, fs } from 'mz';
|
||||
import * as path from 'path';
|
||||
import { PinejsClientRequest } from 'pinejs-client-request';
|
||||
import * as rimraf from 'rimraf';
|
||||
|
||||
const mkdirpAsync = Bluebird.promisify(mkdirp);
|
||||
const rimrafAsync = Bluebird.promisify(rimraf);
|
||||
|
||||
import { ApplicationManager } from '../application-manager';
|
||||
import * as apiBinder from '../api-binder';
|
||||
import * as config from '../config';
|
||||
import * as db from '../db';
|
||||
import * as volumeManager from '../compose/volume-manager';
|
||||
import * as serviceManager from '../compose/service-manager';
|
||||
import DeviceState from '../device-state';
|
||||
import * as deviceState from '../device-state';
|
||||
import * as constants from '../lib/constants';
|
||||
import { BackupError, DatabaseParseError, NotFoundError } from '../lib/errors';
|
||||
import {
|
||||
BackupError,
|
||||
DatabaseParseError,
|
||||
NotFoundError,
|
||||
InternalInconsistencyError,
|
||||
} from '../lib/errors';
|
||||
import { docker } from '../lib/docker-utils';
|
||||
import { pathExistsOnHost } from '../lib/fs-utils';
|
||||
import { log } from '../lib/supervisor-console';
|
||||
@ -108,10 +112,16 @@ export function convertLegacyAppsJson(appsArray: any[]): AppsJsonFormat {
|
||||
return { apps, config: deviceConfig } as AppsJsonFormat;
|
||||
}
|
||||
|
||||
export async function normaliseLegacyDatabase(
|
||||
application: ApplicationManager,
|
||||
balenaApi: PinejsClientRequest,
|
||||
) {
|
||||
export async function normaliseLegacyDatabase() {
|
||||
await apiBinder.initialized;
|
||||
await deviceState.initialized;
|
||||
|
||||
if (apiBinder.balenaApi == null) {
|
||||
throw new InternalInconsistencyError(
|
||||
'API binder is not initialized correctly',
|
||||
);
|
||||
}
|
||||
|
||||
// When legacy apps are present, we kill their containers and migrate their /data to a named volume
|
||||
log.info('Migrating ids for legacy app...');
|
||||
|
||||
@ -147,7 +157,7 @@ export async function normaliseLegacyDatabase(
|
||||
}
|
||||
|
||||
log.debug(`Getting release ${app.commit} for app ${app.appId} from API`);
|
||||
const releases = await balenaApi.get({
|
||||
const releases = await apiBinder.balenaApi.get({
|
||||
resource: 'release',
|
||||
options: {
|
||||
$filter: {
|
||||
@ -248,7 +258,7 @@ export async function normaliseLegacyDatabase(
|
||||
await serviceManager.killAllLegacy();
|
||||
log.debug('Migrating legacy app volumes');
|
||||
|
||||
const targetApps = await application.getTargetApps();
|
||||
const targetApps = await deviceState.applications.getTargetApps();
|
||||
|
||||
for (const appId of _.keys(targetApps)) {
|
||||
await volumeManager.createFromLegacy(parseInt(appId, 10));
|
||||
@ -260,7 +270,6 @@ export async function normaliseLegacyDatabase(
|
||||
}
|
||||
|
||||
export async function loadBackupFromMigration(
|
||||
deviceState: DeviceState,
|
||||
targetState: TargetState,
|
||||
retryDelay: number,
|
||||
): Promise<void> {
|
||||
@ -340,6 +349,6 @@ export async function loadBackupFromMigration(
|
||||
log.error(`Error restoring migration backup, retrying: ${err}`);
|
||||
|
||||
await Bluebird.delay(retryDelay);
|
||||
return loadBackupFromMigration(deviceState, targetState, retryDelay);
|
||||
return loadBackupFromMigration(targetState, retryDelay);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import * as apiBinder from './api-binder';
|
||||
import * as db from './db';
|
||||
import * as config from './config';
|
||||
import DeviceState from './device-state';
|
||||
import * as deviceState from './device-state';
|
||||
import * as eventTracker from './event-tracker';
|
||||
import { intialiseContractRequirements } from './lib/contracts';
|
||||
import { normaliseLegacyDatabase } from './lib/migration';
|
||||
@ -31,26 +31,18 @@ const startupConfigFields: config.ConfigKey[] = [
|
||||
];
|
||||
|
||||
export class Supervisor {
|
||||
private deviceState: DeviceState;
|
||||
private api: SupervisorAPI;
|
||||
|
||||
public constructor() {
|
||||
this.deviceState = new DeviceState({
|
||||
apiBinder,
|
||||
});
|
||||
|
||||
// workaround the circular dependency
|
||||
apiBinder.setDeviceState(this.deviceState);
|
||||
|
||||
// FIXME: rearchitect proxyvisor to avoid this circular dependency
|
||||
// by storing current state and having the APIBinder query and report it / provision devices
|
||||
this.deviceState.applications.proxyvisor.bindToAPI(apiBinder);
|
||||
deviceState.applications.proxyvisor.bindToAPI(apiBinder);
|
||||
|
||||
this.api = new SupervisorAPI({
|
||||
routers: [apiBinder.router, this.deviceState.router],
|
||||
routers: [apiBinder.router, deviceState.router],
|
||||
healthchecks: [
|
||||
apiBinder.healthcheck,
|
||||
this.deviceState.healthcheck.bind(this.deviceState),
|
||||
deviceState.healthcheck,
|
||||
],
|
||||
});
|
||||
}
|
||||
@ -79,20 +71,19 @@ export class Supervisor {
|
||||
log.debug('Starting api binder');
|
||||
await apiBinder.initialized;
|
||||
|
||||
await deviceState.initialized;
|
||||
|
||||
logger.logSystemMessage('Supervisor starting', {}, 'Supervisor start');
|
||||
if (conf.legacyAppsPresent && apiBinder.balenaApi != null) {
|
||||
log.info('Legacy app detected, running migration');
|
||||
await normaliseLegacyDatabase(
|
||||
this.deviceState.applications,
|
||||
apiBinder.balenaApi,
|
||||
);
|
||||
await normaliseLegacyDatabase();
|
||||
}
|
||||
|
||||
await this.deviceState.init();
|
||||
await deviceState.loadInitialState();
|
||||
|
||||
log.info('Starting API server');
|
||||
this.api.listen(conf.listenPort, conf.apiTimeout);
|
||||
this.deviceState.on('shutdown', () => this.api.stop());
|
||||
deviceState.on('shutdown', () => this.api.stop());
|
||||
|
||||
await apiBinder.start();
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ process.env.DATABASE_PATH_2 = './test/data/database2.sqlite';
|
||||
process.env.DATABASE_PATH_3 = './test/data/database3.sqlite';
|
||||
process.env.LED_FILE = './test/data/led_file';
|
||||
|
||||
import './lib/mocked-dockerode';
|
||||
import './lib/mocked-iptables';
|
||||
import './lib/mocked-event-tracker';
|
||||
|
||||
|
@ -122,6 +122,11 @@ describe('Config', () => {
|
||||
expect(osVariant).to.be.undefined;
|
||||
});
|
||||
|
||||
it('reads and exposes MAC addresses', async () => {
|
||||
const macAddress = await conf.get('macAddress');
|
||||
expect(macAddress).to.have.length.greaterThan(0);
|
||||
});
|
||||
|
||||
describe('Function config providers', () => {
|
||||
it('should throw if a non-mutable function provider is set', () => {
|
||||
expect(conf.set({ version: 'some-version' })).to.be.rejected;
|
||||
|
@ -1,17 +1,14 @@
|
||||
import * as Bluebird from 'bluebird';
|
||||
import { stripIndent } from 'common-tags';
|
||||
import * as _ from 'lodash';
|
||||
import { SinonSpy, SinonStub, spy, stub } from 'sinon';
|
||||
import { stub } from 'sinon';
|
||||
|
||||
import chai = require('./lib/chai-config');
|
||||
import { StatusCodeError } from '../src/lib/errors';
|
||||
import prepare = require('./lib/prepare');
|
||||
import Log from '../src/lib/supervisor-console';
|
||||
import * as dockerUtils from '../src/lib/docker-utils';
|
||||
import * as config from '../src/config';
|
||||
import * as images from '../src/compose/images';
|
||||
import { ConfigTxt } from '../src/config/backends/config-txt';
|
||||
import DeviceState from '../src/device-state';
|
||||
import * as deviceState from '../src/device-state';
|
||||
import * as deviceConfig from '../src/device-config';
|
||||
import { loadTargetFromFile } from '../src/device-state/preload';
|
||||
import Service from '../src/compose/service';
|
||||
@ -215,14 +212,16 @@ const testTargetInvalid = {
|
||||
};
|
||||
|
||||
describe('deviceState', () => {
|
||||
let deviceState: DeviceState;
|
||||
let source: string;
|
||||
const originalImagesSave = images.save;
|
||||
const originalImagesInspect = images.inspectByName;
|
||||
const originalGetCurrent = deviceConfig.getCurrent;
|
||||
|
||||
before(async () => {
|
||||
await prepare();
|
||||
await config.initialized;
|
||||
await deviceState.initialized;
|
||||
|
||||
source = await config.get('apiEndpoint');
|
||||
|
||||
stub(Service as any, 'extendEnvVars').callsFake((env) => {
|
||||
@ -235,10 +234,6 @@ describe('deviceState', () => {
|
||||
deviceType: 'intel-nuc',
|
||||
});
|
||||
|
||||
deviceState = new DeviceState({
|
||||
apiBinder: null as any,
|
||||
});
|
||||
|
||||
stub(dockerUtils, 'getNetworkGateway').returns(
|
||||
Promise.resolve('172.17.0.1'),
|
||||
);
|
||||
@ -277,10 +272,7 @@ describe('deviceState', () => {
|
||||
});
|
||||
|
||||
it('loads a target state from an apps.json file and saves it as target state, then returns it', async () => {
|
||||
await loadTargetFromFile(
|
||||
process.env.ROOT_MOUNTPOINT + '/apps.json',
|
||||
deviceState,
|
||||
);
|
||||
await loadTargetFromFile(process.env.ROOT_MOUNTPOINT + '/apps.json');
|
||||
const targetState = await deviceState.getTarget();
|
||||
|
||||
const testTarget = _.cloneDeep(testTarget1);
|
||||
@ -300,19 +292,16 @@ describe('deviceState', () => {
|
||||
});
|
||||
|
||||
it('stores info for pinning a device after loading an apps.json with a pinDevice field', async () => {
|
||||
await loadTargetFromFile(
|
||||
process.env.ROOT_MOUNTPOINT + '/apps-pin.json',
|
||||
deviceState,
|
||||
);
|
||||
await loadTargetFromFile(process.env.ROOT_MOUNTPOINT + '/apps-pin.json');
|
||||
|
||||
const pinned = await config.get('pinDevice');
|
||||
expect(pinned).to.have.property('app').that.equals(1234);
|
||||
expect(pinned).to.have.property('commit').that.equals('abcdef');
|
||||
});
|
||||
|
||||
it('emits a change event when a new state is reported', () => {
|
||||
it('emits a change event when a new state is reported', (done) => {
|
||||
deviceState.once('change', done);
|
||||
deviceState.reportCurrentState({ someStateDiff: 'someValue' } as any);
|
||||
return (expect as any)(deviceState).to.emit('change');
|
||||
});
|
||||
|
||||
it('returns the current state');
|
||||
@ -347,105 +336,30 @@ describe('deviceState', () => {
|
||||
});
|
||||
|
||||
it('allows triggering applying the target state', (done) => {
|
||||
stub(deviceState as any, 'applyTarget').returns(Promise.resolve());
|
||||
const applyTargetStub = stub(deviceState, 'applyTarget').returns(
|
||||
Promise.resolve(),
|
||||
);
|
||||
|
||||
deviceState.triggerApplyTarget({ force: true });
|
||||
expect((deviceState as any).applyTarget).to.not.be.called;
|
||||
expect(applyTargetStub).to.not.be.called;
|
||||
|
||||
setTimeout(() => {
|
||||
expect((deviceState as any).applyTarget).to.be.calledWith({
|
||||
expect(applyTargetStub).to.be.calledWith({
|
||||
force: true,
|
||||
initial: false,
|
||||
});
|
||||
(deviceState as any).applyTarget.restore();
|
||||
applyTargetStub.restore();
|
||||
done();
|
||||
}, 5);
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
it('cancels current promise applying the target state', (done) => {
|
||||
(deviceState as any).scheduledApply = { force: false, delay: 100 };
|
||||
(deviceState as any).applyInProgress = true;
|
||||
(deviceState as any).applyCancelled = false;
|
||||
// TODO: There is no easy way to test this behaviour with the current
|
||||
// interface of device-state. We should really think about the device-state
|
||||
// interface to allow this flexibility (and to avoid having to change module
|
||||
// internal variables)
|
||||
it.skip('cancels current promise applying the target state');
|
||||
|
||||
new Bluebird((resolve, reject) => {
|
||||
setTimeout(resolve, 100000);
|
||||
(deviceState as any).cancelDelay = reject;
|
||||
})
|
||||
.catch(() => {
|
||||
(deviceState as any).applyCancelled = true;
|
||||
})
|
||||
.finally(() => {
|
||||
expect((deviceState as any).scheduledApply).to.deep.equal({
|
||||
force: true,
|
||||
delay: 0,
|
||||
});
|
||||
expect((deviceState as any).applyCancelled).to.be.true;
|
||||
done();
|
||||
});
|
||||
it.skip('applies the target state for device config');
|
||||
|
||||
deviceState.triggerApplyTarget({ force: true, isFromApi: true });
|
||||
});
|
||||
|
||||
it('applies the target state for device config');
|
||||
|
||||
it('applies the target state for applications');
|
||||
|
||||
describe('healthchecks', () => {
|
||||
let configStub: SinonStub;
|
||||
let infoLobSpy: SinonSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
// This configStub will be modified in each test case so we can
|
||||
// create the exact conditions we want to for testing healthchecks
|
||||
configStub = stub(config, 'get');
|
||||
infoLobSpy = spy(Log, 'info');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
configStub.restore();
|
||||
infoLobSpy.restore();
|
||||
});
|
||||
|
||||
it('passes with correct conditions', async () => {
|
||||
// Setup passing condition
|
||||
const previousValue = deviceState.applyInProgress;
|
||||
deviceState.applyInProgress = false;
|
||||
expect(await deviceState.healthcheck()).to.equal(true);
|
||||
// Restore value
|
||||
deviceState.applyInProgress = previousValue;
|
||||
});
|
||||
|
||||
it('passes if unmanaged is true and exit early', async () => {
|
||||
// Setup failing conditions
|
||||
const previousValue = deviceState.applyInProgress;
|
||||
deviceState.applyInProgress = true;
|
||||
// Verify this causes healthcheck to fail
|
||||
expect(await deviceState.healthcheck()).to.equal(false);
|
||||
// Do it again but set unmanaged to true
|
||||
configStub.resolves({
|
||||
unmanaged: true,
|
||||
});
|
||||
expect(await deviceState.healthcheck()).to.equal(true);
|
||||
// Restore value
|
||||
deviceState.applyInProgress = previousValue;
|
||||
});
|
||||
|
||||
it('fails when applyTargetHealthy is false', async () => {
|
||||
// Copy previous values to restore later
|
||||
const previousValue = deviceState.applyInProgress;
|
||||
// Setup failing conditions
|
||||
deviceState.applyInProgress = true;
|
||||
expect(await deviceState.healthcheck()).to.equal(false);
|
||||
expect(Log.info).to.be.calledOnce;
|
||||
expect((Log.info as SinonSpy).lastCall?.lastArg).to.equal(
|
||||
stripIndent`
|
||||
Healthcheck failure - At least ONE of the following conditions must be true:
|
||||
- No applyInProgress ? false
|
||||
- fetchesInProgress ? false
|
||||
- cycleTimeWithinInterval ? false`,
|
||||
);
|
||||
// Restore value
|
||||
deviceState.applyInProgress = previousValue;
|
||||
});
|
||||
});
|
||||
it.skip('applies the target state for applications');
|
||||
});
|
||||
|
@ -5,7 +5,7 @@ import { SinonSpy, SinonStub, spy, stub } from 'sinon';
|
||||
|
||||
import prepare = require('./lib/prepare');
|
||||
import * as config from '../src/config';
|
||||
import DeviceState from '../src/device-state';
|
||||
import * as deviceState from '../src/device-state';
|
||||
import Log from '../src/lib/supervisor-console';
|
||||
import chai = require('./lib/chai-config');
|
||||
import balenaAPI = require('./lib/mocked-balena-api');
|
||||
@ -47,11 +47,8 @@ const initModels = async (obj: Dictionary<any>, filename: string) => {
|
||||
await ApiBinder.initialized;
|
||||
obj.apiBinder = ApiBinder;
|
||||
|
||||
obj.deviceState = new DeviceState({
|
||||
apiBinder: obj.apiBinder,
|
||||
});
|
||||
|
||||
obj.apiBinder.setDeviceState(obj.deviceState);
|
||||
await deviceState.initialized;
|
||||
obj.deviceState = deviceState;
|
||||
};
|
||||
|
||||
const mockProvisioningOpts = {
|
||||
@ -462,12 +459,12 @@ describe('ApiBinder', () => {
|
||||
// Copy previous values to restore later
|
||||
const previousStateReportErrors = components.apiBinder.stateReportErrors;
|
||||
const previousDeviceStateConnected =
|
||||
components.apiBinder.deviceState.connected;
|
||||
// @ts-ignore
|
||||
components.deviceState.connected;
|
||||
|
||||
// Set additional conditions not in configStub to cause a fail
|
||||
// @ts-expect-error
|
||||
CurrentState.stateReportErrors = 4;
|
||||
components.apiBinder.deviceState.connected = true;
|
||||
components.apiBinder.stateReportErrors = 4;
|
||||
components.deviceState.connected = true;
|
||||
|
||||
expect(await components.apiBinder.healthcheck()).to.equal(false);
|
||||
|
||||
@ -481,9 +478,8 @@ describe('ApiBinder', () => {
|
||||
);
|
||||
|
||||
// Restore previous values
|
||||
// @ts-expect-error
|
||||
CurrentState.stateReportErrors = previousStateReportErrors;
|
||||
components.apiBinder.deviceState.connected = previousDeviceStateConnected;
|
||||
components.apiBinder.stateReportErrors = previousStateReportErrors;
|
||||
components.deviceState.connected = previousDeviceStateConnected;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -6,7 +6,7 @@ import Network from '../src/compose/network';
|
||||
|
||||
import Service from '../src/compose/service';
|
||||
import Volume from '../src/compose/volume';
|
||||
import DeviceState from '../src/device-state';
|
||||
import * as deviceState from '../src/device-state';
|
||||
import * as dockerUtils from '../src/lib/docker-utils';
|
||||
import * as images from '../src/compose/images';
|
||||
|
||||
@ -67,10 +67,9 @@ describe('ApplicationManager', function () {
|
||||
const originalInspectByName = images.inspectByName;
|
||||
before(async function () {
|
||||
await prepare();
|
||||
this.deviceState = new DeviceState({
|
||||
apiBinder: null as any,
|
||||
});
|
||||
this.applications = this.deviceState.applications;
|
||||
await deviceState.initialized;
|
||||
|
||||
this.applications = deviceState.applications;
|
||||
|
||||
// @ts-expect-error assigning to a RO property
|
||||
images.inspectByName = () =>
|
||||
|
@ -1,35 +1,26 @@
|
||||
import { SinonStub, stub } from 'sinon';
|
||||
import { expect } from './lib/chai-config';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import * as APIBinder from '../src/api-binder';
|
||||
import * as apiBinder from '../src/api-binder';
|
||||
import { ApplicationManager } from '../src/application-manager';
|
||||
import DeviceState from '../src/device-state';
|
||||
import * as deviceState from '../src/device-state';
|
||||
import * as constants from '../src/lib/constants';
|
||||
import { docker } from '../src/lib/docker-utils';
|
||||
import { Supervisor } from '../src/supervisor';
|
||||
import { expect } from './lib/chai-config';
|
||||
import _ = require('lodash');
|
||||
|
||||
describe('Startup', () => {
|
||||
let reportCurrentStateStub: SinonStub;
|
||||
let startStub: SinonStub;
|
||||
let vpnStatusPathStub: SinonStub;
|
||||
let appManagerStub: SinonStub;
|
||||
let deviceStateStub: SinonStub;
|
||||
let dockerStub: SinonStub;
|
||||
|
||||
before(() => {
|
||||
reportCurrentStateStub = stub(
|
||||
DeviceState.prototype as any,
|
||||
'reportCurrentState',
|
||||
).resolves();
|
||||
startStub = stub(APIBinder as any, 'start').returns(Promise.resolve());
|
||||
appManagerStub = stub(ApplicationManager.prototype, 'init').returns(
|
||||
Promise.resolve(),
|
||||
);
|
||||
before(async () => {
|
||||
startStub = stub(apiBinder as any, 'start').resolves();
|
||||
deviceStateStub = stub(deviceState, 'applyTarget').resolves();
|
||||
appManagerStub = stub(ApplicationManager.prototype, 'init').resolves();
|
||||
vpnStatusPathStub = stub(constants, 'vpnStatusPath').returns('');
|
||||
deviceStateStub = stub(DeviceState.prototype as any, 'applyTarget').returns(
|
||||
Promise.resolve(),
|
||||
);
|
||||
dockerStub = stub(docker, 'listContainers').returns(Promise.resolve([]));
|
||||
});
|
||||
|
||||
@ -39,12 +30,12 @@ describe('Startup', () => {
|
||||
vpnStatusPathStub.restore();
|
||||
deviceStateStub.restore();
|
||||
dockerStub.restore();
|
||||
reportCurrentStateStub.restore();
|
||||
});
|
||||
|
||||
it('should startup correctly', async () => {
|
||||
const supervisor = new Supervisor();
|
||||
expect(await supervisor.init()).to.not.throw;
|
||||
await supervisor.init();
|
||||
|
||||
// Cast as any to access private properties
|
||||
const anySupervisor = supervisor as any;
|
||||
expect(anySupervisor.db).to.not.be.null;
|
||||
@ -52,20 +43,5 @@ describe('Startup', () => {
|
||||
expect(anySupervisor.logger).to.not.be.null;
|
||||
expect(anySupervisor.deviceState).to.not.be.null;
|
||||
expect(anySupervisor.apiBinder).to.not.be.null;
|
||||
|
||||
let macAddresses: string[] = [];
|
||||
reportCurrentStateStub.getCalls().forEach((call) => {
|
||||
const m: string = call.args[0]['mac_address'];
|
||||
if (!m) {
|
||||
return;
|
||||
}
|
||||
|
||||
macAddresses = _.union(macAddresses, m.split(' '));
|
||||
});
|
||||
|
||||
const allMacAddresses = macAddresses.join(' ');
|
||||
|
||||
expect(allMacAddresses).to.have.length.greaterThan(0);
|
||||
expect(allMacAddresses).to.not.contain('NO:');
|
||||
});
|
||||
});
|
||||
|
@ -3,7 +3,7 @@ import { spy, stub, SinonStub } from 'sinon';
|
||||
import * as supertest from 'supertest';
|
||||
|
||||
import * as apiBinder from '../src/api-binder';
|
||||
import DeviceState from '../src/device-state';
|
||||
import * as deviceState from '../src/device-state';
|
||||
import Log from '../src/lib/supervisor-console';
|
||||
import * as images from '../src/compose/images';
|
||||
import SupervisorAPI from '../src/supervisor-api';
|
||||
@ -25,11 +25,12 @@ describe('SupervisorAPI', () => {
|
||||
|
||||
before(async () => {
|
||||
await apiBinder.initialized;
|
||||
await deviceState.initialized;
|
||||
|
||||
// Stub health checks so we can modify them whenever needed
|
||||
healthCheckStubs = [
|
||||
stub(apiBinder, 'healthcheck'),
|
||||
stub(DeviceState.prototype, 'healthcheck'),
|
||||
stub(deviceState, 'healthcheck'),
|
||||
];
|
||||
// The mockedAPI contains stubs that might create unexpected results
|
||||
// See the module to know what has been stubbed
|
||||
|
@ -10,8 +10,8 @@ import * as config from '../../src/config';
|
||||
import * as db from '../../src/db';
|
||||
import { createV1Api } from '../../src/device-api/v1';
|
||||
import { createV2Api } from '../../src/device-api/v2';
|
||||
import * as APIBinder from '../../src/api-binder';
|
||||
import DeviceState from '../../src/device-state';
|
||||
import * as apiBinder from '../../src/api-binder';
|
||||
import * as deviceState from '../../src/device-state';
|
||||
import SupervisorAPI from '../../src/supervisor-api';
|
||||
|
||||
const DB_PATH = './test/data/supervisor-api.sqlite';
|
||||
@ -67,14 +67,12 @@ const STUBBED_VALUES = {
|
||||
|
||||
async function create(): Promise<SupervisorAPI> {
|
||||
// Get SupervisorAPI construct options
|
||||
const { deviceState, apiBinder } = await createAPIOpts();
|
||||
await createAPIOpts();
|
||||
|
||||
// Stub functions
|
||||
setupStubs();
|
||||
// Create ApplicationManager
|
||||
const appManager = new ApplicationManager({
|
||||
deviceState,
|
||||
apiBinder: null,
|
||||
});
|
||||
const appManager = new ApplicationManager();
|
||||
// Create SupervisorAPI
|
||||
const api = new SupervisorAPI({
|
||||
routers: [deviceState.router, buildRoutes(appManager)],
|
||||
@ -101,19 +99,12 @@ async function cleanUp(): Promise<void> {
|
||||
return restoreStubs();
|
||||
}
|
||||
|
||||
async function createAPIOpts(): Promise<SupervisorAPIOpts> {
|
||||
async function createAPIOpts(): Promise<void> {
|
||||
await db.initialized;
|
||||
await deviceState.initialized;
|
||||
|
||||
// Initialize and set values for mocked Config
|
||||
await initConfig();
|
||||
// Create deviceState
|
||||
const deviceState = new DeviceState({
|
||||
apiBinder: null as any,
|
||||
});
|
||||
const apiBinder = APIBinder;
|
||||
return {
|
||||
deviceState,
|
||||
apiBinder,
|
||||
};
|
||||
}
|
||||
|
||||
async function initConfig(): Promise<void> {
|
||||
@ -168,9 +159,4 @@ function restoreStubs() {
|
||||
serviceManager.getAllByAppId = originalSvcGetAppId;
|
||||
}
|
||||
|
||||
interface SupervisorAPIOpts {
|
||||
deviceState: DeviceState;
|
||||
apiBinder: typeof APIBinder;
|
||||
}
|
||||
|
||||
export = { create, cleanUp, STUBBED_VALUES };
|
||||
|
@ -1,3 +1,5 @@
|
||||
process.env.DOCKER_HOST = 'unix:///your/dockerode/mocks/are/not/working';
|
||||
|
||||
import * as dockerode from 'dockerode';
|
||||
|
||||
export interface TestData {
|
||||
|
Loading…
x
Reference in New Issue
Block a user