Commit 426c1b7eadd74e0d37c62df31a06f89bfd3153a9
1 parent
86895541
Exists in
dev
updating how environment variables are read, renaming env file
Showing
4 changed files
with
69 additions
and
55 deletions
Show diff stats
.gitignore
docker/docker-compose.yml
pyros.py
... | ... | @@ -137,7 +137,7 @@ SQL_USER = "" |
137 | 137 | SQL_PSWD = "" |
138 | 138 | MYSQL_EXE_PATH = "" |
139 | 139 | |
140 | -ENV_PATH = "docker/.env" | |
140 | +ENV_PATH = "docker/variables.env" | |
141 | 141 | ENV_SAMPLE_PATH = "docker/.env-sample" |
142 | 142 | |
143 | 143 | |
... | ... | @@ -298,41 +298,47 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s |
298 | 298 | env_path (str): path to .env file |
299 | 299 | env_sample_path (str): path to .env-sample file |
300 | 300 | """ |
301 | + """ | |
301 | 302 | variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"] |
302 | 303 | is_environment_variables_defined = True |
303 | 304 | while(is_environment_variables_defined): |
304 | 305 | for variable in variables_names: |
305 | 306 | if( os.environ.get(variable) is None): |
306 | 307 | is_environment_variables_defined = False |
308 | + break | |
307 | 309 | if(not is_environment_variables_defined): |
308 | - print("Some environment variables are not configured...") | |
309 | - try: | |
310 | - with open(env_path,"r") as env_file: | |
311 | - # env file is empty | |
312 | - if len(env_file) <= 0: | |
313 | - raise BaseException() | |
314 | - | |
315 | - print("Reading env file") | |
316 | - for line in env_file: | |
317 | - if(line.startswith("#") and not line.strip()): | |
318 | - continue | |
319 | - else: | |
320 | - key,value = line.split("=") | |
321 | - # setting variables as environment variables | |
322 | - os.environ[key] = value | |
323 | - except: | |
324 | - print(f".env not found at {ENV_PATH} or is empty, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \ | |
325 | - values from .env-sample will be used as environment variables") | |
326 | - with open(env_sample_path,'r') as env_sample_file: | |
327 | - with open(env_path,"w") as env_file: | |
328 | - for env_sample_line in env_sample_file: | |
329 | - if(env_sample_line.startswith("#") or not env_sample_line.strip()): | |
330 | - continue | |
331 | - key,value = env_sample_line.split("=") | |
310 | + print("Some environment variables are not configured...") | |
311 | + """ | |
312 | + try: | |
313 | + with open(env_path,"r") as env_file: | |
314 | + # env file is empty | |
315 | + if os.stat(env_path).st_size == 0: | |
316 | + raise BaseException() | |
317 | + print("Reading env file") | |
318 | + for line in env_file: | |
319 | + if(line.startswith("#") and not line.strip()): | |
320 | + continue | |
321 | + else: | |
322 | + line = line.rstrip() | |
323 | + key,value = line.split("=") | |
324 | + # setting variables as environment variables | |
325 | + if os.environ[key] != value: | |
326 | + print(f"WARNING: Environment value for '{key}' needs to be uptaded (To remove this message : restart docker container to update them). PyROS will take the value from '{env_path}'") | |
332 | 327 | os.environ[key] = value |
333 | - env_file.write(env_sample_line) | |
334 | - else: | |
335 | - print("The environment variables are already configured, skipping this step...") | |
328 | + except: | |
329 | + print(f".env not found at {env_path} or is empty, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\nvalues from .env-sample will be used as environment variables") | |
330 | + with open(env_sample_path,'r') as env_sample_file: | |
331 | + with open(env_path,"w") as env_file: | |
332 | + for env_sample_line in env_sample_file: | |
333 | + if(env_sample_line.startswith("#") or not env_sample_line.strip()): | |
334 | + continue | |
335 | + key,value = env_sample_line.split("=") | |
336 | + os.environ[key] = value | |
337 | + env_file.write(env_sample_line) | |
338 | +""" | |
339 | +else: | |
340 | + print("The environment variables are already configured, skipping this step...") | |
341 | +""" | |
336 | 342 | |
337 | 343 | |
338 | 344 | ... | ... |
src/core/pyros_django/pyros/settings.py
... | ... | @@ -55,41 +55,47 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s |
55 | 55 | env_path (str): path to .env file |
56 | 56 | env_sample_path (str): path to .env-sample file |
57 | 57 | """ |
58 | + """ | |
58 | 59 | variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"] |
59 | 60 | is_environment_variables_defined = True |
60 | 61 | while(is_environment_variables_defined): |
61 | 62 | for variable in variables_names: |
62 | 63 | if( os.environ.get(variable) is None): |
63 | 64 | is_environment_variables_defined = False |
65 | + break | |
64 | 66 | if(not is_environment_variables_defined): |
65 | - print("Some environment variables are not configured...") | |
66 | - try: | |
67 | - with open(env_path,"r") as env_file: | |
68 | - # env file is empty | |
69 | - if len(env_file) <= 0: | |
70 | - raise BaseException() | |
71 | - | |
72 | - print("Reading env file") | |
73 | - for line in env_file: | |
74 | - if(line.startswith("#") and not line.strip()): | |
75 | - continue | |
76 | - else: | |
77 | - key,value = line.split("=") | |
78 | - # setting variables as environment variables | |
79 | - os.environ[key] = value | |
80 | - except: | |
81 | - print(f".env not found at {ENV_PATH} or is empty, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \ | |
82 | - values from .env-sample will be used as environment variables") | |
83 | - with open(env_sample_path,'r') as env_sample_file: | |
84 | - with open(env_path,"w") as env_file: | |
85 | - for env_sample_line in env_sample_file: | |
86 | - if(env_sample_line.startswith("#") or not env_sample_line.strip()): | |
87 | - continue | |
88 | - key,value = env_sample_line.split("=") | |
67 | + print("Some environment variables are not configured...") | |
68 | + """ | |
69 | + try: | |
70 | + with open(env_path,"r") as env_file: | |
71 | + # env file is empty | |
72 | + if os.stat(env_path).st_size == 0: | |
73 | + raise BaseException() | |
74 | + print("Reading env file") | |
75 | + for line in env_file: | |
76 | + if(line.startswith("#") and not line.strip()): | |
77 | + continue | |
78 | + else: | |
79 | + line = line.rstrip() | |
80 | + key,value = line.split("=") | |
81 | + # setting variables as environment variables | |
82 | + if os.environ[key] != value: | |
83 | + print(f"WARNING: Environment value for '{key}' needs to be uptaded (To remove this message : restart docker container to update them). PyROS will take the value from '{env_path}'") | |
89 | 84 | os.environ[key] = value |
90 | - env_file.write(env_sample_line) | |
85 | + except: | |
86 | + print(f".env not found at {env_path} or is empty, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\nvalues from .env-sample will be used as environment variables") | |
87 | + with open(env_sample_path,'r') as env_sample_file: | |
88 | + with open(env_path,"w") as env_file: | |
89 | + for env_sample_line in env_sample_file: | |
90 | + if(env_sample_line.startswith("#") or not env_sample_line.strip()): | |
91 | + continue | |
92 | + key,value = env_sample_line.split("=") | |
93 | + os.environ[key] = value | |
94 | + env_file.write(env_sample_line) | |
95 | + """ | |
91 | 96 | else: |
92 | 97 | print("The environment variables are already configured, skipping this step...") |
98 | + """ | |
93 | 99 | |
94 | 100 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) |
95 | 101 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
... | ... | @@ -118,7 +124,7 @@ if type(WITH_DOCKER) is str and re.match("^y$|^Y$|^yes$|^Yes$",WITH_DOCKER.rstri |
118 | 124 | else : |
119 | 125 | WITH_DOCKER = False |
120 | 126 | HTTP_PORT = "" |
121 | -ENV_PATH = os.path.join(BASE_DIR,"../../../docker/.env") | |
127 | +ENV_PATH = os.path.join(BASE_DIR,"../../../docker/variables.env") | |
122 | 128 | ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample") |
123 | 129 | # default value of mysql port |
124 | 130 | MYSQL_PORT = "3306" | ... | ... |