2019-04-04 20:46:01 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2020-07-24 14:44:41 +02:00
|
|
|
startSftp: startSftp,
|
|
|
|
|
rebuild: rebuild
|
2019-04-04 20:46:01 -07:00
|
|
|
};
|
|
|
|
|
|
2020-07-24 14:44:41 +02: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');
|
|
|
|
|
|
2020-07-24 14:44:41 +02:00
|
|
|
if (existingInfra.version === infra.version && infra.images.sftp.tag === existingInfra.images.sftp.tag) return callback();
|
|
|
|
|
|
2020-07-29 15:02:30 +02:00
|
|
|
rebuild({}, callback);
|
2020-07-24 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-29 15:02:30 +02:00
|
|
|
// options only supports ignoredDataDirs = [ pathString ]
|
|
|
|
|
function rebuild(options, callback) {
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
2020-07-24 14:44:41 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2020-07-29 15:02:30 +02:00
|
|
|
if (options.ignoredDataDirs) assert(Array.isArray(options.ignoredDataDirs), 'Expecting ignoredDataDirs to be an array');
|
|
|
|
|
|
2020-07-24 14:44:41 +02:00
|
|
|
debug('rebuilding container');
|
|
|
|
|
|
2019-04-04 20:46:01 -07:00
|
|
|
const tag = infra.images.sftp.tag;
|
|
|
|
|
const memoryLimit = 256;
|
|
|
|
|
|
2020-07-24 14:44:41 +02:00
|
|
|
apps.getAll(function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
let dataDirs = [];
|
|
|
|
|
result.forEach(function (app) {
|
|
|
|
|
if (!app.dataDir) return;
|
|
|
|
|
|
2020-07-29 15:02:30 +02:00
|
|
|
if (options.ignoredDataDirs && options.ignoredDataDirs.indexOf(app.dataDir) !== -1) {
|
|
|
|
|
debug(`Ignoring dataDir ${app.dataDir}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 14:44:41 +02:00
|
|
|
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
|
|
|
|
2020-07-24 14:44:41 +02:00
|
|
|
shell.exec('startSftp', cmd, callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-04-04 20:46:01 -07:00
|
|
|
}
|