settings: move backup settings
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getBackupPolicy,
|
||||
setBackupPolicy,
|
||||
|
||||
getBackupConfig,
|
||||
setBackupConfig,
|
||||
|
||||
getRegistryConfig,
|
||||
setRegistryConfig,
|
||||
|
||||
@@ -88,31 +82,18 @@ exports = module.exports = {
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
backups = require('./backups.js'),
|
||||
cron = require('./cron.js'),
|
||||
database = require('./database.js'),
|
||||
debug = require('debug')('box:settings'),
|
||||
docker = require('./docker.js'),
|
||||
mounts = require('./mounts.js'),
|
||||
paths = require('./paths.js'),
|
||||
safe = require('safetydance'),
|
||||
_ = require('underscore');
|
||||
safe = require('safetydance');
|
||||
|
||||
const SETTINGS_FIELDS = [ 'name', 'value' ].join(',');
|
||||
const SETTINGS_BLOB_FIELDS = [ 'name', 'valueBlob' ].join(',');
|
||||
|
||||
const gDefaults = (function () {
|
||||
const result = { };
|
||||
result[exports.BACKUP_CONFIG_KEY] = {
|
||||
provider: 'filesystem',
|
||||
backupFolder: paths.DEFAULT_BACKUP_DIR,
|
||||
format: 'tgz',
|
||||
encryption: null,
|
||||
};
|
||||
result[exports.BACKUP_POLICY_KEY] = {
|
||||
retention: { keepWithinSecs: 2 * 24 * 60 * 60 }, // 2 days
|
||||
schedule: '00 00 23 * * *' // every day at 11pm
|
||||
};
|
||||
result[exports.REGISTRY_CONFIG_KEY] = {
|
||||
provider: 'noop'
|
||||
};
|
||||
@@ -187,81 +168,6 @@ async function clear() {
|
||||
await database.query('DELETE FROM settings');
|
||||
}
|
||||
|
||||
async function getBackupPolicy() {
|
||||
const result = await get(exports.BACKUP_POLICY_KEY);
|
||||
if (result === null) return gDefaults[exports.BACKUP_POLICY_KEY];
|
||||
return JSON.parse(result);
|
||||
}
|
||||
|
||||
async function setBackupPolicy(policy) {
|
||||
assert.strictEqual(typeof policy, 'object');
|
||||
|
||||
const error = await backups.validatePolicy(policy);
|
||||
if (error) throw error;
|
||||
|
||||
await set(exports.BACKUP_POLICY_KEY, JSON.stringify(policy));
|
||||
notifyChange(exports.BACKUP_POLICY_KEY, policy);
|
||||
}
|
||||
|
||||
async function getBackupConfig() {
|
||||
const value = await get(exports.BACKUP_CONFIG_KEY);
|
||||
if (value === null) return gDefaults[exports.BACKUP_CONFIG_KEY];
|
||||
|
||||
const backupConfig = JSON.parse(value); // { provider, token, password, region, prefix, bucket }
|
||||
return backupConfig;
|
||||
}
|
||||
|
||||
async function setBackupConfig(backupConfig) {
|
||||
assert.strictEqual(typeof backupConfig, 'object');
|
||||
|
||||
const oldConfig = await getBackupConfig();
|
||||
|
||||
backups.injectPrivateFields(backupConfig, oldConfig);
|
||||
|
||||
if (mounts.isManagedProvider(backupConfig.provider)) {
|
||||
let error = mounts.validateMountOptions(backupConfig.provider, backupConfig.mountOptions);
|
||||
if (error) throw error;
|
||||
|
||||
[error] = await safe(mounts.tryAddMount(mounts.mountObjectFromBackupConfig(backupConfig), { timeout: 10 })); // 10 seconds
|
||||
|
||||
if (error) {
|
||||
if (mounts.isManagedProvider(oldConfig.provider)) { // put back the old mount configuration
|
||||
debug('setBackupConfig: rolling back to previous mount configuration');
|
||||
|
||||
await safe(mounts.tryAddMount(mounts.mountObjectFromBackupConfig(oldConfig), { timeout: 10 }));
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const error = await backups.testConfig(backupConfig);
|
||||
if (error) throw error;
|
||||
|
||||
if ('password' in backupConfig) { // user set password
|
||||
const error = await backups.validateEncryptionPassword(backupConfig.password);
|
||||
if (error) throw error;
|
||||
|
||||
backupConfig.encryption = backups.generateEncryptionKeysSync(backupConfig.password);
|
||||
delete backupConfig.password;
|
||||
}
|
||||
|
||||
// if any of these changes, we have to clear the cache
|
||||
if (!_.isEqual(_.omit(backupConfig, 'limits'), _.omit(oldConfig, 'limits'))) {
|
||||
debug('setBackupConfig: clearing backup cache');
|
||||
backups.cleanupCacheFilesSync();
|
||||
}
|
||||
|
||||
await set(exports.BACKUP_CONFIG_KEY, JSON.stringify(backupConfig));
|
||||
|
||||
if (mounts.isManagedProvider(oldConfig.provider) && !mounts.isManagedProvider(backupConfig.provider)) {
|
||||
debug('setBackupConfig: removing old backup mount point');
|
||||
await safe(mounts.removeMount(mounts.mountObjectFromBackupConfig(oldConfig)));
|
||||
}
|
||||
|
||||
notifyChange(exports.BACKUP_CONFIG_KEY, backupConfig);
|
||||
}
|
||||
|
||||
async function getRegistryConfig() {
|
||||
const value = await get(exports.REGISTRY_CONFIG_KEY);
|
||||
if (value === null) return gDefaults[exports.REGISTRY_CONFIG_KEY];
|
||||
|
||||
Reference in New Issue
Block a user