github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/runconfig/hostconfig.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/engine"
     7  	"github.com/docker/docker/nat"
     8  	"github.com/docker/docker/utils"
     9  )
    10  
    11  type NetworkMode string
    12  
    13  // IsPrivate indicates whether container use it's private network stack
    14  func (n NetworkMode) IsPrivate() bool {
    15  	return !(n.IsHost() || n.IsContainer() || n.IsNone())
    16  }
    17  
    18  func (n NetworkMode) IsHost() bool {
    19  	return n == "host"
    20  }
    21  
    22  func (n NetworkMode) IsContainer() bool {
    23  	parts := strings.SplitN(string(n), ":", 2)
    24  	return len(parts) > 1 && parts[0] == "container"
    25  }
    26  
    27  func (n NetworkMode) IsNone() bool {
    28  	return n == "none"
    29  }
    30  
    31  type IpcMode string
    32  
    33  // IsPrivate indicates whether container use it's private ipc stack
    34  func (n IpcMode) IsPrivate() bool {
    35  	return !(n.IsHost() || n.IsContainer())
    36  }
    37  
    38  func (n IpcMode) IsHost() bool {
    39  	return n == "host"
    40  }
    41  
    42  func (n IpcMode) IsContainer() bool {
    43  	parts := strings.SplitN(string(n), ":", 2)
    44  	return len(parts) > 1 && parts[0] == "container"
    45  }
    46  
    47  func (n IpcMode) Valid() bool {
    48  	parts := strings.Split(string(n), ":")
    49  	switch mode := parts[0]; mode {
    50  	case "", "host":
    51  	case "container":
    52  		if len(parts) != 2 || parts[1] == "" {
    53  			return false
    54  		}
    55  	default:
    56  		return false
    57  	}
    58  	return true
    59  }
    60  
    61  func (n IpcMode) Container() string {
    62  	parts := strings.SplitN(string(n), ":", 2)
    63  	if len(parts) > 1 {
    64  		return parts[1]
    65  	}
    66  	return ""
    67  }
    68  
    69  type DeviceMapping struct {
    70  	PathOnHost        string
    71  	PathInContainer   string
    72  	CgroupPermissions string
    73  }
    74  
    75  type RestartPolicy struct {
    76  	Name              string
    77  	MaximumRetryCount int
    78  }
    79  
    80  type HostConfig struct {
    81  	Binds           []string
    82  	ContainerIDFile string
    83  	LxcConf         []utils.KeyValuePair
    84  	Privileged      bool
    85  	PortBindings    nat.PortMap
    86  	Links           []string
    87  	PublishAllPorts bool
    88  	Dns             []string
    89  	DnsSearch       []string
    90  	ExtraHosts      []string
    91  	VolumesFrom     []string
    92  	Devices         []DeviceMapping
    93  	NetworkMode     NetworkMode
    94  	IpcMode         IpcMode
    95  	CapAdd          []string
    96  	CapDrop         []string
    97  	RestartPolicy   RestartPolicy
    98  	SecurityOpt     []string
    99  }
   100  
   101  // This is used by the create command when you want to set both the
   102  // Config and the HostConfig in the same call
   103  type ConfigAndHostConfig struct {
   104  	Config
   105  	HostConfig HostConfig
   106  }
   107  
   108  func MergeConfigs(config *Config, hostConfig *HostConfig) *ConfigAndHostConfig {
   109  	return &ConfigAndHostConfig{
   110  		*config,
   111  		*hostConfig,
   112  	}
   113  }
   114  
   115  func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
   116  	if job.EnvExists("HostConfig") {
   117  		hostConfig := HostConfig{}
   118  		job.GetenvJson("HostConfig", &hostConfig)
   119  		return &hostConfig
   120  	}
   121  
   122  	hostConfig := &HostConfig{
   123  		ContainerIDFile: job.Getenv("ContainerIDFile"),
   124  		Privileged:      job.GetenvBool("Privileged"),
   125  		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
   126  		NetworkMode:     NetworkMode(job.Getenv("NetworkMode")),
   127  		IpcMode:         IpcMode(job.Getenv("IpcMode")),
   128  	}
   129  
   130  	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
   131  	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
   132  	job.GetenvJson("Devices", &hostConfig.Devices)
   133  	job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
   134  	hostConfig.SecurityOpt = job.GetenvList("SecurityOpt")
   135  	if Binds := job.GetenvList("Binds"); Binds != nil {
   136  		hostConfig.Binds = Binds
   137  	}
   138  	if Links := job.GetenvList("Links"); Links != nil {
   139  		hostConfig.Links = Links
   140  	}
   141  	if Dns := job.GetenvList("Dns"); Dns != nil {
   142  		hostConfig.Dns = Dns
   143  	}
   144  	if DnsSearch := job.GetenvList("DnsSearch"); DnsSearch != nil {
   145  		hostConfig.DnsSearch = DnsSearch
   146  	}
   147  	if ExtraHosts := job.GetenvList("ExtraHosts"); ExtraHosts != nil {
   148  		hostConfig.ExtraHosts = ExtraHosts
   149  	}
   150  	if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
   151  		hostConfig.VolumesFrom = VolumesFrom
   152  	}
   153  	if CapAdd := job.GetenvList("CapAdd"); CapAdd != nil {
   154  		hostConfig.CapAdd = CapAdd
   155  	}
   156  	if CapDrop := job.GetenvList("CapDrop"); CapDrop != nil {
   157  		hostConfig.CapDrop = CapDrop
   158  	}
   159  
   160  	return hostConfig
   161  }