github.com/circular-dark/docker@v1.7.0/runconfig/hostconfig.go (about) 1 package runconfig 2 3 import ( 4 "encoding/json" 5 "io" 6 "strings" 7 8 "github.com/docker/docker/nat" 9 "github.com/docker/docker/pkg/ulimit" 10 ) 11 12 type KeyValuePair struct { 13 Key string 14 Value string 15 } 16 17 type NetworkMode string 18 19 // IsPrivate indicates whether container use it's private network stack 20 func (n NetworkMode) IsPrivate() bool { 21 return !(n.IsHost() || n.IsContainer()) 22 } 23 24 func (n NetworkMode) IsBridge() bool { 25 return n == "bridge" 26 } 27 28 func (n NetworkMode) IsHost() bool { 29 return n == "host" 30 } 31 32 func (n NetworkMode) IsContainer() bool { 33 parts := strings.SplitN(string(n), ":", 2) 34 return len(parts) > 1 && parts[0] == "container" 35 } 36 37 func (n NetworkMode) IsNone() bool { 38 return n == "none" 39 } 40 41 type IpcMode string 42 43 // IsPrivate indicates whether container use it's private ipc stack 44 func (n IpcMode) IsPrivate() bool { 45 return !(n.IsHost() || n.IsContainer()) 46 } 47 48 func (n IpcMode) IsHost() bool { 49 return n == "host" 50 } 51 52 func (n IpcMode) IsContainer() bool { 53 parts := strings.SplitN(string(n), ":", 2) 54 return len(parts) > 1 && parts[0] == "container" 55 } 56 57 func (n IpcMode) Valid() bool { 58 parts := strings.Split(string(n), ":") 59 switch mode := parts[0]; mode { 60 case "", "host": 61 case "container": 62 if len(parts) != 2 || parts[1] == "" { 63 return false 64 } 65 default: 66 return false 67 } 68 return true 69 } 70 71 func (n IpcMode) Container() string { 72 parts := strings.SplitN(string(n), ":", 2) 73 if len(parts) > 1 { 74 return parts[1] 75 } 76 return "" 77 } 78 79 type UTSMode string 80 81 // IsPrivate indicates whether container use it's private UTS namespace 82 func (n UTSMode) IsPrivate() bool { 83 return !(n.IsHost()) 84 } 85 86 func (n UTSMode) IsHost() bool { 87 return n == "host" 88 } 89 90 func (n UTSMode) Valid() bool { 91 parts := strings.Split(string(n), ":") 92 switch mode := parts[0]; mode { 93 case "", "host": 94 default: 95 return false 96 } 97 return true 98 } 99 100 type PidMode string 101 102 // IsPrivate indicates whether container use it's private pid stack 103 func (n PidMode) IsPrivate() bool { 104 return !(n.IsHost()) 105 } 106 107 func (n PidMode) IsHost() bool { 108 return n == "host" 109 } 110 111 func (n PidMode) Valid() bool { 112 parts := strings.Split(string(n), ":") 113 switch mode := parts[0]; mode { 114 case "", "host": 115 default: 116 return false 117 } 118 return true 119 } 120 121 type DeviceMapping struct { 122 PathOnHost string 123 PathInContainer string 124 CgroupPermissions string 125 } 126 127 type RestartPolicy struct { 128 Name string 129 MaximumRetryCount int 130 } 131 132 func (rp *RestartPolicy) IsNone() bool { 133 return rp.Name == "no" 134 } 135 136 func (rp *RestartPolicy) IsAlways() bool { 137 return rp.Name == "always" 138 } 139 140 func (rp *RestartPolicy) IsOnFailure() bool { 141 return rp.Name == "on-failure" 142 } 143 144 type LogConfig struct { 145 Type string 146 Config map[string]string 147 } 148 149 type LxcConfig struct { 150 values []KeyValuePair 151 } 152 153 func (c *LxcConfig) MarshalJSON() ([]byte, error) { 154 if c == nil { 155 return []byte{}, nil 156 } 157 return json.Marshal(c.Slice()) 158 } 159 160 func (c *LxcConfig) UnmarshalJSON(b []byte) error { 161 if len(b) == 0 { 162 return nil 163 } 164 165 var kv []KeyValuePair 166 if err := json.Unmarshal(b, &kv); err != nil { 167 var h map[string]string 168 if err := json.Unmarshal(b, &h); err != nil { 169 return err 170 } 171 for k, v := range h { 172 kv = append(kv, KeyValuePair{k, v}) 173 } 174 } 175 c.values = kv 176 177 return nil 178 } 179 180 func (c *LxcConfig) Len() int { 181 if c == nil { 182 return 0 183 } 184 return len(c.values) 185 } 186 187 func (c *LxcConfig) Slice() []KeyValuePair { 188 if c == nil { 189 return nil 190 } 191 return c.values 192 } 193 194 func NewLxcConfig(values []KeyValuePair) *LxcConfig { 195 return &LxcConfig{values} 196 } 197 198 type HostConfig struct { 199 Binds []string 200 ContainerIDFile string 201 LxcConf *LxcConfig 202 Memory int64 // Memory limit (in bytes) 203 MemorySwap int64 // Total memory usage (memory + swap); set `-1` to disable swap 204 CpuShares int64 // CPU shares (relative weight vs. other containers) 205 CpuPeriod int64 206 CpusetCpus string // CpusetCpus 0-2, 0,1 207 CpusetMems string // CpusetMems 0-2, 0,1 208 CpuQuota int64 209 BlkioWeight int64 // Block IO weight (relative weight vs. other containers) 210 OomKillDisable bool // Whether to disable OOM Killer or not 211 Privileged bool 212 PortBindings nat.PortMap 213 Links []string 214 PublishAllPorts bool 215 Dns []string 216 DnsSearch []string 217 ExtraHosts []string 218 VolumesFrom []string 219 Devices []DeviceMapping 220 NetworkMode NetworkMode 221 IpcMode IpcMode 222 PidMode PidMode 223 UTSMode UTSMode 224 CapAdd []string 225 CapDrop []string 226 RestartPolicy RestartPolicy 227 SecurityOpt []string 228 ReadonlyRootfs bool 229 Ulimits []*ulimit.Ulimit 230 LogConfig LogConfig 231 CgroupParent string // Parent cgroup. 232 } 233 234 func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper { 235 return &ContainerConfigWrapper{ 236 config, 237 &hostConfigWrapper{InnerHostConfig: hostConfig}, 238 } 239 } 240 241 type hostConfigWrapper struct { 242 InnerHostConfig *HostConfig `json:"HostConfig,omitempty"` 243 Cpuset string `json:",omitempty"` // Deprecated. Exported for backwards compatibility. 244 245 *HostConfig // Deprecated. Exported to read attrubutes from json that are not in the inner host config structure. 246 } 247 248 func (w hostConfigWrapper) GetHostConfig() *HostConfig { 249 hc := w.HostConfig 250 251 if hc == nil && w.InnerHostConfig != nil { 252 hc = w.InnerHostConfig 253 } else if w.InnerHostConfig != nil { 254 if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 { 255 w.InnerHostConfig.Memory = hc.Memory 256 } 257 if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 { 258 w.InnerHostConfig.MemorySwap = hc.MemorySwap 259 } 260 if hc.CpuShares != 0 && w.InnerHostConfig.CpuShares == 0 { 261 w.InnerHostConfig.CpuShares = hc.CpuShares 262 } 263 264 hc = w.InnerHostConfig 265 } 266 267 if hc != nil && w.Cpuset != "" && hc.CpusetCpus == "" { 268 hc.CpusetCpus = w.Cpuset 269 } 270 271 return hc 272 } 273 274 func DecodeHostConfig(src io.Reader) (*HostConfig, error) { 275 decoder := json.NewDecoder(src) 276 277 var w hostConfigWrapper 278 if err := decoder.Decode(&w); err != nil { 279 return nil, err 280 } 281 282 hc := w.GetHostConfig() 283 284 return hc, nil 285 }