-
Notifications
You must be signed in to change notification settings - Fork 64
/
20171129064057_multicontainer.js
321 lines (314 loc) · 9.06 KB
/
20171129064057_multicontainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import Bluebird from 'bluebird';
import _ from 'lodash';
const tryParse = function (obj) {
try {
return JSON.parse(obj);
} catch {
return {};
}
};
const singleToMulticontainerApp = function (app) {
// From *very* old supervisors, env or config may be null
// so we ignore errors parsing them
const conf = tryParse(app.config);
const env = tryParse(app.env);
const environment = {};
const appId = parseInt(app.appId, 10);
for (const key in env) {
if (!/^RESIN_/.test(key)) {
environment[key] = env[key];
}
}
const newApp = {
appId: appId,
commit: app.commit,
name: app.name,
releaseId: 1,
networks: {},
volumes: {},
};
const defaultVolume = 'resin-data';
newApp.volumes[defaultVolume] = {};
const updateStrategy = _.get(
conf,
'RESIN_SUPERVISOR_UPDATE_STRATEGY',
'download-then-kill',
);
const handoverTimeout = _.get(conf, 'RESIN_SUPERVISOR_HANDOVER_TIMEOUT', '');
const restartPolicy = _.get(conf, 'RESIN_APP_RESTART_POLICY', 'always');
newApp.services = [
{
serviceId: 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;
};
const jsonifyAppFields = function (app) {
const newApp = _.clone(app);
newApp.services = JSON.stringify(app.services);
newApp.networks = JSON.stringify(app.networks);
newApp.volumes = JSON.stringify(app.volumes);
return newApp;
};
const imageForApp = function (app) {
const service = app.services[0];
return {
name: service.image,
appId: service.appId,
serviceId: service.serviceId,
serviceName: service.serviceName,
imageId: service.imageId,
releaseId: service.releaseId,
dependent: 0,
};
};
const imageForDependentApp = function (app) {
return {
name: app.image,
appId: app.appId,
serviceId: null,
serviceName: null,
imageId: app.imageId,
releaseId: null,
dependent: 1,
};
};
exports.up = function (knex) {
return Bluebird.resolve(
knex.schema.createTable('image', (t) => {
t.increments('id').primary();
t.string('name');
t.integer('appId');
t.integer('serviceId');
t.string('serviceName');
t.integer('imageId');
t.integer('releaseId');
t.boolean('dependent');
}),
)
.then(() =>
knex('app')
.select()
.whereNot({ markedForDeletion: true })
.orWhereNull('markedForDeletion'),
)
.tap((apps) => {
if (apps.length > 0) {
return knex('config').insert({
key: 'legacyAppsPresent',
value: 'true',
});
}
})
.tap(() => {
// We're in a transaction, and it's easier to drop and recreate
// than to migrate each field...
return knex.schema.dropTable('app').then(() => {
return knex.schema.createTable('app', (t) => {
t.increments('id').primary();
t.string('name');
t.integer('releaseId');
t.string('commit');
t.integer('appId');
t.json('services');
t.json('networks');
t.json('volumes');
});
});
})
.map((app) => {
const migratedApp = singleToMulticontainerApp(app);
return knex('app')
.insert(jsonifyAppFields(migratedApp))
.then(() => knex('image').insert(imageForApp(migratedApp)));
})
.then(() => {
// For some reason dropping a column in this table doesn't work. Anyways, we don't want to store old targetValues.
// Instead, on first run the supervisor will store current device config values as targets - so we want to
// make the old values that refer to supervisor config be the *current* values, and we do that by inserting
// to the config table.
return knex('deviceConfig')
.select()
.then((deviceConf) => {
return knex.schema.dropTable('deviceConfig').then(() => {
const values = JSON.parse(deviceConf[0].values);
const configKeys = {
RESIN_SUPERVISOR_POLL_INTERVAL: 'appUpdatePollInterval',
RESIN_SUPERVISOR_LOCAL_MODE: 'localMode',
RESIN_SUPERVISOR_CONNECTIVITY_CHECK: 'connectivityCheckEnabled',
RESIN_SUPERVISOR_LOG_CONTROL: 'loggingEnabled',
RESIN_SUPERVISOR_DELTA: 'delta',
RESIN_SUPERVISOR_DELTA_REQUEST_TIMEOUT: 'deltaRequestTimeout',
RESIN_SUPERVISOR_DELTA_APPLY_TIMEOUT: 'deltaApplyTimeout',
RESIN_SUPERVISOR_DELTA_RETRY_COUNT: 'deltaRetryCount',
RESIN_SUPERVISOR_DELTA_RETRY_INTERVAL: 'deltaRequestTimeout',
RESIN_SUPERVISOR_OVERRIDE_LOCK: 'lockOverride',
};
return Bluebird.map(Object.keys(values), (envVarName) => {
if (configKeys[envVarName] != null) {
return knex('config').insert({
key: configKeys[envVarName],
value: values[envVarName],
});
}
});
});
})
.then(() => {
return knex.schema.createTable('deviceConfig', (t) => {
t.json('targetValues');
});
})
.then(() => knex('deviceConfig').insert({ targetValues: '{}' }));
})
.then(() => knex('dependentApp').select())
.then((dependentApps) => {
return knex.schema
.dropTable('dependentApp')
.then(() => {
return knex.schema.createTable('dependentApp', (t) => {
t.increments('id').primary();
t.integer('appId');
t.integer('parentApp');
t.string('name');
t.string('commit');
t.integer('releaseId');
t.integer('imageId');
t.string('image');
t.json('environment');
t.json('config');
});
})
.then(() => {
return knex.schema.createTable('dependentAppTarget', (t) => {
t.increments('id').primary();
t.integer('appId');
t.integer('parentApp');
t.string('name');
t.string('commit');
t.integer('releaseId');
t.integer('imageId');
t.string('image');
t.json('environment');
t.json('config');
});
})
.then(() => {
return Bluebird.map(dependentApps, (app) => {
const newApp = {
appId: parseInt(app.appId, 10),
parentApp: parseInt(app.parentAppId, 10),
image: app.imageId,
releaseId: null,
commit: app.commit,
name: app.name,
config: JSON.stringify(tryParse(app.config)),
environment: JSON.stringify(tryParse(app.environment)),
};
const image = imageForDependentApp(newApp);
return knex('image')
.insert(image)
.then(() => knex('dependentApp').insert(newApp))
.then(() => knex('dependentAppTarget').insert(newApp));
});
});
})
.then(() => knex('dependentDevice').select())
.then((dependentDevices) => {
return knex.schema
.dropTable('dependentDevice')
.then(() => {
return knex.schema.createTable('dependentDevice', (t) => {
t.increments('id').primary();
t.string('uuid');
t.integer('appId');
t.string('localId');
t.string('device_type');
t.string('logs_channel');
t.integer('deviceId');
t.boolean('is_online');
t.string('name');
t.string('status');
t.string('download_progress');
t.integer('is_managed_by');
t.dateTime('lock_expiry_date');
t.string('commit');
t.string('targetCommit');
t.json('environment');
t.json('targetEnvironment');
t.json('config');
t.json('targetConfig');
t.boolean('markedForDeletion');
});
})
.then(() => {
return knex.schema.createTable('dependentDeviceTarget', (t) => {
t.increments('id').primary();
t.string('uuid');
t.string('name');
t.json('apps');
});
})
.then(() => {
return Bluebird.map(dependentDevices, (device) => {
const newDevice = _.clone(device);
newDevice.appId = parseInt(device.appId, 10);
newDevice.deviceId = parseInt(device.deviceId, 10);
if (device.is_managed_by != null) {
newDevice.is_managed_by = parseInt(device.is_managed_by, 10);
}
newDevice.config = JSON.stringify(tryParse(device.config));
newDevice.environment = JSON.stringify(
tryParse(device.environment),
);
newDevice.targetConfig = JSON.stringify(
tryParse(device.targetConfig),
);
newDevice.targetEnvironment = JSON.stringify(
tryParse(device.targetEnvironment),
);
if (newDevice.markedForDeletion == null) {
newDevice.markedForDeletion = false;
}
const deviceTarget = {
uuid: device.uuid,
name: device.name,
apps: {},
};
deviceTarget.apps[device.appId] = {
commit: newDevice.targetCommit,
config: newDevice.targetConfig,
environment: newDevice.targetEnvironment,
};
return knex('dependentDevice')
.insert(newDevice)
.then(() => knex('dependentDeviceTarget').insert(deviceTarget));
});
});
});
};
exports.down = function () {
return Promise.reject(new Error('Not implemented'));
};