|
7 | 7 | from collections.abc import Iterator |
8 | 8 | from time import sleep |
9 | 9 | from typing import Dict, List |
| 10 | +from urllib.parse import urlparse |
10 | 11 |
|
11 | 12 | import conffwk |
12 | 13 | import grpc |
|
49 | 50 | resolve_localhost_and_127_ip_to_network_ip, |
50 | 51 | resolve_localhost_to_hostname, |
51 | 52 | strip_non_drunc_loggers, |
| 53 | + touch_and_chmod, |
52 | 54 | ) |
53 | 55 |
|
54 | 56 |
|
@@ -141,6 +143,9 @@ def boot( |
141 | 143 | # Step 3 - check for port conflicts and update configuration/DAL as needed |
142 | 144 | db, session_dal = self.check_port_conflicts(db, session_dal) |
143 | 145 |
|
| 146 | + # step 3.5 update localhost mapping |
| 147 | + session_dal = self.resolve_localhost(session_dal) |
| 148 | + |
144 | 149 | # Step 4 - connect to the connection service |
145 | 150 | csc, connection_server, connection_port = self._connect_to_service( |
146 | 151 | session_dal, session_name |
@@ -190,6 +195,24 @@ def boot( |
190 | 195 | previous_host = this_host |
191 | 196 | last_boot_on_host_at[this_host] = time.time() |
192 | 197 |
|
| 198 | + # ensures users can access the opmon files (permissions) |
| 199 | + # This is for the opmon files of the apps |
| 200 | + |
| 201 | + if session_dal.opmon_uri.type == "file": |
| 202 | + # For future, this should probably be taken from the metadata |
| 203 | + opmon_file = ( |
| 204 | + f"{request.process_description.process_execution_directory}/info." |
| 205 | + + request.process_description.metadata.session |
| 206 | + + "." |
| 207 | + + request.process_description.metadata.name |
| 208 | + + ".json" |
| 209 | + ) |
| 210 | + |
| 211 | + self.log.debug( |
| 212 | + f"Touching and changing permissions for {opmon_file} because opmon is of type {session_dal.opmon_uri.type}" |
| 213 | + ) |
| 214 | + touch_and_chmod(opmon_file) |
| 215 | + |
193 | 216 | try: |
194 | 217 | response = self.stub.boot(request, timeout=timeout) |
195 | 218 | yield response |
@@ -287,7 +310,11 @@ def _build_boot_request( |
287 | 310 | override_logs: bool, |
288 | 311 | pwd: str, |
289 | 312 | ) -> BootRequest: |
290 | | - host = format_hostname(app["restriction"]) |
| 313 | + # Run mapping to physical hostname to enable multi host usage |
| 314 | + host = resolve_localhost_to_hostname(format_hostname(app["restriction"])) |
| 315 | + self.log.info(f"boot resolve {host}") # keep this until big PR gets merged |
| 316 | + |
| 317 | + # this is one of the two minimal changes needed to get this working in general? |
291 | 318 | name = app["name"] |
292 | 319 | exe = app["type"] |
293 | 320 | args = app["args"] |
@@ -403,19 +430,29 @@ def _consolidate_config(self, session_name, conf_file: str) -> str | None: |
403 | 430 | ) |
404 | 431 | return |
405 | 432 |
|
406 | | - def update_connectivity_port_dal( |
407 | | - self, |
408 | | - env_variables: list["conffwk.dal.Variable | conffwk.dal.VariableSet"], |
409 | | - new_port: int, |
410 | | - ) -> None: |
411 | | - """Process a dal::Variable object, placing key/value pairs in a dictionary""" |
412 | | - for item in env_variables: |
413 | | - if item.className() == "VariableSet": |
414 | | - self.update_connectivity_port_dal(item.contains, new_port) |
415 | | - else: |
416 | | - if item.className() == "Variable": |
417 | | - if item.name == "CONNECTION_PORT": |
418 | | - item.value = new_port |
| 433 | + def resolve_localhost(self, session_dal): |
| 434 | + def dal_localhost_mapping(dal_host: str): |
| 435 | + if dal_host != "localhost": |
| 436 | + return dal_host |
| 437 | + |
| 438 | + resolved_address = resolve_localhost_to_hostname(dal_host) |
| 439 | + if "://" not in resolved_address: |
| 440 | + resolved_address = "grpc://" + resolved_address |
| 441 | + |
| 442 | + resolved_server = urlparse(resolved_address).hostname |
| 443 | + self.log.debug( |
| 444 | + f"Resolved connection server 'localhost' to '{resolved_server}' to avoid K8s hairpinning." |
| 445 | + ) |
| 446 | + return resolved_server |
| 447 | + |
| 448 | + session_dal.connectivity_service.host = dal_localhost_mapping( |
| 449 | + session_dal.connectivity_service.host |
| 450 | + ) |
| 451 | + session_dal.segment.controller.runs_on.runs_on.id = dal_localhost_mapping( |
| 452 | + session_dal.segment.controller.runs_on.runs_on.id |
| 453 | + ) |
| 454 | + |
| 455 | + return session_dal |
419 | 456 |
|
420 | 457 | def check_port_conflicts( |
421 | 458 | self, db: conffwk.Configuration, session_dal: "conffwk.dal.Session" |
@@ -545,13 +582,6 @@ def _connect_to_service( |
545 | 582 | connection_server = session_dal.connectivity_service.host |
546 | 583 | connection_port = session_dal.connectivity_service.service.port |
547 | 584 |
|
548 | | - if connection_server == "localhost": |
549 | | - resolved_server = resolve_localhost_to_hostname(connection_server) |
550 | | - self.log.debug( |
551 | | - f"Resolved connection server 'localhost' to '{resolved_server}' to avoid K8s hairpinning." |
552 | | - ) |
553 | | - connection_server = resolved_server |
554 | | - |
555 | 585 | client = ConnectivityServiceClient( |
556 | 586 | session_name, f"{connection_server}:{connection_port}" |
557 | 587 | ) |
|
0 commit comments