Commit f4ef0dd6cb66247ed6898d2db2551aafb4c315fd

Authored by Alexis Koralewski
1 parent 90bc3587
Exists in dev

Add fix IP adress for db_pyros and pyros container, updating database.env files …

…to connect to db_pyros
  1 +30-08-2022 (AKo): V0.5.3
  2 + - Pyros container has fix hostname (hostname of local machine)
  3 + - Adding switch of database if we're not in docker and not on the computer that host the database
  4 + - Add fix IP adress for db_pyros and pyros container, updating database.env files to connect to db_pyros
  5 +
1 29-08-2022 (AKo): v0.5.2.1 6 29-08-2022 (AKo): v0.5.2.1
2 - Adding colors for recieved and sended commands of an agent. 7 - Adding colors for recieved and sended commands of an agent.
3 - Renabling print when pulling git repo of guitastro 8 - Renabling print when pulling git repo of guitastro
docker/docker-compose.yml
@@ -16,7 +16,9 @@ services: @@ -16,7 +16,9 @@ services:
16 - db:/var/lib/mysql/ 16 - db:/var/lib/mysql/
17 # create network to allow images to communicate with other images within the same network 17 # create network to allow images to communicate with other images within the same network
18 networks: 18 networks:
19 - - pyros-network 19 + pyros-network:
  20 + ipv4_address: 172.19.0.2
  21 +
20 healthcheck: 22 healthcheck:
21 test: ["CMD", 'mysqladmin', 'ping', '-h', 'db', '-u', 'root', '-p$$MYSQL_ROOT_PASSWORD' ] 23 test: ["CMD", 'mysqladmin', 'ping', '-h', 'db', '-u', 'root', '-p$$MYSQL_ROOT_PASSWORD' ]
22 timeout: 10s 24 timeout: 10s
@@ -61,7 +63,8 @@ services: @@ -61,7 +63,8 @@ services:
61 - db 63 - db
62 # create network to allow images to communicate with other images within the same network 64 # create network to allow images to communicate with other images within the same network
63 networks: 65 networks:
64 - - pyros-network 66 + pyros-network:
  67 + ipv4_address: 172.19.0.3
65 #ipv4_address: "${IP_PYROS_USER}" 68 #ipv4_address: "${IP_PYROS_USER}"
66 restart: always 69 restart: always
67 phpmyadmin: 70 phpmyadmin:
@@ -75,7 +78,8 @@ services: @@ -75,7 +78,8 @@ services:
75 PMA_ARBITRARY: 1 78 PMA_ARBITRARY: 1
76 restart: always 79 restart: always
77 networks: 80 networks:
78 - - pyros-network 81 + pyros-network:
  82 + ipv4_address: 172.19.0.4
79 ports: 83 ports:
80 - 8081:80 84 - 8081:80
81 85
@@ -88,3 +92,7 @@ networks: @@ -88,3 +92,7 @@ networks:
88 pyros-network: 92 pyros-network:
89 #bridge is the default network driver 93 #bridge is the default network driver
90 driver: bridge 94 driver: bridge
  95 + ipam:
  96 + config:
  97 + - subnet: 172.19.0.0/16
  98 + gateway: 172.19.0.1
privatedev/config/default/.database.env
1 -DATABASE_HOST=test  
2 \ No newline at end of file 1 \ No newline at end of file
  2 +DATABASE_HOST=172.19.0.2
  3 +DATABASE_PORT=3306
3 \ No newline at end of file 4 \ No newline at end of file
privatedev/config/guitalens/.database.env
1 -DATABASE_HOST=localhost  
2 \ No newline at end of file 1 \ No newline at end of file
  2 +DATABASE_HOST=172.19.0.2
  3 +DATABASE_PORT=3306
3 \ No newline at end of file 4 \ No newline at end of file
privatedev/config/tnc/.database.env
1 -DATABASE_HOST=localhost  
2 -DATABASE_PORT=3360  
3 \ No newline at end of file 1 \ No newline at end of file
  2 +DATABASE_HOST=172.19.0.2
  3 +DATABASE_PORT=3306
4 \ No newline at end of file 4 \ No newline at end of file
@@ -810,7 +810,7 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos @@ -810,7 +810,7 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos
810 # TODO: "python -m pip" au lieu de "pip" 810 # TODO: "python -m pip" au lieu de "pip"
811 pip = "pip" if IS_WINDOWS else "pip3" 811 pip = "pip" if IS_WINDOWS else "pip3"
812 process = subprocess.Popen( 812 process = subprocess.Popen(
813 - pip + " install --user --upgrade pykwalify", shell=True) 813 + pip + " install --upgrade pykwalify", shell=True)
814 process.wait() 814 process.wait()
815 if process.returncode == 0: 815 if process.returncode == 0:
816 # self.addExecuted(self.current_command, command) 816 # self.addExecuted(self.current_command, command)
@@ -914,7 +914,7 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos @@ -914,7 +914,7 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos
914 914
915 # Agent "webserver" 915 # Agent "webserver"
916 if agent_name == "webserver": 916 if agent_name == "webserver":
917 - cmd = "manage.py runserver" 917 + cmd = "manage.py runserver 127.0.0.1:8080"
918 if(WITH_DOCKER): 918 if(WITH_DOCKER):
919 # If we're running pyros within docker, we need to specify a specific adress in order to access the website on our host machine 919 # If we're running pyros within docker, we need to specify a specific adress in order to access the website on our host machine
920 PYROS_WEBSITE_PORT = os.environ.get("PYROS_WEBSITE_PORT") 920 PYROS_WEBSITE_PORT = os.environ.get("PYROS_WEBSITE_PORT")
src/core/pyros_django/dashboard/views.py
@@ -218,9 +218,7 @@ def send_agent_cmd(request): @@ -218,9 +218,7 @@ def send_agent_cmd(request):
218 reciever = request.POST.get("agent_name") 218 reciever = request.POST.get("agent_name")
219 cmd_name = request.POST.get("cmd_name") 219 cmd_name = request.POST.get("cmd_name")
220 cmd_args = request.POST.get("cmd_args") 220 cmd_args = request.POST.get("cmd_args")
221 - print(request.POST)  
222 new_cmd = AgentCmd.send_cmd_from_to("Operator",reciever,cmd_name,cmd_args) 221 new_cmd = AgentCmd.send_cmd_from_to("Operator",reciever,cmd_name,cmd_args)
223 - print(new_cmd)  
224 if new_cmd != None: 222 if new_cmd != None:
225 messages.add_message(request, messages.INFO, f"Command sended !") 223 messages.add_message(request, messages.INFO, f"Command sended !")
226 else: 224 else:
src/core/pyros_django/pyros/settings.py
@@ -110,16 +110,17 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s @@ -110,16 +110,17 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
110 raise BaseException() 110 raise BaseException()
111 os.environ[key] = value 111 os.environ[key] = value
112 except: 112 except:
113 - with open(env_sample_path, 'r') as env_sample_file:  
114 - with open(env_path, "w") as env_file:  
115 - for env_sample_line in env_sample_file:  
116 - if(env_sample_line.startswith("#") or not env_sample_line.strip()):  
117 - continue  
118 - key, value = env_sample_line.split("=")  
119 - key = key.strip()  
120 - value = value.strip()  
121 - os.environ[key] = value  
122 - env_file.write(env_sample_line) 113 + if env_sample_path != None:
  114 + with open(env_sample_path, 'r') as env_sample_file:
  115 + with open(env_path, "w") as env_file:
  116 + for env_sample_line in env_sample_file:
  117 + if(env_sample_line.startswith("#") or not env_sample_line.strip()):
  118 + continue
  119 + key, value = env_sample_line.split("=")
  120 + key = key.strip()
  121 + value = value.strip()
  122 + os.environ[key] = value
  123 + env_file.write(env_sample_line)
123 # BASE_DIR is an absolute path to django folder 124 # BASE_DIR is an absolute path to django folder
124 os.environ["DJANGO_PATH"] = BASE_DIR 125 os.environ["DJANGO_PATH"] = BASE_DIR
125 126
@@ -150,7 +151,7 @@ if type(WITH_DOCKER) is str and re.match("^y$|^Y$|^yes$|^Yes$",WITH_DOCKER.rstri @@ -150,7 +151,7 @@ if type(WITH_DOCKER) is str and re.match("^y$|^Y$|^yes$|^Yes$",WITH_DOCKER.rstri
150 WITH_DOCKER = True 151 WITH_DOCKER = True
151 else : 152 else :
152 WITH_DOCKER = False 153 WITH_DOCKER = False
153 -HTTP_PORT = "" 154 +HTTP_PORT = "8080"
154 ENV_PATH = os.path.join(BASE_DIR,"../../../docker/variables.env") 155 ENV_PATH = os.path.join(BASE_DIR,"../../../docker/variables.env")
155 ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample") 156 ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample")
156 # default value of mysql port 157 # default value of mysql port
@@ -158,15 +159,8 @@ MYSQL_PORT = "3306" @@ -158,15 +159,8 @@ MYSQL_PORT = "3306"
158 SQL_USER = "" 159 SQL_USER = ""
159 SQL_PWD = "" 160 SQL_PWD = ""
160 #PATH_TO_OBSCONF_FILE = os.path.join(BASE_DIR,"../../privatedev/config/default/observatory_default.yml") 161 #PATH_TO_OBSCONF_FILE = os.path.join(BASE_DIR,"../../privatedev/config/default/observatory_default.yml")
161 -HOST = os.environ["HOST_NAME"]  
162 -ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'pyros.irap.omp.eu', 'astroguita.hd.free.fr', '0.0.0.0']  
163 -# defining variables when using Docker  
164 -if WITH_DOCKER:  
165 - ALLOWED_HOSTS.append('0.0.0.0')  
166 - HTTP_PORT = ":8000"  
167 -if HOST:  
168 - ALLOWED_HOSTS.append(HOST)  
169 try: 162 try:
  163 + HOST = os.environ["HOST_NAME"]
170 MYSQL_PORT = os.environ['MYSQL_TCP_PORT'].strip() 164 MYSQL_PORT = os.environ['MYSQL_TCP_PORT'].strip()
171 SQL_USER = os.environ["MYSQL_PYROS_LOGIN"].strip() 165 SQL_USER = os.environ["MYSQL_PYROS_LOGIN"].strip()
172 SQL_PWD = os.environ["MYSQL_PYROS_PWD"].strip() 166 SQL_PWD = os.environ["MYSQL_PYROS_PWD"].strip()
@@ -174,6 +168,14 @@ try: @@ -174,6 +168,14 @@ try:
174 168
175 except: 169 except:
176 set_environment_variables_if_not_configured(ENV_PATH,ENV_SAMPLE_PATH) 170 set_environment_variables_if_not_configured(ENV_PATH,ENV_SAMPLE_PATH)
  171 +ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'pyros.irap.omp.eu', 'astroguita.hd.free.fr', '0.0.0.0']
  172 +# defining variables when using Docker
  173 +if WITH_DOCKER:
  174 + ALLOWED_HOSTS.append('0.0.0.0')
  175 + HTTP_PORT = ":8000"
  176 +HOST = os.environ["HOST_NAME"]
  177 +if HOST:
  178 + ALLOWED_HOSTS.append(HOST)
177 # TODO : Change ALLOWED_HOSTS depending if you are using docker with windows (the domain is "localhost") or docker with another OS (the domain is "0.0.0.0"). If you are running PyROS without Docker, the domain is "localhost". 179 # TODO : Change ALLOWED_HOSTS depending if you are using docker with windows (the domain is "localhost") or docker with another OS (the domain is "0.0.0.0"). If you are running PyROS without Docker, the domain is "localhost".
178 DEFAULT_DOMAIN = f'{ALLOWED_HOSTS[0]}{HTTP_PORT}' 180 DEFAULT_DOMAIN = f'{ALLOWED_HOSTS[0]}{HTTP_PORT}'
179 181
@@ -288,6 +290,13 @@ so Django highly recommends activating a strict mode for MySQL to prevent data l @@ -288,6 +290,13 @@ so Django highly recommends activating a strict mode for MySQL to prevent data l
288 (either STRICT_TRANS_TABLES or STRICT_ALL_TABLES) 290 (either STRICT_TRANS_TABLES or STRICT_ALL_TABLES)
289 ''' 291 '''
290 mysql_options = { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" } 292 mysql_options = { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" }
  293 +from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
  294 +import socket
  295 +obsconfig = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"])
  296 +database_computer = obsconfig.get_database_for_unit(obsconfig.unit_name)["computer"]
  297 +database_file = obsconfig.get_database_environment(obsconfig.unit_name)
  298 +current_computer = socket.gethostname()
  299 +set_environment_variables_if_not_configured(database_file, None)
291 300
292 # DEFAULT (NORMAL) RUN MODE, use pyros (normal) database 301 # DEFAULT (NORMAL) RUN MODE, use pyros (normal) database
293 if MYSQL: 302 if MYSQL:
@@ -317,6 +326,9 @@ if MYSQL: @@ -317,6 +326,9 @@ if MYSQL:
317 DATABASES['default']['PORT'] = '3306' 326 DATABASES['default']['PORT'] = '3306'
318 else: 327 else:
319 DATABASES['default']['HOST'] = "localhost" 328 DATABASES['default']['HOST'] = "localhost"
  329 + if current_computer != database_computer:
  330 + DATABASES['default']['HOST'] = os.environ["DATABASE_HOST"]
  331 + DATABASES['default']['PORT'] = os.environ["DATABASE_PORT"]
320 else: 332 else:
321 DATABASES = { 333 DATABASES = {
322 'default': { 334 'default': {