utils.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/archive"
  5. "github.com/dotcloud/docker/nat"
  6. "github.com/dotcloud/docker/pkg/namesgenerator"
  7. "github.com/dotcloud/docker/utils"
  8. "io"
  9. "strings"
  10. "sync/atomic"
  11. )
  12. type Change struct {
  13. archive.Change
  14. }
  15. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  16. // If OpenStdin is set, then it differs
  17. func CompareConfig(a, b *Config) bool {
  18. if a == nil || b == nil ||
  19. a.OpenStdin || b.OpenStdin {
  20. return false
  21. }
  22. if a.AttachStdout != b.AttachStdout ||
  23. a.AttachStderr != b.AttachStderr ||
  24. a.User != b.User ||
  25. a.Memory != b.Memory ||
  26. a.MemorySwap != b.MemorySwap ||
  27. a.CpuShares != b.CpuShares ||
  28. a.OpenStdin != b.OpenStdin ||
  29. a.Tty != b.Tty ||
  30. a.VolumesFrom != b.VolumesFrom {
  31. return false
  32. }
  33. if len(a.Cmd) != len(b.Cmd) ||
  34. len(a.Dns) != len(b.Dns) ||
  35. len(a.Env) != len(b.Env) ||
  36. len(a.PortSpecs) != len(b.PortSpecs) ||
  37. len(a.ExposedPorts) != len(b.ExposedPorts) ||
  38. len(a.Entrypoint) != len(b.Entrypoint) ||
  39. len(a.Volumes) != len(b.Volumes) {
  40. return false
  41. }
  42. for i := 0; i < len(a.Cmd); i++ {
  43. if a.Cmd[i] != b.Cmd[i] {
  44. return false
  45. }
  46. }
  47. for i := 0; i < len(a.Dns); i++ {
  48. if a.Dns[i] != b.Dns[i] {
  49. return false
  50. }
  51. }
  52. for i := 0; i < len(a.Env); i++ {
  53. if a.Env[i] != b.Env[i] {
  54. return false
  55. }
  56. }
  57. for i := 0; i < len(a.PortSpecs); i++ {
  58. if a.PortSpecs[i] != b.PortSpecs[i] {
  59. return false
  60. }
  61. }
  62. for k := range a.ExposedPorts {
  63. if _, exists := b.ExposedPorts[k]; !exists {
  64. return false
  65. }
  66. }
  67. for i := 0; i < len(a.Entrypoint); i++ {
  68. if a.Entrypoint[i] != b.Entrypoint[i] {
  69. return false
  70. }
  71. }
  72. for key := range a.Volumes {
  73. if _, exists := b.Volumes[key]; !exists {
  74. return false
  75. }
  76. }
  77. return true
  78. }
  79. func MergeConfig(userConf, imageConf *Config) error {
  80. if userConf.User == "" {
  81. userConf.User = imageConf.User
  82. }
  83. if userConf.Memory == 0 {
  84. userConf.Memory = imageConf.Memory
  85. }
  86. if userConf.MemorySwap == 0 {
  87. userConf.MemorySwap = imageConf.MemorySwap
  88. }
  89. if userConf.CpuShares == 0 {
  90. userConf.CpuShares = imageConf.CpuShares
  91. }
  92. if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
  93. userConf.ExposedPorts = imageConf.ExposedPorts
  94. } else if imageConf.ExposedPorts != nil {
  95. if userConf.ExposedPorts == nil {
  96. userConf.ExposedPorts = make(nat.PortSet)
  97. }
  98. for port := range imageConf.ExposedPorts {
  99. if _, exists := userConf.ExposedPorts[port]; !exists {
  100. userConf.ExposedPorts[port] = struct{}{}
  101. }
  102. }
  103. }
  104. if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
  105. if userConf.ExposedPorts == nil {
  106. userConf.ExposedPorts = make(nat.PortSet)
  107. }
  108. ports, _, err := nat.ParsePortSpecs(userConf.PortSpecs)
  109. if err != nil {
  110. return err
  111. }
  112. for port := range ports {
  113. if _, exists := userConf.ExposedPorts[port]; !exists {
  114. userConf.ExposedPorts[port] = struct{}{}
  115. }
  116. }
  117. userConf.PortSpecs = nil
  118. }
  119. if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
  120. utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
  121. if userConf.ExposedPorts == nil {
  122. userConf.ExposedPorts = make(nat.PortSet)
  123. }
  124. ports, _, err := nat.ParsePortSpecs(imageConf.PortSpecs)
  125. if err != nil {
  126. return err
  127. }
  128. for port := range ports {
  129. if _, exists := userConf.ExposedPorts[port]; !exists {
  130. userConf.ExposedPorts[port] = struct{}{}
  131. }
  132. }
  133. }
  134. if !userConf.Tty {
  135. userConf.Tty = imageConf.Tty
  136. }
  137. if !userConf.OpenStdin {
  138. userConf.OpenStdin = imageConf.OpenStdin
  139. }
  140. if !userConf.StdinOnce {
  141. userConf.StdinOnce = imageConf.StdinOnce
  142. }
  143. if userConf.Env == nil || len(userConf.Env) == 0 {
  144. userConf.Env = imageConf.Env
  145. } else {
  146. for _, imageEnv := range imageConf.Env {
  147. found := false
  148. imageEnvKey := strings.Split(imageEnv, "=")[0]
  149. for _, userEnv := range userConf.Env {
  150. userEnvKey := strings.Split(userEnv, "=")[0]
  151. if imageEnvKey == userEnvKey {
  152. found = true
  153. }
  154. }
  155. if !found {
  156. userConf.Env = append(userConf.Env, imageEnv)
  157. }
  158. }
  159. }
  160. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  161. userConf.Cmd = imageConf.Cmd
  162. }
  163. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  164. userConf.Dns = imageConf.Dns
  165. } else {
  166. //duplicates aren't an issue here
  167. userConf.Dns = append(userConf.Dns, imageConf.Dns...)
  168. }
  169. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  170. userConf.Entrypoint = imageConf.Entrypoint
  171. }
  172. if userConf.WorkingDir == "" {
  173. userConf.WorkingDir = imageConf.WorkingDir
  174. }
  175. if userConf.VolumesFrom == "" {
  176. userConf.VolumesFrom = imageConf.VolumesFrom
  177. }
  178. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  179. userConf.Volumes = imageConf.Volumes
  180. } else {
  181. for k, v := range imageConf.Volumes {
  182. userConf.Volumes[k] = v
  183. }
  184. }
  185. return nil
  186. }
  187. func parseLxcConfOpts(opts ListOpts) ([]KeyValuePair, error) {
  188. out := make([]KeyValuePair, opts.Len())
  189. for i, o := range opts.GetAll() {
  190. k, v, err := parseLxcOpt(o)
  191. if err != nil {
  192. return nil, err
  193. }
  194. out[i] = KeyValuePair{Key: k, Value: v}
  195. }
  196. return out, nil
  197. }
  198. func parseLxcOpt(opt string) (string, string, error) {
  199. parts := strings.SplitN(opt, "=", 2)
  200. if len(parts) != 2 {
  201. return "", "", fmt.Errorf("Unable to parse lxc conf option: %s", opt)
  202. }
  203. return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
  204. }
  205. func migratePortMappings(config *Config, hostConfig *HostConfig) error {
  206. if config.PortSpecs != nil {
  207. ports, bindings, err := nat.ParsePortSpecs(config.PortSpecs)
  208. if err != nil {
  209. return err
  210. }
  211. config.PortSpecs = nil
  212. if len(bindings) > 0 {
  213. if hostConfig == nil {
  214. hostConfig = &HostConfig{}
  215. }
  216. hostConfig.PortBindings = bindings
  217. }
  218. if config.ExposedPorts == nil {
  219. config.ExposedPorts = make(nat.PortSet, len(ports))
  220. }
  221. for k, v := range ports {
  222. config.ExposedPorts[k] = v
  223. }
  224. }
  225. return nil
  226. }
  227. // Links come in the format of
  228. // name:alias
  229. func parseLink(rawLink string) (map[string]string, error) {
  230. return utils.PartParser("name:alias", rawLink)
  231. }
  232. type checker struct {
  233. runtime *Runtime
  234. }
  235. func (c *checker) Exists(name string) bool {
  236. return c.runtime.containerGraph.Exists("/" + name)
  237. }
  238. // Generate a random and unique name
  239. func generateRandomName(runtime *Runtime) (string, error) {
  240. return namesgenerator.GenerateRandomName(&checker{runtime})
  241. }
  242. // Read an io.Reader and call a function when it returns EOF
  243. func EofReader(r io.Reader, callback func()) *eofReader {
  244. return &eofReader{
  245. Reader: r,
  246. callback: callback,
  247. }
  248. }
  249. type eofReader struct {
  250. io.Reader
  251. gotEOF int32
  252. callback func()
  253. }
  254. func (r *eofReader) Read(p []byte) (n int, err error) {
  255. n, err = r.Reader.Read(p)
  256. if err == io.EOF {
  257. // Use atomics to make the gotEOF check threadsafe
  258. if atomic.CompareAndSwapInt32(&r.gotEOF, 0, 1) {
  259. r.callback()
  260. }
  261. }
  262. return
  263. }