mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-22 04:18:10 +00:00
refactor: Remove unnecessary validations and casts
Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
db74e748a1
commit
6a3148ff80
@ -20,11 +20,11 @@ import {
|
|||||||
} from './lib/errors';
|
} from './lib/errors';
|
||||||
import { pathExistsOnHost } from './lib/fs-utils';
|
import { pathExistsOnHost } from './lib/fs-utils';
|
||||||
import { request, requestOpts } from './lib/request';
|
import { request, requestOpts } from './lib/request';
|
||||||
import { ConfigValue } from './lib/types';
|
|
||||||
import { writeLock } from './lib/update-lock';
|
import { writeLock } from './lib/update-lock';
|
||||||
import { checkInt, checkTruthy } from './lib/validation';
|
|
||||||
import { DeviceApplicationState } from './types/state';
|
import { DeviceApplicationState } from './types/state';
|
||||||
|
|
||||||
|
import { SchemaReturn as ConfigSchemaType } from './config/schema-type';
|
||||||
|
|
||||||
const REPORT_SUCCESS_DELAY = 1000;
|
const REPORT_SUCCESS_DELAY = 1000;
|
||||||
const MAX_REPORT_RETRY_DELAY = 60000;
|
const MAX_REPORT_RETRY_DELAY = 60000;
|
||||||
|
|
||||||
@ -46,16 +46,8 @@ interface APIBinderConstructOpts {
|
|||||||
eventTracker: EventTracker;
|
eventTracker: EventTracker;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface KeyExchangeOpts {
|
|
||||||
uuid: ConfigValue;
|
|
||||||
deviceApiKey: ConfigValue;
|
|
||||||
apiTimeout: ConfigValue;
|
|
||||||
apiEndpoint: ConfigValue;
|
|
||||||
provisioningApiKey: ConfigValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Device {
|
interface Device {
|
||||||
id: string;
|
id: number;
|
||||||
|
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
@ -65,6 +57,8 @@ interface DevicePinInfo {
|
|||||||
commit: string;
|
commit: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type KeyExchangeOpts = ConfigSchemaType<'provisioningOptions'>;
|
||||||
|
|
||||||
export class APIBinder {
|
export class APIBinder {
|
||||||
public router: express.Router;
|
public router: express.Router;
|
||||||
|
|
||||||
@ -126,8 +120,7 @@ export class APIBinder {
|
|||||||
const timeSinceLastFetch = process.hrtime(this.lastTargetStateFetch);
|
const timeSinceLastFetch = process.hrtime(this.lastTargetStateFetch);
|
||||||
const timeSinceLastFetchMs =
|
const timeSinceLastFetchMs =
|
||||||
timeSinceLastFetch[0] * 1000 + timeSinceLastFetch[1] / 1e6;
|
timeSinceLastFetch[0] * 1000 + timeSinceLastFetch[1] / 1e6;
|
||||||
const stateFetchHealthy =
|
const stateFetchHealthy = timeSinceLastFetchMs < 2 * appUpdatePollInterval;
|
||||||
timeSinceLastFetchMs < 2 * (appUpdatePollInterval as number);
|
|
||||||
const stateReportHealthy =
|
const stateReportHealthy =
|
||||||
!connectivityCheckEnabled ||
|
!connectivityCheckEnabled ||
|
||||||
!this.deviceState.connected ||
|
!this.deviceState.connected ||
|
||||||
@ -146,7 +139,7 @@ export class APIBinder {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseUrl = url.resolve(apiEndpoint as string, '/v5/');
|
const baseUrl = url.resolve(apiEndpoint, '/v5/');
|
||||||
const passthrough = _.cloneDeep(requestOpts);
|
const passthrough = _.cloneDeep(requestOpts);
|
||||||
passthrough.headers =
|
passthrough.headers =
|
||||||
passthrough.headers != null ? passthrough.headers : {};
|
passthrough.headers != null ? passthrough.headers : {};
|
||||||
@ -210,16 +203,13 @@ export class APIBinder {
|
|||||||
// Either we haven't reported our initial config or we've been re-provisioned
|
// Either we haven't reported our initial config or we've been re-provisioned
|
||||||
if (apiEndpoint !== initialConfigReported) {
|
if (apiEndpoint !== initialConfigReported) {
|
||||||
console.log('Reporting initial configuration');
|
console.log('Reporting initial configuration');
|
||||||
await this.reportInitialConfig(
|
await this.reportInitialConfig(apiEndpoint, bootstrapRetryDelay);
|
||||||
apiEndpoint as string,
|
|
||||||
bootstrapRetryDelay as number,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Starting current state report');
|
console.log('Starting current state report');
|
||||||
await this.startCurrentStateReport();
|
await this.startCurrentStateReport();
|
||||||
|
|
||||||
await this.loadBackupFromMigration(bootstrapRetryDelay as number);
|
await this.loadBackupFromMigration(bootstrapRetryDelay);
|
||||||
|
|
||||||
this.readyForUpdates = true;
|
this.readyForUpdates = true;
|
||||||
console.log('Starting target state poll');
|
console.log('Starting target state poll');
|
||||||
@ -288,7 +278,7 @@ export class APIBinder {
|
|||||||
id,
|
id,
|
||||||
body: updatedFields,
|
body: updatedFields,
|
||||||
})
|
})
|
||||||
.timeout(conf.apiTimeout as number);
|
.timeout(conf.apiTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async provisionDependentDevice(device: Device): Promise<Device> {
|
public async provisionDependentDevice(device: Device): Promise<Device> {
|
||||||
@ -325,7 +315,7 @@ export class APIBinder {
|
|||||||
return (await this.balenaApi
|
return (await this.balenaApi
|
||||||
.post({ resource: 'device', body: device })
|
.post({ resource: 'device', body: device })
|
||||||
// TODO: Remove the `as number` when we fix the config typings
|
// TODO: Remove the `as number` when we fix the config typings
|
||||||
.timeout(conf.apiTimeout as number)) as Device;
|
.timeout(conf.apiTimeout)) as Device;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getTargetState(): Promise<DeviceApplicationState> {
|
public async getTargetState(): Promise<DeviceApplicationState> {
|
||||||
@ -354,7 +344,7 @@ export class APIBinder {
|
|||||||
|
|
||||||
return await this.cachedBalenaApi
|
return await this.cachedBalenaApi
|
||||||
._request(requestParams)
|
._request(requestParams)
|
||||||
.timeout(apiTimeout as number);
|
.timeout(apiTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Once 100% typescript, change this to a native promise
|
// TODO: Once 100% typescript, change this to a native promise
|
||||||
@ -451,7 +441,7 @@ export class APIBinder {
|
|||||||
'localMode',
|
'localMode',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (checkTruthy(conf.localMode || false)) {
|
if (conf.localMode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,12 +450,17 @@ export class APIBinder {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const apiEndpoint = conf.apiEndpoint;
|
||||||
|
const uuid = conf.uuid;
|
||||||
|
if (uuid == null || apiEndpoint == null) {
|
||||||
|
throw new InternalInconsistencyError(
|
||||||
|
'No uuid or apiEndpoint provided to ApiBinder.report',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
await Bluebird.resolve(
|
await Bluebird.resolve(
|
||||||
this.sendReportPatch(stateDiff, conf as {
|
this.sendReportPatch(stateDiff, { apiEndpoint, uuid }),
|
||||||
uuid: string;
|
).timeout(conf.apiTimeout);
|
||||||
apiEndpoint: string;
|
|
||||||
}),
|
|
||||||
).timeout(conf.apiTimeout as number);
|
|
||||||
|
|
||||||
this.stateReportErrors = 0;
|
this.stateReportErrors = 0;
|
||||||
_.assign(this.lastReportedState.local, stateDiff.local);
|
_.assign(this.lastReportedState.local, stateDiff.local);
|
||||||
@ -523,14 +518,7 @@ export class APIBinder {
|
|||||||
|
|
||||||
private async pollTargetState(): Promise<void> {
|
private async pollTargetState(): Promise<void> {
|
||||||
// TODO: Remove the checkInt here with the config changes
|
// TODO: Remove the checkInt here with the config changes
|
||||||
let pollInterval = checkInt((await this.config.get(
|
let pollInterval = await this.config.get('appUpdatePollInterval');
|
||||||
'appUpdatePollInterval',
|
|
||||||
)) as string);
|
|
||||||
if (!_.isNumber(pollInterval)) {
|
|
||||||
throw new InternalInconsistencyError(
|
|
||||||
'appUpdatePollInterval not a number in ApiBinder.pollTargetState',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.getAndSetTargetState(false);
|
await this.getAndSetTargetState(false);
|
||||||
@ -556,6 +544,13 @@ export class APIBinder {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const deviceId = await this.config.get('deviceId');
|
const deviceId = await this.config.get('deviceId');
|
||||||
|
|
||||||
|
if (deviceId == null) {
|
||||||
|
throw new InternalInconsistencyError(
|
||||||
|
'Device ID not defined in ApiBinder.pinDevice',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const release = await this.balenaApi.get({
|
const release = await this.balenaApi.get({
|
||||||
resource: 'release',
|
resource: 'release',
|
||||||
options: {
|
options: {
|
||||||
@ -577,7 +572,7 @@ export class APIBinder {
|
|||||||
|
|
||||||
await this.balenaApi.patch({
|
await this.balenaApi.patch({
|
||||||
resource: 'device',
|
resource: 'device',
|
||||||
id: deviceId as number,
|
id: deviceId,
|
||||||
body: {
|
body: {
|
||||||
should_be_running__release: releaseId,
|
should_be_running__release: releaseId,
|
||||||
},
|
},
|
||||||
@ -665,15 +660,11 @@ export class APIBinder {
|
|||||||
opts?: KeyExchangeOpts,
|
opts?: KeyExchangeOpts,
|
||||||
): Promise<Device> {
|
): Promise<Device> {
|
||||||
if (opts == null) {
|
if (opts == null) {
|
||||||
// FIXME: This casting shouldn't be necessary and stems from the
|
opts = await this.config.get('provisioningOptions');
|
||||||
// meta-option provioningOptions not returning a ConfigValue
|
|
||||||
opts = ((await this.config.get(
|
|
||||||
'provisioningOptions',
|
|
||||||
)) as any) as KeyExchangeOpts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const uuid = opts.uuid as string;
|
const uuid = opts.uuid;
|
||||||
const apiTimeout = opts.apiTimeout as number;
|
const apiTimeout = opts.apiTimeout;
|
||||||
if (!(uuid && apiTimeout)) {
|
if (!(uuid && apiTimeout)) {
|
||||||
throw new InternalInconsistencyError(
|
throw new InternalInconsistencyError(
|
||||||
'UUID and apiTimeout should be defined in exchangeKeyAndGetDevice',
|
'UUID and apiTimeout should be defined in exchangeKeyAndGetDevice',
|
||||||
@ -685,7 +676,7 @@ export class APIBinder {
|
|||||||
if (opts.deviceApiKey != null) {
|
if (opts.deviceApiKey != null) {
|
||||||
const device = await this.fetchDevice(
|
const device = await this.fetchDevice(
|
||||||
uuid,
|
uuid,
|
||||||
opts.deviceApiKey as string,
|
opts.deviceApiKey,
|
||||||
apiTimeout,
|
apiTimeout,
|
||||||
);
|
);
|
||||||
if (device != null) {
|
if (device != null) {
|
||||||
@ -695,9 +686,14 @@ export class APIBinder {
|
|||||||
|
|
||||||
// If it's not valid or doesn't exist then we try to use the
|
// If it's not valid or doesn't exist then we try to use the
|
||||||
// user/provisioning api key for the exchange
|
// user/provisioning api key for the exchange
|
||||||
|
if (!opts.provisioningApiKey) {
|
||||||
|
throw new InternalInconsistencyError(
|
||||||
|
'Required a provisioning key in exchangeKeyAndGetDevice',
|
||||||
|
);
|
||||||
|
}
|
||||||
const device = await this.fetchDevice(
|
const device = await this.fetchDevice(
|
||||||
uuid,
|
uuid,
|
||||||
opts.provisioningApiKey as string,
|
opts.provisioningApiKey,
|
||||||
apiTimeout,
|
apiTimeout,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -746,10 +742,7 @@ export class APIBinder {
|
|||||||
private async provision() {
|
private async provision() {
|
||||||
let device: Device | null = null;
|
let device: Device | null = null;
|
||||||
// FIXME: Config typing
|
// FIXME: Config typing
|
||||||
const opts = ((await this.config.get(
|
const opts = await this.config.get('provisioningOptions');
|
||||||
'provisioningOptions',
|
|
||||||
)) as any) as Dictionary<any>;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
opts.registered_at != null &&
|
opts.registered_at != null &&
|
||||||
opts.deviceId != null &&
|
opts.deviceId != null &&
|
||||||
@ -762,10 +755,7 @@ export class APIBinder {
|
|||||||
console.log(
|
console.log(
|
||||||
'Device is registered but no device id available, attempting key exchange',
|
'Device is registered but no device id available, attempting key exchange',
|
||||||
);
|
);
|
||||||
device =
|
device = (await this.exchangeKeyAndGetDeviceOrRegenerate(opts)) || null;
|
||||||
(await this.exchangeKeyAndGetDeviceOrRegenerate(
|
|
||||||
opts as KeyExchangeOpts,
|
|
||||||
)) || null;
|
|
||||||
} else if (opts.registered_at == null) {
|
} else if (opts.registered_at == null) {
|
||||||
console.log('New device detected. Provisioning...');
|
console.log('New device detected. Provisioning...');
|
||||||
try {
|
try {
|
||||||
@ -774,9 +764,7 @@ export class APIBinder {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (DuplicateUuidError(err)) {
|
if (DuplicateUuidError(err)) {
|
||||||
console.log('UUID already registered, trying a key exchange');
|
console.log('UUID already registered, trying a key exchange');
|
||||||
await this.exchangeKeyAndGetDeviceOrRegenerate(
|
await this.exchangeKeyAndGetDeviceOrRegenerate(opts);
|
||||||
opts as KeyExchangeOpts,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
@ -785,7 +773,7 @@ export class APIBinder {
|
|||||||
console.log(
|
console.log(
|
||||||
'Device is registered but we still have an apiKey, attempting key exchange',
|
'Device is registered but we still have an apiKey, attempting key exchange',
|
||||||
);
|
);
|
||||||
device = await this.exchangeKeyAndGetDevice(opts as KeyExchangeOpts);
|
device = await this.exchangeKeyAndGetDevice(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!device) {
|
if (!device) {
|
||||||
@ -811,13 +799,7 @@ export class APIBinder {
|
|||||||
this.eventTracker.track('Device bootstrap success');
|
this.eventTracker.track('Device bootstrap success');
|
||||||
|
|
||||||
// Now check if we need to pin the device
|
// Now check if we need to pin the device
|
||||||
const toPin = await this.config.get('pinDevice');
|
const pinValue = await this.config.get('pinDevice');
|
||||||
let pinValue: DevicePinInfo | null = null;
|
|
||||||
try {
|
|
||||||
pinValue = JSON.parse(toPin as string);
|
|
||||||
} catch (e) {
|
|
||||||
console.log('Warning: Malformed pinDevice value in supervisor database');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pinValue != null) {
|
if (pinValue != null) {
|
||||||
if (pinValue.app == null || pinValue.commit == null) {
|
if (pinValue.app == null || pinValue.commit == null) {
|
||||||
|
@ -140,7 +140,7 @@ module.exports = class ApplicationManager extends EventEmitter
|
|||||||
@images.save(step.image)
|
@images.save(step.image)
|
||||||
cleanup: (step) =>
|
cleanup: (step) =>
|
||||||
@config.get('localMode').then (localMode) =>
|
@config.get('localMode').then (localMode) =>
|
||||||
if !checkTruthy(localMode)
|
if !localMode
|
||||||
@images.cleanup()
|
@images.cleanup()
|
||||||
createNetworkOrVolume: (step) =>
|
createNetworkOrVolume: (step) =>
|
||||||
if step.model is 'network'
|
if step.model is 'network'
|
||||||
@ -762,7 +762,7 @@ module.exports = class ApplicationManager extends EventEmitter
|
|||||||
getTargetApps: =>
|
getTargetApps: =>
|
||||||
@config.getMany(['apiEndpoint', 'localMode']). then ({ apiEndpoint, localMode }) =>
|
@config.getMany(['apiEndpoint', 'localMode']). then ({ apiEndpoint, localMode }) =>
|
||||||
source = apiEndpoint
|
source = apiEndpoint
|
||||||
if checkTruthy(localMode)
|
if localMode
|
||||||
source = 'local'
|
source = 'local'
|
||||||
Promise.map(@db.models('app').where({ source }), @normaliseAndExtendAppFromDB)
|
Promise.map(@db.models('app').where({ source }), @normaliseAndExtendAppFromDB)
|
||||||
.map (app) =>
|
.map (app) =>
|
||||||
@ -847,8 +847,6 @@ module.exports = class ApplicationManager extends EventEmitter
|
|||||||
return { imagesToSave, imagesToRemove }
|
return { imagesToSave, imagesToRemove }
|
||||||
|
|
||||||
_inferNextSteps: (cleanupNeeded, availableImages, downloading, supervisorNetworkReady, current, target, ignoreImages, { localMode, delta }) =>
|
_inferNextSteps: (cleanupNeeded, availableImages, downloading, supervisorNetworkReady, current, target, ignoreImages, { localMode, delta }) =>
|
||||||
localMode = checkTruthy(localMode)
|
|
||||||
delta = checkTruthy(delta)
|
|
||||||
Promise.try =>
|
Promise.try =>
|
||||||
if localMode
|
if localMode
|
||||||
ignoreImages = true
|
ignoreImages = true
|
||||||
@ -895,7 +893,7 @@ module.exports = class ApplicationManager extends EventEmitter
|
|||||||
return Promise.try(fn)
|
return Promise.try(fn)
|
||||||
@config.get('lockOverride')
|
@config.get('lockOverride')
|
||||||
.then (lockOverride) ->
|
.then (lockOverride) ->
|
||||||
return checkTruthy(lockOverride) or force
|
return lockOverride or force
|
||||||
.then (force) ->
|
.then (force) ->
|
||||||
updateLock.lock(appId, { force }, fn)
|
updateLock.lock(appId, { force }, fn)
|
||||||
|
|
||||||
@ -919,7 +917,7 @@ module.exports = class ApplicationManager extends EventEmitter
|
|||||||
|
|
||||||
getRequiredSteps: (currentState, targetState, extraState, ignoreImages = false) =>
|
getRequiredSteps: (currentState, targetState, extraState, ignoreImages = false) =>
|
||||||
{ cleanupNeeded, availableImages, downloading, supervisorNetworkReady, delta, localMode } = extraState
|
{ cleanupNeeded, availableImages, downloading, supervisorNetworkReady, delta, localMode } = extraState
|
||||||
conf = _.mapValues({ delta, localMode }, (v) -> checkTruthy(v))
|
conf = { delta, localMode }
|
||||||
if conf.localMode
|
if conf.localMode
|
||||||
cleanupNeeded = false
|
cleanupNeeded = false
|
||||||
@_inferNextSteps(cleanupNeeded, availableImages, downloading, supervisorNetworkReady, currentState, targetState, ignoreImages, conf)
|
@_inferNextSteps(cleanupNeeded, availableImages, downloading, supervisorNetworkReady, currentState, targetState, ignoreImages, conf)
|
||||||
|
@ -59,7 +59,7 @@ module.exports = class Images extends EventEmitter
|
|||||||
.catch =>
|
.catch =>
|
||||||
@reportChange(image.imageId, _.merge(_.clone(image), { status: 'Downloading', downloadProgress: 0 }))
|
@reportChange(image.imageId, _.merge(_.clone(image), { status: 'Downloading', downloadProgress: 0 }))
|
||||||
Promise.try =>
|
Promise.try =>
|
||||||
if validation.checkTruthy(opts.delta) and opts.deltaSource?
|
if opts.delta and opts.deltaSource?
|
||||||
@logger.logSystemEvent(logTypes.downloadImageDelta, { image })
|
@logger.logSystemEvent(logTypes.downloadImageDelta, { image })
|
||||||
@inspectByName(opts.deltaSource)
|
@inspectByName(opts.deltaSource)
|
||||||
.then (srcImage) =>
|
.then (srcImage) =>
|
||||||
|
@ -10,7 +10,6 @@ import {
|
|||||||
serviceNotFoundMessage,
|
serviceNotFoundMessage,
|
||||||
v2ServiceEndpointInputErrorMessage,
|
v2ServiceEndpointInputErrorMessage,
|
||||||
} from '../lib/messages';
|
} from '../lib/messages';
|
||||||
import { checkTruthy } from '../lib/validation';
|
|
||||||
import { doPurge, doRestart, serviceAction } from './common';
|
import { doPurge, doRestart, serviceAction } from './common';
|
||||||
|
|
||||||
import supervisorVersion = require('../lib/supervisor-version');
|
import supervisorVersion = require('../lib/supervisor-version');
|
||||||
@ -232,7 +231,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
|
|||||||
|
|
||||||
router.get('/v2/local/target-state', async (_req, res) => {
|
router.get('/v2/local/target-state', async (_req, res) => {
|
||||||
try {
|
try {
|
||||||
const localMode = checkTruthy(await deviceState.config.get('localMode'));
|
const localMode = await deviceState.config.get('localMode');
|
||||||
if (!localMode) {
|
if (!localMode) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
status: 'failed',
|
status: 'failed',
|
||||||
@ -258,7 +257,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
|
|||||||
// TODO: We really should refactor the config module to provide bools
|
// TODO: We really should refactor the config module to provide bools
|
||||||
// as bools etc
|
// as bools etc
|
||||||
try {
|
try {
|
||||||
const localMode = checkTruthy(await deviceState.config.get('localMode'));
|
const localMode = await deviceState.config.get('localMode');
|
||||||
if (!localMode) {
|
if (!localMode) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
status: 'failed',
|
status: 'failed',
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import Config from './config';
|
import Config from './config';
|
||||||
|
import { SchemaTypeKey } from './config/schema-type';
|
||||||
import Database, { Transaction } from './db';
|
import Database, { Transaction } from './db';
|
||||||
import Logger from './logger';
|
import Logger from './logger';
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ export class DeviceConfig {
|
|||||||
private actionExecutors: DeviceActionExecutors;
|
private actionExecutors: DeviceActionExecutors;
|
||||||
private configBackend: DeviceConfigBackend | null = null;
|
private configBackend: DeviceConfigBackend | null = null;
|
||||||
|
|
||||||
private static configKeys: Dictionary<ConfigOption> = {
|
private static readonly configKeys: Dictionary<ConfigOption> = {
|
||||||
appUpdatePollInterval: {
|
appUpdatePollInterval: {
|
||||||
envVarName: 'SUPERVISOR_POLL_INTERVAL',
|
envVarName: 'SUPERVISOR_POLL_INTERVAL',
|
||||||
varType: 'int',
|
varType: 'int',
|
||||||
@ -144,7 +145,9 @@ export class DeviceConfig {
|
|||||||
if (!_.isObject(step.target)) {
|
if (!_.isObject(step.target)) {
|
||||||
throw new Error('Non-dictionary value passed to changeConfig');
|
throw new Error('Non-dictionary value passed to changeConfig');
|
||||||
}
|
}
|
||||||
await this.config.set(step.target as Dictionary<string>);
|
// TODO: Change the typing of step so that the types automatically
|
||||||
|
// work out and we don't need this cast to any
|
||||||
|
await this.config.set(step.target as { [key in SchemaTypeKey]: any });
|
||||||
if (step.humanReadableTarget) {
|
if (step.humanReadableTarget) {
|
||||||
this.logger.logConfigChange(step.humanReadableTarget, {
|
this.logger.logConfigChange(step.humanReadableTarget, {
|
||||||
success: true,
|
success: true,
|
||||||
@ -200,9 +203,6 @@ export class DeviceConfig {
|
|||||||
return this.configBackend;
|
return this.configBackend;
|
||||||
}
|
}
|
||||||
const dt = await this.config.get('deviceType');
|
const dt = await this.config.get('deviceType');
|
||||||
if (!_.isString(dt)) {
|
|
||||||
throw new Error('Could not detect device type');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.configBackend = configUtils.getConfigBackend(dt) || null;
|
this.configBackend = configUtils.getConfigBackend(dt) || null;
|
||||||
|
|
||||||
@ -253,9 +253,9 @@ export class DeviceConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getCurrent() {
|
public async getCurrent() {
|
||||||
const conf = await this.config.getMany(
|
const conf = await this.config.getMany(['deviceType'].concat(
|
||||||
['deviceType'].concat(_.keys(DeviceConfig.configKeys)),
|
_.keys(DeviceConfig.configKeys),
|
||||||
);
|
) as SchemaTypeKey[]);
|
||||||
|
|
||||||
const configBackend = await this.getConfigBackend();
|
const configBackend = await this.getConfigBackend();
|
||||||
|
|
||||||
@ -271,7 +271,7 @@ export class DeviceConfig {
|
|||||||
|
|
||||||
for (const key in DeviceConfig.configKeys) {
|
for (const key in DeviceConfig.configKeys) {
|
||||||
const { envVarName } = DeviceConfig.configKeys[key];
|
const { envVarName } = DeviceConfig.configKeys[key];
|
||||||
const confValue = conf[key];
|
const confValue = conf[key as SchemaTypeKey];
|
||||||
currentConf[envVarName] = confValue != null ? confValue.toString() : '';
|
currentConf[envVarName] = confValue != null ? confValue.toString() : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,7 +391,7 @@ export class DeviceConfig {
|
|||||||
|
|
||||||
// Check for special case actions for the VPN
|
// Check for special case actions for the VPN
|
||||||
if (
|
if (
|
||||||
!checkTruthy(unmanaged || false) &&
|
!unmanaged &&
|
||||||
!_.isEmpty(target['SUPERVISOR_VPN_CONTROL']) &&
|
!_.isEmpty(target['SUPERVISOR_VPN_CONTROL']) &&
|
||||||
DeviceConfig.checkBoolChanged(current, target, 'SUPERVISOR_VPN_CONTROL')
|
DeviceConfig.checkBoolChanged(current, target, 'SUPERVISOR_VPN_CONTROL')
|
||||||
) {
|
) {
|
||||||
|
@ -54,7 +54,7 @@ createDeviceStateRouter = (deviceState) ->
|
|||||||
rebootOrShutdown = (req, res, action) ->
|
rebootOrShutdown = (req, res, action) ->
|
||||||
deviceState.config.get('lockOverride')
|
deviceState.config.get('lockOverride')
|
||||||
.then (lockOverride) ->
|
.then (lockOverride) ->
|
||||||
force = validation.checkTruthy(req.body.force) or validation.checkTruthy(lockOverride)
|
force = validation.checkTruthy(req.body.force) or lockOverride
|
||||||
deviceState.executeStepAction({ action }, { force })
|
deviceState.executeStepAction({ action }, { force })
|
||||||
.then (response) ->
|
.then (response) ->
|
||||||
res.status(202).json(response)
|
res.status(202).json(response)
|
||||||
@ -246,7 +246,7 @@ module.exports = class DeviceState extends EventEmitter
|
|||||||
init: ->
|
init: ->
|
||||||
@config.on 'change', (changedConfig) =>
|
@config.on 'change', (changedConfig) =>
|
||||||
if changedConfig.loggingEnabled?
|
if changedConfig.loggingEnabled?
|
||||||
@logger.enable(validation.checkTruthy(changedConfig.loggingEnabled))
|
@logger.enable(changedConfig.loggingEnabled)
|
||||||
if changedConfig.apiSecret?
|
if changedConfig.apiSecret?
|
||||||
@reportCurrentState(api_secret: changedConfig.apiSecret)
|
@reportCurrentState(api_secret: changedConfig.apiSecret)
|
||||||
|
|
||||||
@ -258,7 +258,7 @@ module.exports = class DeviceState extends EventEmitter
|
|||||||
.then (conf) =>
|
.then (conf) =>
|
||||||
@applications.init()
|
@applications.init()
|
||||||
.then =>
|
.then =>
|
||||||
if !validation.checkTruthy(conf.initialConfigSaved)
|
if !conf.initialConfigSaved
|
||||||
@saveInitialConfig()
|
@saveInitialConfig()
|
||||||
.then =>
|
.then =>
|
||||||
@initNetworkChecks(conf)
|
@initNetworkChecks(conf)
|
||||||
@ -280,7 +280,7 @@ module.exports = class DeviceState extends EventEmitter
|
|||||||
.then =>
|
.then =>
|
||||||
@applications.getTargetApps()
|
@applications.getTargetApps()
|
||||||
.then (targetApps) =>
|
.then (targetApps) =>
|
||||||
if !conf.provisioned or (_.isEmpty(targetApps) and !validation.checkTruthy(conf.targetStateSet))
|
if !conf.provisioned or (_.isEmpty(targetApps) and !conf.targetStateSet)
|
||||||
@loadTargetFromFile()
|
@loadTargetFromFile()
|
||||||
.finally =>
|
.finally =>
|
||||||
@config.set({ targetStateSet: 'true' })
|
@config.set({ targetStateSet: 'true' })
|
||||||
@ -296,7 +296,7 @@ module.exports = class DeviceState extends EventEmitter
|
|||||||
@triggerApplyTarget({ initial: true })
|
@triggerApplyTarget({ initial: true })
|
||||||
|
|
||||||
initNetworkChecks: ({ apiEndpoint, connectivityCheckEnabled, unmanaged }) =>
|
initNetworkChecks: ({ apiEndpoint, connectivityCheckEnabled, unmanaged }) =>
|
||||||
return if validation.checkTruthy(unmanaged)
|
return if unmanaged
|
||||||
network.startConnectivityCheck apiEndpoint, connectivityCheckEnabled, (connected) =>
|
network.startConnectivityCheck apiEndpoint, connectivityCheckEnabled, (connected) =>
|
||||||
@connected = connected
|
@connected = connected
|
||||||
@config.on 'change', (changedConfig) ->
|
@config.on 'change', (changedConfig) ->
|
||||||
@ -499,9 +499,9 @@ module.exports = class DeviceState extends EventEmitter
|
|||||||
console.log('Device will be pinned')
|
console.log('Device will be pinned')
|
||||||
if commitToPin? and appToPin?
|
if commitToPin? and appToPin?
|
||||||
@config.set
|
@config.set
|
||||||
pinDevice: JSON.stringify {
|
pinDevice: {
|
||||||
commit: commitToPin,
|
commit: commitToPin,
|
||||||
app: appToPin,
|
app: parseInt(appToPin, 10),
|
||||||
}
|
}
|
||||||
# Ensure that this is actually a file, and not an empty path
|
# Ensure that this is actually a file, and not an empty path
|
||||||
# It can be an empty path because if the file does not exist
|
# It can be an empty path because if the file does not exist
|
||||||
|
@ -5,20 +5,3 @@ export interface EnvVarObject {
|
|||||||
export interface LabelObject {
|
export interface LabelObject {
|
||||||
[name: string]: string;
|
[name: string]: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// For backwards compatibility we need to use export = Config in config.ts
|
|
||||||
// so to export these types they have been moved here
|
|
||||||
export type ConfigValue = string | number | boolean | null;
|
|
||||||
|
|
||||||
export interface ConfigMap {
|
|
||||||
[key: string]: ConfigValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ConfigSchema {
|
|
||||||
[key: string]: {
|
|
||||||
source: string;
|
|
||||||
default?: any;
|
|
||||||
mutable?: boolean;
|
|
||||||
removeIfNull?: boolean;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
@ -3,8 +3,8 @@ import * as Docker from 'dockerode';
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import Config from './config';
|
import Config from './config';
|
||||||
|
import { SchemaReturn, SchemaTypeKey } from './config/schema-type';
|
||||||
import Database from './db';
|
import Database from './db';
|
||||||
import { checkTruthy } from './lib/validation';
|
|
||||||
import { Logger } from './logger';
|
import { Logger } from './logger';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,25 +25,28 @@ export class LocalModeManager {
|
|||||||
|
|
||||||
public async init() {
|
public async init() {
|
||||||
// Setup a listener to catch state changes relating to local mode
|
// Setup a listener to catch state changes relating to local mode
|
||||||
this.config.on('change', changed => {
|
this.config.on(
|
||||||
if (changed.localMode != null) {
|
'change',
|
||||||
const localMode = checkTruthy(changed.localMode) || false;
|
(changed: { [key in SchemaTypeKey]: SchemaReturn<key> }) => {
|
||||||
|
if (changed.localMode != null) {
|
||||||
|
const localMode = changed.localMode || false;
|
||||||
|
|
||||||
// First switch the logger to it's correct state
|
// First switch the logger to it's correct state
|
||||||
this.logger.switchBackend(localMode);
|
this.logger.switchBackend(localMode);
|
||||||
|
|
||||||
// If we're leaving local mode, make sure to remove all of the
|
// If we're leaving local mode, make sure to remove all of the
|
||||||
// leftover artifacts
|
// leftover artifacts
|
||||||
if (!localMode) {
|
if (!localMode) {
|
||||||
this.removeLocalModeArtifacts();
|
this.removeLocalModeArtifacts();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
// On startup, check if we're in unmanaged mode,
|
// On startup, check if we're in unmanaged mode,
|
||||||
// as local mode needs to be set
|
// as local mode needs to be set
|
||||||
let unmanagedLocalMode = false;
|
let unmanagedLocalMode = false;
|
||||||
if (checkTruthy((await this.config.get('unmanaged')) || false)) {
|
if (await this.config.get('unmanaged')) {
|
||||||
console.log('Starting up in unmanaged mode, activating local mode');
|
console.log('Starting up in unmanaged mode, activating local mode');
|
||||||
await this.config.set({ localMode: true });
|
await this.config.set({ localMode: true });
|
||||||
unmanagedLocalMode = true;
|
unmanagedLocalMode = true;
|
||||||
@ -51,8 +54,7 @@ export class LocalModeManager {
|
|||||||
|
|
||||||
const localMode =
|
const localMode =
|
||||||
// short circuit the next get if we know we're in local mode
|
// short circuit the next get if we know we're in local mode
|
||||||
unmanagedLocalMode ||
|
unmanagedLocalMode || (await this.config.get('localMode'));
|
||||||
checkTruthy((await this.config.get('localMode')) || false);
|
|
||||||
|
|
||||||
if (!localMode) {
|
if (!localMode) {
|
||||||
// Remove any leftovers if necessary
|
// Remove any leftovers if necessary
|
||||||
|
@ -4,6 +4,7 @@ import * as _ from 'lodash';
|
|||||||
import * as morgan from 'morgan';
|
import * as morgan from 'morgan';
|
||||||
|
|
||||||
import Config from './config';
|
import Config from './config';
|
||||||
|
import { SchemaReturn, SchemaTypeKey } from './config/schema-type';
|
||||||
import { EventTracker } from './event-tracker';
|
import { EventTracker } from './event-tracker';
|
||||||
import blink = require('./lib/blink');
|
import blink = require('./lib/blink');
|
||||||
import * as iptables from './lib/iptables';
|
import * as iptables from './lib/iptables';
|
||||||
@ -141,24 +142,23 @@ export class SupervisorAPI {
|
|||||||
port: number,
|
port: number,
|
||||||
apiTimeout: number,
|
apiTimeout: number,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const localMode = (await this.config.get('localMode')) || false;
|
const localMode = await this.config.get('localMode');
|
||||||
await this.applyListeningRules(
|
await this.applyListeningRules(localMode || false, port, allowedInterfaces);
|
||||||
checkTruthy(localMode) || false,
|
|
||||||
port,
|
|
||||||
allowedInterfaces,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Monitor the switching of local mode, and change which interfaces will
|
// Monitor the switching of local mode, and change which interfaces will
|
||||||
// be listened to based on that
|
// be listened to based on that
|
||||||
this.config.on('change', (changedConfig: Dictionary<string>) => {
|
this.config.on(
|
||||||
if (changedConfig.localMode != null) {
|
'change',
|
||||||
this.applyListeningRules(
|
(changedConfig: { [key in SchemaTypeKey]: SchemaReturn<key> }) => {
|
||||||
checkTruthy(changedConfig.localMode || false) || false,
|
if (changedConfig.localMode != null) {
|
||||||
port,
|
this.applyListeningRules(
|
||||||
allowedInterfaces,
|
changedConfig.localMode || false,
|
||||||
);
|
port,
|
||||||
}
|
allowedInterfaces,
|
||||||
});
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
this.server = this.api.listen(port);
|
this.server = this.api.listen(port);
|
||||||
this.server.timeout = apiTimeout;
|
this.server.timeout = apiTimeout;
|
||||||
|
@ -7,7 +7,6 @@ EventEmitter = require 'events'
|
|||||||
DeviceState = require './device-state'
|
DeviceState = require './device-state'
|
||||||
{ SupervisorAPI } = require './supervisor-api'
|
{ SupervisorAPI } = require './supervisor-api'
|
||||||
{ Logger } = require './logger'
|
{ Logger } = require './logger'
|
||||||
{ checkTruthy } = require './lib/validation'
|
|
||||||
|
|
||||||
constants = require './lib/constants'
|
constants = require './lib/constants'
|
||||||
|
|
||||||
@ -57,12 +56,12 @@ module.exports = class Supervisor extends EventEmitter
|
|||||||
apiEndpoint: conf.apiEndpoint,
|
apiEndpoint: conf.apiEndpoint,
|
||||||
uuid: conf.uuid,
|
uuid: conf.uuid,
|
||||||
deviceApiKey: conf.deviceApiKey,
|
deviceApiKey: conf.deviceApiKey,
|
||||||
unmanaged: checkTruthy(conf.unmanaged),
|
unmanaged: conf.unmanaged,
|
||||||
enableLogs: checkTruthy(conf.loggingEnabled),
|
enableLogs: conf.loggingEnabled,
|
||||||
localMode: checkTruthy(conf.localMode)
|
localMode: conf.localMode
|
||||||
})
|
})
|
||||||
.then =>
|
.then =>
|
||||||
if checkTruthy(conf.legacyAppsPresent)
|
if conf.legacyAppsPresent
|
||||||
console.log('Legacy app detected, running migration')
|
console.log('Legacy app detected, running migration')
|
||||||
@deviceState.normaliseLegacy(@apiBinder.balenaApi)
|
@deviceState.normaliseLegacy(@apiBinder.balenaApi)
|
||||||
.then =>
|
.then =>
|
||||||
|
Loading…
Reference in New Issue
Block a user