55import tempfile
66import time
77from collections .abc import Iterator
8+ from pprint import pprint
89from time import sleep
910from typing import Dict , List
1011from urllib .parse import urlparse
@@ -58,6 +59,18 @@ def touch_and_chmod(filepath, mode=0o777):
5859 os .utime (filepath , None )
5960 os .chmod (filepath , mode )
6061
62+
63+ def to_dict (obj ):
64+ if hasattr (obj , "__dict__" ):
65+ return {k : to_dict (v ) for k , v in obj .__dict__ .items ()}
66+ elif isinstance (obj , list ):
67+ return [to_dict (i ) for i in obj ]
68+ elif isinstance (obj , dict ):
69+ return {k : to_dict (v ) for k , v in obj .items ()}
70+ else :
71+ return obj
72+
73+
6174class ProcessManagerDriver :
6275 controller_address = ""
6376
@@ -145,8 +158,13 @@ def boot(
145158 db , session_dal = self ._initialise_session (conf_file , conf_id )
146159
147160 # Step 3 - check for port conflicts and update configuration/DAL as needed
161+ # Should remap the local host here actually
148162 db , session_dal = self .check_port_conflicts (db , session_dal )
149163
164+ # step 3.5 update localhost mapping
165+
166+ # session_dal = self.change_localhost(session_dal)
167+
150168 # Step 4 - connect to the connection service
151169 csc , connection_server , connection_port = self ._connect_to_service (
152170 session_dal , session_name
@@ -155,7 +173,7 @@ def boot(
155173 # Step 5 - track boot timings per host
156174 last_boot_on_host_at = {}
157175 previous_host = None
158-
176+ self . log . warning ( "step 6" )
159177 # Step 6: iterate over boot requests
160178 for request in self ._convert_oks_to_boot_request (
161179 oks_conf = conf_file ,
@@ -175,7 +193,7 @@ def boot(
175193 f"Skipping connectivity service readiness check for application { request .process_description .metadata .name } "
176194 )
177195 else :
178- self .log .debug (
196+ self .log .warning (
179197 f"Checking connectivity service readiness before booting application { request .process_description .metadata .name } "
180198 )
181199 if csc and not csc .is_ready (timeout = 10 ):
@@ -195,14 +213,35 @@ def boot(
195213
196214 previous_host = this_host
197215 last_boot_on_host_at [this_host ] = time .time ()
216+
217+ self .log .critical (request )
218+
219+ self .log .critical (
220+ f"info name: { request .process_description .metadata .name } , { request .process_description .metadata .session } , { request .process_description .process_execution_directory } "
221+ )
222+
198223 newfile = (
199224 f"{ request .process_description .process_execution_directory } /info."
200225 + request .process_description .metadata .session
201226 + "."
202227 + request .process_description .metadata .name
203228 + ".json"
204229 )
230+
231+ self .log .critical (f"{ newfile = } " )
232+
205233 touch_and_chmod (newfile )
234+ # touch_and_chmod(request.process_description.process_logs_path)
235+
236+ # we should also do the same for the log file if necessary
237+
238+ # although the CD should keep us all safe
239+
240+ # touch this and make this easily accessible
241+
242+ # BefORE YOU TRY BOOT PARSE OUT THE CD PATH AND PING IT
243+
244+ # ah wait you need to do it as the ssh-ee
206245
207246 try :
208247 response = self .stub .boot (request , timeout = timeout )
@@ -220,6 +259,7 @@ def boot(
220259 handle_grpc_error (e )
221260
222261 # Step 7: discover segment root controller
262+ # maybe the problem is here?
223263 self ._discover_controller (
224264 session_dal , session_name , csc , connection_server , connection_port
225265 )
@@ -253,7 +293,8 @@ def _collect_all_apps(
253293
254294 apps = infra_apps + apps
255295
256- self .log .debug (f"{ json .dumps (apps , indent = 4 )} " )
296+ # self.log.warning("json dumps")
297+ # self.log.debug(f"{json.dumps(apps, indent=4)}")
257298
258299 return apps
259300
@@ -362,6 +403,8 @@ def _build_boot_request(
362403 process_restriction = process_restriction ,
363404 )
364405 self .log .debug (f"{ breq = } \n \n " )
406+
407+ # boot_request.process_description.metadata.name
365408 return breq
366409
367410 def _convert_oks_to_boot_request (
@@ -421,7 +464,7 @@ def _consolidate_config(self, session_name, conf_file: str) -> str | None:
421464 )
422465 return
423466
424- def update_connectivity_port_dal (
467+ def update_connectivity_port_dal ( # why is th9is hanging?
425468 self ,
426469 env_variables : list ["conffwk.dal.Variable | conffwk.dal.VariableSet" ],
427470 new_port : int ,
@@ -435,6 +478,28 @@ def update_connectivity_port_dal(
435478 if item .name == "CONNECTION_PORT" :
436479 item .value = new_port
437480
481+ def change_localhost (self , session_dal ):
482+ self .log .critical ("in session dalling" )
483+
484+ connectivity_service_host : str = session_dal .connectivity_service .host
485+ if connectivity_service_host == "localhost" :
486+ # resolve localhost to hostname to enable multi host
487+ resolved_address = resolve_localhost_to_hostname (connectivity_service_host )
488+ if "://" not in resolved_address :
489+ resolved_address = "grpc://" + resolved_address
490+
491+ resolved_server = urlparse (resolved_address ).hostname
492+ self .log .debug (
493+ f"Resolved connection server 'localhost' to '{ resolved_server } ' to avoid K8s hairpinning."
494+ )
495+ session_dal .connectivity_service .host = resolved_server
496+
497+ output = to_dict (session_dal .connectivity_service )
498+
499+ self .log .critical (pprint (output ))
500+
501+ return session_dal
502+
438503 def check_port_conflicts (
439504 self , db : conffwk .Configuration , session_dal : "conffwk.dal.Session"
440505 ) -> tuple [conffwk .Configuration , "conffwk.dal.Session" ]:
@@ -500,8 +565,13 @@ def check_port_conflicts(
500565 # Temporarily removed to allow integration tests to pass without restructuring
501566 # Note - if infrastructure applications outside of the connectivity service are spawned, this will need to be adjusted.
502567 if session_dal .infrastructure_applications : # Check if the own application needs to be spawned, or if an externally managed one is in use (e.g. if using ehn1 connectivity service or integration tests.)
568+ #! WE NEEED TO REPLACE THE CONNECTIVITY SERVICE HOST HERE
569+
503570 connectivity_service_host : str = session_dal .connectivity_service .host
504571 connectivity_service_port = session_dal .connectivity_service .service .port
572+ self .log .info (
573+ f"infrastructure apps: { connectivity_service_host = } , { connectivity_service_port = } "
574+ )
505575 if not is_port_available (
506576 connectivity_service_host , connectivity_service_port
507577 ):
@@ -608,14 +678,14 @@ def get_controller_address(session_dal, session_name):
608678
609679 # 1: Try dynamic lookup via Connectivity Service
610680 if csc :
611- self .log .debug (
681+ self .log .critical (
612682 f"Attempting to discover controller '{ top_controller_name } ' via connectivity service at { connection_server } :{ connection_port } "
613683 )
614684 try :
615685 timeout = (
616686 get_segment_lookup_timeout (session_dal .segment , 60 ) + 60
617687 ) # root-controller timeout to find all its children + 60s for the root controller to start itself
618- self .log .debug (
688+ self .log .critical (
619689 f"Using a timeout of { timeout } s to find the [green]{ top_controller_name } [/] on the connectivity service"
620690 )
621691 _ , uri = get_control_type_and_uri_from_connectivity_service (
@@ -628,7 +698,7 @@ def get_controller_address(session_dal, session_name):
628698 )
629699
630700 address = uri .replace ("grpc://" , "" )
631- self .log .debug (
701+ self .log .warning (
632702 f"Successfully discovered controller '{ top_controller_name } ' via connectivity service: { address } "
633703 )
634704 return address
0 commit comments