reverseproxy: create configs in subdirectories for easy management

This commit is contained in:
Girish Ramakrishnan
2022-11-17 12:02:39 +01:00
parent cd90864bc3
commit e3b0d3960a
2 changed files with 12 additions and 14 deletions

View File

@@ -525,7 +525,7 @@ async function writeAppNginxConfig(app, vhost, type, certificatePath) {
}
const nginxConf = ejs.render(NGINX_APPCONFIG_EJS, data);
const filename = path.join(paths.NGINX_APPCONFIG_DIR, `${app.id}-${vhost.replace('*', '_')}.conf`);
const filename = path.join(paths.NGINX_APPCONFIG_DIR, app.id, `${vhost.replace('*', '_')}.conf`);
debug(`writeAppNginxConfig: writing config for "${vhost}" to ${filename} with options ${JSON.stringify(data)}`);
if (!safe.fs.writeFileSync(filename, nginxConf)) {
debug(`Error creating nginx config for "${app.fqdn}" : ${safe.error.message}`);
@@ -538,6 +538,8 @@ async function writeAppConfigs(app) {
const locations = apps.getLocationsSync(app);
if (!safe.fs.mkdirSync(path.join(paths.NGINX_APPCONFIG_DIR, app.id), { recursive: true })) throw new BoxError(BoxError.FS_ERROR, `Could not create nginx config directory: ${safe.error.message}`);
for (const location of locations) {
const certificatePath = await getCertificatePath(location.fqdn, location.domain);
await writeAppNginxConfig(app, location.fqdn, location.type, certificatePath);
@@ -581,15 +583,7 @@ async function configureApp(app, auditSource) {
async function unconfigureApp(app) {
assert.strictEqual(typeof app, 'object');
const configFilenames = safe.fs.readdirSync(paths.NGINX_APPCONFIG_DIR);
if (!configFilenames) throw new BoxError(BoxError.FS_ERROR, `Error loading nginx config files: ${safe.error.message}`);
for (const filename of configFilenames) {
if (!filename.startsWith(app.id)) continue;
safe.fs.unlinkSync(path.join(paths.NGINX_APPCONFIG_DIR, filename));
}
if (!safe.fs.rmSync(path.join(paths.NGINX_APPCONFIG_DIR, app.id), { recursive: true, force: true })) throw new BoxError(BoxError.FS_ERROR, `Could not remove nginx config directory: ${safe.error.message}`);
await reload();
}
@@ -734,12 +728,15 @@ async function checkCerts(auditSource, progressCallback) {
function removeAppConfigs() {
const dashboardConfigFilename = `${settings.dashboardFqdn()}.conf`;
debug('removeAppConfigs: reomving nginx configs of apps');
debug('removeAppConfigs: removing app nginx configs');
// remove all configs which are not the default or current dashboard
for (const appConfigFile of fs.readdirSync(paths.NGINX_APPCONFIG_DIR)) {
if (appConfigFile !== constants.NGINX_DEFAULT_CONFIG_FILE_NAME && appConfigFile !== dashboardConfigFilename) {
fs.unlinkSync(path.join(paths.NGINX_APPCONFIG_DIR, appConfigFile));
for (const entry of fs.readdirSync(paths.NGINX_APPCONFIG_DIR, { withFileTypes: true })) {
const fullPath = path.join(paths.NGINX_APPCONFIG_DIR, entry.name);
if (entry.isDirectory()) {
fs.rmSync(fullPath, { recursive: true, force: true });
} else if (entry.isFile() && entry.name !== constants.NGINX_DEFAULT_CONFIG_FILE_NAME && entry.name !== dashboardConfigFilename) {
fs.unlinkSync(fullPath);
}
}
}