Add migrate route

This commit is contained in:
Girish Ramakrishnan
2016-06-27 22:24:30 -05:00
parent eaa2058b10
commit dac9f29900
4 changed files with 237 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ exports = module.exports = {
updateToLatest: updateToLatest,
reboot: reboot,
retire: retire,
migrate: migrate,
isConfiguredSync: isConfiguredSync,
@@ -709,3 +710,45 @@ function retire(callback) {
shell.sudo('retire', [ RETIRE_CMD, JSON.stringify(data) ], callback);
}
function migrate(size, region, callback) {
assert.strictEqual(typeof size, 'string');
assert.strictEqual(typeof region, 'string');
assert.strictEqual(typeof callback, 'function');
var error = locker.lock(locker.OP_MIGRATE);
if (error) return callback(new CloudronError(CloudronError.BAD_STATE, error.message));
function unlock(error) {
if (error) {
debug('Failed to migrate', error);
locker.unlock(locker.OP_MIGRATE);
} else {
debug('Migration initiated successfully');
// do not unlock; cloudron is migrating
}
return;
}
// initiate the migration in the background
backups.backupBoxAndApps({ userId: null, username: 'migrator' }, function (error, backupId) {
if (error) return unlock(error);
debug('migrate: size %s region %s', size, region);
superagent
.post(config.apiServerOrigin() + '/api/v1/boxes/' + config.fqdn() + '/migrate')
.query({ token: config.token() })
.send({ domain: config.fqdn(), size: size, region: region, restoreKey: backupId })
.end(function (error, result) {
if (error && !error.response) return unlock(error); // network error
if (result.statusCode === 409) return unlock(new CloudronError(CloudronError.BAD_STATE));
if (result.statusCode === 404) return unlock(new CloudronError(CloudronError.NOT_FOUND));
if (result.statusCode !== 202) return unlock(new CloudronError(CloudronError.EXTERNAL_ERROR, util.format('%s %j', result.status, result.body)));
unlock(null);
});
});
callback(null);
}