hostconfig.go 4.3 KB

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