'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'), path = require('path'), 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 ignoredApps = [ appId ] function rebuild(options, callback) { assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); if (options.ignoredApps) assert(Array.isArray(options.ignoredApps), 'Expecting ignoredApps to be an array'); debug('rebuilding container'); 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.ignoredApps && options.ignoredApps.indexOf(app.id) !== -1) { debug(`Ignoring volume for ${app.id}`); return; } dataDirs.push({ hostDir: app.dataDir || path.join(paths.APPS_DATA_DIR, app.id, 'data'), // /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 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}"`; shell.exec('startSftp', cmd, callback); }); }); }