Files
cloudron-box/src/janitor.js

130 lines
3.7 KiB
JavaScript
Raw Normal View History

'use strict';
var assert = require('assert'),
async = require('async'),
authcodedb = require('./authcodedb.js'),
backups = require('./backups.js'),
debug = require('debug')('box:src/janitor'),
docker = require('./docker.js').connection,
settings = require('./settings.js'),
tokendb = require('./tokendb.js');
exports = module.exports = {
2015-10-14 23:08:36 -07:00
cleanupTokens: cleanupTokens,
cleanupDockerVolumes: cleanupDockerVolumes,
cleanupBackups: cleanupBackups
};
2015-10-17 13:57:19 -07:00
var NOOP_CALLBACK = function () { };
function ignoreError(func) {
return function (callback) {
func(function (error) {
if (error) console.error('Ignored error:', error);
callback();
});
};
}
function cleanupExpiredTokens(callback) {
assert.strictEqual(typeof callback, 'function');
tokendb.delExpired(function (error, result) {
if (error) return callback(error);
debug('Cleaned up %s expired tokens.', result);
callback(null);
});
}
function cleanupExpiredAuthCodes(callback) {
assert.strictEqual(typeof callback, 'function');
authcodedb.delExpired(function (error, result) {
if (error) return callback(error);
debug('Cleaned up %s expired authcodes.', result);
callback(null);
});
}
function cleanupTokens(callback) {
2015-10-15 12:06:38 -07:00
assert(!callback || typeof callback === 'function'); // callback is null when called from cronjob
2015-10-14 23:08:36 -07:00
debug('Cleaning up expired tokens');
async.series([
ignoreError(cleanupExpiredTokens),
ignoreError(cleanupExpiredAuthCodes)
], callback);
}
2015-10-14 23:08:36 -07:00
2015-10-15 14:34:02 -07:00
function cleanupTmpVolume(containerInfo, callback) {
assert.strictEqual(typeof containerInfo, 'object');
2015-10-14 23:08:36 -07:00
assert.strictEqual(typeof callback, 'function');
var cmd = 'find /tmp -mtime +10 -exec rm -rf {} +'.split(' '); // 10 days old
2015-10-15 14:34:02 -07:00
debug('cleanupTmpVolume %j', containerInfo.Names);
2015-10-14 23:08:36 -07:00
2015-10-15 14:34:02 -07:00
docker.getContainer(containerInfo.Id).exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true, Tty: false }, function (error, execContainer) {
if (error) return callback(new Error('Failed to exec container : ' + error.message));
2015-10-14 23:08:36 -07:00
execContainer.start(function(err, stream) {
2015-10-15 14:34:02 -07:00
if (error) return callback(new Error('Failed to start exec container : ' + error.message));
2015-10-14 23:08:36 -07:00
stream.on('error', callback);
stream.on('end', callback);
stream.setEncoding('utf8');
stream.pipe(process.stdout);
});
});
}
function cleanupDockerVolumes(callback) {
2015-10-15 14:25:38 -07:00
assert(!callback || typeof callback === 'function'); // callback is null when called from cronjob
2015-10-14 23:08:36 -07:00
2015-10-17 13:57:19 -07:00
callback = callback || NOOP_CALLBACK;
2015-10-14 23:08:36 -07:00
debug('Cleaning up docker volumes');
docker.listContainers({ all: 0 }, function (error, containers) {
2015-10-14 23:08:36 -07:00
if (error) return callback(error);
2015-10-15 14:34:02 -07:00
async.eachSeries(containers, function (container, iteratorDone) {
cleanupTmpVolume(container, function (error) {
if (error) debug('Error cleaning tmp: %s', error);
iteratorDone(); // intentionally ignore error
});
2015-10-14 23:08:36 -07:00
}, callback);
});
}
function cleanupBackups(callback) {
assert(!callback || typeof callback === 'function'); // callback is null when called from cronjob
callback = callback || NOOP_CALLBACK;
debug('Cleaning backups');
settings.getBackupConfig(function (error, backupConfig) {
if (error) return callback(error);
// nothing to do here
if (backupConfig.provider !== 'filesystem') return callback();
backups.getPaged(1, 1000, function (error, result) {
if (error) return callback(error);
debug('Current backups:', result);
callback();
});
});
}