@@ -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 ()
@@ -37,19 +38,11 @@ def __init__(self, args):
3738 def add_label (self , label ):
3839 self .add (["--label" , label ])
3940
40- def add_container_labels (self ):
41- label_map = {
42- "MODEL" : "ai.ramalama.model" ,
43- "engine" : "ai.ramalama.engine" ,
44- "runtime" : "ai.ramalama.runtime" ,
45- "port" : "ai.ramalama.port" ,
46- "subcommand" : "ai.ramalama.command" ,
47- }
48- for arg , label_prefix in label_map .items ():
49- if hasattr (self .args , arg ):
50- value = getattr (self .args , arg )
51- if value :
52- self .add_label (f"{ label_prefix } ={ value } " )
41+ def add_name (self , name ):
42+ self .add (["--name" , name ])
43+
44+ def add_labels (self ):
45+ add_labels (self .args , self .add_label )
5346
5447 def add_pull_newer (self ):
5548 if not self .args .dryrun and self .use_docker and self .args .pull == "newer" :
@@ -89,6 +82,9 @@ def add_privileged_options(self):
8982 "--security-opt=no-new-privileges" ,
9083 ]
9184
85+ def cap_add (self , cap ):
86+ self .exec_args += ["--cap-add" , cap ]
87+
9288 def add_subcommand_env (self ):
9389 if EMOJI and hasattr (self .args , "subcommand" ) and self .args .subcommand == "run" :
9490 if os .path .basename (self .args .engine ) == "podman" :
@@ -110,7 +106,12 @@ def add_detach_option(self):
110106 self .exec_args += ["-d" ]
111107
112108 def add_port_option (self ):
113- if hasattr (self .args , "port" ):
109+ if not hasattr (self .args , "port" ) or not self .args .port or self .args .port == "" :
110+ return
111+
112+ if self .args .port .count (":" ) > 0 :
113+ self .exec_args += ["-p" , self .args .port ]
114+ else :
114115 self .exec_args += ["-p" , f"{ self .args .port } :{ self .args .port } " ]
115116
116117 def add_device_options (self ):
@@ -242,26 +243,79 @@ def info(args):
242243 return str (e )
243244
244245
245- def stop_container (args , name ):
246+ def inspect (args , name , format = None , ignore_stderr = False ):
246247 if not name :
247248 raise ValueError ("must specify a container name" )
248249 conman = args .engine
249250 if conman == "" :
250251 raise ValueError ("no container manager (Podman, Docker) found" )
251252
252- conman_args = [conman , "stop" , "-t=0" ]
253- ignore_stderr = False
254- if args .ignore :
255- if conman == "podman" :
256- conman_args += ["--ignore" , str (args .ignore )]
257- else :
258- ignore_stderr = True
253+ conman_args = [conman , "inspect" ]
254+ if format :
255+ conman_args += ["--format" , format ]
259256
260257 conman_args += [name ]
258+ return run_cmd (conman_args , ignore_stderr = ignore_stderr , debug = args .debug ).stdout .decode ("utf-8" ).strip ()
259+
260+
261+ def stop_container (args , name ):
262+ if not name :
263+ raise ValueError ("must specify a container name" )
264+ conman = args .engine
265+ if conman == "" :
266+ raise ValueError ("no container manager (Podman, Docker) found" )
267+
268+ ignore_stderr = False
269+ pod = ""
270+ try :
271+ pod = inspect (args , name , format = "{{ .Pod }}" , ignore_stderr = True )
272+ except Exception : # Ignore errors, the stop command will handle it.
273+ pass
274+
275+ if pod != "" :
276+ conman_args = [conman , "pod" , "rm" , "-t=0" , "--ignore" , "--force" , pod ]
277+ else :
278+ conman_args = [conman , "stop" , "-t=0" ]
279+ if args .ignore :
280+ if conman == "podman" :
281+ conman_args += ["--ignore" , str (args .ignore )]
282+ else :
283+ ignore_stderr = True
284+
285+ conman_args += [name ]
261286 try :
262287 run_cmd (conman_args , ignore_stderr = ignore_stderr )
263288 except subprocess .CalledProcessError :
264289 if args .ignore and conman == "docker" :
265290 return
266291 else :
267292 raise
293+
294+
295+ def container_connection (args , name , port ):
296+ if not name :
297+ raise ValueError ("must specify a container name" )
298+ if not port :
299+ raise ValueError ("must specify a port to check" )
300+
301+ conman = args .engine
302+ if conman == "" :
303+ raise ValueError ("no container manager (Podman, Docker) found" )
304+
305+ conman_args = [conman , "port" , name , port ]
306+ output = run_cmd (conman_args , debug = args .debug ).stdout .decode ("utf-8" ).strip ()
307+ return "" if output == "" else output .split (">" )[- 1 ].strip ()
308+
309+
310+ def add_labels (args , add_label ):
311+ label_map = {
312+ "MODEL" : "ai.ramalama.model" ,
313+ "engine" : "ai.ramalama.engine" ,
314+ "runtime" : "ai.ramalama.runtime" ,
315+ "port" : "ai.ramalama.port" ,
316+ "subcommand" : "ai.ramalama.command" ,
317+ }
318+ for arg , label_prefix in label_map .items ():
319+ if hasattr (args , arg ):
320+ if value := getattr (args , arg ):
321+ add_label (f"{ label_prefix } ={ value } " )
0 commit comments