Environment handling to simplify development environments
This repo aims to be a cloud and local compatible approach to modern application environment handling.
Environment files committed with the repo bring us some clear advantages that we should consider when building software.
At KUNGFU.AI, many of our usecases are single container microservice deployment into a cloud. By committing secrets to cloud Secret Manager, and keeping Secret IDs + other environment data within our env files, we're able to:
- Keep our deployed assets secure
- Developers don't have to pass keys, know about keys, or share env files (YUCK)
- Developers can pull and run their repos immediately.
The kungfuai/env repo aims to simplify these use-cases.
Python 3.8
pip install kungfuai-env
The flow is 3 steps - Construction, Registration, and Loading. e.g.
# Construction
env = Environment('src/env')
# Registration
env.register_environment("test")
env.register_environment("prod")
# Loading
env.load_env()
In the above example, Environment()
is constructed, and the path to environment files is set to be located at
src/env
.
Next, we state that we have a test
environment, and a prod
environment that we intend to load.
The names are important - seeing test
indicates that env.load_env()
will seek a .test.env
file in the src/env
directory.
The same would be true for prod
/ .prod.env
Now, we know how to handle the environments. How do we know which ones to load when the application starts?
There is a special environment variable that we always watch named ENV
.
With the above example in mind, if you set ENV=PROD
or ENV=prod
, you will load the .prod.env
file.
If you set ENV=TEST
or ENV=test
, you will load the .test.env
file.
If you do not set anything for ENV
, you will load, by default, the .local.env
file.
In the most simple of applications, imagine a main.py
with a .local.env
environment
# main.py
import os
from kfai_env.env import Environment
if __name__ == "__main__":
print("Simple Python App Example")
e = Environment()
e.register_environment("prod")
e.load_env()
print(os.getenv("TEST_ENV"))
# .local.env
TEST_ENV=HELLOWORLDFROMLOCAL
# .prod.env
TEST_ENV=HELLOWORLDFROMPROD
If we were to run python3 main.py
with this above example program, we would see HELLOWORLDFROMLOCAL
.
If we were to run ENV=PROD python3 main.py
, we would see HELLOWORLDFROMPROD
.
See the open issues for a list of proposed features (and known issues).
- Upgrade project to use poetry as the build and deploy system (instead of pip + versioneer)
Distributed under the MIT License. See LICENSE
for more information.
Endurance Idehen - endurance.idehen@kungfu.ai