-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboot.py
300 lines (244 loc) · 9.34 KB
/
boot.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""
Boot module for Application
Version: 1.0.5
"""
import json
import logging
import os
import sys
if __package__:
current_path = os.path.abspath(os.path.dirname(__file__)).replace('/' + str(__package__), '', 1)
else:
current_path = os.path.abspath(os.path.dirname(__file__))
if not current_path[-1] == '/':
current_path += '/'
ROOT_DIR = current_path
_LOADED = False
_ENV_KEYS = []
try:
import chalicelib
APP_TYPE = 'Chalice'
except Exception as err:
APP_TYPE = 'Flask'
_DEFAULT_ENV_CONFIGS = {
"APP_ENV": "development",
"DEBUG": "true",
"LOG_LEVEL": "info",
"APP_TYPE": APP_TYPE
}
def set_root_dir(root_dir):
global ROOT_DIR
ROOT_DIR = root_dir
def is_loaded():
global _LOADED
return _LOADED
def reset():
global _LOADED, _ENV_KEYS
_LOADED = False
_ENV_KEYS = []
def get_env_keys():
global _ENV_KEYS
return _ENV_KEYS
def get_internal_logger():
from application import APP_NAME
try:
from application.logging import LoggerProfile, get_logger, get_log_level, reset as reset_logger
logger = get_logger(LoggerProfile.CONSOLE)
logger.setLevel(get_log_level())
reset_logger()
except Exception as err:
import logging
log_name = APP_NAME
logger = logging.getLogger(log_name)
logger.error('load_env: Unable to load logger: {}'.format(err))
return logger
def load_dot_env(env='development', force=False, debug=False):
result = False
from dotenv import dotenv_values
# env default value
if env is None:
env = 'development'
logger = get_internal_logger()
global _LOADED, _ENV_KEYS, _DEFAULT_ENV_CONFIGS
if not _LOADED or force:
if debug:
logger.info('Boot - load_dot_env - Loading env: {}'.format(env))
# Default
for k, v in _DEFAULT_ENV_CONFIGS.items():
_ENV_KEYS.append(k)
if k == 'APP_ENV':
v = env
# se não estiver setado ainda no env, cria o valor default
if k not in os.environ:
os.environ[k] = v
config_path = '{}env/{}.env'.format(current_path, env)
if os.path.isfile(config_path):
env_vars = dotenv_values(config_path)
for k, v in env_vars.items():
_ENV_KEYS.append(k)
os.environ[k] = v
_LOADED = True
result = True
else:
# try with development
if env == 'dev':
result = load_dot_env('development')
else:
# Try to load via secrets manager
result = load_secrets(env=env)
if result:
_LOADED = True
else:
if debug:
logger.error('Unable to load config - secrets scenario')
else:
result = True
return result
def load_secrets(env='staging', debug=False):
global _ENV_KEYS
from application.aws.secrets import Secrets
from application import APP_NAME
logger = get_internal_logger()
result = False
logger.info('Boot - Loading env by secret manager: {}'.format(env))
app_name = os.environ["APP_NAME"] if "APP_NAME" in os.environ else APP_NAME
secret_name = app_name + "-" + env
try:
logger.info('secret name: {}'.format(secret_name))
secrets_dict = Secrets().get_secrets(secret_name=secret_name)
except Exception as err:
secrets_dict = None
if debug:
logger.error(err)
if secrets_dict is not None:
for k, v in secrets_dict.items():
os.environ[k] = v
result = True
return result
def load_env(env='dev', force=False, debug=False):
result = False
# env default value
if env is None:
env = 'dev'
logger = get_internal_logger()
# só para testes
logger.level = logging.INFO
global _LOADED, _ENV_KEYS, _DEFAULT_ENV_CONFIGS
if not _LOADED or force:
chalice_config_path = '{}.chalice/config.json'.format(current_path)
if debug:
logger.info('Boot - load_env - Loading env: {}'.format(env))
logger.info('Boot - load_env - chalice_config_path: {}'.format(chalice_config_path))
# todo tratar cenários novos que não serão baseado em arquivos
if os.path.isfile(chalice_config_path):
if debug:
logger.info('Boot - load_env - Chalice retro compatibility')
file = open(chalice_config_path, 'r')
data = file.read()
configs = json.loads(data)
# solution for projects with integration tests
if env not in configs['stages'] and env == 'integration':
env = 'staging'
# solution for projects with dev flag instead of development
if env not in configs['stages'] and env == 'development':
env = 'dev'
if env in configs['stages']:
env_vars = configs['stages'][env]['environment_variables']
if isinstance(env_vars, dict):
for k, v in env_vars.items():
_ENV_KEYS.append(k)
# não remover, senão vai sobrescrever as variaveis pelo arquivo
# config.json, sabemos que em ambientes como de prod isso é ruim
if k not in os.environ:
os.environ[k] = v
if debug:
logger.info(
'Boot - load_env - setting env configs.stages.env k = v {} = {}'.format(k, v))
else:
if debug:
logger.info(
'Boot - load_env - ENV k = v {} = {}'.format(k, os.environ[k]))
_LOADED = True
result = True
else:
if debug:
logger.error(
"Boot - load_env - Unable to load env_vars from chalice: {}".format(
env_vars))
else:
# solution for projects with development flag instead of dev
if env == 'dev':
result = load_env('development')
else:
if debug:
logger.error('Unable to load config - chalice scenario')
_LOADED = False
# close the file
file.close()
else:
if debug:
logger.info('Boot - load_env - Application compatibility')
# todo implementar uma logica melhor para identificar se as variáveis do projeto estão
# print(os.environ['API_SERVER'])
# registradas
if 'APP_QUEUE' in os.environ or 'DB_HOST' in os.environ or 'APP_BUCKET' in os.environ:
_LOADED = True
if debug:
logger.info('Boot - load_env - Environment variables already defined')
# logger.info('APP_QUEUE = {}'.format('APP_QUEUE' in os.environ))
# logger.info('API_SERVER = {}'.format('API_SERVER' in os.environ))
# logger.info('DB_HOST = {}'.format('DB_HOST' in os.environ))
# logger.info('APP_BUCKET = {}'.format('APP_BUCKET' in os.environ))
# logger.info('FLASK_ENV = {}'.format('FLASK_ENV' in os.environ))
result = True
# logger.info(os.environ)
else:
if debug:
logger.error('Boot - load_env - Unable to load config - environment scenario')
_LOADED = False
# Carrega as variaves default, só seta valores caso não tenham sido carregadas via
# arquivo ou env (secrets)
for k, v in _DEFAULT_ENV_CONFIGS.items():
_ENV_KEYS.append(k)
if k == 'APP_ENV':
v = env
# se não estiver setado ainda no env, cria o valor default
if k not in os.environ:
os.environ[k] = v
else:
result = True
return result
def register_vendor():
vendor_path = current_path + "vendor"
# print(vendor_path)
if not os.path.isdir(vendor_path):
vendor_path = current_path + "/vendor"
# print(vendor_path)
sys.path.insert(0, vendor_path)
def register_path(path):
if os.path.isdir(path):
sys.path.insert(0, path)
def print_env(app, logger):
logger.info('Environment: %s' % get_environment())
# logger.info('Host: %s' % os.getenv('APP_HOST'))
# logger.info('Port: %s' % os.getenv('APP_PORT'))
# logger.info('Database: %s' % os.getenv('DB_HOST'))
logger.info('Log Level: %s' % os.getenv('LOG_LEVEL'))
logger.info('Debug: %s' % os.getenv('DEBUG'))
def get_environment():
environment = 'development'
if 'ENVIRONMENT' in os.environ:
environment = os.environ['ENVIRONMENT']
elif 'ENVIRONMENT_NAME' in os.environ:
environment = os.environ['ENVIRONMENT_NAME']
elif 'APP_ENV' in os.environ:
environment = os.environ['APP_ENV']
if environment == "dev":
environment = "development"
return environment
# register the vendor folder
register_vendor()
# load env
loaded = load_env(get_environment(), debug=True)
if not loaded:
loaded = load_dot_env(get_environment(), debug=True)