-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuration from file-based env variables
As an alternative to configuration via environment variable, this commit allows for configuration from files instead. This avoids the need to populate a container environment with secret values directly. The PATCH_ENVIRON_PATH can be pointed to a directory containing files named for the variables to set, containing the desired values for those variables. The README has been updated to document this feature.
- Loading branch information
Showing
6 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
!crawler/**/*.py | ||
|
||
!patch_environ/**/*.py | ||
|
||
!requirements | ||
|
||
!sample/sample.sqlite3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import os | ||
import os.path | ||
import re | ||
|
||
|
||
ENV_FILENAME_RE = re.compile(r"[A-Z_]+") | ||
|
||
|
||
def patch_environ(path, environ=os.environ): | ||
"""Patch the environment with file-based values. | ||
Given a path to a directory containing files named for environment | ||
variables, set those variables with the content of the files. | ||
Do not overwrite variables already set in the environment. | ||
""" | ||
if not path: | ||
return | ||
|
||
for filename in os.listdir(path): | ||
if ENV_FILENAME_RE.match(filename): | ||
with open(os.path.join(path, filename)) as f: | ||
environ.setdefault(filename, f.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os.path | ||
from tempfile import TemporaryDirectory | ||
from unittest import TestCase | ||
|
||
from patch_environ import patch_environ | ||
|
||
|
||
def make_file(filename, contents): | ||
with open(filename, "w") as f: | ||
f.write(contents) | ||
|
||
|
||
class PatchEnvironTests(TestCase): | ||
def test_pass_none_does_nothing(self): | ||
environ = { | ||
"FOO": "foo", | ||
"BAR": "bar", | ||
} | ||
|
||
patch_environ(None, environ) | ||
|
||
self.assertEqual(environ, { | ||
"FOO": "foo", | ||
"BAR": "bar", | ||
}) | ||
|
||
|
||
|
||
def test_patching(self): | ||
environ = { | ||
"FOO": "foo", | ||
"BAR": "bar", | ||
} | ||
|
||
with TemporaryDirectory() as tempdir: | ||
make_file(os.path.join(tempdir, "BAR"), "test") | ||
make_file(os.path.join(tempdir, "FOO_BAR"), "baz") | ||
make_file(os.path.join(tempdir, "ignore_lowercase"), "ignore") | ||
|
||
patch_environ(tempdir, environ) | ||
|
||
self.assertEqual(environ, { | ||
"FOO": "foo", | ||
"BAR": "bar", | ||
"FOO_BAR": "baz", | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters