@@ -76,6 +76,33 @@ def _container_pull_image(self):
7676 # end if result
7777 self .Pd (f"Image { full_ref } pulled successfully: { result .decode ('utf-8' , errors = 'ignore' )} " , score = 30 )
7878 return pulled
79+
80+
81+ def _get_default_env_vars (self ):
82+ """
83+ Get the default environment variables for the container.
84+
85+ WARNING: This is a critical method that should be thoroughly reviewed for attack vectors.
86+
87+ Returns:
88+ dict: Default environment variables.
89+ """
90+ localhost_ip = self .log .get_localhost_ip ()
91+ chainstore_peers = getattr (self , 'cfg_chainstore_peers' , [])
92+ str_chainstore_peers = self .json_dumps (chainstore_peers )
93+ dct_env = {
94+ "CONTAINER_NAME" : self .container_name ,
95+ "EE_CONTAINER_NAME" : self .container_name ,
96+ "EE_HOST_IP" : localhost_ip ,
97+ "EE_HOST_ID" : self .ee_id ,
98+ "EE_HOST_ADDR" : self .ee_addr ,
99+ "EE_HOST_ETH_ADDR" : self .bc .eth_address ,
100+ "EE_CHAINSTORE_API_URL" : f"http://{ localhost_ip } :31234" ,
101+ "EE_R1FS_API_URL" : f"http://{ localhost_ip } :31235" ,
102+ "EE_CHAINSTORE_PEERS" : str_chainstore_peers ,
103+ }
104+
105+ return dct_env
79106
80107
81108 def _get_container_run_command (self ):
@@ -110,18 +137,10 @@ def _get_container_run_command(self):
110137
111138 for key , val in self .dynamic_env .items ():
112139 cmd += ["-e" , f"{ key } ={ val } " ]
113-
114- cmd += ["-e" , f"CONTAINER_NAME={ self .container_name } " ]
115-
116- # TODO: check if this is a potential security issue (host is a container itself but we need to make sure)
117- host_ip = self ._setup_dynamic_env_var_host_ip ()
118- cmd += ["-e" , f"EE_HOST_IP={ host_ip } " ]
119- cmd += ["-e" , f"EE_CHAINSTORE_API_URL=http://{ self ._setup_dynamic_env_var_host_ip ()} :31234" ]
120- cmd += ["-e" , f"EE_R1FS_API_URL=http://{ self ._setup_dynamic_env_var_host_ip ()} :31235" ]
121-
122- chainstore_peers = getattr (self , 'cfg_chainstore_peers' , [])
123- cmd += ["-e" , f"EE_CHAINSTORE_PEERS='{ self .json_dumps (chainstore_peers )} '" ]
124140
141+ # now add the default env vars
142+ for key , val in self ._get_default_env_vars ().items ():
143+ cmd += ["-e" , f"{ key } ={ val } " ]
125144
126145 # Volume mounts
127146 if len (self .volumes ) > 0 :
@@ -243,15 +262,21 @@ def _restart_container(self):
243262 self ._reload_server ()
244263 self .container_id = None
245264 self .container_start_time = self .time () # Reset the start time after restart
265+ return
246266
247267 def _maybe_set_container_id_and_show_app_info (self ):
248268 if self .container_id is None :
269+ # this is the first time we are starting the container, so we need to get its ID
249270 container_id = self ._get_container_id ()
250271 if container_id :
251272 self .container_id = container_id
252273 self .P (f"Container ID set to: { self .container_id } " )
274+ self .on_post_container_start () # Call the lifecycle hoo
253275 self ._maybe_send_plugin_start_confirmation ()
254276 self ._show_container_app_info ()
277+ #endif
278+ #endif
279+ return
255280
256281 def _maybe_send_plugin_start_confirmation (self ):
257282 """
@@ -352,4 +377,24 @@ def _show_container_app_info(self):
352377 msg += f" CLI Tool: { self .cli_tool } \n "
353378 self .P (msg )
354379 return
355- ## END CONTAINER MIXIN ###
380+
381+
382+ def _run_command_in_container (self , command ):
383+ """
384+ Run a command inside the container.
385+
386+ Args:
387+ command (str): The command to run inside the container.
388+ """
389+ if not self .container_id :
390+ self .P ("Container ID is not set. Cannot run command." )
391+ return
392+
393+ cmd = [self .cli_tool , "exec" , "-i" , self .container_id ] + command .split ()
394+ try :
395+ result = subprocess .run (cmd , capture_output = True , text = True , check = True )
396+ self .P (f"Command output: { result .stdout } " )
397+ except subprocess .CalledProcessError as e :
398+ self .P (f"Error running command in container: { e .stderr } " , color = 'r' )
399+
400+ ## END CONTAINER MIXIN ###
0 commit comments