You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
allow setting configuration using env variables (#600)
## Overview
Previously configuration options must be set in the local environment
file (`birdhouse/env.local` by default).
This change allows configuration options to be set as environment
variables which would take precedence over those
set in the local environment file.
For example, you can now set the `BIRDHOUSE_FQDN` variable when starting
the stack like so:
```sh
BIRDHOUSE_FQDN=myhost.example.com bin/birdhouse compose up -d
# OR
export BIRDHOUSE_FQDN=myhost.example.com
bin/birdhouse compose up -d
```
This change has the following advantages:
- flexibility: the user has more options for how they can customize
their deployment
- good dev-ops: this change further aligns birdhouse with the [12 factor
app principles](https://12factor.net/),
specifically the [Config](https://12factor.net/config) principle which
recommends that configuration
options be settable as environment variables.
- security: sensitive settings (credentials, secrets) can be set as
environment variables, ensuring that they are
not easily visible in the plain text local environment file.
- consistency: users can store non-sensitive settings in the local
environment file and share that file freely without
worrying that sensitive setting will be leaked.
Note that this includes some necessary changes to the tests that are
unrelated to this change directly. The tests explicitly set
`BIRDHOUSE_BACKWARD_COMPATIBLE_ALLOWED=False` as an environment variable
in order to get around the fact that it is set to True by default when
running scripts not through a supported interface. For tests that want
to explicitly set `BIRDHOUSE_BACKWARD_COMPATIBLE_ALLOWED=True` we now
have to set that as an environment variable (not in env.local) since
environment variables now take precedence over settings in env.local.
## Changes
**Non-breaking changes**
- Adds new configuration strategy
- test refactoring
**Breaking changes**
- None
## Related Issue / Discussion
## Additional Information
## CI Operations
<!--
The test suite can be run using a different DACCS config with
``birdhouse_daccs_configs_branch: branch_name`` in the PR description.
To globally skip the test suite regardless of the commit message use
``birdhouse_skip_ci`` set to ``true`` in the PR description.
Using ``[<cmd>]`` (with the brackets) where ``<cmd> = skip ci`` in the
commit message will override ``birdhouse_skip_ci`` from the PR
description.
Such commit command can be used to override the PR description behavior
for a specific commit update.
However, a commit message cannot 'force run' a PR which the description
turns off the CI.
To run the CI, the PR should instead be updated with a ``true`` value,
and a running message can be posted in following PR comments to trigger
tests once again.
-->
birdhouse_daccs_configs_branch: master
birdhouse_skip_ci: false
Copy file name to clipboardExpand all lines: birdhouse/README.rst
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,6 +106,9 @@ This will source the ``env.local`` file, apply the appropriate variable substitu
106
106
".template", and run ``docker-compose`` with all the command line arguments after the ``compose`` argument.
107
107
See `env.local.example <env.local.example>`_ (:download:`download </birdhouse/env.local.example>`) for more details on what can go into the ``env.local`` file.
108
108
109
+
Most variables that can be set in the local environment file (``env.local`` by default) can also be specified as environment variables when running ``bin/birdhouse``
110
+
commands. Environment variables will take precedence over those specified in the ``env.local`` file.
111
+
109
112
If the file `env.local` is somewhere else, symlink it here, next to `docker-compose.yml <docker-compose.yml>`_ (:download:`download </birdhouse/docker-compose.yml>`) because many scripts assume this location.
110
113
If autodeploy scheduler job is enabled, the folder containing the `env.local` file needs to be added to `BIRDHOUSE_AUTODEPLOY_EXTRA_REPOS`.
# Record all currently exported environment variables and store them in the __BIRDHOUSE_PROCESS_ENV
463
+
# variable. Calling reset_process_env later will re-export all these variables, setting them back to their
464
+
# original value. See reset_process_env for an example.
465
+
stage_process_env() {
466
+
__BIRDHOUSE_PROCESS_ENV="$(export -p)"
467
+
}
468
+
469
+
# Re-export and set all environment variables that were exported when stage_process_env was last called. This
470
+
# effectively resets these variables to the values that they had when stage_process_env was last called.
471
+
#
472
+
# Note that this will not unset variables that were not exported when stage_process_env was last called.
473
+
#
474
+
# For example:
475
+
#
476
+
# export X=10
477
+
# stage_process_env
478
+
# echo $X # prints 10
479
+
# X=11
480
+
# export Y=12
481
+
# echo $X # prints 11
482
+
# reset_process_env
483
+
# echo $X # prints 10
484
+
# echo $Y # prints 12
485
+
reset_process_env() {
486
+
eval"${__BIRDHOUSE_PROCESS_ENV}"
487
+
}
462
488
463
489
### Similar code between read_configs() and read_basic_configs_only().
464
490
_read_basic_configs_pre() {
491
+
stage_process_env
465
492
set_backwards_compatible_as_default
466
493
discover_compose_dir
467
494
discover_env_local
@@ -470,8 +497,9 @@ _read_basic_configs_pre() {
470
497
471
498
472
499
_read_basic_configs_post() {
473
-
read_env_local # again to override components default.env, need discover_env_local
474
-
set_old_backwards_compatible_variables # after read_env_local to use updated value and not default value
500
+
read_env_local # override default env (for a second time if called from read_configs), needs discover_env_local to run first
501
+
reset_process_env # override local env and default env with variables declared in the calling process' environment, needs stage_process_env to run first
502
+
set_old_backwards_compatible_variables # after read_env_local to use updated value and not default value for backwards compatible variables
475
503
process_backwards_compatible_variables
476
504
check_default_vars
477
505
process_delayed_eval
@@ -485,7 +513,8 @@ read_configs() {
485
513
_read_basic_configs_pre
486
514
487
515
### This section is different than read_basic_configs_only() below, the rest should be IDENTICAL.
488
-
read_env_local # for BIRDHOUSE_EXTRA_CONF_DIRS and BIRDHOUSE_DEFAULT_CONF_DIRS, need discover_env_local
516
+
read_env_local # for BIRDHOUSE_EXTRA_CONF_DIRS and BIRDHOUSE_DEFAULT_CONF_DIRS, needs discover_env_local to run first
517
+
reset_process_env # use BIRDHOUSE_EXTRA_CONF_DIRS and BIRDHOUSE_DEFAULT_CONF_DIRS if they're declared in the calling process' environment, needs stage_process_env to run first
0 commit comments