Convert application-manager.coffee to javascript

Change-type: patch
This commit is contained in:
Pagan Gazzard 2020-04-06 17:29:59 +01:00
parent 2be3322fc0
commit a2ec35456b
15 changed files with 1774 additions and 1044 deletions

View File

@ -55,7 +55,7 @@ modprobe ip6_tables || true
if [ "${LIVEPUSH}" -eq "1" ]; then
exec npx nodemon --watch src --watch typings --ignore tests \
--exec node -r ts-node/register/transpile-only -r coffeescript/register src/app.ts
--exec node -r ts-node/register/transpile-only src/app.ts
else
exec node /usr/src/app/dist/app.js
fi

View File

@ -10,7 +10,7 @@
"scripts": {
"start": "./entry.sh",
"build": "npm run typescript:release && webpack",
"build:debug": "npm run typescript:release && npm run coffeescript:release && npm run packagejson:copy",
"build:debug": "npm run typescript:release && npm run packagejson:copy",
"lint": "npm run lint:coffee && npm run lint:typescript",
"test": "npm run lint && npm run test-nolint",
"test-nolint": "npm run test:build && TEST=1 JUNIT_REPORT_PATH=report.xml istanbul cover _mocha && npm run coverage",
@ -22,10 +22,9 @@
"typescript:test-build": "tsc --project tsconfig.json",
"typescript:release": "tsc --project tsconfig.release.json && cp -r build/src/* build && rm -rf build/src",
"coffeescript:test": "coffee -m -c -o build .",
"coffeescript:release": "coffee -m -c -o build src",
"packagejson:copy": "cp package.json build/",
"testitems:copy": "cp -r test/data build/test/",
"lint:coffee": "balena-lint src/ test/",
"lint:coffee": "balena-lint test/",
"lint:typescript": "balena-lint -e ts -e js --typescript src/ test/ typings/ && tsc --noEmit && tsc --noEmit --project tsconfig.js.json",
"sync": "ts-node sync/sync.ts"
},

File diff suppressed because it is too large Load Diff

View File

@ -76,6 +76,7 @@ class ApplicationManager extends EventEmitter {
db: DB,
eventTracker: EventTracker,
deviceState: DeviceState,
apiBinder: APIBinder,
});
public init(): Promise<void>;
@ -95,7 +96,7 @@ class ApplicationManager extends EventEmitter {
dependent: any,
source: string,
transaction: Knex.Transaction,
): Promise<void>;
): Bluebird<void>;
public getStatus(): Promise<{
local: DeviceStatus.local.apps;
@ -107,7 +108,7 @@ class ApplicationManager extends EventEmitter {
public stopAll(opts: { force?: boolean; skipLock?: boolean }): Promise<void>;
public serviceNameFromId(serviceId: number): Bluebird<string>;
public imageForService(svc: any): Promise<Image>;
public imageForService(svc: any): Image;
public getDependentTargets(): Promise<any>;
public getCurrentForComparison(): Promise<any>;
public getDependentState(): Promise<any>;

1727
src/application-manager.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -38,7 +38,7 @@ interface FetchProgressEvent {
}
export interface Image {
id: number;
id?: number;
// image registry/repo@digest or registry/repo:tag
name: string;
appId: number;
@ -48,9 +48,9 @@ export interface Image {
imageId: number;
releaseId: number;
dependent: number;
dockerImageId: string;
status: 'Downloading' | 'Downloaded' | 'Deleting';
downloadProgress: Nullable<number>;
dockerImageId?: string;
status?: 'Downloading' | 'Downloaded' | 'Deleting';
downloadProgress?: number | null;
}
// TODO: Remove the need for this type...
@ -309,7 +309,10 @@ export class Images extends (EventEmitter as new () => ImageEventEmitter) {
},
);
const ids = _.map(imagesToRemove, 'id');
const ids = _(imagesToRemove)
.map('id')
.compact()
.value();
await this.db
.models('image')
.del()

View File

@ -164,7 +164,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
commit: string;
services: {
[serviceName: string]: {
status: string;
status?: string;
releaseId: number;
downloadProgress: number | null;
};
@ -200,7 +200,7 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
return service.imageId === img.imageId;
});
let status: string;
let status: string | undefined;
if (svc == null) {
status = img.status;
} else {

View File

@ -26,6 +26,7 @@ import * as updateLock from './lib/update-lock';
import * as validation from './lib/validation';
import * as network from './network';
import APIBinder from './api-binder';
import { ApplicationManager } from './application-manager';
import DeviceConfig, { ConfigStep } from './device-config';
import { log } from './lib/supervisor-console';
@ -179,6 +180,7 @@ interface DeviceStateConstructOpts {
config: Config;
eventTracker: EventTracker;
logger: Logger;
apiBinder: APIBinder;
}
interface DeviceStateEvents {
@ -243,7 +245,13 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
public connected: boolean;
public router: express.Router;
constructor({ db, config, eventTracker, logger }: DeviceStateConstructOpts) {
constructor({
db,
config,
eventTracker,
logger,
apiBinder,
}: DeviceStateConstructOpts) {
super();
this.db = db;
this.config = config;
@ -260,6 +268,7 @@ export class DeviceState extends (EventEmitter as new () => DeviceStateEventEmit
db: this.db,
eventTracker: this.eventTracker,
deviceState: this,
apiBinder,
});
this.on('error', err => log.error('deviceState error: ', err));

View File

@ -65,7 +65,7 @@ export async function loadTargetFromFile(
releaseId: app.releaseId,
appId,
};
images.push(await deviceState.applications.imageForService(svc));
images.push(deviceState.applications.imageForService(svc));
}
}

View File

@ -22,7 +22,7 @@ const LABEL_NAME_REGEX = /^[a-zA-Z][a-zA-Z0-9\.\-]*$/;
export function checkInt(
s: unknown,
options: CheckIntOptions = {},
): number | void {
): number | undefined {
if (s == null) {
return;
}

View File

@ -40,12 +40,6 @@ export class Supervisor {
this.config = new Config({ db: this.db });
this.eventTracker = new EventTracker();
this.logger = new Logger({ db: this.db, eventTracker: this.eventTracker });
this.deviceState = new DeviceState({
config: this.config,
db: this.db,
eventTracker: this.eventTracker,
logger: this.logger,
});
this.apiBinder = new APIBinder({
config: this.config,
db: this.db,
@ -53,12 +47,17 @@ export class Supervisor {
eventTracker: this.eventTracker,
logger: this.logger,
});
this.deviceState = new DeviceState({
config: this.config,
db: this.db,
eventTracker: this.eventTracker,
logger: this.logger,
apiBinder: this.apiBinder,
});
// 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(this.apiBinder);
// We could also do without the below dependency, but it's part of a much larger refactor
this.deviceState.applications.apiBinder = this.apiBinder;
this.api = new SupervisorAPI({
config: this.config,

View File

@ -48,7 +48,7 @@ export class TargetStateAccessor {
return _.find(this.targetState, app => app.appId === appId);
}
public async getTargetApps(): Promise<DatabaseApp> {
public async getTargetApps(): Promise<DatabaseApps> {
if (this.targetState == null) {
const { apiEndpoint, localMode } = await this.config.getMany([
'apiEndpoint',

View File

@ -232,6 +232,7 @@ describe('deviceState', () => {
config,
eventTracker: eventTracker as any,
logger: logger as any,
apiBinder: null as any,
});
stub(deviceState.applications.docker, 'getNetworkGateway').returns(

View File

@ -29,13 +29,6 @@ const initModels = async (obj: Dictionary<any>, filename: string) => {
},
} as any;
obj.deviceState = new DeviceState({
db: obj.db,
config: obj.config,
eventTracker: obj.eventTracker,
logger: obj.logger,
});
obj.apiBinder = new ApiBinder({
db: obj.db,
config: obj.config,
@ -43,6 +36,14 @@ const initModels = async (obj: Dictionary<any>, filename: string) => {
eventTracker: obj.eventTracker,
deviceState: obj.deviceState,
});
obj.deviceState = new DeviceState({
db: obj.db,
config: obj.config,
eventTracker: obj.eventTracker,
logger: obj.logger,
apiBinder: obj.apiBinder,
});
await obj.db.init();
await obj.config.init();
await obj.apiBinder.initClient(); // Initializes the clients but doesn't trigger provisioning

View File

@ -224,7 +224,7 @@ describe 'ApplicationManager', ->
expect(steps).to.eventually.deep.equal([{
action: 'kill'
current: current.local.apps['1234'].services[1]
target: null
target: undefined
serviceId: 24
appId: 1234
options: {}
@ -358,7 +358,7 @@ describe 'ApplicationManager', ->
{
action: 'kill'
current: current.local.apps['1234'].services[0]
target: null
target: undefined
serviceId: 23
appId: 1234
options: {}