Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proposal of management of .env* parameters files in Symfony+Docker way #85

Closed
wants to merge 53 commits into from

Conversation

rv2931
Copy link
Collaborator

@rv2931 rv2931 commented Feb 26, 2024

The proposition is to managed .env files according to Symfony standard

  • we load .env as default values. In it, APP_ENV=dev is declared
  • then override with content of .env.local if exists. This file is not index in git, we can overload APP_ENV=test by example
  • then override with content of .env.${APP_ENV} if exists. This file contains default sepcific to APP_ENV env (dev/test/prod) in addition to common default values in .env
  • then override with content of .env.${APP_ENV}.local if exists. This file is not indexed in git.

For compliance with docker standard, .env is reserved to store the merged result of all variables So when all files has been parsed, the result is written in .env and reloaded from .env in bloom.config

All keys are transformed to lower case
ATTR=VaLue will give => settings.attr:VaLue

To assure a minimal documentation of parameters, as Symfony standard in .env.dist The extracted values in all theses files MUST BE present in .env So for adding a new parameter, you MUST declare it in .env with a default value If not, this new parameters won't be available in python scripts

To access settings

from bloom.config import settings
print(settings.db_url)
print(settings.POSTGRES_USER)

To overload a default .env value, you have to create a .env.local file with the new key/value pair, it will override the default value at runtime

The proposition is to managed .env files according to Symfony standard
- we load .env.template as default values. In it, APP_ENV=dev is declared
- then override with content of .env.local if exists. This file is not index in git, we can overload APP_ENV=test by example
- then override with content of .env.${APP_ENV} if exists. This file contains default sepcific to APP_ENV env (dev/test/prod) in addition to common default values in .env.template
- then override with content of .env.${APP_ENV}.local if exists. This file is not indexed in git.
For compliance with docker standard, .env is reserved to store the merged result of all variables
So when all files has been parsed, the result is written in .env and reloaded from .env in bloom.config

All keys are transformed to lower case
ATTR=VaLue will give => settings.attr:VaLue

To assure a minimal documentation of parameters, as Symfony standard in .env.dist
The extracted values in all theses files MUST BE present in .env.template
So for adding a new parameter, you MUST declare it in .env.template with a default value
If not, this new parameters won't be available in python scripts

To access settings
```
from bloom.config import settings
print(settings.db_url)
print(settings.postgres_user)
```
@rv2931
Copy link
Collaborator Author

rv2931 commented Feb 26, 2024

En terme d'amélioration, on pourrait gérer la APP_ENV via arguments de lancement de l'application plutôt qu'en le surchargeant dans .env.local. Cela permet de lancer plusieurs instances dans des environnements différents sans conflits de lecture/écriture au sein du même répertoire.

@rv2931
Copy link
Collaborator Author

rv2931 commented Feb 26, 2024

Là je génère un fichier .env qui contient le résultat compilé pour utilisation avec Docker --env-file ./.env mais j'aime pas trop cette solution car si on lance deux applis dans deux modes différents, le .env va être modifié par la dernière lancé. Ce n'est pas bon mais tant qu'on utilise qu'un mode y a pas trop de souci
Faudrait créer un script shell et/ou une Target/Rule qui fait la même chose pour que les lancement Docker aient bien leur .env mergé

rv2931 and others added 22 commits February 26, 2024 18:50
…ocker compliance --env-file + keep .env.template case for parameters

The bloom.config already knows extract and override key/value pairs from .env.* files based on .env.template default key/value pairs
The idea here is to add env vars extraction and override pre existing/extracted values from files

This is compliant with Docker standard but also with standard deployement:
- for the same key/value, env value is prioritay to file value
- if no env value, then file value
- if no file value, then default value from .env.template
@rv2931 rv2931 changed the title feat: proposal of management of .env* parameters files in Symfony way feat: proposal of management of .env* parameters files in Symfony+Docker way Feb 28, 2024
@rv2931 rv2931 marked this pull request as ready for review February 28, 2024 13:54
@rv2931 rv2931 force-pushed the env_management_proposal branch 3 times, most recently from d1efe35 to b81550a Compare February 29, 2024 09:53
dev: source .env; source .env.local; source .env.dev; source .env.dev.local; export APP_ENV=dev; docker compose -f docker-env\docker-compose-base.yml -f docker-env\docker-compose-dev.yml up
test: source .env; source .env.local; source .env.test; source .env.test.local; export APP_ENV=test; docker compose -f docker-env\docker-compose-base.yml -f docker-env\docker-compose-test.yml up
prod: source .env; source .env.local; source .env.prod; source .env.prod.local; export APP_ENV=prod;  docker compose -f docker-env\docker-compose-base.yml up app (only app)

all configurations files .env, .env.local, .env.dev|test|prod, .env.dev|test|prod.local are systematically mounted as volumes in all docker configuration, they load (source) before docker compose to services that are using ENV var like posgtrds (i.e: POSTGRES_HOSTNAME) to ge tconsistant configuration file in docker et launch variable for docker services. Their use is totallement conditionned by APP_ENV value in the .env.local file but is override if we export it in environment variable
@rv2931 rv2931 marked this pull request as draft March 1, 2024 14:03
@rv2931 rv2931 mentioned this pull request Mar 3, 2024
@rv2931
Copy link
Collaborator Author

rv2931 commented Mar 6, 2024

abandonned, to complex according project management and difficult to "non docker developers" to maintain

@rv2931 rv2931 closed this Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant