github.com/lmars/docker@v1.6.0-rc2/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/pkg/ulimit"
     9  	"github.com/docker/docker/utils"
    10  )
    11  
    12  type NetworkMode string
    13  
    14  // IsPrivate indicates whether container use it's private network stack
    15  func (n NetworkMode) IsPrivate() bool {
    16  	return !(n.IsHost() || n.IsContainer() || n.IsNone())
    17  }
    18  
    19  func (n NetworkMode) IsHost() bool {
    20  	return n == "host"
    21  }
    22  
    23  func (n NetworkMode) IsContainer() bool {
    24  	parts := strings.SplitN(string(n), ":", 2)
    25  	return len(parts) > 1 && parts[0] == "container"
    26  }
    27  
    28  func (n NetworkMode) IsNone() bool {
    29  	return n == "none"
    30  }
    31  
    32  type IpcMode string
    33  
    34  // IsPrivate indicates whether container use it's private ipc stack
    35  func (n IpcMode) IsPrivate() bool {
    36  	return !(n.IsHost() || n.IsContainer())
    37  }
    38  
    39  func (n IpcMode) IsHost() bool {
    40  	return n == "host"
    41  }
    42  
    43  func (n IpcMode) IsContainer() bool {
    44  	parts := strings.SplitN(string(n), ":", 2)
    45  	return len(parts) > 1 && parts[0] == "container"
    46  }
    47  
    48  func (n IpcMode) Valid() bool {
    49  	parts := strings.Split(string(n), ":")
    50  	switch mode := parts[0]; mode {
    51  	case "", "host":
    52  	case "container":
    53  		if len(parts) != 2 || parts[1] == "" {
    54  			return false
    55  		}
    56  	default:
    57  		return false
    58  	}
    59  	return true
    60  }
    61  
    62  func (n IpcMode) Container() string {
    63  	parts := strings.SplitN(string(n), ":", 2)
    64  	if len(parts) > 1 {
    65  		return parts[1]
    66  	}
    67  	return ""
    68  }
    69  
    70  type PidMode string
    71  
    72  // IsPrivate indicates whether container use it's private pid stack
    73  func (n PidMode) IsPrivate() bool {
    74  	return !(n.IsHost())
    75  }
    76  
    77  func (n PidMode) IsHost() bool {
    78  	return n == "host"
    79  }
    80  
    81  func (n PidMode) Valid() bool {
    82  	parts := strings.Split(string(n), ":")
    83  	switch mode := parts[0]; mode {
    84  	case "", "host":
    85  	default:
    86  		return false
    87  	}
    88  	return true
    89  }
    90  
    91  type DeviceMapping struct {
    92  	PathOnHost        string
    93  	PathInContainer   string
    94  	CgroupPermissions string
    95  }
    96  
    97  type RestartPolicy struct {
    98  	Name              string
    99  	MaximumRetryCount int
   100  }
   101  
   102  type LogConfig struct {
   103  	Type   string
   104  	Config map[string]string
   105  }
   106  
   107  type HostConfig struct {
   108  	Binds           []string
   109  	ContainerIDFile string
   110  	LxcConf         []utils.KeyValuePair
   111  	Memory          int64  // Memory limit (in bytes)
   112  	MemorySwap      int64  // Total memory usage (memory + swap); set `-1` to disable swap
   113  	CpuShares       int64  // CPU shares (relative weight vs. other containers)
   114  	CpusetCpus      string // CpusetCpus 0-2, 0,1
   115  	Privileged      bool
   116  	PortBindings    nat.PortMap
   117  	Links           []string
   118  	PublishAllPorts bool
   119  	Dns             []string
   120  	DnsSearch       []string
   121  	ExtraHosts      []string
   122  	VolumesFrom     []string
   123  	Devices         []DeviceMapping
   124  	NetworkMode     NetworkMode
   125  	IpcMode         IpcMode
   126  	PidMode         PidMode
   127  	CapAdd          []string
   128  	CapDrop         []string
   129  	RestartPolicy   RestartPolicy
   130  	SecurityOpt     []string
   131  	ReadonlyRootfs  bool
   132  	Ulimits         []*ulimit.Ulimit
   133  	LogConfig       LogConfig
   134  	CgroupParent    string // Parent cgroup.
   135  }
   136  
   137  // This is used by the create command when you want to set both the
   138  // Config and the HostConfig in the same call
   139  type ConfigAndHostConfig struct {
   140  	Config
   141  	HostConfig HostConfig
   142  }
   143  
   144  func MergeConfigs(config *Config, hostConfig *HostConfig) *ConfigAndHostConfig {
   145  	return &ConfigAndHostConfig{
   146  		*config,
   147  		*hostConfig,
   148  	}
   149  }
   150  
   151  func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
   152  	if job.EnvExists("HostConfig") {
   153  		hostConfig := HostConfig{}
   154  		job.GetenvJson("HostConfig", &hostConfig)
   155  
   156  		// FIXME: These are for backward compatibility, if people use these
   157  		// options with `HostConfig`, we should still make them workable.
   158  		if job.EnvExists("Memory") && hostConfig.Memory == 0 {
   159  			hostConfig.Memory = job.GetenvInt64("Memory")
   160  		}
   161  		if job.EnvExists("MemorySwap") && hostConfig.MemorySwap == 0 {
   162  			hostConfig.MemorySwap = job.GetenvInt64("MemorySwap")
   163  		}
   164  		if job.EnvExists("CpuShares") && hostConfig.CpuShares == 0 {
   165  			hostConfig.CpuShares = job.GetenvInt64("CpuShares")
   166  		}
   167  		if job.EnvExists("Cpuset") && hostConfig.CpusetCpus == "" {
   168  			hostConfig.CpusetCpus = job.Getenv("Cpuset")
   169  		}
   170  
   171  		return &hostConfig
   172  	}
   173  
   174  	hostConfig := &HostConfig{
   175  		ContainerIDFile: job.Getenv("ContainerIDFile"),
   176  		Memory:          job.GetenvInt64("Memory"),
   177  		MemorySwap:      job.GetenvInt64("MemorySwap"),
   178  		CpuShares:       job.GetenvInt64("CpuShares"),
   179  		CpusetCpus:      job.Getenv("CpusetCpus"),
   180  		Privileged:      job.GetenvBool("Privileged"),
   181  		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
   182  		NetworkMode:     NetworkMode(job.Getenv("NetworkMode")),
   183  		IpcMode:         IpcMode(job.Getenv("IpcMode")),
   184  		PidMode:         PidMode(job.Getenv("PidMode")),
   185  		ReadonlyRootfs:  job.GetenvBool("ReadonlyRootfs"),
   186  		CgroupParent:    job.Getenv("CgroupParent"),
   187  	}
   188  
   189  	// FIXME: This is for backward compatibility, if people use `Cpuset`
   190  	// in json, make it workable, we will only pass hostConfig.CpusetCpus
   191  	// to execDriver.
   192  	if job.EnvExists("Cpuset") && hostConfig.CpusetCpus == "" {
   193  		hostConfig.CpusetCpus = job.Getenv("Cpuset")
   194  	}
   195  
   196  	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
   197  	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
   198  	job.GetenvJson("Devices", &hostConfig.Devices)
   199  	job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
   200  	job.GetenvJson("Ulimits", &hostConfig.Ulimits)
   201  	job.GetenvJson("LogConfig", &hostConfig.LogConfig)
   202  	hostConfig.SecurityOpt = job.GetenvList("SecurityOpt")
   203  	if Binds := job.GetenvList("Binds"); Binds != nil {
   204  		hostConfig.Binds = Binds
   205  	}
   206  	if Links := job.GetenvList("Links"); Links != nil {
   207  		hostConfig.Links = Links
   208  	}
   209  	if Dns := job.GetenvList("Dns"); Dns != nil {
   210  		hostConfig.Dns = Dns
   211  	}
   212  	if DnsSearch := job.GetenvList("DnsSearch"); DnsSearch != nil {
   213  		hostConfig.DnsSearch = DnsSearch
   214  	}
   215  	if ExtraHosts := job.GetenvList("ExtraHosts"); ExtraHosts != nil {
   216  		hostConfig.ExtraHosts = ExtraHosts
   217  	}
   218  	if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
   219  		hostConfig.VolumesFrom = VolumesFrom
   220  	}
   221  	if CapAdd := job.GetenvList("CapAdd"); CapAdd != nil {
   222  		hostConfig.CapAdd = CapAdd
   223  	}
   224  	if CapDrop := job.GetenvList("CapDrop"); CapDrop != nil {
   225  		hostConfig.CapDrop = CapDrop
   226  	}
   227  
   228  	return hostConfig
   229  }