docker.js and services.js: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-08-25 19:41:46 -07:00
parent 1cc11fece8
commit 42774eac8c
24 changed files with 1618 additions and 2130 deletions

View File

@@ -2019,19 +2019,18 @@ function checkManifestConstraints(manifest) {
return null;
}
function exec(app, options, callback) {
async function exec(app, options) {
assert.strictEqual(typeof app, 'object');
assert(options && typeof options === 'object');
assert.strictEqual(typeof callback, 'function');
let cmd = options.cmd || [ '/bin/bash' ];
const cmd = options.cmd || [ '/bin/bash' ];
assert(Array.isArray(cmd) && cmd.length > 0);
if (app.installationState !== exports.ISTATE_INSTALLED || app.runState !== exports.RSTATE_RUNNING) {
return callback(new BoxError(BoxError.BAD_STATE, 'App not installed or running'));
throw new BoxError(BoxError.BAD_STATE, 'App not installed or running');
}
var execOptions = {
const execOptions = {
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
@@ -2043,7 +2042,7 @@ function exec(app, options, callback) {
Cmd: cmd
};
var startOptions = {
const startOptions = {
Detach: false,
Tty: options.tty,
// hijacking upgrades the docker connection from http to tcp. because of this upgrade,
@@ -2058,12 +2057,8 @@ function exec(app, options, callback) {
stderr: true
};
docker.execContainer(app.containerId, { execOptions, startOptions, rows: options.rows, columns: options.columns }, function (error, stream) {
if (error && error.statusCode === 409) return callback(new BoxError(BoxError.BAD_STATE, error.message)); // container restarting/not running
if (error) return callback(error);
callback(null, stream);
});
const stream = await docker.execContainer(app.containerId, { execOptions, startOptions, rows: options.rows, columns: options.columns });
return stream;
}
function canAutoupdateApp(app, updateInfo) {
@@ -2205,8 +2200,6 @@ async function configureInstalledApps() {
async function restartAppsUsingAddons(changedAddons) {
assert(Array.isArray(changedAddons));
const stopContainers = util.promisify(docker.stopContainers);
let apps = await list();
apps = apps.filter(app => app.manifest.addons && _.intersection(Object.keys(app.manifest.addons), changedAddons).length !== 0);
apps = apps.filter(app => app.installationState !== exports.ISTATE_ERROR); // remove errored apps. let them be 'repaired' by hand
@@ -2222,7 +2215,7 @@ async function restartAppsUsingAddons(changedAddons) {
};
// stop apps before updating the databases because postgres will "lock" them preventing import
const [stopError] = await safe(stopContainers(app.id));
const [stopError] = await safe(docker.stopContainers(app.id));
if (stopError) debug(`restartAppsUsingAddons: error stopping ${app.fqdn}`, stopError);
const [addTaskError, taskId] = await safe(addTask(app.id, exports.ISTATE_PENDING_RESTART, task));
@@ -2235,7 +2228,7 @@ async function restartAppsUsingAddons(changedAddons) {
async function schedulePendingTasks() {
debug('schedulePendingTasks: scheduling app tasks');
const result = list();
const result = await list();
result.forEach(function (app) {
if (!app.taskId) return; // if not in any pending state, do nothing