@@ -43,7 +43,8 @@ pub struct Podman {
43
43
env : Vec < ( String , String ) > ,
44
44
port_mappings : Vec < ( Option < u16 > , u16 ) > ,
45
45
container : Container ,
46
- stopped : bool ,
46
+ // `None` means that the container hasn't been created yet.
47
+ is_running : Option < bool > ,
47
48
}
48
49
49
50
impl Podman {
@@ -57,7 +58,7 @@ impl Podman {
57
58
env : Vec :: new ( ) ,
58
59
port_mappings : Vec :: new ( ) ,
59
60
container,
60
- stopped : false ,
61
+ is_running : None ,
61
62
}
62
63
}
63
64
@@ -76,7 +77,6 @@ impl Podman {
76
77
let mut command = std:: process:: Command :: new ( "podman" ) ;
77
78
command. arg ( "run" ) ;
78
79
command. arg ( "--detach" ) ;
79
- command. arg ( "--rm" ) ;
80
80
command. arg ( "--name" ) ;
81
81
command. arg ( & self . name ) ;
82
82
for ( key, value) in & self . env {
@@ -91,18 +91,8 @@ impl Podman {
91
91
} ;
92
92
}
93
93
command. arg ( self . container . container_name ( ) ) ;
94
- logging:: log:: debug!(
95
- "Podman run command args: {:?}" ,
96
- command. get_args( ) . map( |s| s. to_string_lossy( ) ) . collect:: <Vec <_>>( ) . join( " " )
97
- ) ;
98
- let output = command. output ( ) . unwrap ( ) ;
99
- assert ! (
100
- output. status. success( ) ,
101
- "Failed to run podman command: {:?}\n {}" ,
102
- command,
103
- String :: from_utf8_lossy( & output. stderr)
104
- ) ;
105
- self . stopped = false ;
94
+ Self :: run_command ( command) ;
95
+ self . is_running = Some ( true ) ;
106
96
}
107
97
108
98
pub fn get_port_mapping ( & self , container_port : u16 ) -> Option < u16 > {
@@ -111,17 +101,7 @@ impl Podman {
111
101
command. arg ( & self . name ) ;
112
102
command. arg ( format ! ( "{}" , container_port) ) ;
113
103
114
- let output = command. output ( ) . unwrap ( ) ;
115
- logging:: log:: debug!(
116
- "Podman ports command args: {:?}" ,
117
- command. get_args( ) . map( |s| s. to_string_lossy( ) ) . collect:: <Vec <_>>( ) . join( " " )
118
- ) ;
119
- assert ! (
120
- output. status. success( ) ,
121
- "Failed to run podman command: {:?}\n {}" ,
122
- command,
123
- String :: from_utf8_lossy( & output. stderr)
124
- ) ;
104
+ let output = Self :: run_command ( command) ;
125
105
let stdout = String :: from_utf8 ( output. stdout ) . unwrap ( ) ;
126
106
let line = stdout. lines ( ) . next ( ) ?;
127
107
let parts = line. split ( ':' ) . collect :: < Vec < & str > > ( ) ;
@@ -138,36 +118,28 @@ impl Podman {
138
118
let mut command = std:: process:: Command :: new ( "podman" ) ;
139
119
command. arg ( "stop" ) ;
140
120
command. arg ( & self . name ) ;
141
- let output = command . output ( ) . unwrap ( ) ;
142
- logging :: log :: debug! (
143
- "Podman stop command args: {:?}" ,
144
- command . get_args ( ) . map ( |s| s . to_string_lossy ( ) ) . collect :: < Vec <_>> ( ) . join ( " " )
145
- ) ;
121
+ Self :: run_command ( command ) ;
122
+ self . is_running = Some ( false ) ;
123
+ }
124
+
125
+ pub fn restart ( & mut self ) {
146
126
assert ! (
147
- output. status. success( ) ,
148
- "Failed to run podman command: {:?}\n {}" ,
149
- command,
150
- String :: from_utf8_lossy( & output. stderr)
127
+ self . is_running == Some ( false ) ,
128
+ "The container must have been created and stopped before it can be restarted"
151
129
) ;
152
- self . stopped = true ;
130
+ let mut command = std:: process:: Command :: new ( "podman" ) ;
131
+ command. arg ( "start" ) ;
132
+ command. arg ( & self . name ) ;
133
+ Self :: run_command ( command) ;
134
+ self . is_running = Some ( true ) ;
153
135
}
154
136
155
137
/// Uses the command `podman logs` to print the logs of the container.
156
138
pub fn print_logs ( & mut self ) {
157
139
let mut command = std:: process:: Command :: new ( "podman" ) ;
158
140
command. arg ( "logs" ) ;
159
141
command. arg ( & self . name ) ;
160
- let output = command. output ( ) . unwrap ( ) ;
161
- logging:: log:: debug!(
162
- "Podman logs command args: {:?}" ,
163
- command. get_args( ) . map( |s| s. to_string_lossy( ) ) . collect:: <Vec <_>>( ) . join( " " )
164
- ) ;
165
- assert ! (
166
- output. status. success( ) ,
167
- "Failed to run podman command: {:?}\n {}" ,
168
- command,
169
- String :: from_utf8_lossy( & output. stderr)
170
- ) ;
142
+ let output = Self :: run_command ( command) ;
171
143
172
144
{
173
145
let mut logs = String :: new ( ) ;
@@ -192,10 +164,35 @@ impl Podman {
192
164
}
193
165
}
194
166
167
+ fn run_command ( mut command : std:: process:: Command ) -> std:: process:: Output {
168
+ let output = command. output ( ) . unwrap ( ) ;
169
+ logging:: log:: debug!(
170
+ "Podman command args: {:?}" ,
171
+ command. get_args( ) . map( |s| s. to_string_lossy( ) ) . collect:: <Vec <_>>( ) . join( " " )
172
+ ) ;
173
+ assert ! (
174
+ output. status. success( ) ,
175
+ "Failed to run podman command: {:?}\n {}" ,
176
+ command,
177
+ String :: from_utf8_lossy( & output. stderr)
178
+ ) ;
179
+ output
180
+ }
181
+
182
+ fn remove_container ( & mut self ) {
183
+ let mut command = std:: process:: Command :: new ( "podman" ) ;
184
+ command. arg ( "rm" ) ;
185
+ command. arg ( & self . name ) ;
186
+ Self :: run_command ( command) ;
187
+ }
188
+
195
189
fn destructor ( & mut self ) {
196
- self . print_logs ( ) ;
197
- if !self . stopped {
198
- self . stop ( ) ;
190
+ if let Some ( is_running) = self . is_running {
191
+ self . print_logs ( ) ;
192
+ if is_running {
193
+ self . stop ( ) ;
194
+ }
195
+ self . remove_container ( ) ;
199
196
}
200
197
}
201
198
0 commit comments