refactor: Convert migration module to typescript

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2018-12-12 15:13:33 +00:00
parent dc1450d65d
commit 2ea657c95d
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
3 changed files with 81 additions and 53 deletions

View File

@ -1,53 +0,0 @@
exports.defaultLegacyVolume = -> 'resin-data'
exports.singleToMulticontainerApp = (app) ->
environment = {}
for key of app.env
if !/^RESIN_/.test(key)
environment[key] = app.env[key]
appId = app.appId
conf = app.config ? {}
newApp = {
appId: appId
commit: app.commit
name: app.name
releaseId: 1
networks: {}
volumes: {}
}
defaultVolume = exports.defaultLegacyVolume()
newApp.volumes[defaultVolume] = {}
updateStrategy = conf['RESIN_SUPERVISOR_UPDATE_STRATEGY'] ? 'download-then-kill'
handoverTimeout = conf['RESIN_SUPERVISOR_HANDOVER_TIMEOUT'] ? ''
restartPolicy = conf['RESIN_APP_RESTART_POLICY'] ? 'always'
newApp.services = {
'1': {
appId: appId
serviceName: 'main'
imageId: 1
commit: app.commit
releaseId: 1
image: app.imageId
privileged: true
networkMode: 'host'
volumes: [
"#{defaultVolume}:/data"
],
labels: {
'io.resin.features.kernel-modules': '1'
'io.resin.features.firmware': '1'
'io.resin.features.dbus': '1',
'io.resin.features.supervisor-api': '1'
'io.resin.features.resin-api': '1'
'io.resin.update.strategy': updateStrategy
'io.resin.update.handover-timeout': handoverTimeout
'io.resin.legacy-container': '1'
},
environment: environment
restart: restartPolicy
running: true
}
}
return newApp

69
src/lib/migration.ts Normal file
View File

@ -0,0 +1,69 @@
import * as _ from 'lodash';
import { Application } from '../types/application';
export const defaultLegacyVolume = () => 'resin-data';
export function singleToMulticontainerApp(app: Dictionary<any>): Application {
const environment: Dictionary<string> = {};
for (const key in app.env) {
if (!/^RESIN_/.test(key)) {
environment[key] = app.env[key];
}
}
const { appId } = app;
const conf = app.config != null ? app.config : {};
const newApp = new Application();
_.assign(newApp, {
appId,
commit: app.commit,
name: app.name,
releaseId: 1,
networks: {},
volumes: {},
});
const defaultVolume = exports.defaultLegacyVolume();
newApp.volumes[defaultVolume] = {};
const updateStrategy =
conf['RESIN_SUPERVISOR_UPDATE_STRATEGY'] != null
? conf['RESIN_SUPERVISOR_UPDATE_STRATEGY']
: 'download-then-kill';
const handoverTimeout =
conf['RESIN_SUPERVISOR_HANDOVER_TIMEOUT'] != null
? conf['RESIN_SUPERVISOR_HANDOVER_TIMEOUT']
: '';
const restartPolicy =
conf['RESIN_APP_RESTART_POLICY'] != null
? conf['RESIN_APP_RESTART_POLICY']
: 'always';
newApp.services = {
// Disable the next line, as this *has* to be a string
// tslint:disable-next-line
'1': {
appId,
serviceName: 'main',
imageId: 1,
commit: app.commit,
releaseId: 1,
image: app.imageId,
privileged: true,
networkMode: 'host',
volumes: [`${defaultVolume}:/data`],
labels: {
'io.resin.features.kernel-modules': '1',
'io.resin.features.firmware': '1',
'io.resin.features.dbus': '1',
'io.resin.features.supervisor-api': '1',
'io.resin.features.resin-api': '1',
'io.resin.update.strategy': updateStrategy,
'io.resin.update.handover-timeout': handoverTimeout,
'io.resin.legacy-container': '1',
},
environment,
restart: restartPolicy,
running: true,
},
};
return newApp;
}

12
src/types/application.ts Normal file
View File

@ -0,0 +1,12 @@
import { ServiceComposeConfig } from '../compose/types/service';
export class Application {
public appId: number;
public commit: string;
public name: string;
public releaseId: number;
public networks: Dictionary<any>;
public volumes: Dictionary<any>;
public services: Dictionary<ServiceComposeConfig>;
}