certbot.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const dnsPlugins = require('../global/certbot-dns-plugins.json');
  2. const utils = require('./utils');
  3. const error = require('./error');
  4. const logger = require('../logger').certbot;
  5. const batchflow = require('batchflow');
  6. const CERTBOT_VERSION_REPLACEMENT = '$(certbot --version | grep -Eo \'[0-9](\\.[0-9]+)+\')';
  7. const certbot = {
  8. /**
  9. * @param {array} pluginKeys
  10. */
  11. installPlugins: async function (pluginKeys) {
  12. let hasErrors = false;
  13. return new Promise((resolve, reject) => {
  14. if (pluginKeys.length === 0) {
  15. return;
  16. }
  17. batchflow(pluginKeys).sequential()
  18. .each((i, pluginKey, next) => {
  19. certbot.installPlugin(pluginKey)
  20. .then(() => {
  21. next();
  22. })
  23. .catch((err) => {
  24. hasErrors = true;
  25. next(err);
  26. });
  27. })
  28. .error((err) => {
  29. logger.error(err.message);
  30. })
  31. .end(() => {
  32. if (hasErrors) {
  33. reject(new error.CommandError('Some plugins failed to install. Please check the logs above', 1));
  34. } else {
  35. resolve();
  36. }
  37. });
  38. });
  39. },
  40. /**
  41. * Installs a cerbot plugin given the key for the object from
  42. * ../global/certbot-dns-plugins.json
  43. *
  44. * @param {string} pluginKey
  45. * @returns {Object}
  46. */
  47. installPlugin: async function (pluginKey) {
  48. if (typeof dnsPlugins[pluginKey] === 'undefined') {
  49. // throw Error(`Certbot plugin ${pluginKey} not found`);
  50. throw new error.ItemNotFoundError(pluginKey);
  51. }
  52. const plugin = dnsPlugins[pluginKey];
  53. logger.start(`Installing ${pluginKey}...`);
  54. plugin.version = plugin.version.replace(/{{certbot-version}}/g, CERTBOT_VERSION_REPLACEMENT);
  55. plugin.dependencies = plugin.dependencies.replace(/{{certbot-version}}/g, CERTBOT_VERSION_REPLACEMENT);
  56. const cmd = '. /opt/certbot/bin/activate && pip install --no-cache-dir ' + plugin.dependencies + ' ' + plugin.package_name + plugin.version + ' ' + ' && deactivate';
  57. return utils.exec(cmd)
  58. .then((result) => {
  59. logger.complete(`Installed ${pluginKey}`);
  60. return result;
  61. })
  62. .catch((err) => {
  63. throw err;
  64. });
  65. },
  66. };
  67. module.exports = certbot;