runconfig/hostconfig.go
6393c383
 package runconfig
 
 import (
767df67e
 	"encoding/json"
 	"io"
0b187b90
 	"strings"
 
b3ee9ac7
 	"github.com/docker/docker/nat"
3f390506
 	"github.com/docker/docker/pkg/ulimit"
6393c383
 )
 
c30a55f1
 type KeyValuePair struct {
 	Key   string
 	Value string
 }
 
0b187b90
 type NetworkMode string
 
080ca861
 // IsPrivate indicates whether container use it's private network stack
 func (n NetworkMode) IsPrivate() bool {
d18919e3
 	return !(n.IsHost() || n.IsContainer())
080ca861
 }
 
0e08e9ac
 func (n NetworkMode) IsBridge() bool {
 	return n == "bridge"
 }
 
0b187b90
 func (n NetworkMode) IsHost() bool {
 	return n == "host"
 }
 
 func (n NetworkMode) IsContainer() bool {
 	parts := strings.SplitN(string(n), ":", 2)
 	return len(parts) > 1 && parts[0] == "container"
 }
 
ba248202
 func (n NetworkMode) IsNone() bool {
 	return n == "none"
 }
 
497fc887
 type IpcMode string
 
 // IsPrivate indicates whether container use it's private ipc stack
 func (n IpcMode) IsPrivate() bool {
 	return !(n.IsHost() || n.IsContainer())
 }
 
 func (n IpcMode) IsHost() bool {
 	return n == "host"
 }
 
 func (n IpcMode) IsContainer() bool {
 	parts := strings.SplitN(string(n), ":", 2)
 	return len(parts) > 1 && parts[0] == "container"
 }
 
 func (n IpcMode) Valid() bool {
 	parts := strings.Split(string(n), ":")
 	switch mode := parts[0]; mode {
 	case "", "host":
 	case "container":
 		if len(parts) != 2 || parts[1] == "" {
 			return false
 		}
 	default:
 		return false
 	}
 	return true
 }
 
 func (n IpcMode) Container() string {
 	parts := strings.SplitN(string(n), ":", 2)
 	if len(parts) > 1 {
 		return parts[1]
 	}
 	return ""
 }
 
f2e5207f
 type UTSMode string
 
 // IsPrivate indicates whether container use it's private UTS namespace
 func (n UTSMode) IsPrivate() bool {
 	return !(n.IsHost())
 }
 
 func (n UTSMode) IsHost() bool {
 	return n == "host"
 }
 
 func (n UTSMode) Valid() bool {
 	parts := strings.Split(string(n), ":")
 	switch mode := parts[0]; mode {
 	case "", "host":
 	default:
 		return false
 	}
 	return true
 }
 
23feaaa2
 type PidMode string
 
 // IsPrivate indicates whether container use it's private pid stack
 func (n PidMode) IsPrivate() bool {
 	return !(n.IsHost())
 }
 
 func (n PidMode) IsHost() bool {
 	return n == "host"
 }
 
 func (n PidMode) Valid() bool {
 	parts := strings.Split(string(n), ":")
 	switch mode := parts[0]; mode {
 	case "", "host":
 	default:
 		return false
 	}
 	return true
 }
 
e855c4b9
 type DeviceMapping struct {
 	PathOnHost        string
 	PathInContainer   string
 	CgroupPermissions string
 }
 
d9753ba2
 type RestartPolicy struct {
 	Name              string
 	MaximumRetryCount int
 }
 
624bf81f
 func (rp *RestartPolicy) IsNone() bool {
 	return rp.Name == "no"
 }
 
 func (rp *RestartPolicy) IsAlways() bool {
 	return rp.Name == "always"
 }
 
 func (rp *RestartPolicy) IsOnFailure() bool {
 	return rp.Name == "on-failure"
 }
 
47a6afb9
 type LogConfig struct {
 	Type   string
 	Config map[string]string
 }
 
767df67e
 type LxcConfig struct {
 	values []KeyValuePair
 }
 
 func (c *LxcConfig) MarshalJSON() ([]byte, error) {
 	if c == nil {
 		return []byte{}, nil
 	}
 	return json.Marshal(c.Slice())
 }
 
 func (c *LxcConfig) UnmarshalJSON(b []byte) error {
 	if len(b) == 0 {
 		return nil
 	}
 
 	var kv []KeyValuePair
 	if err := json.Unmarshal(b, &kv); err != nil {
 		var h map[string]string
 		if err := json.Unmarshal(b, &h); err != nil {
 			return err
 		}
 		for k, v := range h {
 			kv = append(kv, KeyValuePair{k, v})
 		}
 	}
 	c.values = kv
 
 	return nil
 }
 
 func (c *LxcConfig) Len() int {
 	if c == nil {
 		return 0
 	}
 	return len(c.values)
 }
 
 func (c *LxcConfig) Slice() []KeyValuePair {
 	if c == nil {
 		return nil
 	}
 	return c.values
 }
 
 func NewLxcConfig(values []KeyValuePair) *LxcConfig {
 	return &LxcConfig{values}
 }
 
6393c383
 type HostConfig struct {
2c2cc051
 	Binds           []string
 	ContainerIDFile string
767df67e
 	LxcConf         *LxcConfig
dccb8b5c
 	Memory          int64 // Memory limit (in bytes)
 	MemorySwap      int64 // Total memory usage (memory + swap); set `-1` to disable swap
 	CpuShares       int64 // CPU shares (relative weight vs. other containers)
 	CpuPeriod       int64
837eec06
 	CpusetCpus      string // CpusetCpus 0-2, 0,1
8077b2fb
 	CpusetMems      string // CpusetMems 0-2, 0,1
dcc50e1d
 	CpuQuota        int64
f133f11a
 	BlkioWeight     int64 // Block IO weight (relative weight vs. other containers)
 	OomKillDisable  bool  // Whether to disable OOM Killer or not
2c2cc051
 	Privileged      bool
 	PortBindings    nat.PortMap
 	Links           []string
 	PublishAllPorts bool
 	Dns             []string
 	DnsSearch       []string
68e48b65
 	ExtraHosts      []string
2c2cc051
 	VolumesFrom     []string
e855c4b9
 	Devices         []DeviceMapping
0b187b90
 	NetworkMode     NetworkMode
497fc887
 	IpcMode         IpcMode
23feaaa2
 	PidMode         PidMode
f2e5207f
 	UTSMode         UTSMode
94e6dc97
 	CapAdd          []string
 	CapDrop         []string
d9753ba2
 	RestartPolicy   RestartPolicy
294843ef
 	SecurityOpt     []string
40940709
 	ReadonlyRootfs  bool
3f390506
 	Ulimits         []*ulimit.Ulimit
47a6afb9
 	LogConfig       LogConfig
0b1e2b5a
 	CgroupParent    string // Parent cgroup.
6393c383
 }
 
767df67e
 func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper {
 	return &ContainerConfigWrapper{
 		config,
 		&hostConfigWrapper{InnerHostConfig: hostConfig},
 	}
3a90004f
 }
 
767df67e
 type hostConfigWrapper struct {
 	InnerHostConfig *HostConfig `json:"HostConfig,omitempty"`
 	Cpuset          string      `json:",omitempty"` // Deprecated. Exported for backwards compatibility.
 
 	*HostConfig // Deprecated. Exported to read attrubutes from json that are not in the inner host config structure.
3a90004f
 }
 
767df67e
 func (w hostConfigWrapper) GetHostConfig() *HostConfig {
 	hc := w.HostConfig
837eec06
 
767df67e
 	if hc == nil && w.InnerHostConfig != nil {
 		hc = w.InnerHostConfig
 	} else if w.InnerHostConfig != nil {
 		if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 {
 			w.InnerHostConfig.Memory = hc.Memory
837eec06
 		}
767df67e
 		if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
 			w.InnerHostConfig.MemorySwap = hc.MemorySwap
837eec06
 		}
767df67e
 		if hc.CpuShares != 0 && w.InnerHostConfig.CpuShares == 0 {
 			w.InnerHostConfig.CpuShares = hc.CpuShares
837eec06
 		}
 
767df67e
 		hc = w.InnerHostConfig
3a90004f
 	}
 
767df67e
 	if hc != nil && w.Cpuset != "" && hc.CpusetCpus == "" {
 		hc.CpusetCpus = w.Cpuset
6393c383
 	}
d9753ba2
 
767df67e
 	return hc
 }
837eec06
 
767df67e
 func DecodeHostConfig(src io.Reader) (*HostConfig, error) {
 	decoder := json.NewDecoder(src)
 
 	var w hostConfigWrapper
 	if err := decoder.Decode(&w); err != nil {
 		return nil, err
8344b6d7
 	}
d9753ba2
 
767df67e
 	hc := w.GetHostConfig()
 
 	return hc, nil
6393c383
 }