Refactor getAppBackupCredentials()
This commit is contained in:
+19
-46
@@ -114,33 +114,6 @@ function getByAppIdPaged(page, perPage, appId, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
function getAppBackupCredentials(app, manifest, callback) {
|
||||
assert.strictEqual(typeof app, 'object');
|
||||
assert(manifest && typeof manifest === 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
var now = new Date();
|
||||
var filebase = util.format('appbackup_%s_%s-v%s', app.id, now.toISOString(), manifest.version);
|
||||
var configFilename = filebase + '.json', dataFilename = filebase + '.tar.gz';
|
||||
|
||||
settings.getBackupConfig(function (error, backupConfig) {
|
||||
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
||||
|
||||
api(backupConfig.provider).getBackupCredentials(backupConfig, function (error, result) {
|
||||
if (error) return callback(error);
|
||||
|
||||
result.id = dataFilename;
|
||||
result.s3ConfigUrl = 's3://' + backupConfig.bucket + '/' + backupConfig.prefix + '/' + configFilename;
|
||||
result.s3DataUrl = 's3://' + backupConfig.bucket + '/' + backupConfig.prefix + '/' + dataFilename;
|
||||
result.backupKey = backupConfig.key;
|
||||
|
||||
debug('getAppBackupCredentials: %j', result);
|
||||
|
||||
callback(null, result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// backupId is the s3 filename. appbackup_%s_%s-v%s.tar.gz
|
||||
function getRestoreConfig(backupId, callback) {
|
||||
assert.strictEqual(typeof backupId, 'string');
|
||||
@@ -298,31 +271,31 @@ function createNewAppBackup(app, manifest, callback) {
|
||||
assert(manifest && typeof manifest === 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
getAppBackupCredentials(app, manifest, function (error, result) {
|
||||
if (error) return callback(error);
|
||||
var now = new Date();
|
||||
var filebase = util.format('appbackup_%s_%s-v%s', app.id, now.toISOString(), manifest.version);
|
||||
var configFilename = filebase + '.json', dataFilename = filebase + '.tar.gz';
|
||||
|
||||
debugApp(app, 'createNewAppBackup: backup url:%s backup config url:%s', result.s3DataUrl, result.s3ConfigUrl);
|
||||
settings.getBackupConfig(function (error, backupConfig) {
|
||||
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
||||
|
||||
var args;
|
||||
if (result.provider === 'filesystem') {
|
||||
args = [ 'filesystem', '/tmp/backups', result.id + '.json', result.id, result.backupKey ];
|
||||
} else {
|
||||
args = [ 's3', app.id, result.s3ConfigUrl, result.s3DataUrl, result.accessKeyId, result.secretAccessKey, result.region, result.backupKey ];
|
||||
if (result.sessionToken) args.push(result.sessionToken);
|
||||
}
|
||||
api(backupConfig.provider).getAppBackupDetails(backupConfig, app.id, dataFilename, configFilename, function (error, result) {
|
||||
if (error) return callback(error);
|
||||
|
||||
async.series([
|
||||
addons.backupAddons.bind(null, app, manifest.addons),
|
||||
shell.sudo.bind(null, 'backupApp', [ BACKUP_APP_CMD ].concat(args))
|
||||
], function (error) {
|
||||
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
||||
debug('createNewAppBackup: backup details %j', result);
|
||||
|
||||
debugApp(app, 'createNewAppBackup: %s done', result.id);
|
||||
|
||||
backupdb.add({ id: result.id, version: manifest.version, type: backupdb.BACKUP_TYPE_APP, dependsOn: [ ] }, function (error) {
|
||||
async.series([
|
||||
addons.backupAddons.bind(null, app, manifest.addons),
|
||||
shell.sudo.bind(null, 'backupApp', [ BACKUP_APP_CMD ].concat(result.backupScriptArguments))
|
||||
], function (error) {
|
||||
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
||||
|
||||
callback(null, result.id);
|
||||
debugApp(app, 'createNewAppBackup: %s done', result.id);
|
||||
|
||||
backupdb.add({ id: result.id, version: manifest.version, type: backupdb.BACKUP_TYPE_APP, dependsOn: [ ] }, function (error) {
|
||||
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
||||
|
||||
callback(null, result.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,20 +17,20 @@ readonly DATA_DIR="${HOME}/data"
|
||||
# verify argument count
|
||||
if [[ "$1" == "s3" && $# -lt 8 ]]; then
|
||||
echo "Usage: backupapp.sh s3 <appId> <s3 config url> <s3 data url> <access key id> <access key> <region> <password> [session token]"
|
||||
echo "Usage: backupapp.sh filesystem <backupFolder> <configFileName> <dataFileName> <password>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$1" == "filesystem" && $# -lt 5 ]]; then
|
||||
echo "Usage: backupapp.sh filesystem <backupFolder> <configFileName> <dataFileName> <password>"
|
||||
if [[ "$1" == "filesystem" && $# -lt 6 ]]; then
|
||||
echo "Usage: backupapp.sh filesystem <appId> <backupFolder> <configFileName> <dataFileName> <password>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
# extract arguments
|
||||
readonly app_id="$2"
|
||||
|
||||
if [[ "$1" == "s3" ]]; then
|
||||
# env vars used by the awscli
|
||||
readonly app_id="$2"
|
||||
readonly s3_config_url="$3"
|
||||
readonly s3_data_url="$4"
|
||||
export AWS_ACCESS_KEY_ID="$5"
|
||||
@@ -44,10 +44,10 @@ if [[ "$1" == "s3" ]]; then
|
||||
fi
|
||||
|
||||
if [[ "$1" == "filesystem" ]]; then
|
||||
readonly backup_folder="$2"
|
||||
readonly backup_config_fileName="$3"
|
||||
readonly backup_data_fileName="$4"
|
||||
readonly password="$5"
|
||||
readonly backup_folder="$3"
|
||||
readonly backup_config_fileName="$4"
|
||||
readonly backup_data_fileName="$5"
|
||||
readonly password="$6"
|
||||
fi
|
||||
|
||||
# perform backup
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
exports = module.exports = {
|
||||
getBackupDetails: getBackupDetails,
|
||||
getAppBackupDetails: getAppBackupDetails,
|
||||
|
||||
getAllPaged: getAllPaged,
|
||||
|
||||
getRestoreUrl: getRestoreUrl,
|
||||
@@ -59,6 +61,28 @@ function getBackupDetails(apiConfig, id, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
function getAppBackupDetails(apiConfig, appId, dataId, configId, callback) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
assert.strictEqual(typeof appId, 'string');
|
||||
assert.strictEqual(typeof dataId, 'string');
|
||||
assert.strictEqual(typeof configId, '');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
getBackupCredentials(apiConfig, function (error, result) {
|
||||
if (error) return callback(error);
|
||||
|
||||
var s3DataUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + dataId;
|
||||
var s3ConfigUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + configId;
|
||||
var region = apiConfig.region || 'us-east-1';
|
||||
|
||||
var details = {
|
||||
backupScriptArguments: [ 's3', appId, s3ConfigUrl, s3DataUrl, apiConfig.accessKeyId, apiConfig.secretAccessKey, region, apiConfig.key, result.sessionToken ]
|
||||
};
|
||||
|
||||
callback(null, details);
|
||||
});
|
||||
}
|
||||
|
||||
function getAllPaged(apiConfig, page, perPage, callback) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
assert.strictEqual(typeof page, 'number');
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
exports = module.exports = {
|
||||
getBackupDetails: getBackupDetails,
|
||||
getAppBackupDetails: getAppBackupDetails,
|
||||
|
||||
getAllPaged: getAllPaged,
|
||||
|
||||
getRestoreUrl: getRestoreUrl,
|
||||
@@ -25,6 +27,22 @@ function getBackupDetails(apiConfig, id, callback) {
|
||||
callback(null, details);
|
||||
}
|
||||
|
||||
function getAppBackupDetails(apiConfig, appId, dataId, configId, callback) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
assert.strictEqual(typeof appId, 'string');
|
||||
assert.strictEqual(typeof dataId, 'string');
|
||||
assert.strictEqual(typeof configId, '');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
var backupFolder = apiConfig.backupFolder || '/tmp/backups';
|
||||
|
||||
var details = {
|
||||
backupScriptArguments: [ 'filesystem', appId, backupFolder, configId, dataId, apiConfig.key ]
|
||||
};
|
||||
|
||||
callback(null, details);
|
||||
}
|
||||
|
||||
function getAllPaged(apiConfig, page, perPage, callback) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
assert.strictEqual(typeof page, 'number');
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
exports = module.exports = {
|
||||
getBackupDetails: getBackupDetails,
|
||||
getAppBackupDetails: getAppBackupDetails,
|
||||
|
||||
getAllPaged: getAllPaged,
|
||||
|
||||
getRestoreUrl: getRestoreUrl,
|
||||
@@ -45,6 +47,24 @@ function getBackupDetails(apiConfig, id, callback) {
|
||||
callback(null, details);
|
||||
}
|
||||
|
||||
function getAppBackupDetails(apiConfig, appId, dataId, configId, callback) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
assert.strictEqual(typeof appId, 'string');
|
||||
assert.strictEqual(typeof dataId, 'string');
|
||||
assert.strictEqual(typeof configId, '');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
var s3DataUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + dataId;
|
||||
var s3ConfigUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + configId;
|
||||
var region = apiConfig.region || 'us-east-1';
|
||||
|
||||
var details = {
|
||||
backupScriptArguments: [ 's3', appId, s3ConfigUrl, s3DataUrl, apiConfig.accessKeyId, apiConfig.secretAccessKey, region, apiConfig.key ]
|
||||
};
|
||||
|
||||
callback(null, details);
|
||||
}
|
||||
|
||||
function getAllPaged(apiConfig, page, perPage, callback) {
|
||||
assert.strictEqual(typeof apiConfig, 'object');
|
||||
assert.strictEqual(typeof page, 'number');
|
||||
|
||||
Reference in New Issue
Block a user