Merge pull request #1043 from jameswynn/variable-substitution

Added support for environment variable substitution
pull/1078/head
Jason Fischer 2 years ago committed by GitHub
commit 3db5435c19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,
@ -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 [];

@ -2,8 +2,13 @@
import { join } from "path";
import { existsSync, copyFile, readFileSync } from "fs";
import cache from "memory-cache";
import yaml from "js-yaml";
const cacheKey = "homepageEnvironmentVariables";
const homepageVarPrefix = "HOMEPAGE_VAR_";
const homepageFilePrefix = "HOMEPAGE_FILE_";
export default function checkAndCopyConfig(config) {
const configYaml = join(process.cwd(), "config", config);
if (!existsSync(configYaml)) {
@ -27,10 +32,38 @@ export default function checkAndCopyConfig(config) {
}
}
function getCachedEnvironmentVars() {
let cachedVars = cache.get(cacheKey);
if (!cachedVars) {
// initialize cache
cachedVars = Object.entries(process.env).filter(([key, ]) => key.includes(homepageVarPrefix) || key.includes(homepageFilePrefix));
cache.put(cacheKey, cachedVars);
}
return cachedVars;
}
export function substituteEnvironmentVars(str) {
let result = str;
if (result.includes('{{')) { // crude check if we have vars to replace
const cachedVars = getCachedEnvironmentVars();
cachedVars.forEach(([key, value]) => {
if (key.startsWith(homepageVarPrefix)) {
result = result.replaceAll(`{{${key}}}`, value);
} else if (key.startsWith(homepageFilePrefix)) {
const filename = value;
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) ?? {};
}
}

@ -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) {

@ -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();

@ -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) {
@ -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) {

@ -3,7 +3,7 @@ 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"];
@ -11,7 +11,8 @@ 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 [];

Loading…
Cancel
Save