Skip to content

Commit

Permalink
Added support for environment variable substitution
Browse files Browse the repository at this point in the history
* Only environment variables starting with HOMEPAGE_VAR_ and HOMEPAGE_FILE_
  are supported
* The value of env var HOMEPAGE_VAR_XXX will replace {{HOMEPAGE_VAR_XXX}}
  in any config
* The value of env var HOMEPAGE_FILE_XXX must be a file path, the contents
  of which will be used to replace {{HOMEPAGE_FILE_XXX}} in any config
* If a substituted value contains a variable reference it may also be
  replaced, but the behavior is non-deterministic
  • Loading branch information
jameswynn committed Feb 23, 2023
1 parent b65f6fc commit e0f1aae
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/utils/config/api-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";

import yaml from "js-yaml";

import checkAndCopyConfig, { getSettings } from "utils/config/config";
import checkAndCopyConfig, { getSettings, substituteEnvironmentVars } from "utils/config/config";
import {
servicesFromConfig,
servicesFromDocker,
Expand All @@ -28,7 +28,8 @@ export async function bookmarksResponse() {
checkAndCopyConfig("bookmarks.yaml");

const bookmarksYaml = path.join(process.cwd(), "config", "bookmarks.yaml");
const fileContents = await fs.readFile(bookmarksYaml, "utf8");
const rawFileContents = await fs.readFile(bookmarksYaml, "utf8");
const fileContents = substituteEnvironmentVars(rawFileContents);
const bookmarks = yaml.load(fileContents);

if (!bookmarks) return [];
Expand Down
22 changes: 20 additions & 2 deletions src/utils/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,28 @@ export default function checkAndCopyConfig(config) {
}
}

export function substituteEnvironmentVars(str) {
const homepageVarPrefix = "HOMEPAGE_VAR_";
const homepageFilePrefix = "HOMEPAGE_FILE_";

let result = str;
Object.keys(process.env).forEach(key => {
if (key.startsWith(homepageVarPrefix)) {
result = result.replaceAll(`{{${key}}}`, process.env[key]);
} else if (key.startsWith(homepageFilePrefix)) {
const filename = process.env[key];
const fileContents = readFileSync(filename, "utf8");
result = result.replaceAll(`{{${key}}}`, fileContents);
}
});
return result;
}

export function getSettings() {
checkAndCopyConfig("settings.yaml");

const settingsYaml = join(process.cwd(), "config", "settings.yaml");
const fileContents = readFileSync(settingsYaml, "utf8");
const rawFileContents = readFileSync(settingsYaml, "utf8");
const fileContents = substituteEnvironmentVars(rawFileContents);
return yaml.load(fileContents) ?? {};
}
}
5 changes: 3 additions & 2 deletions src/utils/config/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { readFileSync } from "fs";

import yaml from "js-yaml";

import checkAndCopyConfig from "utils/config/config";
import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";

export default function getDockerArguments(server) {
checkAndCopyConfig("docker.yaml");

const configFile = path.join(process.cwd(), "config", "docker.yaml");
const configData = readFileSync(configFile, "utf8");
const rawConfigData = readFileSync(configFile, "utf8");
const configData = substituteEnvironmentVars(rawConfigData);
const servers = yaml.load(configData);

if (!server) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/config/kubernetes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { readFileSync } from "fs";
import yaml from "js-yaml";
import { KubeConfig } from "@kubernetes/client-node";

import checkAndCopyConfig from "utils/config/config";
import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";

export default function getKubeConfig() {
checkAndCopyConfig("kubernetes.yaml");

const configFile = path.join(process.cwd(), "config", "kubernetes.yaml");
const configData = readFileSync(configFile, "utf8");
const rawConfigData = readFileSync(configFile, "utf8");
const configData = substituteEnvironmentVars(rawConfigData);
const config = yaml.load(configData);
const kc = new KubeConfig();

Expand Down
9 changes: 6 additions & 3 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import * as shvl from "shvl";
import { NetworkingV1Api } from "@kubernetes/client-node";

import createLogger from "utils/logger";
import checkAndCopyConfig from "utils/config/config";
import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";
import getDockerArguments from "utils/config/docker";
import getKubeConfig from "utils/config/kubernetes";

const logger = createLogger("service-helpers");


export async function servicesFromConfig() {
checkAndCopyConfig("services.yaml");

const servicesYaml = path.join(process.cwd(), "config", "services.yaml");
const fileContents = await fs.readFile(servicesYaml, "utf8");
const rawFileContents = await fs.readFile(servicesYaml, "utf8");
const fileContents = substituteEnvironmentVars(rawFileContents);
const services = yaml.load(fileContents);

if (!services) {
Expand Down Expand Up @@ -49,7 +51,8 @@ export async function servicesFromDocker() {
checkAndCopyConfig("docker.yaml");

const dockerYaml = path.join(process.cwd(), "config", "docker.yaml");
const dockerFileContents = await fs.readFile(dockerYaml, "utf8");
const rawDockerFileContents = await fs.readFile(dockerYaml, "utf8");
const dockerFileContents = substituteEnvironmentVars(rawDockerFileContents);
const servers = yaml.load(dockerFileContents);

if (!servers) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/config/widget-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import path from "path";

import yaml from "js-yaml";

import checkAndCopyConfig from "utils/config/config";
import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";

const exemptWidgets = ["search"];

export async function widgetsFromConfig() {
checkAndCopyConfig("widgets.yaml");

const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const rawFileContents = await fs.readFile(widgetsYaml, "utf8");
const fileContents = substituteEnvironmentVars(rawFileContents);
const widgets = yaml.load(fileContents);

if (!widgets) return [];
Expand Down

0 comments on commit e0f1aae

Please sign in to comment.