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