24
24
25
25
import time
26
26
27
- from podman . tests import errors
27
+ from podman import PodmanCommand
28
28
29
29
logger = logging .getLogger ("podman.service" )
30
30
@@ -41,54 +41,27 @@ def __init__(
41
41
log_level : str = "WARNING" ,
42
42
) -> None :
43
43
"""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 )
49
45
46
+ self .timeout = timeout
47
+ self .socket_uri : str = socket_uri
50
48
self .socket_file : str = socket_uri .replace ('unix://' , '' )
51
49
self .log_level = log_level
52
50
53
51
self .proc : Optional [subprocess .Popen [bytes ]] = None
54
52
self .reference_id = hash (time .monotonic ())
55
53
56
- self .cmd : list [str ] = []
57
- if privileged :
58
- self .cmd .append ('sudo' )
59
-
60
- self .cmd .append (podman_exe )
61
-
62
54
logger .setLevel (logging .getLevelName (log_level ))
63
55
64
56
# 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 ()
67
58
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"
78
60
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 ]
83
62
84
63
def start (self , check_socket = True ) -> None :
85
64
"""start podman service"""
86
- logger .info (
87
- "Launching(%s) %s refid=%s" ,
88
- self .version ,
89
- ' ' .join (self .cmd ),
90
- self .reference_id ,
91
- )
92
65
93
66
def consume_lines (pipe , consume_fn ):
94
67
with pipe :
@@ -98,32 +71,35 @@ def consume_lines(pipe, consume_fn):
98
71
def consume (line : str ):
99
72
logger .debug (line .strip ("\n " ) + f" refid={ self .reference_id } " )
100
73
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
+ timeout = self .timeout ,
77
+ stdout = subprocess .PIPE ,
78
+ stderr = subprocess .STDOUT ,
79
+ )
80
+
81
+ logger .info (
82
+ "Launched(%s) %s pid=%s refid=%s" ,
83
+ self .version ,
84
+ ' ' .join (self .proc .args ),
85
+ self .proc .pid ,
86
+ self .reference_id ,
87
+ )
88
+
102
89
threading .Thread (target = consume_lines , args = [self .proc .stdout , consume ]).start ()
103
90
104
91
if not check_socket :
105
92
return
106
93
107
94
# 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 )
95
+ self .podman .wait_for_service (self .socket_uri , self .proc , timeout = 30 )
113
96
114
97
def stop (self ) -> None :
115
98
"""stop podman service"""
116
99
if not self .proc :
117
100
return
118
101
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
-
102
+ return_code = self .podman .stop_service (self .proc , timeout = 15 )
127
103
with suppress (FileNotFoundError ):
128
104
os .remove (self .socket_file )
129
105
0 commit comments