34
34
*/
35
35
36
36
use std:: {
37
+ collections:: HashMap ,
37
38
path:: { Path , PathBuf } ,
38
39
sync:: Arc ,
39
40
time:: Duration ,
@@ -203,64 +204,60 @@ impl GlobalOpts {
203
204
self . args ( )
204
205
}
205
206
206
- fn output ( & self ) -> Result < ( PathBuf , Vec < String > ) , Error > {
207
+ fn output ( & self ) -> Result < ( PathBuf , HashMap < String , String > ) , Error > {
207
208
let path = self
208
209
. command
209
210
. clone ( )
210
211
. unwrap_or_else ( || PathBuf :: from ( "runc" ) ) ;
211
212
212
213
let command = utils:: binary_path ( path) . ok_or ( Error :: NotFound ) ?;
213
-
214
- let mut args = Vec :: new ( ) ;
214
+ let mut global_args_map = HashMap :: < String , String > :: new ( ) ;
215
215
216
216
// --root path : Set the root directory to store containers' state.
217
217
if let Some ( root) = & self . root {
218
- args. push ( ROOT . into ( ) ) ;
219
- args. push ( utils:: abs_string ( root) ?) ;
218
+ global_args_map. insert ( ROOT . into ( ) , utils:: abs_string ( root) ?) ;
220
219
}
221
220
222
221
// --debug : Enable debug logging.
223
222
if self . debug {
224
- args . push ( DEBUG . into ( ) ) ;
223
+ global_args_map . insert ( DEBUG . into ( ) , String :: new ( ) ) ;
225
224
}
226
225
227
226
// --log path : Set the log destination to path. The default is to log to stderr.
228
227
if let Some ( log_path) = & self . log {
229
- args. push ( LOG . into ( ) ) ;
230
- args. push ( utils:: abs_string ( log_path) ?) ;
228
+ global_args_map. insert ( LOG . into ( ) , utils:: abs_string ( log_path) ?) ;
231
229
}
232
230
233
231
// --log-format text|json : Set the log format (default is text).
234
- args. push ( LOG_FORMAT . into ( ) ) ;
235
- args. push ( self . log_format . to_string ( ) ) ;
232
+ global_args_map. insert ( LOG_FORMAT . into ( ) , self . log_format . to_string ( ) ) ;
236
233
237
234
// --systemd-cgroup : Enable systemd cgroup support.
238
235
if self . systemd_cgroup {
239
- args . push ( SYSTEMD_CGROUP . into ( ) ) ;
236
+ global_args_map . insert ( SYSTEMD_CGROUP . into ( ) , String :: new ( ) ) ;
240
237
}
241
238
242
239
// --rootless true|false|auto : Enable or disable rootless mode.
243
240
if let Some ( mode) = self . rootless {
244
- let arg = format ! ( "{}={}" , ROOTLESS , mode) ;
245
- args. push ( arg) ;
241
+ global_args_map. insert ( ROOTLESS . to_string ( ) , mode. to_string ( ) ) ;
246
242
}
247
- Ok ( ( command, args ) )
243
+ Ok ( ( command, global_args_map ) )
248
244
}
249
245
}
250
246
251
247
impl Args for GlobalOpts {
252
248
type Output = Result < Runc , Error > ;
253
249
254
250
fn args ( & self ) -> Self :: Output {
255
- let ( command, args ) = self . output ( ) ?;
251
+ let ( command, client_global_args ) = self . output ( ) ?;
256
252
let executor = if let Some ( exec) = self . executor . clone ( ) {
257
253
exec
258
254
} else {
259
255
Arc :: new ( DefaultExecutor { } )
260
256
} ;
257
+
261
258
Ok ( Runc {
262
259
command,
263
- args ,
260
+ client_global_args ,
264
261
spawner : executor,
265
262
} )
266
263
}
@@ -353,6 +350,7 @@ impl CreateOpts {
353
350
#[ derive( Clone , Default ) ]
354
351
pub struct ExecOpts {
355
352
pub io : Option < Arc < dyn Io > > ,
353
+ pub global_args : HashMap < String , String > ,
356
354
/// Path to where a pid file should be created.
357
355
pub pid_file : Option < PathBuf > ,
358
356
/// Path to where a console socket should be created.
@@ -591,19 +589,30 @@ mod tests {
591
589
assert_eq ! ( KillOpts :: new( ) . all( true ) . args( ) , vec![ "--all" . to_string( ) ] , ) ;
592
590
}
593
591
592
+ #[ allow( dead_code) ]
593
+ fn map_to_vec ( map : HashMap < String , String > ) -> Vec < String > {
594
+ let mut args = Vec :: with_capacity ( map. len ( ) * 2 ) ;
595
+ for ( key, value) in map {
596
+ args. push ( key) ;
597
+ args. push ( value) ;
598
+ }
599
+ args
600
+ }
594
601
#[ cfg( target_os = "linux" ) ]
595
602
#[ test]
596
603
fn global_opts_test ( ) {
597
604
let cfg = GlobalOpts :: default ( ) . command ( "true" ) ;
598
605
let runc = cfg. build ( ) . unwrap ( ) ;
599
- let args = & runc. args ;
606
+ let args_map = & runc. client_global_args ;
607
+
608
+ let args = map_to_vec ( args_map. clone ( ) ) ;
600
609
assert_eq ! ( args. len( ) , 2 ) ;
601
610
assert ! ( args. contains( & LOG_FORMAT . to_string( ) ) ) ;
602
611
assert ! ( args. contains( & TEXT . to_string( ) ) ) ;
603
612
604
613
let cfg = GlobalOpts :: default ( ) . command ( "/bin/true" ) ;
605
614
let runc = cfg. build ( ) . unwrap ( ) ;
606
- assert_eq ! ( runc. args . len( ) , 2 ) ;
615
+ assert_eq ! ( map_to_vec ( runc. client_global_args ) . len( ) , 2 ) ;
607
616
608
617
let cfg = GlobalOpts :: default ( )
609
618
. command ( "true" )
@@ -614,16 +623,17 @@ mod tests {
614
623
. systemd_cgroup ( true )
615
624
. rootless ( true ) ;
616
625
let runc = cfg. build ( ) . unwrap ( ) ;
617
- let args = & runc. args ;
626
+ let args = map_to_vec ( runc. client_global_args ) ;
618
627
assert ! ( args. contains( & ROOT . to_string( ) ) ) ;
619
628
assert ! ( args. contains( & DEBUG . to_string( ) ) ) ;
620
629
assert ! ( args. contains( & "/tmp" . to_string( ) ) ) ;
621
630
assert ! ( args. contains( & LOG . to_string( ) ) ) ;
622
631
assert ! ( args. contains( & "/tmp/runc.log" . to_string( ) ) ) ;
623
632
assert ! ( args. contains( & LOG_FORMAT . to_string( ) ) ) ;
624
633
assert ! ( args. contains( & JSON . to_string( ) ) ) ;
625
- assert ! ( args. contains( & "--rootless=true" . to_string( ) ) ) ;
634
+
635
+ assert ! ( args. contains( & "--rootless" . to_string( ) ) ) ;
626
636
assert ! ( args. contains( & SYSTEMD_CGROUP . to_string( ) ) ) ;
627
- assert_eq ! ( args. len( ) , 9 ) ;
637
+ assert_eq ! ( args. len( ) , 12 ) ;
628
638
}
629
639
}
0 commit comments