2018-07-31 11:35:23 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-01-31 20:46:55 -08:00
|
|
|
updateToLatest,
|
|
|
|
|
update
|
2018-07-31 11:35:23 -07:00
|
|
|
};
|
|
|
|
|
|
2021-08-31 11:16:58 -07:00
|
|
|
const apps = require('./apps.js'),
|
2019-02-25 10:03:46 -08:00
|
|
|
assert = require('assert'),
|
2019-10-23 09:39:26 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2021-08-31 11:16:58 -07:00
|
|
|
backuptask = require('./backuptask.js'),
|
2019-07-25 14:40:52 -07:00
|
|
|
constants = require('./constants.js'),
|
2018-08-01 15:38:40 -07:00
|
|
|
crypto = require('crypto'),
|
2018-07-31 11:35:23 -07:00
|
|
|
debug = require('debug')('box:updater'),
|
2019-08-12 21:09:22 -07:00
|
|
|
df = require('@sindresorhus/df'),
|
2018-12-09 03:20:00 -08:00
|
|
|
eventlog = require('./eventlog.js'),
|
2020-06-11 08:27:48 -07:00
|
|
|
fs = require('fs'),
|
2018-12-09 03:20:00 -08:00
|
|
|
locker = require('./locker.js'),
|
2018-08-01 15:38:40 -07:00
|
|
|
os = require('os'),
|
2018-07-31 11:35:23 -07:00
|
|
|
path = require('path'),
|
2018-08-01 15:38:40 -07:00
|
|
|
paths = require('./paths.js'),
|
2021-08-31 13:12:14 -07:00
|
|
|
promiseRetry = require('./promise-retry.js'),
|
2018-08-01 15:38:40 -07:00
|
|
|
safe = require('safetydance'),
|
2019-02-25 10:03:46 -08:00
|
|
|
semver = require('semver'),
|
2021-02-01 14:07:23 -08:00
|
|
|
settings = require('./settings.js'),
|
2018-07-31 11:35:23 -07:00
|
|
|
shell = require('./shell.js'),
|
2018-11-19 20:01:02 -08:00
|
|
|
tasks = require('./tasks.js'),
|
2021-09-26 18:37:04 -07:00
|
|
|
updateChecker = require('./updatechecker.js');
|
2018-07-31 11:35:23 -07:00
|
|
|
|
2018-08-01 15:38:40 -07:00
|
|
|
const RELEASES_PUBLIC_KEY = path.join(__dirname, 'releases.gpg');
|
2018-07-31 11:35:23 -07:00
|
|
|
const UPDATE_CMD = path.join(__dirname, 'scripts/update.sh');
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function downloadUrl(url, file) {
|
2018-08-01 15:38:40 -07:00
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
|
|
|
|
|
// do not assert since it comes from the appstore
|
2021-08-31 13:12:14 -07:00
|
|
|
if (typeof url !== 'string') throw new BoxError(BoxError.EXTERNAL_ERROR, `url cannot be download to ${file} as it is not a string`);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
|
|
|
|
safe.fs.unlinkSync(file);
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
let retryCount = 0;
|
|
|
|
|
await promiseRetry({ times: 10, interval: 5000 }, async function () {
|
2018-08-01 15:38:40 -07:00
|
|
|
debug(`Downloading ${url} to ${file}. Try ${++retryCount}`);
|
|
|
|
|
|
|
|
|
|
const args = `-s --fail ${url} -o ${file}`;
|
|
|
|
|
|
|
|
|
|
debug(`downloadUrl: curl ${args}`);
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
const [error] = await safe(shell.promises.spawn('downloadUrl', '/usr/bin/curl', args.split(' '), {}));
|
|
|
|
|
if (error) throw new BoxError(BoxError.NETWORK_ERROR, `Failed to download ${url}: ${error.message}`);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
debug(`downloadUrl: downloaded ${url} to ${file}`);
|
|
|
|
|
});
|
2018-08-01 15:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function gpgVerify(file, sig) {
|
|
|
|
|
assert.strictEqual(typeof file, 'string');
|
|
|
|
|
assert.strictEqual(typeof sig, 'string');
|
|
|
|
|
|
2018-08-01 15:38:40 -07:00
|
|
|
const cmd = `/usr/bin/gpg --status-fd 1 --no-default-keyring --keyring ${RELEASES_PUBLIC_KEY} --verify ${sig} ${file}`;
|
|
|
|
|
|
|
|
|
|
debug(`gpgVerify: ${cmd}`);
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
const [error, stdout] = await safe(shell.promises.exec('gpgVerify', cmd));
|
|
|
|
|
if (error) {
|
|
|
|
|
debug(`gpgVerify: command failed. error: ${error}\n stdout: ${error.stdout}\n stderr: ${error.stderr}`);
|
|
|
|
|
throw new BoxError(BoxError.NOT_SIGNED, `The signature in ${path.basename(sig)} could not be verified (command failed)`);
|
|
|
|
|
}
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
if (stdout.indexOf('[GNUPG:] VALIDSIG 0EADB19CDDA23CD0FE71E3470A372F8703C493CC') !== -1) return; // success
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
debug(`gpgVerify: verification of ${sig} failed: ${stdout}\n`);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
throw new BoxError(BoxError.NOT_SIGNED, `The signature in ${path.basename(sig)} could not be verified (bad sig)`);
|
2018-08-01 15:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function extractTarball(tarball, dir) {
|
|
|
|
|
assert.strictEqual(typeof tarball, 'string');
|
|
|
|
|
assert.strictEqual(typeof dir, 'string');
|
|
|
|
|
|
2018-08-01 15:38:40 -07:00
|
|
|
const args = `-zxf ${tarball} -C ${dir}`;
|
|
|
|
|
|
|
|
|
|
debug(`extractTarball: tar ${args}`);
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
const [error] = await safe(shell.promises.spawn('extractTarball', '/bin/tar', args.split(' '), {}));
|
|
|
|
|
if (error) throw new BoxError(BoxError.FS_ERROR, `Failed to extract release package: ${error.message}`);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
safe.fs.unlinkSync(tarball);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
debug(`extractTarball: extracted ${tarball} to ${dir}`);
|
2018-08-01 15:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function verifyUpdateInfo(versionsFile, updateInfo) {
|
2018-08-01 15:38:40 -07:00
|
|
|
assert.strictEqual(typeof versionsFile, 'string');
|
|
|
|
|
assert.strictEqual(typeof updateInfo, 'object');
|
|
|
|
|
|
2021-03-03 10:21:52 -08:00
|
|
|
const releases = safe.JSON.parse(safe.fs.readFileSync(versionsFile, 'utf8')) || {};
|
2021-08-31 13:12:14 -07:00
|
|
|
if (!releases[constants.VERSION]) throw new BoxError(BoxError.EXTERNAL_ERROR, `No version info for ${constants.VERSION}`);
|
|
|
|
|
if (!releases[constants.VERSION].next) throw new BoxError(BoxError.EXTERNAL_ERROR, `No next version info for ${constants.VERSION}`);
|
2021-03-03 10:21:52 -08:00
|
|
|
const nextVersion = releases[constants.VERSION].next;
|
2021-08-31 13:12:14 -07:00
|
|
|
if (typeof releases[nextVersion] !== 'object' || !releases[nextVersion]) throw new BoxError(BoxError.EXTERNAL_ERROR, 'No next version info');
|
|
|
|
|
if (releases[nextVersion].sourceTarballUrl !== updateInfo.sourceTarballUrl) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Version info mismatch');
|
2018-08-01 15:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function downloadAndVerifyRelease(updateInfo) {
|
2018-08-01 15:38:40 -07:00
|
|
|
assert.strictEqual(typeof updateInfo, 'object');
|
2021-08-31 13:12:14 -07:00
|
|
|
|
|
|
|
|
const newBoxSource = path.join(os.tmpdir(), 'box-' + crypto.randomBytes(4).readUInt32LE(0));
|
|
|
|
|
|
|
|
|
|
await downloadUrl(updateInfo.boxVersionsUrl, `${paths.UPDATE_DIR}/versions.json`);
|
|
|
|
|
await downloadUrl(updateInfo.boxVersionsSigUrl, `${paths.UPDATE_DIR}/versions.json.sig`);
|
|
|
|
|
await gpgVerify(`${paths.UPDATE_DIR}/versions.json`, `${paths.UPDATE_DIR}/versions.json.sig`);
|
|
|
|
|
await verifyUpdateInfo(`${paths.UPDATE_DIR}/versions.json`, updateInfo);
|
|
|
|
|
await downloadUrl(updateInfo.sourceTarballUrl, `${paths.UPDATE_DIR}/box.tar.gz`);
|
|
|
|
|
await downloadUrl(updateInfo.sourceTarballSigUrl, `${paths.UPDATE_DIR}/box.tar.gz.sig`);
|
|
|
|
|
await gpgVerify(`${paths.UPDATE_DIR}/box.tar.gz`, `${paths.UPDATE_DIR}/box.tar.gz.sig`);
|
|
|
|
|
const [mkdirError] = await safe(fs.promises.mkdir(newBoxSource, { recursive: true }));
|
|
|
|
|
if (mkdirError) throw new BoxError(BoxError.FS_ERROR, `Failed to create directory ${newBoxSource}: ${mkdirError.message}`);
|
|
|
|
|
await extractTarball(`${paths.UPDATE_DIR}/box.tar.gz`, newBoxSource);
|
|
|
|
|
|
|
|
|
|
return { file: newBoxSource };
|
2018-08-01 15:38:40 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function checkFreeDiskSpace(neededSpace) {
|
2019-08-12 21:09:22 -07:00
|
|
|
assert.strictEqual(typeof neededSpace, 'number');
|
|
|
|
|
|
|
|
|
|
// can probably be a bit more aggressive here since a new update can bring in new docker images
|
2021-08-31 13:12:14 -07:00
|
|
|
const [error, diskUsage] = await safe(df.file('/'));
|
|
|
|
|
if (error) throw new BoxError(BoxError.FS_ERROR, error);
|
2019-08-12 21:47:22 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
if (diskUsage.available < neededSpace) throw new BoxError(BoxError.FS_ERROR, 'Not enough disk space');
|
2019-08-12 21:09:22 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
async function update(boxUpdateInfo, options, progressCallback) {
|
2018-08-01 15:38:40 -07:00
|
|
|
assert(boxUpdateInfo && typeof boxUpdateInfo === 'object');
|
2019-05-12 13:28:53 -07:00
|
|
|
assert(options && typeof options === 'object');
|
2018-11-30 16:00:47 -08:00
|
|
|
assert.strictEqual(typeof progressCallback, 'function');
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2019-08-12 21:09:22 -07:00
|
|
|
progressCallback({ percent: 1, message: 'Checking disk space' });
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
await checkFreeDiskSpace(1024*1024*1024);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
progressCallback({ percent: 5, message: 'Downloading and verifying release' });
|
2019-08-12 21:09:22 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
const packageInfo = await downloadAndVerifyRelease(boxUpdateInfo);
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
if (!options.skipBackup) {
|
|
|
|
|
progressCallback({ percent: 10, message: 'Backing up' });
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-09-26 18:37:04 -07:00
|
|
|
await backuptask.fullBackup({ preserveSecs: 3*7*24*60*60 }, (progress) => progressCallback({ percent: 10+progress.percent*70/100, message: progress.message }));
|
2021-11-16 18:20:12 -08:00
|
|
|
|
|
|
|
|
await checkFreeDiskSpace(1024*1024*1024); // check again in case backup is in same disk
|
2021-08-31 13:12:14 -07:00
|
|
|
}
|
2018-08-01 15:38:40 -07:00
|
|
|
|
2021-11-16 18:20:12 -08:00
|
|
|
debug(`Updating box with ${boxUpdateInfo.sourceTarballUrl}`);
|
2019-08-12 21:09:22 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
progressCallback({ percent: 70, message: 'Installing update' });
|
2019-08-12 21:09:22 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
// run installer.sh from new box code as a separate service
|
|
|
|
|
await shell.promises.sudo('update', [ UPDATE_CMD, packageInfo.file ], {});
|
2019-08-12 21:09:22 -07:00
|
|
|
|
2021-08-31 13:12:14 -07:00
|
|
|
// Do not add any code here. The installer script will stop the box code any instant
|
2018-08-01 15:38:40 -07:00
|
|
|
}
|
2018-07-31 11:35:23 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
async function canUpdate(boxUpdateInfo) {
|
2019-02-25 10:03:46 -08:00
|
|
|
assert.strictEqual(typeof boxUpdateInfo, 'object');
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const result = await apps.list();
|
2019-02-25 10:03:46 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
for (let app of result) {
|
|
|
|
|
const maxBoxVersion = app.manifest.maxBoxVersion;
|
|
|
|
|
if (semver.valid(maxBoxVersion) && semver.gt(boxUpdateInfo.version, maxBoxVersion)) {
|
|
|
|
|
throw new BoxError(BoxError.BAD_STATE, `Cannot update to v${boxUpdateInfo.version} because ${app.fqdn} has a maxBoxVersion of ${maxBoxVersion}`);
|
2019-02-25 10:03:46 -08:00
|
|
|
}
|
2021-08-20 09:19:44 -07:00
|
|
|
}
|
2019-02-25 10:03:46 -08:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
async function updateToLatest(options, auditSource) {
|
2019-05-12 13:28:53 -07:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
2018-07-31 11:35:23 -07:00
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const boxUpdateInfo = updateChecker.getUpdateInfo().box;
|
|
|
|
|
if (!boxUpdateInfo) throw new BoxError(BoxError.NOT_FOUND, 'No update available');
|
|
|
|
|
if (!boxUpdateInfo.sourceTarballUrl) throw new BoxError(BoxError.BAD_STATE, 'No automatic update available');
|
2018-07-31 11:35:23 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
await canUpdate(boxUpdateInfo);
|
2018-12-04 14:04:43 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const error = locker.lock(locker.OP_BOX_UPDATE);
|
|
|
|
|
if (error) throw new BoxError(BoxError.BAD_STATE, `Cannot update now: ${error.message}`);
|
2018-12-09 03:20:00 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const [getError, backupConfig] = await safe(settings.getBackupConfig());
|
|
|
|
|
if (getError) throw getError;
|
2019-02-25 10:03:46 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const memoryLimit = 'memoryLimit' in backupConfig ? Math.max(backupConfig.memoryLimit/1024/1024, 400) : 400;
|
2019-10-14 09:30:20 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const [taskError, taskId] = await safe(tasks.add(tasks.TASK_UPDATE, [ boxUpdateInfo, options ]));
|
|
|
|
|
if (taskError) throw taskError;
|
2021-02-01 14:07:23 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
eventlog.add(eventlog.ACTION_UPDATE, auditSource, { taskId, boxUpdateInfo });
|
2019-08-27 22:39:59 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
tasks.startTask(taskId, { timeout: 20 * 60 * 60 * 1000 /* 20 hours */, nice: 15, memoryLimit }, (error) => {
|
|
|
|
|
locker.unlock(locker.OP_BOX_UPDATE);
|
2021-02-01 14:07:23 -08:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
debug('Update failed with error', error);
|
2021-07-12 23:35:30 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
const timedOut = error.code === tasks.ETIMEOUT;
|
|
|
|
|
eventlog.add(eventlog.ACTION_UPDATE_FINISH, auditSource, { taskId, errorMessage: error.message, timedOut });
|
2018-12-09 03:20:00 -08:00
|
|
|
});
|
2021-08-20 09:19:44 -07:00
|
|
|
|
|
|
|
|
return taskId;
|
2018-07-31 11:35:23 -07:00
|
|
|
}
|