github.com/dougm/docker@v1.5.0/runconfig/hostconfig.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/engine"
     7  	"github.com/docker/docker/nat"
     8  	"github.com/docker/docker/utils"
     9  )
    10  
    11  type NetworkMode string
    12  
    13  // IsPrivate indicates whether container use it's private network stack
    14  func (n NetworkMode) IsPrivate() bool {
    15  	return !(n.IsHost() || n.IsContainer() || n.IsNone())
    16  }
    17  
    18  func (n NetworkMode) IsHost() bool {
    19  	return n == "host"
    20  }
    21  
    22  func (n NetworkMode) IsContainer() bool {
    23  	parts := strings.SplitN(string(n), ":", 2)
    24  	return len(parts) > 1 && parts[0] == "container"
    25  }
    26  
    27  func (n NetworkMode) IsNone() bool {
    28  	return n == "none"
    29  }
    30  
    31  type IpcMode string
    32  
    33  // IsPrivate indicates whether container use it's private ipc stack
    34  func (n IpcMode) IsPrivate() bool {
    35  	return !(n.IsHost() || n.IsContainer())
    36  }
    37  
    38  func (n IpcMode) IsHost() bool {
    39  	return n == "host"
    40  }
    41  
    42  func (n IpcMode) IsContainer() bool {
    43  	parts := strings.SplitN(string(n), ":", 2)
    44  	return len(parts) > 1 && parts[0] == "container"
    45  }
    46  
    47  func (n IpcMode) Valid() bool {
    48  	parts := strings.Split(string(n), ":")
    49  	switch mode := parts[0]; mode {
    50  	case "", "host":
    51  	case "container":
    52  		if len(parts) != 2 || parts[1] == "" {
    53  			return false
    54  		}
    55  	default:
    56  		return false
    57  	}
    58  	return true
    59  }
    60  
    61  func (n IpcMode) Container() string {
    62  	parts := strings.SplitN(string(n), ":", 2)
    63  	if len(parts) > 1 {
    64  		return parts[1]
    65  	}
    66  	return ""
    67  }
    68  
    69  type PidMode string
    70  
    71  // IsPrivate indicates whether container use it's private pid stack
    72  func (n PidMode) IsPrivate() bool {
    73  	return !(n.IsHost())
    74  }
    75  
    76  func (n PidMode) IsHost() bool {
    77  	return n == "host"
    78  }
    79  
    80  func (n PidMode) Valid() bool {
    81  	parts := strings.Split(string(n), ":")
    82  	switch mode := parts[0]; mode {
    83  	case "", "host":
    84  	default:
    85  		return false
    86  	}
    87  	return true
    88  }
    89  
    90  type DeviceMapping struct {
    91  	PathOnHost        string
    92  	PathInContainer   string
    93  	CgroupPermissions string
    94  }
    95  
    96  type RestartPolicy struct {
    97  	Name              string
    98  	MaximumRetryCount int
    99  }
   100  
   101  type HostConfig struct {
   102  	Binds           []string
   103  	ContainerIDFile string
   104  	LxcConf         []utils.KeyValuePair
   105  	Privileged      bool
   106  	PortBindings    nat.PortMap
   107  	Links           []string
   108  	PublishAllPorts bool
   109  	Dns             []string
   110  	DnsSearch       []string
   111  	ExtraHosts      []string
   112  	VolumesFrom     []string
   113  	Devices         []DeviceMapping
   114  	NetworkMode     NetworkMode
   115  	IpcMode         IpcMode
   116  	PidMode         PidMode
   117  	CapAdd          []string
   118  	CapDrop         []string
   119  	RestartPolicy   RestartPolicy
   120  	SecurityOpt     []string
   121  	ReadonlyRootfs  bool
   122  }
   123  
   124  // This is used by the create command when you want to set both the
   125  // Config and the HostConfig in the same call
   126  type ConfigAndHostConfig struct {
   127  	Config
   128  	HostConfig HostConfig
   129  }
   130  
   131  func MergeConfigs(config *Config, hostConfig *HostConfig) *ConfigAndHostConfig {
   132  	return &ConfigAndHostConfig{
   133  		*config,
   134  		*hostConfig,
   135  	}
   136  }
   137  
   138  func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
   139  	if job.EnvExists("HostConfig") {
   140  		hostConfig := HostConfig{}
   141  		job.GetenvJson("HostConfig", &hostConfig)
   142  		return &hostConfig
   143  	}
   144  
   145  	hostConfig := &HostConfig{
   146  		ContainerIDFile: job.Getenv("ContainerIDFile"),
   147  		Privileged:      job.GetenvBool("Privileged"),
   148  		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
   149  		NetworkMode:     NetworkMode(job.Getenv("NetworkMode")),
   150  		IpcMode:         IpcMode(job.Getenv("IpcMode")),
   151  		PidMode:         PidMode(job.Getenv("PidMode")),
   152  		ReadonlyRootfs:  job.GetenvBool("ReadonlyRootfs"),
   153  	}
   154  
   155  	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
   156  	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
   157  	job.GetenvJson("Devices", &hostConfig.Devices)
   158  	job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
   159  	hostConfig.SecurityOpt = job.GetenvList("SecurityOpt")
   160  	if Binds := job.GetenvList("Binds"); Binds != nil {
   161  		hostConfig.Binds = Binds
   162  	}
   163  	if Links := job.GetenvList("Links"); Links != nil {
   164  		hostConfig.Links = Links
   165  	}
   166  	if Dns := job.GetenvList("Dns"); Dns != nil {
   167  		hostConfig.Dns = Dns
   168  	}
   169  	if DnsSearch := job.GetenvList("DnsSearch"); DnsSearch != nil {
   170  		hostConfig.DnsSearch = DnsSearch
   171  	}
   172  	if ExtraHosts := job.GetenvList("ExtraHosts"); ExtraHosts != nil {
   173  		hostConfig.ExtraHosts = ExtraHosts
   174  	}
   175  	if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
   176  		hostConfig.VolumesFrom = VolumesFrom
   177  	}
   178  	if CapAdd := job.GetenvList("CapAdd"); CapAdd != nil {
   179  		hostConfig.CapAdd = CapAdd
   180  	}
   181  	if CapDrop := job.GetenvList("CapDrop"); CapDrop != nil {
   182  		hostConfig.CapDrop = CapDrop
   183  	}
   184  
   185  	return hostConfig
   186  }