-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_env.py
99 lines (81 loc) · 3.1 KB
/
load_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
from typing import Optional
import yaml
from pathlib import Path
from services.ssm_service import SSMService
LOCAL_SECRETS_FILE = 'config/local-secrets.yaml'
ENV_VAR_TO_SSM_NAME = {
'CONTENT_CAFE_USER': 'contentcafe/user',
'CONTENT_CAFE_PSWD': 'contentcafe/pswd',
'ELASTICSEARCH_PSWD': 'elasticsearch/pswd',
'ELASTICSEARCH_USER': 'elasticsearch/user',
'GITHUB_API_KEY': 'github-key',
'GOOGLE_BOOKS_KEY': 'google-books/api-key',
'HATHI_API_KEY': 'hathitrust/api-key',
'HATHI_API_SECRET': 'hathitrust/api-secret',
'NEW_RELIC_LICENSE_KEY': 'newrelic/key',
'NYPL_API_CLIENT_ID': 'nypl-api/client-id',
'NYPL_API_CLIENT_PUBLIC_KEY': 'nypl-api/public-key',
'NYPL_API_CLIENT_SECRET': 'nypl-api/client-secret',
'NYPL_BIB_PSWD': 'postgres/nypl-pswd',
'NYPL_BIB_USER': 'postgres/nypl-user',
'OCLC_METADATA_ID': 'oclc-metadata-clientid',
'OCLC_METADATA_SECRET': 'oclc-metadata-secret',
'OCLC_CLIENT_ID': 'oclc-search-clientid',
'OCLC_CLIENT_SECRET': 'oclc-search-secret',
'POSTGRES_PSWD': 'postgres/pswd',
'POSTGRES_USER': 'postgres/user',
'RABBIT_PSWD': 'rabbit-pswd',
'RABBIT_USER': 'rabbit-user',
}
def load_env_file(run_type: str, file_string: Optional[str]=None) -> None:
"""Loads configuration details from a specific yaml file.
Arguments:
runType {string} -- The environment to load configuration details for.
fileString {string} -- The file string format indicating where to load
the configuration file from.
Raises:
YAMLError: Indicates malformed yaml markup in the configuration file
Returns:
dict -- A dictionary containing the configuration details parsed from
the specificied yaml file.
"""
env_dict = None
if file_string:
open_file = file_string.format(run_type)
else:
open_file = 'local.yaml'
try:
with open(open_file) as env_stream:
try:
env_dict = yaml.full_load(env_stream)
except yaml.YAMLError as err:
print(f'{open_file} Invalid! Please review')
raise err
except FileNotFoundError as err:
print('Missing config YAML file! Check directory')
raise err
if env_dict:
for key, value in env_dict.items():
os.environ[key] = value
load_secrets()
def _set_env_vars(config: dict) -> None:
for key, value in config.items():
if key not in os.environ:
os.environ[key] = str(value)
def _load_yaml_config(file_path: str) -> None:
try:
with open(file_path, "r") as file:
return yaml.safe_load(file) or {}
except:
return {}
def load_secrets():
if Path(LOCAL_SECRETS_FILE).exists():
secrets_config = _load_yaml_config(LOCAL_SECRETS_FILE)
_set_env_vars(secrets_config)
ssm_service = SSMService()
for env_var, param_name in ENV_VAR_TO_SSM_NAME.items():
if os.environ.get(env_var, None) is None:
param = ssm_service.get_parameter(param_name)
if param is not None:
os.environ[env_var] = param