mirror of
				https://github.com/karl0ss/homepage.git
				synced 2025-11-04 08:20:58 +00:00 
			
		
		
		
	Added support for environment variable substitution
* 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
			
			
This commit is contained in:
		
							parent
							
								
									b65f6fca19
								
							
						
					
					
						commit
						e0f1aae4d5
					
				@ -4,7 +4,7 @@ import path from "path";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import yaml from "js-yaml";
 | 
					import yaml from "js-yaml";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import checkAndCopyConfig, { getSettings } from "utils/config/config";
 | 
					import checkAndCopyConfig, { getSettings, substituteEnvironmentVars } from "utils/config/config";
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  servicesFromConfig,
 | 
					  servicesFromConfig,
 | 
				
			||||||
  servicesFromDocker,
 | 
					  servicesFromDocker,
 | 
				
			||||||
@ -28,7 +28,8 @@ export async function bookmarksResponse() {
 | 
				
			|||||||
  checkAndCopyConfig("bookmarks.yaml");
 | 
					  checkAndCopyConfig("bookmarks.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const bookmarksYaml = path.join(process.cwd(), "config", "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);
 | 
					  const bookmarks = yaml.load(fileContents);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!bookmarks) return [];
 | 
					  if (!bookmarks) return [];
 | 
				
			||||||
 | 
				
			|||||||
@ -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() {
 | 
					export function getSettings() {
 | 
				
			||||||
  checkAndCopyConfig("settings.yaml");
 | 
					  checkAndCopyConfig("settings.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const settingsYaml = join(process.cwd(), "config", "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) ?? {};
 | 
					  return yaml.load(fileContents) ?? {};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -3,13 +3,14 @@ import { readFileSync } from "fs";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import yaml from "js-yaml";
 | 
					import yaml from "js-yaml";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import checkAndCopyConfig from "utils/config/config";
 | 
					import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default function getDockerArguments(server) {
 | 
					export default function getDockerArguments(server) {
 | 
				
			||||||
  checkAndCopyConfig("docker.yaml");
 | 
					  checkAndCopyConfig("docker.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const configFile = path.join(process.cwd(), "config", "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);
 | 
					  const servers = yaml.load(configData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!server) {
 | 
					  if (!server) {
 | 
				
			||||||
 | 
				
			|||||||
@ -4,13 +4,14 @@ import { readFileSync } from "fs";
 | 
				
			|||||||
import yaml from "js-yaml";
 | 
					import yaml from "js-yaml";
 | 
				
			||||||
import { KubeConfig } from "@kubernetes/client-node";
 | 
					import { KubeConfig } from "@kubernetes/client-node";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import checkAndCopyConfig from "utils/config/config";
 | 
					import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default function getKubeConfig() {
 | 
					export default function getKubeConfig() {
 | 
				
			||||||
  checkAndCopyConfig("kubernetes.yaml");
 | 
					  checkAndCopyConfig("kubernetes.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const configFile = path.join(process.cwd(), "config", "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 config = yaml.load(configData);
 | 
				
			||||||
  const kc = new KubeConfig();
 | 
					  const kc = new KubeConfig();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -7,17 +7,19 @@ import * as shvl from "shvl";
 | 
				
			|||||||
import { NetworkingV1Api } from "@kubernetes/client-node";
 | 
					import { NetworkingV1Api } from "@kubernetes/client-node";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import createLogger from "utils/logger";
 | 
					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 getDockerArguments from "utils/config/docker";
 | 
				
			||||||
import getKubeConfig from "utils/config/kubernetes";
 | 
					import getKubeConfig from "utils/config/kubernetes";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const logger = createLogger("service-helpers");
 | 
					const logger = createLogger("service-helpers");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function servicesFromConfig() {
 | 
					export async function servicesFromConfig() {
 | 
				
			||||||
  checkAndCopyConfig("services.yaml");
 | 
					  checkAndCopyConfig("services.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const servicesYaml = path.join(process.cwd(), "config", "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);
 | 
					  const services = yaml.load(fileContents);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!services) {
 | 
					  if (!services) {
 | 
				
			||||||
@ -49,7 +51,8 @@ export async function servicesFromDocker() {
 | 
				
			|||||||
  checkAndCopyConfig("docker.yaml");
 | 
					  checkAndCopyConfig("docker.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const dockerYaml = path.join(process.cwd(), "config", "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);
 | 
					  const servers = yaml.load(dockerFileContents);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!servers) {
 | 
					  if (!servers) {
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@ import path from "path";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import yaml from "js-yaml";
 | 
					import yaml from "js-yaml";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import checkAndCopyConfig from "utils/config/config";
 | 
					import checkAndCopyConfig, { substituteEnvironmentVars } from "utils/config/config";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const exemptWidgets = ["search"];
 | 
					const exemptWidgets = ["search"];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -11,7 +11,8 @@ export async function widgetsFromConfig() {
 | 
				
			|||||||
    checkAndCopyConfig("widgets.yaml");
 | 
					    checkAndCopyConfig("widgets.yaml");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const widgetsYaml = path.join(process.cwd(), "config", "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);
 | 
					    const widgets = yaml.load(fileContents);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!widgets) return [];
 | 
					    if (!widgets) return [];
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user