|
@@ -1,9 +1,10 @@
|
|
|
package runconfig
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+ "io"
|
|
|
"strings"
|
|
|
|
|
|
- "github.com/docker/docker/engine"
|
|
|
"github.com/docker/docker/nat"
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
|
|
)
|
|
@@ -108,10 +109,59 @@ type LogConfig struct {
|
|
|
Config map[string]string
|
|
|
}
|
|
|
|
|
|
+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}
|
|
|
+}
|
|
|
+
|
|
|
type HostConfig struct {
|
|
|
Binds []string
|
|
|
ContainerIDFile string
|
|
|
- LxcConf []KeyValuePair
|
|
|
+ LxcConf *LxcConfig
|
|
|
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)
|
|
@@ -138,96 +188,55 @@ type HostConfig struct {
|
|
|
CgroupParent string // Parent cgroup.
|
|
|
}
|
|
|
|
|
|
-// This is used by the create command when you want to set both the
|
|
|
-// Config and the HostConfig in the same call
|
|
|
-type ConfigAndHostConfig struct {
|
|
|
- Config
|
|
|
- HostConfig HostConfig
|
|
|
+func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper {
|
|
|
+ return &ContainerConfigWrapper{
|
|
|
+ config,
|
|
|
+ &hostConfigWrapper{InnerHostConfig: hostConfig},
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func MergeConfigs(config *Config, hostConfig *HostConfig) *ConfigAndHostConfig {
|
|
|
- return &ConfigAndHostConfig{
|
|
|
- *config,
|
|
|
- *hostConfig,
|
|
|
- }
|
|
|
+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.
|
|
|
}
|
|
|
|
|
|
-func ContainerHostConfigFromJob(env *engine.Env) *HostConfig {
|
|
|
- if env.Exists("HostConfig") {
|
|
|
- hostConfig := HostConfig{}
|
|
|
- env.GetJson("HostConfig", &hostConfig)
|
|
|
+func (w hostConfigWrapper) GetHostConfig() *HostConfig {
|
|
|
+ hc := w.HostConfig
|
|
|
|
|
|
- // FIXME: These are for backward compatibility, if people use these
|
|
|
- // options with `HostConfig`, we should still make them workable.
|
|
|
- if env.Exists("Memory") && hostConfig.Memory == 0 {
|
|
|
- hostConfig.Memory = env.GetInt64("Memory")
|
|
|
- }
|
|
|
- if env.Exists("MemorySwap") && hostConfig.MemorySwap == 0 {
|
|
|
- hostConfig.MemorySwap = env.GetInt64("MemorySwap")
|
|
|
+ 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
|
|
|
}
|
|
|
- if env.Exists("CpuShares") && hostConfig.CpuShares == 0 {
|
|
|
- hostConfig.CpuShares = env.GetInt64("CpuShares")
|
|
|
+ if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
|
|
|
+ w.InnerHostConfig.MemorySwap = hc.MemorySwap
|
|
|
}
|
|
|
- if env.Exists("Cpuset") && hostConfig.CpusetCpus == "" {
|
|
|
- hostConfig.CpusetCpus = env.Get("Cpuset")
|
|
|
+ if hc.CpuShares != 0 && w.InnerHostConfig.CpuShares == 0 {
|
|
|
+ w.InnerHostConfig.CpuShares = hc.CpuShares
|
|
|
}
|
|
|
|
|
|
- return &hostConfig
|
|
|
+ hc = w.InnerHostConfig
|
|
|
}
|
|
|
|
|
|
- hostConfig := &HostConfig{
|
|
|
- ContainerIDFile: env.Get("ContainerIDFile"),
|
|
|
- Memory: env.GetInt64("Memory"),
|
|
|
- MemorySwap: env.GetInt64("MemorySwap"),
|
|
|
- CpuShares: env.GetInt64("CpuShares"),
|
|
|
- CpusetCpus: env.Get("CpusetCpus"),
|
|
|
- Privileged: env.GetBool("Privileged"),
|
|
|
- PublishAllPorts: env.GetBool("PublishAllPorts"),
|
|
|
- NetworkMode: NetworkMode(env.Get("NetworkMode")),
|
|
|
- IpcMode: IpcMode(env.Get("IpcMode")),
|
|
|
- PidMode: PidMode(env.Get("PidMode")),
|
|
|
- ReadonlyRootfs: env.GetBool("ReadonlyRootfs"),
|
|
|
- CgroupParent: env.Get("CgroupParent"),
|
|
|
+ if hc != nil && w.Cpuset != "" && hc.CpusetCpus == "" {
|
|
|
+ hc.CpusetCpus = w.Cpuset
|
|
|
}
|
|
|
|
|
|
- // FIXME: This is for backward compatibility, if people use `Cpuset`
|
|
|
- // in json, make it workable, we will only pass hostConfig.CpusetCpus
|
|
|
- // to execDriver.
|
|
|
- if env.Exists("Cpuset") && hostConfig.CpusetCpus == "" {
|
|
|
- hostConfig.CpusetCpus = env.Get("Cpuset")
|
|
|
- }
|
|
|
+ return hc
|
|
|
+}
|
|
|
|
|
|
- env.GetJson("LxcConf", &hostConfig.LxcConf)
|
|
|
- env.GetJson("PortBindings", &hostConfig.PortBindings)
|
|
|
- env.GetJson("Devices", &hostConfig.Devices)
|
|
|
- env.GetJson("RestartPolicy", &hostConfig.RestartPolicy)
|
|
|
- env.GetJson("Ulimits", &hostConfig.Ulimits)
|
|
|
- env.GetJson("LogConfig", &hostConfig.LogConfig)
|
|
|
- hostConfig.SecurityOpt = env.GetList("SecurityOpt")
|
|
|
- if Binds := env.GetList("Binds"); Binds != nil {
|
|
|
- hostConfig.Binds = Binds
|
|
|
- }
|
|
|
- if Links := env.GetList("Links"); Links != nil {
|
|
|
- hostConfig.Links = Links
|
|
|
- }
|
|
|
- if Dns := env.GetList("Dns"); Dns != nil {
|
|
|
- hostConfig.Dns = Dns
|
|
|
- }
|
|
|
- if DnsSearch := env.GetList("DnsSearch"); DnsSearch != nil {
|
|
|
- hostConfig.DnsSearch = DnsSearch
|
|
|
- }
|
|
|
- if ExtraHosts := env.GetList("ExtraHosts"); ExtraHosts != nil {
|
|
|
- hostConfig.ExtraHosts = ExtraHosts
|
|
|
- }
|
|
|
- if VolumesFrom := env.GetList("VolumesFrom"); VolumesFrom != nil {
|
|
|
- hostConfig.VolumesFrom = VolumesFrom
|
|
|
- }
|
|
|
- if CapAdd := env.GetList("CapAdd"); CapAdd != nil {
|
|
|
- hostConfig.CapAdd = CapAdd
|
|
|
- }
|
|
|
- if CapDrop := env.GetList("CapDrop"); CapDrop != nil {
|
|
|
- hostConfig.CapDrop = CapDrop
|
|
|
+func DecodeHostConfig(src io.Reader) (*HostConfig, error) {
|
|
|
+ decoder := json.NewDecoder(src)
|
|
|
+
|
|
|
+ var w hostConfigWrapper
|
|
|
+ if err := decoder.Decode(&w); err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- return hostConfig
|
|
|
+ hc := w.GetHostConfig()
|
|
|
+
|
|
|
+ return hc, nil
|
|
|
}
|