Files
cloudron-box/src/janitor.js
T

70 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-10-14 22:34:00 -07:00
'use strict';
2021-06-04 09:28:40 -07:00
const assert = require('assert'),
2015-10-14 22:34:00 -07:00
async = require('async'),
2019-12-04 10:29:06 -08:00
BoxError = require('./boxerror.js'),
2017-04-23 21:53:59 -07:00
debug = require('debug')('box:janitor'),
2019-12-04 13:17:58 -08:00
Docker = require('dockerode'),
2021-06-04 09:28:40 -07:00
safe = require('safetydance'),
tokens = require('./tokens.js');
2015-10-14 22:34:00 -07:00
exports = module.exports = {
2021-05-01 11:21:09 -07:00
cleanupTokens,
cleanupDockerVolumes
2015-10-14 22:34:00 -07:00
};
2019-12-04 13:17:58 -08:00
const NOOP_CALLBACK = function () { };
const gConnection = new Docker({ socketPath: '/var/run/docker.sock' });
2015-10-17 13:57:19 -07:00
2021-06-04 09:28:40 -07:00
async function cleanupTokens() {
2020-08-02 11:43:18 -07:00
debug('Cleaning up expired tokens');
2015-10-14 22:34:00 -07:00
2021-06-04 09:28:40 -07:00
const [error, result] = await safe(tokens.delExpired());
if (error) return debug('cleanupTokens: error removing expired tokens', error);
2015-10-14 22:34:00 -07:00
2021-06-04 09:28:40 -07:00
debug(`Cleaned up ${result} expired tokens`,);
2015-10-14 22:34:00 -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 -type f -mtime +10 -exec rm -rf {} +'.split(' '); // 10 day old files
2015-10-14 23:08:36 -07:00
2015-10-15 14:34:02 -07:00
debug('cleanupTmpVolume %j', containerInfo.Names);
2015-10-14 23:08:36 -07:00
2019-12-04 13:17:58 -08:00
gConnection.getContainer(containerInfo.Id).exec({ Cmd: cmd, AttachStdout: true, AttachStderr: true, Tty: false }, function (error, execContainer) {
2019-12-04 10:29:06 -08:00
if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Failed to exec container: ${error.message}`));
2015-10-14 23:08:36 -07:00
execContainer.start({ hijack: true }, function (error, stream) {
2019-12-04 10:29:06 -08:00
if (error) return callback(new BoxError(BoxError.DOCKER_ERROR, `Failed to start exec container: ${error.message}`));
2015-10-14 23:08:36 -07:00
stream.on('error', callback);
stream.on('end', callback);
2019-12-04 13:17:58 -08:00
gConnection.modem.demuxStream(stream, process.stdout, process.stderr);
2015-10-14 23:08:36 -07:00
});
});
}
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');
2019-12-04 13:17:58 -08:00
gConnection.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);
});
}