Skip to content

Commit 10d9760

Browse files
committed
Refactor certbot plugin install for setup
1 parent c722eb1 commit 10d9760

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

backend/lib/certbot.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,47 @@ const dnsPlugins = require('../global/certbot-dns-plugins.json');
22
const utils = require('./utils');
33
const error = require('./error');
44
const logger = require('../logger').certbot;
5+
const batchflow = require('batchflow');
56

6-
// const letsencryptStaging = config.useLetsencryptStaging();
7-
// const letsencryptConfig = '/etc/letsencrypt.ini';
8-
// const certbotCommand = 'certbot';
9-
10-
// const acmeVersion = '1.32.0';
117
const CERTBOT_VERSION_REPLACEMENT = '$(certbot --version | grep -Eo \'[0-9](\\.[0-9]+)+\')';
128

139
const certbot = {
1410

11+
/**
12+
* @param {array} pluginKeys
13+
*/
14+
installPlugins: async function (pluginKeys) {
15+
let hasErrors = false;
16+
17+
return new Promise((resolve, reject) => {
18+
if (pluginKeys.length === 0) {
19+
return;
20+
}
21+
22+
batchflow(pluginKeys).sequential()
23+
.each((i, pluginKey, next) => {
24+
certbot.installPlugin(pluginKey)
25+
.then(() => {
26+
next();
27+
})
28+
.catch((err) => {
29+
hasErrors = true;
30+
next(err);
31+
});
32+
})
33+
.error((err) => {
34+
logger.error(err.message);
35+
})
36+
.end(() => {
37+
if (hasErrors) {
38+
reject(new error.CommandError('Some plugins failed to install. Please check the logs above', 1));
39+
} else {
40+
resolve();
41+
}
42+
});
43+
});
44+
},
45+
1546
/**
1647
* Installs a cerbot plugin given the key for the object from
1748
* ../global/certbot-dns-plugins.json

backend/setup.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const utils = require('./lib/utils');
77
const authModel = require('./models/auth');
88
const settingModel = require('./models/setting');
99
const dns_plugins = require('./global/certbot-dns-plugins');
10-
10+
const certbot = require('./lib/certbot');
1111
/**
1212
* Creates a default admin users if one doesn't already exist in the database
1313
*
@@ -116,10 +116,7 @@ const setupCertbotPlugins = () => {
116116

117117
certificates.map(function (certificate) {
118118
if (certificate.meta && certificate.meta.dns_challenge === true) {
119-
const dns_plugin = dns_plugins[certificate.meta.dns_provider];
120-
121-
const packages_to_install = `${dns_plugin.package_name}${dns_plugin.version_requirement || ''} ${dns_plugin.dependencies}`;
122-
if (plugins.indexOf(packages_to_install) === -1) plugins.push(packages_to_install);
119+
plugins.push(certificate.meta.dns_provider);
123120

124121
// Make sure credentials file exists
125122
const credentials_loc = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
@@ -130,17 +127,15 @@ const setupCertbotPlugins = () => {
130127
}
131128
});
132129

133-
if (plugins.length) {
134-
const install_cmd = '. /opt/certbot/bin/activate && pip install --no-cache-dir ' + plugins.join(' ') + ' && deactivate';
135-
promises.push(utils.exec(install_cmd));
136-
}
137-
138-
if (promises.length) {
139-
return Promise.all(promises)
140-
.then(() => {
141-
logger.info('Added Certbot plugins ' + plugins.join(', '));
142-
});
143-
}
130+
return certbot.installPlugins(plugins)
131+
.then(() => {
132+
if (promises.length) {
133+
return Promise.all(promises)
134+
.then(() => {
135+
logger.info('Added Certbot plugins ' + plugins.join(', '));
136+
});
137+
}
138+
});
144139
}
145140
});
146141
};

0 commit comments

Comments
 (0)