certbot.js 2.3 KB

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