@@ -236,7 +236,7 @@ def convert_port_bindings(port_bindings):
236
236
for k , v in six .iteritems (port_bindings ):
237
237
key = str (k )
238
238
if '/' not in key :
239
- key = key + '/tcp'
239
+ key += '/tcp'
240
240
if isinstance (v , list ):
241
241
result [key ] = [_convert_port_binding (binding ) for binding in v ]
242
242
else :
@@ -434,7 +434,7 @@ def parse_bytes(s):
434
434
s = 0
435
435
else :
436
436
if s [- 2 :- 1 ].isalpha () and s [- 1 ].isalpha ():
437
- if ( s [- 1 ] == "b" or s [- 1 ] == "B" ) :
437
+ if s [- 1 ] == "b" or s [- 1 ] == "B" :
438
438
s = s [:- 1 ]
439
439
units = BYTE_UNITS
440
440
suffix = s [- 1 ].lower ()
@@ -467,16 +467,32 @@ def parse_bytes(s):
467
467
return s
468
468
469
469
470
- def create_host_config (
471
- binds = None , port_bindings = None , lxc_conf = None , publish_all_ports = False ,
472
- links = None , privileged = False , dns = None , dns_search = None , volumes_from = None ,
473
- network_mode = None , restart_policy = None , cap_add = None , cap_drop = None ,
474
- devices = None , extra_hosts = None , read_only = None , pid_mode = None ,
475
- ipc_mode = None , security_opt = None , ulimits = None , log_config = None ,
476
- mem_limit = None , memswap_limit = None , mem_swappiness = None ,
477
- cgroup_parent = None , group_add = None , cpu_quota = None , cpu_period = None ,
478
- oom_kill_disable = False , version = None
479
- ):
470
+ def host_config_type_error (param , param_value , expected ):
471
+ error_msg = 'Invalid type for {0} param: expected {1} but found {2}'
472
+ return TypeError (error_msg .format (param , expected , type (param_value )))
473
+
474
+
475
+ def host_config_version_error (param , version , less_than = True ):
476
+ operator = '<' if less_than else '>'
477
+ error_msg = '{0} param is not supported in API versions {1} {2}'
478
+ return errors .InvalidVersion (error_msg .format (param , operator , version ))
479
+
480
+
481
+ def host_config_value_error (param , param_value ):
482
+ error_msg = 'Invalid value for {0} param: {1}'
483
+ return ValueError (error_msg .format (param , param_value ))
484
+
485
+
486
+ def create_host_config (binds = None , port_bindings = None , lxc_conf = None ,
487
+ publish_all_ports = False , links = None , privileged = False ,
488
+ dns = None , dns_search = None , volumes_from = None ,
489
+ network_mode = None , restart_policy = None , cap_add = None ,
490
+ cap_drop = None , devices = None , extra_hosts = None ,
491
+ read_only = None , pid_mode = None , ipc_mode = None ,
492
+ security_opt = None , ulimits = None , log_config = None ,
493
+ mem_limit = None , memswap_limit = None , mem_swappiness = None ,
494
+ cgroup_parent = None , group_add = None , cpu_quota = None ,
495
+ cpu_period = None , oom_kill_disable = False , version = None ):
480
496
481
497
host_config = {}
482
498
@@ -496,24 +512,21 @@ def create_host_config(
496
512
if memswap_limit is not None :
497
513
if isinstance (memswap_limit , six .string_types ):
498
514
memswap_limit = parse_bytes (memswap_limit )
515
+
499
516
host_config ['MemorySwap' ] = memswap_limit
500
517
501
518
if mem_swappiness is not None :
502
519
if version_lt (version , '1.20' ):
503
- raise errors .InvalidVersion (
504
- 'mem_swappiness param not supported for API version < 1.20'
505
- )
520
+ raise host_config_version_error ('mem_swappiness' , '1.20' )
506
521
if not isinstance (mem_swappiness , int ):
507
- raise TypeError (
508
- 'Invalid type for mem_swappiness param: expected int but'
509
- ' found {0}' .format (type (mem_swappiness ))
522
+ raise host_config_type_error (
523
+ 'mem_swappiness' , mem_swappiness , 'int'
510
524
)
525
+
511
526
host_config ['MemorySwappiness' ] = mem_swappiness
512
527
513
528
if pid_mode not in (None , 'host' ):
514
- raise errors .DockerException (
515
- 'Invalid value for pid param: {0}' .format (pid_mode )
516
- )
529
+ raise host_config_value_error ('pid_mode' , pid_mode )
517
530
elif pid_mode :
518
531
host_config ['PidMode' ] = pid_mode
519
532
@@ -524,10 +537,9 @@ def create_host_config(
524
537
host_config ['Privileged' ] = privileged
525
538
526
539
if oom_kill_disable :
527
- if version_lt (version , '1.19' ):
528
- raise errors .InvalidVersion (
529
- 'oom_kill_disable param not supported for API version < 1.19'
530
- )
540
+ if version_lt (version , '1.20' ):
541
+ raise host_config_version_error ('oom_kill_disable' , '1.19' )
542
+
531
543
host_config ['OomKillDisable' ] = oom_kill_disable
532
544
533
545
if publish_all_ports :
@@ -545,6 +557,11 @@ def create_host_config(
545
557
host_config ['NetworkMode' ] = 'default'
546
558
547
559
if restart_policy :
560
+ if not isinstance (restart_policy , dict ):
561
+ raise host_config_type_error (
562
+ 'restart_policy' , restart_policy , 'dict'
563
+ )
564
+
548
565
host_config ['RestartPolicy' ] = restart_policy
549
566
550
567
if cap_add :
@@ -558,34 +575,30 @@ def create_host_config(
558
575
559
576
if group_add :
560
577
if version_lt (version , '1.20' ):
561
- raise errors .InvalidVersion (
562
- 'group_add param not supported for API version < 1.20'
563
- )
578
+ raise host_config_version_error ('group_add' , '1.20' )
579
+
564
580
host_config ['GroupAdd' ] = [six .text_type (grp ) for grp in group_add ]
565
581
566
582
if dns is not None :
567
583
host_config ['Dns' ] = dns
568
584
569
585
if security_opt is not None :
570
586
if not isinstance (security_opt , list ):
571
- raise errors .DockerException (
572
- 'Invalid type for security_opt param: expected list but found'
573
- ' {0}' .format (type (security_opt ))
574
- )
587
+ raise host_config_type_error ('security_opt' , security_opt , 'list' )
588
+
575
589
host_config ['SecurityOpt' ] = security_opt
576
590
577
591
if volumes_from is not None :
578
592
if isinstance (volumes_from , six .string_types ):
579
593
volumes_from = volumes_from .split (',' )
594
+
580
595
host_config ['VolumesFrom' ] = volumes_from
581
596
582
597
if binds is not None :
583
598
host_config ['Binds' ] = convert_volume_binds (binds )
584
599
585
600
if port_bindings is not None :
586
- host_config ['PortBindings' ] = convert_port_bindings (
587
- port_bindings
588
- )
601
+ host_config ['PortBindings' ] = convert_port_bindings (port_bindings )
589
602
590
603
if extra_hosts is not None :
591
604
if isinstance (extra_hosts , dict ):
@@ -600,9 +613,7 @@ def create_host_config(
600
613
if isinstance (links , dict ):
601
614
links = six .iteritems (links )
602
615
603
- formatted_links = [
604
- '{0}:{1}' .format (k , v ) for k , v in sorted (links )
605
- ]
616
+ formatted_links = ['{0}:{1}' .format (k , v ) for k , v in sorted (links )]
606
617
607
618
host_config ['Links' ] = formatted_links
608
619
@@ -620,10 +631,7 @@ def create_host_config(
620
631
621
632
if ulimits is not None :
622
633
if not isinstance (ulimits , list ):
623
- raise errors .DockerException (
624
- 'Invalid type for ulimits param: expected list but found'
625
- ' {0}' .format (type (ulimits ))
626
- )
634
+ raise host_config_type_error ('ulimits' , ulimits , 'list' )
627
635
host_config ['Ulimits' ] = []
628
636
for l in ulimits :
629
637
if not isinstance (l , Ulimit ):
@@ -633,35 +641,27 @@ def create_host_config(
633
641
if log_config is not None :
634
642
if not isinstance (log_config , LogConfig ):
635
643
if not isinstance (log_config , dict ):
636
- raise errors .DockerException (
637
- 'Invalid type for log_config param: expected LogConfig but'
638
- ' found {0}' .format (type (log_config ))
644
+ raise host_config_type_error (
645
+ 'log_config' , log_config , 'LogConfig'
639
646
)
640
647
log_config = LogConfig (** log_config )
648
+
641
649
host_config ['LogConfig' ] = log_config
642
650
643
651
if cpu_quota :
644
652
if not isinstance (cpu_quota , int ):
645
- raise TypeError (
646
- 'Invalid type for cpu_quota param: expected int but'
647
- ' found {0}' .format (type (cpu_quota ))
648
- )
653
+ raise host_config_type_error ('cpu_quota' , cpu_quota , 'int' )
649
654
if version_lt (version , '1.19' ):
650
- raise errors .InvalidVersion (
651
- 'cpu_quota param not supported for API version < 1.19'
652
- )
655
+ raise host_config_version_error ('cpu_quota' , '1.19' )
656
+
653
657
host_config ['CpuQuota' ] = cpu_quota
654
658
655
659
if cpu_period :
656
660
if not isinstance (cpu_period , int ):
657
- raise TypeError (
658
- 'Invalid type for cpu_period param: expected int but'
659
- ' found {0}' .format (type (cpu_period ))
660
- )
661
+ raise host_config_type_error ('cpu_period' , cpu_period , 'int' )
661
662
if version_lt (version , '1.19' ):
662
- raise errors .InvalidVersion (
663
- 'cpu_period param not supported for API version < 1.19'
664
- )
663
+ raise host_config_version_error ('cpu_period' , '1.19' )
664
+
665
665
host_config ['CpuPeriod' ] = cpu_period
666
666
667
667
return host_config
0 commit comments