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
CHANGELOG
  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 6 29-08-2022 (AKo): v0.5.2.1
2 7 - Adding colors for recieved and sended commands of an agent.
3 8 - Renabling print when pulling git repo of guitastro
... ...
docker/docker-compose.yml
... ... @@ -16,7 +16,9 @@ services:
16 16 - db:/var/lib/mysql/
17 17 # create network to allow images to communicate with other images within the same network
18 18 networks:
19   - - pyros-network
  19 + pyros-network:
  20 + ipv4_address: 172.19.0.2
  21 +
20 22 healthcheck:
21 23 test: ["CMD", 'mysqladmin', 'ping', '-h', 'db', '-u', 'root', '-p$$MYSQL_ROOT_PASSWORD' ]
22 24 timeout: 10s
... ... @@ -61,7 +63,8 @@ services:
61 63 - db
62 64 # create network to allow images to communicate with other images within the same network
63 65 networks:
64   - - pyros-network
  66 + pyros-network:
  67 + ipv4_address: 172.19.0.3
65 68 #ipv4_address: "${IP_PYROS_USER}"
66 69 restart: always
67 70 phpmyadmin:
... ... @@ -75,7 +78,8 @@ services:
75 78 PMA_ARBITRARY: 1
76 79 restart: always
77 80 networks:
78   - - pyros-network
  81 + pyros-network:
  82 + ipv4_address: 172.19.0.4
79 83 ports:
80 84 - 8081:80
81 85  
... ... @@ -88,3 +92,7 @@ networks:
88 92 pyros-network:
89 93 #bridge is the default network driver
90 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 1 \ No newline at end of file
  2 +DATABASE_HOST=172.19.0.2
  3 +DATABASE_PORT=3306
3 4 \ No newline at end of file
... ...
privatedev/config/guitalens/.database.env
1   -DATABASE_HOST=localhost
2 1 \ No newline at end of file
  2 +DATABASE_HOST=172.19.0.2
  3 +DATABASE_PORT=3306
3 4 \ No newline at end of file
... ...
privatedev/config/tnc/.database.env
1   -DATABASE_HOST=localhost
2   -DATABASE_PORT=3360
3 1 \ No newline at end of file
  2 +DATABASE_HOST=172.19.0.2
  3 +DATABASE_PORT=3306
4 4 \ No newline at end of file
... ...
pyros.py
... ... @@ -810,7 +810,7 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos
810 810 # TODO: "python -m pip" au lieu de "pip"
811 811 pip = "pip" if IS_WINDOWS else "pip3"
812 812 process = subprocess.Popen(
813   - pip + " install --user --upgrade pykwalify", shell=True)
  813 + pip + " install --upgrade pykwalify", shell=True)
814 814 process.wait()
815 815 if process.returncode == 0:
816 816 # self.addExecuted(self.current_command, command)
... ... @@ -914,7 +914,7 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos
914 914  
915 915 # Agent "webserver"
916 916 if agent_name == "webserver":
917   - cmd = "manage.py runserver"
  917 + cmd = "manage.py runserver 127.0.0.1:8080"
918 918 if(WITH_DOCKER):
919 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 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 218 reciever = request.POST.get("agent_name")
219 219 cmd_name = request.POST.get("cmd_name")
220 220 cmd_args = request.POST.get("cmd_args")
221   - print(request.POST)
222 221 new_cmd = AgentCmd.send_cmd_from_to("Operator",reciever,cmd_name,cmd_args)
223   - print(new_cmd)
224 222 if new_cmd != None:
225 223 messages.add_message(request, messages.INFO, f"Command sended !")
226 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 110 raise BaseException()
111 111 os.environ[key] = value
112 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 124 # BASE_DIR is an absolute path to django folder
124 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 151 WITH_DOCKER = True
151 152 else :
152 153 WITH_DOCKER = False
153   -HTTP_PORT = ""
  154 +HTTP_PORT = "8080"
154 155 ENV_PATH = os.path.join(BASE_DIR,"../../../docker/variables.env")
155 156 ENV_SAMPLE_PATH = os.path.join(BASE_DIR,"../../../docker/.env-sample")
156 157 # default value of mysql port
... ... @@ -158,15 +159,8 @@ MYSQL_PORT = "3306"
158 159 SQL_USER = ""
159 160 SQL_PWD = ""
160 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 162 try:
  163 + HOST = os.environ["HOST_NAME"]
170 164 MYSQL_PORT = os.environ['MYSQL_TCP_PORT'].strip()
171 165 SQL_USER = os.environ["MYSQL_PYROS_LOGIN"].strip()
172 166 SQL_PWD = os.environ["MYSQL_PYROS_PWD"].strip()
... ... @@ -174,6 +168,14 @@ try:
174 168  
175 169 except:
176 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 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 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 290 (either STRICT_TRANS_TABLES or STRICT_ALL_TABLES)
289 291 '''
290 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 301 # DEFAULT (NORMAL) RUN MODE, use pyros (normal) database
293 302 if MYSQL:
... ... @@ -317,6 +326,9 @@ if MYSQL:
317 326 DATABASES['default']['PORT'] = '3306'
318 327 else:
319 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 332 else:
321 333 DATABASES = {
322 334 'default': {
... ...