2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2014-05-02 19:59:28 -04:00
|
|
|
"strings"
|
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/nat"
|
2015-02-11 14:21:38 -05:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2014-05-02 19:59:28 -04:00
|
|
|
type NetworkMode string
|
|
|
|
|
2014-09-05 02:43:44 -04:00
|
|
|
// IsPrivate indicates whether container use it's private network stack
|
|
|
|
func (n NetworkMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost() || n.IsContainer() || n.IsNone())
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:59:28 -04:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:50:58 -04:00
|
|
|
func (n NetworkMode) IsNone() bool {
|
|
|
|
return n == "none"
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:14:17 -05:00
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-31 00:00:47 -04:00
|
|
|
type DeviceMapping struct {
|
|
|
|
PathOnHost string
|
|
|
|
PathInContainer string
|
|
|
|
CgroupPermissions string
|
|
|
|
}
|
|
|
|
|
2014-08-04 19:14:43 -04:00
|
|
|
type RestartPolicy struct {
|
|
|
|
Name string
|
|
|
|
MaximumRetryCount int
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
type HostConfig struct {
|
2014-05-02 17:06:05 -04:00
|
|
|
Binds []string
|
|
|
|
ContainerIDFile string
|
|
|
|
LxcConf []utils.KeyValuePair
|
|
|
|
Privileged bool
|
|
|
|
PortBindings nat.PortMap
|
|
|
|
Links []string
|
|
|
|
PublishAllPorts bool
|
|
|
|
Dns []string
|
|
|
|
DnsSearch []string
|
2014-09-13 00:35:59 -04:00
|
|
|
ExtraHosts []string
|
2014-05-02 17:06:05 -04:00
|
|
|
VolumesFrom []string
|
2014-05-31 00:00:47 -04:00
|
|
|
Devices []DeviceMapping
|
2014-05-02 19:59:28 -04:00
|
|
|
NetworkMode NetworkMode
|
2014-11-10 16:14:17 -05:00
|
|
|
IpcMode IpcMode
|
2014-11-25 15:10:53 -05:00
|
|
|
PidMode PidMode
|
2014-07-10 14:41:11 -04:00
|
|
|
CapAdd []string
|
|
|
|
CapDrop []string
|
2014-08-04 19:14:43 -04:00
|
|
|
RestartPolicy RestartPolicy
|
2014-11-03 17:57:18 -05:00
|
|
|
SecurityOpt []string
|
2015-01-13 16:52:51 -05:00
|
|
|
ReadonlyRootfs bool
|
2015-02-11 14:21:38 -05:00
|
|
|
Ulimits []*ulimit.Ulimit
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
|
|
|
|
2014-07-18 20:38:27 -04:00
|
|
|
// This is used by the create command when you want to set both the
|
2014-03-10 09:11:23 -04:00
|
|
|
// Config and the HostConfig in the same call
|
|
|
|
type ConfigAndHostConfig struct {
|
|
|
|
Config
|
|
|
|
HostConfig HostConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func MergeConfigs(config *Config, hostConfig *HostConfig) *ConfigAndHostConfig {
|
|
|
|
return &ConfigAndHostConfig{
|
|
|
|
*config,
|
|
|
|
*hostConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
|
2014-03-10 09:11:23 -04:00
|
|
|
if job.EnvExists("HostConfig") {
|
|
|
|
hostConfig := HostConfig{}
|
|
|
|
job.GetenvJson("HostConfig", &hostConfig)
|
|
|
|
return &hostConfig
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
hostConfig := &HostConfig{
|
|
|
|
ContainerIDFile: job.Getenv("ContainerIDFile"),
|
|
|
|
Privileged: job.GetenvBool("Privileged"),
|
|
|
|
PublishAllPorts: job.GetenvBool("PublishAllPorts"),
|
2014-05-02 19:59:28 -04:00
|
|
|
NetworkMode: NetworkMode(job.Getenv("NetworkMode")),
|
2014-11-10 16:14:17 -05:00
|
|
|
IpcMode: IpcMode(job.Getenv("IpcMode")),
|
2014-11-25 15:10:53 -05:00
|
|
|
PidMode: PidMode(job.Getenv("PidMode")),
|
2015-01-13 16:52:51 -05:00
|
|
|
ReadonlyRootfs: job.GetenvBool("ReadonlyRootfs"),
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
job.GetenvJson("LxcConf", &hostConfig.LxcConf)
|
|
|
|
job.GetenvJson("PortBindings", &hostConfig.PortBindings)
|
2014-05-31 00:00:47 -04:00
|
|
|
job.GetenvJson("Devices", &hostConfig.Devices)
|
2014-08-04 19:14:43 -04:00
|
|
|
job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
|
2015-02-11 14:21:38 -05:00
|
|
|
|
|
|
|
job.GetenvJson("Ulimits", &hostConfig.Ulimits)
|
|
|
|
|
2014-11-03 17:57:18 -05:00
|
|
|
hostConfig.SecurityOpt = job.GetenvList("SecurityOpt")
|
2014-02-11 23:04:39 -05:00
|
|
|
if Binds := job.GetenvList("Binds"); Binds != nil {
|
|
|
|
hostConfig.Binds = Binds
|
|
|
|
}
|
|
|
|
if Links := job.GetenvList("Links"); Links != nil {
|
|
|
|
hostConfig.Links = Links
|
|
|
|
}
|
2014-04-07 22:12:22 -04:00
|
|
|
if Dns := job.GetenvList("Dns"); Dns != nil {
|
|
|
|
hostConfig.Dns = Dns
|
|
|
|
}
|
|
|
|
if DnsSearch := job.GetenvList("DnsSearch"); DnsSearch != nil {
|
|
|
|
hostConfig.DnsSearch = DnsSearch
|
|
|
|
}
|
2014-09-13 00:35:59 -04:00
|
|
|
if ExtraHosts := job.GetenvList("ExtraHosts"); ExtraHosts != nil {
|
|
|
|
hostConfig.ExtraHosts = ExtraHosts
|
|
|
|
}
|
2014-04-08 06:02:17 -04:00
|
|
|
if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
|
|
|
|
hostConfig.VolumesFrom = VolumesFrom
|
|
|
|
}
|
2014-07-10 17:51:15 -04:00
|
|
|
if CapAdd := job.GetenvList("CapAdd"); CapAdd != nil {
|
|
|
|
hostConfig.CapAdd = CapAdd
|
|
|
|
}
|
|
|
|
if CapDrop := job.GetenvList("CapDrop"); CapDrop != nil {
|
|
|
|
hostConfig.CapDrop = CapDrop
|
|
|
|
}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
return hostConfig
|
|
|
|
}
|