utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package docker
  2. import (
  3. "strings"
  4. )
  5. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  6. // If OpenStdin is set, then it differs
  7. func CompareConfig(a, b *Config) bool {
  8. if a == nil || b == nil ||
  9. a.OpenStdin || b.OpenStdin {
  10. return false
  11. }
  12. if a.AttachStdout != b.AttachStdout ||
  13. a.AttachStderr != b.AttachStderr ||
  14. a.User != b.User ||
  15. a.Memory != b.Memory ||
  16. a.MemorySwap != b.MemorySwap ||
  17. a.CpuShares != b.CpuShares ||
  18. a.OpenStdin != b.OpenStdin ||
  19. a.Tty != b.Tty ||
  20. a.VolumesFrom != b.VolumesFrom {
  21. return false
  22. }
  23. if len(a.Cmd) != len(b.Cmd) ||
  24. len(a.Dns) != len(b.Dns) ||
  25. len(a.Env) != len(b.Env) ||
  26. len(a.PortSpecs) != len(b.PortSpecs) ||
  27. len(a.Entrypoint) != len(b.Entrypoint) ||
  28. len(a.Volumes) != len(b.Volumes) {
  29. return false
  30. }
  31. for i := 0; i < len(a.Cmd); i++ {
  32. if a.Cmd[i] != b.Cmd[i] {
  33. return false
  34. }
  35. }
  36. for i := 0; i < len(a.Dns); i++ {
  37. if a.Dns[i] != b.Dns[i] {
  38. return false
  39. }
  40. }
  41. for i := 0; i < len(a.Env); i++ {
  42. if a.Env[i] != b.Env[i] {
  43. return false
  44. }
  45. }
  46. for i := 0; i < len(a.PortSpecs); i++ {
  47. if a.PortSpecs[i] != b.PortSpecs[i] {
  48. return false
  49. }
  50. }
  51. for i := 0; i < len(a.Entrypoint); i++ {
  52. if a.Entrypoint[i] != b.Entrypoint[i] {
  53. return false
  54. }
  55. }
  56. for key := range a.Volumes {
  57. if _, exists := b.Volumes[key]; !exists {
  58. return false
  59. }
  60. }
  61. return true
  62. }
  63. func MergeConfig(userConf, imageConf *Config) {
  64. if userConf.User == "" {
  65. userConf.User = imageConf.User
  66. }
  67. if userConf.Memory == 0 {
  68. userConf.Memory = imageConf.Memory
  69. }
  70. if userConf.MemorySwap == 0 {
  71. userConf.MemorySwap = imageConf.MemorySwap
  72. }
  73. if userConf.CpuShares == 0 {
  74. userConf.CpuShares = imageConf.CpuShares
  75. }
  76. if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 {
  77. userConf.PortSpecs = imageConf.PortSpecs
  78. } else {
  79. for _, imagePortSpec := range imageConf.PortSpecs {
  80. found := false
  81. imageNat, _ := parseNat(imagePortSpec)
  82. for _, userPortSpec := range userConf.PortSpecs {
  83. userNat, _ := parseNat(userPortSpec)
  84. if imageNat.Proto == userNat.Proto && imageNat.Frontend == userNat.Frontend {
  85. found = true
  86. }
  87. }
  88. if !found {
  89. userConf.PortSpecs = append(userConf.PortSpecs, imagePortSpec)
  90. }
  91. }
  92. }
  93. if !userConf.Tty {
  94. userConf.Tty = imageConf.Tty
  95. }
  96. if !userConf.OpenStdin {
  97. userConf.OpenStdin = imageConf.OpenStdin
  98. }
  99. if !userConf.StdinOnce {
  100. userConf.StdinOnce = imageConf.StdinOnce
  101. }
  102. if userConf.Env == nil || len(userConf.Env) == 0 {
  103. userConf.Env = imageConf.Env
  104. } else {
  105. for _, imageEnv := range imageConf.Env {
  106. found := false
  107. imageEnvKey := strings.Split(imageEnv, "=")[0]
  108. for _, userEnv := range userConf.Env {
  109. userEnvKey := strings.Split(userEnv, "=")[0]
  110. if imageEnvKey == userEnvKey {
  111. found = true
  112. }
  113. }
  114. if !found {
  115. userConf.Env = append(userConf.Env, imageEnv)
  116. }
  117. }
  118. }
  119. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  120. userConf.Cmd = imageConf.Cmd
  121. }
  122. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  123. userConf.Dns = imageConf.Dns
  124. } else {
  125. //duplicates aren't an issue here
  126. userConf.Dns = append(userConf.Dns, imageConf.Dns...)
  127. }
  128. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  129. userConf.Entrypoint = imageConf.Entrypoint
  130. }
  131. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  132. userConf.Volumes = imageConf.Volumes
  133. } else {
  134. for k, v := range imageConf.Volumes {
  135. userConf.Volumes[k] = v
  136. }
  137. }
  138. }