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