2424
2525import time
2626
27- from podman . tests import errors
27+ from podman import PodmanCommand
2828
2929logger = logging .getLogger ("podman.service" )
3030
@@ -41,54 +41,27 @@ def __init__(
4141 log_level : str = "WARNING" ,
4242 ) -> None :
4343 """create a launcher and build podman command"""
44- podman_exe : str = podman_path
45- if not podman_exe :
46- podman_exe = shutil .which ('podman' )
47- if podman_exe is None :
48- raise errors .PodmanNotInstalled ()
44+ self .podman = PodmanCommand (path = podman_path , privileged = privileged )
4945
46+ self .timeout = timeout
47+ self .socket_uri : str = socket_uri
5048 self .socket_file : str = socket_uri .replace ('unix://' , '' )
5149 self .log_level = log_level
5250
5351 self .proc : Optional [subprocess .Popen [bytes ]] = None
5452 self .reference_id = hash (time .monotonic ())
5553
56- self .cmd : list [str ] = []
57- if privileged :
58- self .cmd .append ('sudo' )
59-
60- self .cmd .append (podman_exe )
61-
6254 logger .setLevel (logging .getLevelName (log_level ))
6355
6456 # Map from python to go logging levels, FYI trace level breaks cirrus logging
65- self .cmd .append (f"--log-level={ log_level .lower ()} " )
66-
57+ self .podman .options .log_level = log_level .lower ()
6758 if os .environ .get ("container" ) == "oci" :
68- self .cmd .append ("--storage-driver=vfs" )
69-
70- self .cmd .extend (
71- [
72- "system" ,
73- "service" ,
74- f"--time={ timeout } " ,
75- socket_uri ,
76- ]
77- )
59+ self .podman .options .storage_driver = "vfs"
7860
79- process = subprocess .run (
80- [podman_exe , "--version" ], check = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT
81- )
82- self .version = str (process .stdout .decode ("utf-8" )).strip ().split ()[2 ]
61+ self .version = self .podman .run (["--version" ]).split ()[2 ]
8362
8463 def start (self , check_socket = True ) -> None :
8564 """start podman service"""
86- logger .info (
87- "Launching(%s) %s refid=%s" ,
88- self .version ,
89- ' ' .join (self .cmd ),
90- self .reference_id ,
91- )
9265
9366 def consume_lines (pipe , consume_fn ):
9467 with pipe :
@@ -98,32 +71,34 @@ def consume_lines(pipe, consume_fn):
9871 def consume (line : str ):
9972 logger .debug (line .strip ("\n " ) + f" refid={ self .reference_id } " )
10073
101- self .proc = subprocess .Popen (self .cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT ) # pylint: disable=consider-using-with
74+ self .proc = self .podman .start_service (
75+ self .socket_uri ,
76+ stdout = subprocess .PIPE ,
77+ stderr = subprocess .STDOUT ,
78+ )
79+
80+ logger .info (
81+ "Launched(%s) %s pid=%s refid=%s" ,
82+ self .version ,
83+ ' ' .join (self .proc .args ),
84+ self .proc .pid ,
85+ self .reference_id ,
86+ )
87+
10288 threading .Thread (target = consume_lines , args = [self .proc .stdout , consume ]).start ()
10389
10490 if not check_socket :
10591 return
10692
10793 # wait for socket to be created
108- timeout = time .monotonic () + 30
109- while not os .path .exists (self .socket_file ):
110- if time .monotonic () > timeout :
111- raise subprocess .TimeoutExpired ("podman service " , timeout )
112- time .sleep (0.2 )
94+ self .podman .wait_for_service (self .socket_uri , self .proc , timeout = 30 )
11395
11496 def stop (self ) -> None :
11597 """stop podman service"""
11698 if not self .proc :
11799 return
118100
119- self .proc .terminate ()
120- try :
121- return_code = self .proc .wait (timeout = 15 )
122- except subprocess .TimeoutExpired :
123- self .proc .kill ()
124- return_code = self .proc .wait ()
125- self .proc = None
126-
101+ return_code = self .podman .stop_service (self .proc , timeout = 15 )
127102 with suppress (FileNotFoundError ):
128103 os .remove (self .socket_file )
129104
0 commit comments