@@ -17,10 +17,11 @@ def __init__(self, args):
1717 "run" ,
1818 "--rm" ,
1919 ]
20- self .use_docker = os .path .basename (args .engine ) == "docker"
21- self .use_podman = os .path .basename (args .engine ) == "podman"
20+ base = os .path .basename (args .engine )
21+ self .use_docker = base == "docker"
22+ self .use_podman = base == "podman"
2223 self .args = args
23- self .add_container_labels ()
24+ self .add_labels ()
2425 self .add_device_options ()
2526 self .add_env_option ()
2627 self .add_network ()
@@ -38,19 +39,11 @@ def __init__(self, args):
3839 def add_label (self , label ):
3940 self .add (["--label" , label ])
4041
41- def add_container_labels (self ):
42- label_map = {
43- "MODEL" : "ai.ramalama.model" ,
44- "engine" : "ai.ramalama.engine" ,
45- "runtime" : "ai.ramalama.runtime" ,
46- "port" : "ai.ramalama.port" ,
47- "subcommand" : "ai.ramalama.command" ,
48- }
49- for arg , label_prefix in label_map .items ():
50- if hasattr (self .args , arg ):
51- value = getattr (self .args , arg )
52- if value :
53- self .add_label (f"{ label_prefix } ={ value } " )
42+ def add_name (self , name ):
43+ self .add (["--name" , name ])
44+
45+ def add_labels (self ):
46+ add_labels (self .args , self .add_label )
5447
5548 def add_pull_newer (self ):
5649 if not self .args .dryrun and self .use_docker and self .args .pull == "newer" :
@@ -90,6 +83,9 @@ def add_privileged_options(self):
9083 "--security-opt=no-new-privileges" ,
9184 ]
9285
86+ def cap_add (self , cap ):
87+ self .exec_args += ["--cap-add" , cap ]
88+
9389 def add_subcommand_env (self ):
9490 if EMOJI and hasattr (self .args , "subcommand" ) and self .args .subcommand == "run" :
9591 if os .path .basename (self .args .engine ) == "podman" :
@@ -111,7 +107,12 @@ def add_detach_option(self):
111107 self .exec_args += ["-d" ]
112108
113109 def add_port_option (self ):
114- if hasattr (self .args , "port" ):
110+ if not hasattr (self .args , "port" ) or not self .args .port or self .args .port == "" :
111+ return
112+
113+ if self .args .port .count (":" ) > 0 :
114+ self .exec_args += ["-p" , self .args .port ]
115+ else :
115116 self .exec_args += ["-p" , f"{ self .args .port } :{ self .args .port } " ]
116117
117118 def add_device_options (self ):
@@ -243,26 +244,79 @@ def info(args):
243244 return str (e )
244245
245246
246- def stop_container (args , name ):
247+ def inspect (args , name , format = None , ignore_stderr = False ):
247248 if not name :
248249 raise ValueError ("must specify a container name" )
249250 conman = args .engine
250251 if conman == "" :
251252 raise ValueError ("no container manager (Podman, Docker) found" )
252253
253- conman_args = [conman , "stop" , "-t=0" ]
254- ignore_stderr = False
255- if args .ignore :
256- if conman == "podman" :
257- conman_args += ["--ignore" , str (args .ignore )]
258- else :
259- ignore_stderr = True
254+ conman_args = [conman , "inspect" ]
255+ if format :
256+ conman_args += ["--format" , format ]
260257
261258 conman_args += [name ]
259+ return run_cmd (conman_args , ignore_stderr = ignore_stderr , debug = args .debug ).stdout .decode ("utf-8" ).strip ()
260+
261+
262+ def stop_container (args , name ):
263+ if not name :
264+ raise ValueError ("must specify a container name" )
265+ conman = args .engine
266+ if conman == "" :
267+ raise ValueError ("no container manager (Podman, Docker) found" )
268+
269+ ignore_stderr = False
270+ pod = ""
271+ try :
272+ pod = inspect (args , name , format = "{{ .Pod }}" , ignore_stderr = True )
273+ except Exception : # Ignore errors, the stop command will handle it.
274+ pass
275+
276+ if pod != "" :
277+ conman_args = [conman , "pod" , "rm" , "-t=0" , "--ignore" , "--force" , pod ]
278+ else :
279+ conman_args = [conman , "stop" , "-t=0" ]
280+ if args .ignore :
281+ if conman == "podman" :
282+ conman_args += ["--ignore" , str (args .ignore )]
283+ else :
284+ ignore_stderr = True
285+
286+ conman_args += [name ]
262287 try :
263288 run_cmd (conman_args , ignore_stderr = ignore_stderr , debug = args .debug )
264289 except subprocess .CalledProcessError :
265290 if args .ignore and conman == "docker" :
266291 return
267292 else :
268293 raise
294+
295+
296+ def container_connection (args , name , port ):
297+ if not name :
298+ raise ValueError ("must specify a container name" )
299+ if not port :
300+ raise ValueError ("must specify a port to check" )
301+
302+ conman = args .engine
303+ if conman == "" :
304+ raise ValueError ("no container manager (Podman, Docker) found" )
305+
306+ conman_args = [conman , "port" , name , port ]
307+ output = run_cmd (conman_args , debug = args .debug ).stdout .decode ("utf-8" ).strip ()
308+ return "" if output == "" else output .split (">" )[- 1 ].strip ()
309+
310+
311+ def add_labels (args , add_label ):
312+ label_map = {
313+ "MODEL" : "ai.ramalama.model" ,
314+ "engine" : "ai.ramalama.engine" ,
315+ "runtime" : "ai.ramalama.runtime" ,
316+ "port" : "ai.ramalama.port" ,
317+ "subcommand" : "ai.ramalama.command" ,
318+ }
319+ for arg , label_prefix in label_map .items ():
320+ if hasattr (args , arg ):
321+ if value := getattr (args , arg ):
322+ add_label (f"{ label_prefix } ={ value } " )
0 commit comments