Files
cloudron-box/src/sftp.js
2020-08-19 19:13:34 +02:00

92 lines
3.1 KiB
JavaScript

'use strict';
exports = module.exports = {
startSftp: startSftp,
rebuild: rebuild
};
var apps = require('./apps.js'),
assert = require('assert'),
async = require('async'),
debug = require('debug')('box:sftp'),
infra = require('./infra_version.js'),
safe = require('safetydance'),
shell = require('./shell.js');
function startSftp(existingInfra, callback) {
assert.strictEqual(typeof existingInfra, 'object');
assert.strictEqual(typeof callback, 'function');
if (existingInfra.version === infra.version && infra.images.sftp.tag === existingInfra.images.sftp.tag) return callback();
rebuild(callback);
}
var rebuildInProgress = false;
function rebuild(callback) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
if (rebuildInProgress) {
debug('waiting for other rebuild to finish');
return setTimeout(function () { rebuild(callback); }, 5000);
}
rebuildInProgress = true;
function done(error) {
rebuildInProgress = false;
callback(error);
}
debug('rebuilding container');
const tag = infra.images.sftp.tag;
const memoryLimit = 256;
apps.getAll(function (error, result) {
if (error) return done(error);
let dataDirs = [];
result.forEach(function (app) {
if (!app.manifest.addons['localstorage']) return;
const hostDir = apps.getDataDir(app, app.dataDir), mountDir = `/app/data/${app.id}`;
if (!safe.fs.existsSync(hostDir)) {
// do not create host path when cloudron is restoring. this will then create dir with root perms making restore logic fail
debug(`Ignoring volume for ${app.id} since it does not exist`);
return;
}
dataDirs.push({ hostDir, mountDir });
});
const appDataVolumes = dataDirs.map(function (v) { return `-v "${v.hostDir}:${v.mountDir}"`; }).join(' ');
const cmd = `docker run --restart=always -d --name="sftp" \
--hostname sftp \
--net cloudron \
--net-alias sftp \
--log-driver syslog \
--log-opt syslog-address=udp://127.0.0.1:2514 \
--log-opt syslog-format=rfc5424 \
--log-opt tag=sftp \
-m ${memoryLimit}m \
--memory-swap ${memoryLimit * 2}m \
--dns 172.18.0.1 \
--dns-search=. \
-p 222:22 \
${appDataVolumes} \
-v "/etc/ssh:/etc/ssh:ro" \
--label isCloudronManaged=true \
--read-only -v /tmp -v /run "${tag}"`;
// ignore error if container not found (and fail later) so that this code works across restarts
async.series([
shell.exec.bind(null, 'stopSftp', 'docker stop sftp || true'),
shell.exec.bind(null, 'removeSftp', 'docker rm -f sftp || true'),
shell.exec.bind(null, 'startSftp', cmd)
], done);
});
}