add config element to load environment variables from a file

pull/879/head
Sebastian Bäumlisberger 2021-10-30 16:50:53 +02:00
parent 9bfc034807
commit 74438ee10c
6 changed files with 79 additions and 0 deletions

View File

@ -139,6 +139,14 @@ This optional element can be specified multiple times if necessary to specify en
<env name="HOME" value="c:\abc" /> <env name="HOME" value="c:\abc" />
``` ```
### Environment File
This optional element can be specified one time to specify a file to load environment variables from. Each variable definition must be on a separate line and in the format "key=value". Empty lines an lines starting with "#" are ignored. The syntax is:
```xml
<envFile>%BASE%/env.txt</env-file>
```
### interactive ### interactive
If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes. If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes.

View File

@ -171,6 +171,14 @@ env:
value: host1;host2 value: host1;host2
``` ```
### Environment File
This optional element can be specified one time to specify a file to load environment variables from. Each variable definition must be on a separate line and in the format "key=value". Empty lines an lines starting with "#" are ignored. The syntax is:
```yaml
envFile: '%BASE%/env.txt'
```
### interactive ### interactive
If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes. If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes.

View File

@ -129,6 +129,8 @@ namespace WinSW.Configuration
public Dictionary<string, string> EnvironmentVariables => new(0); public Dictionary<string, string> EnvironmentVariables => new(0);
public string? EnvironmentVariablesFile => null;
// Misc // Misc
public bool BeepOnShutdown => false; public bool BeepOnShutdown => false;

View File

@ -65,6 +65,8 @@ namespace WinSW
// Also inject system environment variables // Also inject system environment variables
Environment.SetEnvironmentVariable(WinSWSystem.EnvVarNameServiceId, this.Name); Environment.SetEnvironmentVariable(WinSWSystem.EnvVarNameServiceId, this.Name);
this.LoadEnvironmentVariablesFromFile();
this.environmentVariables = this.LoadEnvironmentVariables(); this.environmentVariables = this.LoadEnvironmentVariables();
} }
@ -528,6 +530,11 @@ namespace WinSW
/// </summary> /// </summary>
public Dictionary<string, string> EnvironmentVariables => new(this.environmentVariables); public Dictionary<string, string> EnvironmentVariables => new(this.environmentVariables);
/// <summary>
/// File from which environment variables are loaded.
/// </summary>
public string? EnvironmentVariablesFile => this.SingleElement("envFile", optional: true);
/// <summary> /// <summary>
/// List of downloads to be performed by the wrapper before starting /// List of downloads to be performed by the wrapper before starting
/// a service. /// a service.
@ -690,6 +697,18 @@ namespace WinSW
return environment; return environment;
} }
private void LoadEnvironmentVariablesFromFile()
{
var envFile = this.SingleElement("envFile", optional: true);
if (envFile is null)
{
return;
}
ConfigHelper.LoadEnvironmentVariablesFile(envFile);
}
public List<YamlExtensionConfig>? YamlExtensions => Defaults.YamlExtensions; public List<YamlExtensionConfig>? YamlExtensions => Defaults.YamlExtensions;
} }
} }

View File

@ -43,6 +43,8 @@ namespace WinSW.Configuration
// Also inject system environment variables // Also inject system environment variables
Environment.SetEnvironmentVariable(WinSWSystem.EnvVarNameServiceId, this.Name); Environment.SetEnvironmentVariable(WinSWSystem.EnvVarNameServiceId, this.Name);
this.LoadEnvironmentVariablesFromFile();
this.LoadEnvironmentVariables(); this.LoadEnvironmentVariables();
} }
@ -201,6 +203,8 @@ namespace WinSW.Configuration
public Dictionary<string, string> EnvironmentVariables { get; set; } = new Dictionary<string, string>(); public Dictionary<string, string> EnvironmentVariables { get; set; } = new Dictionary<string, string>();
public string? EnvironmentVariablesFile => Expand(this.raw.EnvFile) ?? this.defaults.EnvironmentVariablesFile;
public void LoadEnvironmentVariables() public void LoadEnvironmentVariables()
{ {
if (this.raw.Env is null) if (this.raw.Env is null)
@ -224,6 +228,18 @@ namespace WinSW.Configuration
} }
} }
private void LoadEnvironmentVariablesFromFile()
{
string? envFile = Expand(this.raw.EnvFile);
if (envFile is null)
{
return;
}
ConfigHelper.LoadEnvironmentVariablesFile(envFile);
}
public ServiceAccount ServiceAccount public ServiceAccount ServiceAccount
{ {
get get
@ -315,6 +331,7 @@ namespace WinSW.Configuration
public string? Priority; public string? Priority;
public string? BeepOnShutdown; public string? BeepOnShutdown;
public List<RawYamlEnv>? Env; public List<RawYamlEnv>? Env;
public string? EnvFile;
public List<RawYamlFailureAction>? OnFailure; public List<RawYamlFailureAction>? OnFailure;
public string? DelayedAutoStart; public string? DelayedAutoStart;
public string? SecurityDescriptor; public string? SecurityDescriptor;

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
namespace WinSW.Util namespace WinSW.Util
{ {
@ -45,5 +46,29 @@ namespace WinSW.Util
return false; return false;
} }
public static void LoadEnvironmentVariablesFile(string envFile)
{
foreach (string line in File.ReadAllLines(envFile))
{
if (line.Length == 0 || line.StartsWith("#"))
{
// ignore empty lines and comments
continue;
}
int equalsSignIndex = line.IndexOf("=");
if (equalsSignIndex == -1)
{
throw new WinSWException("The environment variables file (env-file) contains one or more invalid entries. Each variable definition must be on a separate line and in the format \"key=value\".");
}
string key = line.Substring(0, equalsSignIndex);
string value = line.Substring(equalsSignIndex + 1);
Environment.SetEnvironmentVariable(key, value);
}
}
} }
} }