2019-04-04 20:46:01 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-01-21 12:53:38 -08:00
|
|
|
start,
|
2021-02-09 22:57:21 -08:00
|
|
|
rebuild,
|
2021-01-21 12:53:38 -08:00
|
|
|
|
|
|
|
|
DEFAULT_MEMORY_LIMIT: 256 * 1024 * 1024
|
2019-04-04 20:46:01 -07:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 17:50:48 -07:00
|
|
|
const apps = require('./apps.js'),
|
2020-07-24 14:44:41 +02:00
|
|
|
assert = require('assert'),
|
2021-06-24 16:19:30 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2020-07-24 14:44:41 +02:00
|
|
|
debug = require('debug')('box:sftp'),
|
2020-10-19 17:26:20 -07:00
|
|
|
hat = require('./hat.js'),
|
2019-04-04 20:46:01 -07:00
|
|
|
infra = require('./infra_version.js'),
|
2020-11-26 11:45:43 -08:00
|
|
|
paths = require('./paths.js'),
|
2020-08-09 12:10:20 -07:00
|
|
|
safe = require('safetydance'),
|
2020-08-20 11:04:31 +02:00
|
|
|
shell = require('./shell.js'),
|
2021-01-21 12:53:38 -08:00
|
|
|
system = require('./system.js'),
|
2021-06-24 16:19:30 -07:00
|
|
|
volumes = require('./volumes.js');
|
2019-04-04 20:46:01 -07:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function rebuild(serviceConfig, options) {
|
2021-01-21 12:53:38 -08:00
|
|
|
assert.strictEqual(typeof serviceConfig, 'object');
|
2021-02-09 22:57:21 -08:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
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;
|
2021-01-21 12:53:38 -08:00
|
|
|
const memoryLimit = serviceConfig.memoryLimit || exports.DEFAULT_MEMORY_LIMIT;
|
|
|
|
|
const memory = system.getMemoryAllocation(memoryLimit);
|
2020-10-19 17:26:20 -07:00
|
|
|
const cloudronToken = hat(8 * 128);
|
2019-04-04 20:46:01 -07:00
|
|
|
|
2021-06-24 16:19:30 -07:00
|
|
|
let dataDirs = [];
|
|
|
|
|
|
|
|
|
|
const stat = safe.fs.lstatSync(paths.APPS_DATA_DIR);
|
2021-08-25 19:41:46 -07:00
|
|
|
if (!stat) throw new BoxError(BoxError.FS_ERROR, safe.error);
|
2021-06-24 16:19:30 -07:00
|
|
|
|
|
|
|
|
const resolvedAppDataDir = stat.isSymbolicLink() ? safe.fs.readlinkSync(paths.APPS_DATA_DIR) : paths.APPS_DATA_DIR;
|
|
|
|
|
|
|
|
|
|
dataDirs.push({ hostDir: resolvedAppDataDir, mountDir: '/mnt/appsdata' });
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
const result = await apps.list();
|
|
|
|
|
|
|
|
|
|
result.forEach(function (app) {
|
|
|
|
|
if (!app.manifest.addons['localstorage'] || !app.dataDir) return;
|
|
|
|
|
|
|
|
|
|
const hostDir = apps.getDataDir(app, app.dataDir), mountDir = `/mnt/${app.id}`;
|
|
|
|
|
if (!safe.fs.existsSync(hostDir)) { // this can fail if external mount does not have permissions for yellowtent user
|
|
|
|
|
// do not create host path when cloudron is restoring. this will then create dir with root perms making restore logic fail
|
|
|
|
|
debug(`Ignoring app data dir ${hostDir} for ${app.id} since it does not exist`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataDirs.push({ hostDir, mountDir });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let allVolumes = await volumes.list();
|
|
|
|
|
|
|
|
|
|
dataDirs.push({ hostDir: '/mnt/volumes', mountDir: '/mnt/volumes' });
|
|
|
|
|
|
|
|
|
|
allVolumes.forEach(function (volume) {
|
|
|
|
|
if (volume.hostPath.startsWith('/mnt/volumes/')) return;
|
|
|
|
|
|
|
|
|
|
if (!safe.fs.existsSync(volume.hostPath)) {
|
|
|
|
|
debug(`Ignoring volume host path ${volume.hostPath} since it does not exist`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataDirs.push({ hostDir: volume.hostPath, mountDir: `/mnt/${volume.id}` });
|
2020-07-24 14:44:41 +02:00
|
|
|
});
|
2021-08-25 19:41:46 -07:00
|
|
|
|
|
|
|
|
const mounts = 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 ${memory} \
|
|
|
|
|
--memory-swap ${memoryLimit} \
|
|
|
|
|
--dns 172.18.0.1 \
|
|
|
|
|
--dns-search=. \
|
|
|
|
|
-p 222:22 \
|
|
|
|
|
${mounts} \
|
|
|
|
|
-e CLOUDRON_SFTP_TOKEN="${cloudronToken}" \
|
|
|
|
|
-v "${paths.SFTP_KEYS_DIR}:/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
|
|
|
|
|
await shell.promises.exec('stopSftp', 'docker stop sftp || true');
|
|
|
|
|
await shell.promises.exec('removeSftp', 'docker rm -f sftp || true');
|
|
|
|
|
await shell.promises.exec('startSftp', cmd);
|
2019-04-04 20:46:01 -07:00
|
|
|
}
|
2021-01-21 12:53:38 -08:00
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
async function start(existingInfra, serviceConfig) {
|
2021-01-21 12:53:38 -08:00
|
|
|
assert.strictEqual(typeof existingInfra, 'object');
|
|
|
|
|
assert.strictEqual(typeof serviceConfig, 'object');
|
|
|
|
|
|
2021-08-25 19:41:46 -07:00
|
|
|
await rebuild(serviceConfig, { force: true }); // force rebuild when infra changed
|
2021-01-21 12:53:38 -08:00
|
|
|
}
|