certbot.js 2.1 KB

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