docker.js and services.js: async'ify
This commit is contained in:
118
src/sftp.js
118
src/sftp.js
@@ -9,7 +9,6 @@ exports = module.exports = {
|
||||
|
||||
const apps = require('./apps.js'),
|
||||
assert = require('assert'),
|
||||
async = require('async'),
|
||||
BoxError = require('./boxerror.js'),
|
||||
debug = require('debug')('box:sftp'),
|
||||
hat = require('./hat.js'),
|
||||
@@ -18,13 +17,11 @@ const apps = require('./apps.js'),
|
||||
safe = require('safetydance'),
|
||||
shell = require('./shell.js'),
|
||||
system = require('./system.js'),
|
||||
util = require('util'),
|
||||
volumes = require('./volumes.js');
|
||||
|
||||
function rebuild(serviceConfig, options, callback) {
|
||||
async function rebuild(serviceConfig, options) {
|
||||
assert.strictEqual(typeof serviceConfig, 'object');
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
debug('rebuilding container');
|
||||
|
||||
@@ -36,78 +33,71 @@ function rebuild(serviceConfig, options, callback) {
|
||||
let dataDirs = [];
|
||||
|
||||
const stat = safe.fs.lstatSync(paths.APPS_DATA_DIR);
|
||||
if (!stat) return callback(new BoxError(BoxError.FS_ERROR, safe.error));
|
||||
if (!stat) throw new BoxError(BoxError.FS_ERROR, safe.error);
|
||||
|
||||
const resolvedAppDataDir = stat.isSymbolicLink() ? safe.fs.readlinkSync(paths.APPS_DATA_DIR) : paths.APPS_DATA_DIR;
|
||||
|
||||
dataDirs.push({ hostDir: resolvedAppDataDir, mountDir: '/mnt/appsdata' });
|
||||
|
||||
util.callbackify(apps.list)(async function (error, result) {
|
||||
if (error) return callback(error);
|
||||
const result = await apps.list();
|
||||
|
||||
result.forEach(function (app) {
|
||||
if (!app.manifest.addons['localstorage'] || !app.dataDir) return;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
[error, allVolumes] = await safe(volumes.list());
|
||||
if (error) return callback(error);
|
||||
|
||||
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}` });
|
||||
});
|
||||
|
||||
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
|
||||
async.series([
|
||||
shell.exec.bind(null, 'stopSftp', 'docker stop sftp || true'),
|
||||
shell.exec.bind(null, 'removeSftp', 'docker rm -f sftp || true'),
|
||||
shell.exec.bind(null, 'startSftp', cmd)
|
||||
], callback);
|
||||
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}` });
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function start(existingInfra, serviceConfig, callback) {
|
||||
async function start(existingInfra, serviceConfig) {
|
||||
assert.strictEqual(typeof existingInfra, 'object');
|
||||
assert.strictEqual(typeof serviceConfig, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
rebuild(serviceConfig, { force: true }, callback); // force rebuild when infra changed
|
||||
await rebuild(serviceConfig, { force: true }); // force rebuild when infra changed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user