certbot.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 (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 (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. // SETUPTOOLS_USE_DISTUTILS is required for certbot plugins to install correctly
  58. // in new versions of Python
  59. let env = Object.assign({}, process.env, {SETUPTOOLS_USE_DISTUTILS: 'stdlib'});
  60. if (typeof plugin.env === 'object') {
  61. env = Object.assign(env, plugin.env);
  62. }
  63. const cmd = `. /opt/certbot/bin/activate && pip install --no-cache-dir ${plugin.dependencies} ${plugin.package_name}${plugin.version} && deactivate`;
  64. return utils.exec(cmd, {env})
  65. .then((result) => {
  66. logger.complete(`Installed ${pluginKey}`);
  67. return result;
  68. })
  69. .catch((err) => {
  70. throw err;
  71. });
  72. },
  73. };
  74. module.exports = certbot;