Files
cloudron-box/src/sftp.js

90 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-04-04 20:46:01 -07:00
'use strict';
exports = module.exports = {
startSftp: startSftp,
rebuild: rebuild
2019-04-04 20:46:01 -07:00
};
var apps = require('./apps.js'),
assert = require('assert'),
async = require('async'),
debug = require('debug')('box:sftp'),
2019-04-04 20:46:01 -07:00
infra = require('./infra_version.js'),
paths = require('./paths.js'),
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);
}
// options only supports ignoredDataDirs = [ pathString ]
function rebuild(options, callback) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
if (options.ignoredDataDirs) assert(Array.isArray(options.ignoredDataDirs), 'Expecting ignoredDataDirs to be an array');
debug('rebuilding container');
2019-04-04 20:46:01 -07:00
const tag = infra.images.sftp.tag;
const memoryLimit = 256;
apps.getAll(function (error, result) {
if (error) return callback(error);
let dataDirs = [];
result.forEach(function (app) {
if (!app.dataDir) return;
if (options.ignoredDataDirs && options.ignoredDataDirs.indexOf(app.dataDir) !== -1) {
debug(`Ignoring dataDir ${app.dataDir}`);
return;
}
dataDirs.push({
hostDir: app.dataDir,
// /data is required since this is where the localstorage data would be in APPS_DATA_DIR
mountDir: `/app/data/${app.id}/data`
});
});
debug('extra app volume mounts', dataDirs);
// ignore error if container not found (and fail later) so that this code works across restarts
async.series([
shell.exec.bind(null, 'stopSftpContainer', 'docker stop sftp || true'),
shell.exec.bind(null, 'stopSftpContainer', 'docker rm -f sftp || true')
], function (error) {
if (error) debug('Failed to stop sftp container. Possibly not running.');
const extraAppDataVolumes = 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 \
-v "${paths.APPS_DATA_DIR}:/app/data" \
${extraAppDataVolumes} \
-v "/etc/ssh:/etc/ssh:ro" \
--label isCloudronManaged=true \
--read-only -v /tmp -v /run "${tag}"`;
2019-04-04 20:46:01 -07:00
shell.exec('startSftp', cmd, callback);
});
});
2019-04-04 20:46:01 -07:00
}