From 426c1b7eadd74e0d37c62df31a06f89bfd3153a9 Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Wed, 28 Jul 2021 12:54:00 +0200 Subject: [PATCH] updating how environment variables are read, renaming env file --- .gitignore | 2 +- docker/docker-compose.yml | 2 ++ pyros.py | 62 ++++++++++++++++++++++++++++++++++---------------------------- src/core/pyros_django/pyros/settings.py | 58 ++++++++++++++++++++++++++++++++-------------------------- 4 files changed, 69 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index a953683..f0611f9 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,4 @@ out.* client.log -docker/.env \ No newline at end of file +docker/*.env \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index faf6a04..c8d6ddb 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -30,6 +30,8 @@ services: tty: true # stdin_open is the -i option in docker exec stdin_open: true + env_file: + - variables.env environment: # environment variables only for Docker - WITH_DOCKER=y diff --git a/pyros.py b/pyros.py index bfcb29b..4f59f5c 100755 --- a/pyros.py +++ b/pyros.py @@ -137,7 +137,7 @@ SQL_USER = "" SQL_PSWD = "" MYSQL_EXE_PATH = "" -ENV_PATH = "docker/.env" +ENV_PATH = "docker/variables.env" ENV_SAMPLE_PATH = "docker/.env-sample" @@ -298,41 +298,47 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s env_path (str): path to .env file env_sample_path (str): path to .env-sample file """ + """ variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"] is_environment_variables_defined = True while(is_environment_variables_defined): for variable in variables_names: if( os.environ.get(variable) is None): is_environment_variables_defined = False + break if(not is_environment_variables_defined): - print("Some environment variables are not configured...") - try: - with open(env_path,"r") as env_file: - # env file is empty - if len(env_file) <= 0: - raise BaseException() - - print("Reading env file") - for line in env_file: - if(line.startswith("#") and not line.strip()): - continue - else: - key,value = line.split("=") - # setting variables as environment variables - os.environ[key] = value - except: - 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 \ - values from .env-sample will be used as environment variables") - with open(env_sample_path,'r') as env_sample_file: - with open(env_path,"w") as env_file: - for env_sample_line in env_sample_file: - if(env_sample_line.startswith("#") or not env_sample_line.strip()): - continue - key,value = env_sample_line.split("=") + print("Some environment variables are not configured...") + """ + try: + with open(env_path,"r") as env_file: + # env file is empty + if os.stat(env_path).st_size == 0: + raise BaseException() + print("Reading env file") + for line in env_file: + if(line.startswith("#") and not line.strip()): + continue + else: + line = line.rstrip() + key,value = line.split("=") + # setting variables as environment variables + if os.environ[key] != value: + 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}'") os.environ[key] = value - env_file.write(env_sample_line) - else: - print("The environment variables are already configured, skipping this step...") + except: + 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") + with open(env_sample_path,'r') as env_sample_file: + with open(env_path,"w") as env_file: + for env_sample_line in env_sample_file: + if(env_sample_line.startswith("#") or not env_sample_line.strip()): + continue + key,value = env_sample_line.split("=") + os.environ[key] = value + env_file.write(env_sample_line) +""" +else: + print("The environment variables are already configured, skipping this step...") +""" diff --git a/src/core/pyros_django/pyros/settings.py b/src/core/pyros_django/pyros/settings.py index 2c1e93d..fc3ea7e 100644 --- a/src/core/pyros_django/pyros/settings.py +++ b/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 env_path (str): path to .env file env_sample_path (str): path to .env-sample file """ + """ variables_names = ["MYSQL_ROOT_PASSWORD","MYSQL_ROOT_LOGIN","MYSQL_TCP_PORT","MYSQL_PYROS_LOGIN","MYSQL_PYROS_PWD","PATH_TO_OBSCONF_FILE"] is_environment_variables_defined = True while(is_environment_variables_defined): for variable in variables_names: if( os.environ.get(variable) is None): is_environment_variables_defined = False + break if(not is_environment_variables_defined): - print("Some environment variables are not configured...") - try: - with open(env_path,"r") as env_file: - # env file is empty - if len(env_file) <= 0: - raise BaseException() - - print("Reading env file") - for line in env_file: - if(line.startswith("#") and not line.strip()): - continue - else: - key,value = line.split("=") - # setting variables as environment variables - os.environ[key] = value - except: - 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 \ - values from .env-sample will be used as environment variables") - with open(env_sample_path,'r') as env_sample_file: - with open(env_path,"w") as env_file: - for env_sample_line in env_sample_file: - if(env_sample_line.startswith("#") or not env_sample_line.strip()): - continue - key,value = env_sample_line.split("=") + print("Some environment variables are not configured...") + """ + try: + with open(env_path,"r") as env_file: + # env file is empty + if os.stat(env_path).st_size == 0: + raise BaseException() + print("Reading env file") + for line in env_file: + if(line.startswith("#") and not line.strip()): + continue + else: + line = line.rstrip() + key,value = line.split("=") + # setting variables as environment variables + if os.environ[key] != value: + 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}'") os.environ[key] = value - env_file.write(env_sample_line) + except: + 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") + with open(env_sample_path,'r') as env_sample_file: + with open(env_path,"w") as env_file: + for env_sample_line in env_sample_file: + if(env_sample_line.startswith("#") or not env_sample_line.strip()): + continue + key,value = env_sample_line.split("=") + os.environ[key] = value + env_file.write(env_sample_line) + """ else: print("The environment variables are already configured, skipping this step...") + """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 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 else : WITH_DOCKER = False HTTP_PORT = "" -ENV_PATH = os.path.join(BASE_DIR,"../../../docker/.env") +ENV_PATH = os.path.join(BASE_DIR,"../../../docker/variables.env") ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample") # default value of mysql port MYSQL_PORT = "3306" -- libgit2 0.21.2