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'),
|
2020-07-29 19:47:37 +02:00
|
|
|
path = require('path'),
|
2019-04-04 20:46:01 -07:00
|
|
|
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 19:47:37 +02:00
|
|
|
// options only supports ignoredApps = [ appId ]
|
2020-07-29 15:02:30 +02:00
|
|
|
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 19:47:37 +02:00
|
|
|
if (options.ignoredApps) assert(Array.isArray(options.ignoredApps), 'Expecting ignoredApps to be an array');
|
2020-07-29 15:02:30 +02:00
|
|
|
|
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) {
|
2020-07-29 19:50:08 +02:00
|
|
|
if (!app.manifest.addons['localstorage']) return;
|
2020-07-24 14:44:41 +02:00
|
|
|
|
2020-07-29 19:47:37 +02:00
|
|
|
if (options.ignoredApps && options.ignoredApps.indexOf(app.id) !== -1) {
|
|
|
|
|
debug(`Ignoring volume for ${app.id}`);
|
2020-07-29 15:02:30 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 14:44:41 +02:00
|
|
|
dataDirs.push({
|
2020-07-29 20:14:14 +02:00
|
|
|
hostDir: apps.getDataDir(app, app.dataDir),
|
|
|
|
|
mountDir: `/app/data/${app.id}`
|
2020-07-24 14:44:41 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-29 20:33:53 +02:00
|
|
|
debug('app volume mounts', dataDirs);
|
2020-07-24 14:44:41 +02:00
|
|
|
|
|
|
|
|
// 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.');
|
|
|
|
|
|
2020-07-29 19:47:37 +02:00
|
|
|
const appDataVolumes = dataDirs.map(function (v) { return `-v "${v.hostDir}:${v.mountDir}"`; }).join(' ');
|
2020-07-24 14:44:41 +02:00
|
|
|
|
|
|
|
|
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 \
|
2020-07-29 19:47:37 +02:00
|
|
|
${appDataVolumes} \
|
2020-07-24 14:44:41 +02:00
|
|
|
-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
|
|
|
}
|