Commit 426c1b7eadd74e0d37c62df31a06f89bfd3153a9

Authored by Alexis Koralewski
1 parent 86895541
Exists in dev

updating how environment variables are read, renaming env file

@@ -39,4 +39,4 @@ out.* @@ -39,4 +39,4 @@ out.*
39 39
40 client.log 40 client.log
41 41
42 -docker/.env  
43 \ No newline at end of file 42 \ No newline at end of file
  43 +docker/*.env
44 \ No newline at end of file 44 \ No newline at end of file
docker/docker-compose.yml
@@ -30,6 +30,8 @@ services: @@ -30,6 +30,8 @@ services:
30 tty: true 30 tty: true
31 # stdin_open is the -i option in docker exec 31 # stdin_open is the -i option in docker exec
32 stdin_open: true 32 stdin_open: true
  33 + env_file:
  34 + - variables.env
33 environment: 35 environment:
34 # environment variables only for Docker 36 # environment variables only for Docker
35 - WITH_DOCKER=y 37 - WITH_DOCKER=y
@@ -137,7 +137,7 @@ SQL_USER = "" @@ -137,7 +137,7 @@ SQL_USER = ""
137 SQL_PSWD = "" 137 SQL_PSWD = ""
138 MYSQL_EXE_PATH = "" 138 MYSQL_EXE_PATH = ""
139 139
140 -ENV_PATH = "docker/.env" 140 +ENV_PATH = "docker/variables.env"
141 ENV_SAMPLE_PATH = "docker/.env-sample" 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,41 +298,47 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
298 env_path (str): path to .env file 298 env_path (str): path to .env file
299 env_sample_path (str): path to .env-sample file 299 env_sample_path (str): path to .env-sample file
300 """ 300 """
  301 + """
301 variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"] 302 variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"]
302 is_environment_variables_defined = True 303 is_environment_variables_defined = True
303 while(is_environment_variables_defined): 304 while(is_environment_variables_defined):
304 for variable in variables_names: 305 for variable in variables_names:
305 if( os.environ.get(variable) is None): 306 if( os.environ.get(variable) is None):
306 is_environment_variables_defined = False 307 is_environment_variables_defined = False
  308 + break
307 if(not is_environment_variables_defined): 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 os.environ[key] = value 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,41 +55,47 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
55 env_path (str): path to .env file 55 env_path (str): path to .env file
56 env_sample_path (str): path to .env-sample file 56 env_sample_path (str): path to .env-sample file
57 """ 57 """
  58 + """
58 variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"] 59 variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"]
59 is_environment_variables_defined = True 60 is_environment_variables_defined = True
60 while(is_environment_variables_defined): 61 while(is_environment_variables_defined):
61 for variable in variables_names: 62 for variable in variables_names:
62 if( os.environ.get(variable) is None): 63 if( os.environ.get(variable) is None):
63 is_environment_variables_defined = False 64 is_environment_variables_defined = False
  65 + break
64 if(not is_environment_variables_defined): 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 os.environ[key] = value 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 else: 96 else:
92 print("The environment variables are already configured, skipping this step...") 97 print("The environment variables are already configured, skipping this step...")
  98 + """
93 99
94 # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 100 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
95 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 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(&quot;^y$|^Y$|^yes$|^Yes$&quot;,WITH_DOCKER.rstri @@ -118,7 +124,7 @@ if type(WITH_DOCKER) is str and re.match(&quot;^y$|^Y$|^yes$|^Yes$&quot;,WITH_DOCKER.rstri
118 else : 124 else :
119 WITH_DOCKER = False 125 WITH_DOCKER = False
120 HTTP_PORT = "" 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 ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample") 128 ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample")
123 # default value of mysql port 129 # default value of mysql port
124 MYSQL_PORT = "3306" 130 MYSQL_PORT = "3306"