Files

91 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2025-09-02 21:43:00 +10:00
import batchflow from "batchflow";
2025-10-26 00:28:03 +10:00
import dnsPlugins from "../certbot/dns-plugins.json" with { type: "json" };
2025-09-02 21:43:00 +10:00
import { certbot as logger } from "../logger.js";
import errs from "./error.js";
import utils from "./utils.js";
2024-01-18 12:26:55 +10:00
2025-09-02 21:43:00 +10:00
/**
* Installs a cerbot plugin given the key for the object from
2025-10-26 00:28:03 +10:00
* ../certbot/dns-plugins.json
2025-09-02 21:43:00 +10:00
*
* @param {string} pluginKey
* @returns {Object}
*/
const installPlugin = async (pluginKey) => {
if (typeof dnsPlugins[pluginKey] === "undefined") {
throw new errs.ItemNotFoundError(pluginKey);
}
2024-01-18 12:26:55 +10:00
2025-09-02 21:43:00 +10:00
const plugin = dnsPlugins[pluginKey];
logger.start(`Installing ${pluginKey}...`);
2024-01-18 12:26:55 +10:00
2026-05-20 08:16:10 +10:00
plugin.version = plugin.version.replace(/{{certbot-version}}/g, process.env.CERTBOT_VERSION);
plugin.dependencies = plugin.dependencies.replace(/{{certbot-version}}/g, process.env.CERTBOT_VERSION);
2024-01-18 12:26:55 +10:00
2026-05-20 08:16:10 +10:00
// SETUPTOOLS_USE_DISTUTILS=local uses setuptools' own bundled distutils.
// "stdlib" breaks Python 3.13+ where distutils was removed from the standard library.
let env = Object.assign({}, process.env, { SETUPTOOLS_USE_DISTUTILS: "local" });
2025-09-02 21:43:00 +10:00
if (typeof plugin.env === "object") {
env = Object.assign(env, plugin.env);
}
2026-05-20 08:16:10 +10:00
const quotedDeps = plugin.dependencies.trim()
? plugin.dependencies
.trim()
.split(/\s+/)
.filter(Boolean)
.map((d) => `'${d}'`)
.join(" ")
: "";
2026-05-25 12:40:10 +10:00
2026-05-20 08:16:10 +10:00
const cmd = `. /opt/certbot/bin/activate && pip install --no-cache-dir ${quotedDeps} '${plugin.package_name}${plugin.version}' && deactivate`;
2025-09-02 21:43:00 +10:00
return utils
.exec(cmd, { env })
.then((result) => {
logger.complete(`Installed ${pluginKey}`);
return result;
})
.catch((err) => {
throw err;
});
2024-01-18 12:26:55 +10:00
};
/**
* @param {array} pluginKeys
*/
const installPlugins = async (pluginKeys) => {
let hasErrors = false;
return new Promise((resolve, reject) => {
if (pluginKeys.length === 0) {
resolve();
return;
}
batchflow(pluginKeys)
.sequential()
.each((_i, pluginKey, next) => {
installPlugin(pluginKey)
.then(() => {
next();
})
.catch((err) => {
hasErrors = true;
next(err);
});
})
.error((err) => {
logger.error(err.message);
})
.end(() => {
if (hasErrors) {
2026-05-20 08:16:10 +10:00
reject(new errs.CommandError("Some plugins failed to install. Please check the logs above", 1));
} else {
resolve();
}
});
});
};
2026-05-20 08:16:10 +10:00
export { installPlugin, installPlugins };