fixup various shell usage

This commit is contained in:
Girish Ramakrishnan
2024-02-28 18:47:53 +01:00
parent 07527fe2b1
commit 3316dd1f42
4 changed files with 47 additions and 21 deletions
+9 -4
View File
@@ -31,6 +31,7 @@ const assert = require('assert'),
docker = require('./docker.js'),
domains = require('./domains.js'),
eventlog = require('./eventlog.js'),
fs = require('fs'),
hat = require('./hat.js'),
infra = require('./infra_version.js'),
Location = require('./location.js'),
@@ -153,7 +154,9 @@ async function configureMail(mailFqdn, mailDomain, serviceConfig) {
const mailCertFilePath = `${paths.MAIL_CONFIG_DIR}/tls_cert.pem`;
const mailKeyFilePath = `${paths.MAIL_CONFIG_DIR}/tls_key.pem`;
const [copyError] = await safe(shell.exec('configureMail', `cp ${paths.DHPARAMS_FILE} ${dhparamsFilePath}`, {}));
const [readError, dhparams] = await safe(fs.promises.readFile(paths.DHPARAMS_FILE));
if (readError) throw new BoxError(BoxError.FS_ERROR, `Could not read dhparams: ${readError.message}`);
const [copyError] = await safe(fs.promises.writeFile(dhparamsFilePath, dhparams));
if (copyError) throw new BoxError(BoxError.FS_ERROR, `Could not copy dhparams: ${copyError.message}`);
if (!safe.fs.writeFileSync(mailCertFilePath, certificate.cert)) throw new BoxError(BoxError.FS_ERROR, `Could not create cert file: ${safe.error.message}`);
if (!safe.fs.writeFileSync(mailKeyFilePath, certificate.key)) throw new BoxError(BoxError.FS_ERROR, `Could not create key file: ${safe.error.message}`);
@@ -161,8 +164,9 @@ async function configureMail(mailFqdn, mailDomain, serviceConfig) {
// if the 'yellowtent' user of OS and the 'cloudron' user of mail container don't match, the keys become inaccessible by mail code
if (!safe.fs.chmodSync(mailKeyFilePath, 0o644)) throw new BoxError(BoxError.FS_ERROR, `Could not chmod key file: ${safe.error.message}`);
await safe(shell.exec('stopMail', 'docker stop mail', {})); // ignore error
await safe(shell.exec('removeMail', 'docker rm -f mail', {})); // ignore error
debug('configureMail: stopping and deleting previous mail container');
await docker.stopContainer('mail');
await docker.deleteContainer('mail');
const allowInbound = await createMailConfig(mailFqdn);
@@ -191,7 +195,8 @@ async function configureMail(mailFqdn, mailDomain, serviceConfig) {
--label isCloudronManaged=true \
${readOnly} -v /run -v /tmp ${image} ${cmd}`;
await shell.exec('startMail', runCmd, { shell: '/bin/bash' });
debug('configureMail: starting mail container');
await shell.exec('configureMail', runCmd, { shell: '/bin/bash' });
}
async function restart() {
+1 -1
View File
@@ -49,7 +49,7 @@ async function pruneInfraImages() {
// cannot blindly remove all unused images since redis image may not be used
const imageNames = Object.keys(infra.images).map(addon => infra.images[addon]);
const [error, output] = await safe(shell.exec('pruneInfraImages', 'docker images --digests --format "{{.ID}} {{.Repository}} {{.Tag}} {{.Digest}}"', {}));
const [error, output] = await safe(shell.exec('pruneInfraImages', 'docker images --digests --format "{{.ID}} {{.Repository}} {{.Tag}} {{.Digest}}"', { shell: '/bin/bash' }));
if (error) {
debug(`Failed to list images ${error.message}`);
throw error;
+31 -12
View File
@@ -952,8 +952,11 @@ async function startTurn(existingInfra) {
--label isCloudronManaged=true \
${readOnly} -v /tmp -v /run ${image} ${cmd}`;
await safe(shell.exec('stopTurn', 'docker stop turn', {})); // ignore error
await safe(shell.exec('removeTurn', 'docker rm -f turn', {})); // ignore error
debug('startTurn: stopping and deleting previous turn container');
await docker.stopContainer('turn');
await docker.deleteContainer('turn');
debug('startTurn: starting turn container');
await shell.exec('startTurn', runCmd, { shell: '/bin/bash' });
}
@@ -1159,8 +1162,11 @@ async function startMysql(existingInfra) {
--cap-add SYS_NICE \
${readOnly} -v /tmp -v /run ${image} ${cmd}`;
await safe(shell.exec('stopMysql', 'docker stop mysql', {})); // ignore error
await safe(shell.exec('removeMysql', 'docker rm -f mysql', {})); // ignore error
debug('startMysql: stopping and deleting previous mysql container');
await docker.stopContainer('mysql');
await docker.deleteContainer('mysql');
debug('startMysql: starting mysql container');
await shell.exec('startMysql', runCmd, { shell: '/bin/bash' });
if (!serviceConfig.recoveryMode) {
@@ -1378,8 +1384,11 @@ async function startPostgresql(existingInfra) {
--label isCloudronManaged=true \
${readOnly} -v /tmp -v /run ${image} ${cmd}`;
await safe(shell.exec('stopPostgresql', 'docker stop postgresql', {})); // ignore error
await safe(shell.exec('removePostgresql', 'docker rm -f postgresql', {})); // ignore error
debug('startPostgresqk: stopping and deleting previous postgresql container');
await docker.stopContainer('postgresql');
await docker.deleteContainer('postgresql');
debug('startPostgresql: starting postgresql container');
await shell.exec('startPostgresql', runCmd, { shell: '/bin/bash' });
if (!serviceConfig.recoveryMode) {
@@ -1521,8 +1530,11 @@ async function startMongodb(existingInfra) {
--label isCloudronManaged=true \
${readOnly} -v /tmp -v /run ${image} ${cmd}`;
await safe(shell.exec('stopMongodb', 'docker stop mongodb', {})); // ignore error
await safe(shell.exec('removeMongodb', 'docker rm -f mongodb', {})); // ignore error
debug('startMongodb: stopping and deleting previous mongodb container');
await docker.stopContainer('mongodb');
await docker.deleteContainer('mongodb');
debug('startMongodb: starting mongodb container');
await shell.exec('startMongodb', runCmd, { shell: '/bin/bash' });
if (!serviceConfig.recoveryMode) {
@@ -1669,9 +1681,13 @@ async function startGraphite(existingInfra) {
--label isCloudronManaged=true \
${readOnly} -v /tmp -v /run ${image} ${cmd}`;
await safe(shell.exec('stopGraphite', 'docker stop graphite', {})); // ignore error
await safe(shell.exec('removeGraphite', 'docker rm -f graphite', {})); // ignore error
debug('startGraphite: stopping and deleting previous graphite container');
await docker.stopContainer('graphite');
await docker.deleteContainer('graphite');
if (upgrading) await shell.promises.sudo('removeGraphiteDir', [ RMADDONDIR_CMD, 'graphite' ], {});
debug('startGraphite: starting graphite container');
await shell.exec('startGraphite', runCmd, { shell: '/bin/bash' });
// restart collectd to get the disk stats after graphite starts. currently, there is no way to do graphite health check
@@ -1714,8 +1730,11 @@ async function startRedis(existingInfra) {
if (upgrading) await backupRedis(app, {});
await safe(shell.exec('stopRedis', `docker stop ${redisName}`, {})); // redis will backup as part of signal handling
await safe(shell.exec('removeRedis', `docker rm -f ${redisName}`, {})); // ignore error
debug(`startRedis: stopping and deleting previous redis container ${redisName}`);
await docker.stopContainer(redisName);
await docker.deleteContainer(redisName);
debug(`startRedis: starting redis container ${redisName}`);
await setupRedis(app, app.manifest.addons.redis); // starts the container
}
+6 -4
View File
@@ -34,7 +34,7 @@ async function ensureKeys() {
debug(`ensureSecrets: generating new sftp keys of type ${keyType}`);
safe.fs.unlinkSync(publicKeyFile);
safe.fs.unlinkSync(privateKeyFile);
const [error] = await safe(shell.exec('ensureKeys', `ssh-keygen -m PEM -t ${keyType} -f "${paths.SFTP_KEYS_DIR}/ssh_host_${keyType}_key" -q -N ""`, {}));
const [error] = await safe(shell.exec('ensureKeys', `ssh-keygen -m PEM -t ${keyType} -f ${paths.SFTP_KEYS_DIR}/ssh_host_${keyType}_key -q -N ""`, { shell: '/bin/bash' }));
if (error) throw new BoxError(BoxError.OPENSSL_ERROR, `Could not generate sftp ${keyType} keys: ${error.message}`);
const newPublicKey = safe.fs.readFileSync(publicKeyFile);
await blobs.set(`sftp_${keyType}_public_key`, newPublicKey);
@@ -122,9 +122,11 @@ async function start(existingInfra) {
--label isCloudronManaged=true \
${readOnly} -v /tmp -v /run ${image} ${cmd}`;
// ignore error if container not found (and fail later) so that this code works across restarts
await safe(shell.exec('stopSftp', 'docker stop sftp', {})); // ignore error
await safe(shell.exec('removeSftp', 'docker rm -f sftp', {})); // ignore error
debug('startSftp: stopping and deleting previous sftp container');
await docker.stopContainer('sftp');
await docker.deleteContainer('sftp');
debug('startSftp: starting sftp container');
await shell.exec('startSftp', runCmd, { shell: '/bin/bash' });
}