utils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. return false
  21. }
  22. if len(a.Cmd) != len(b.Cmd) ||
  23. len(a.Dns) != len(b.Dns) ||
  24. len(a.Env) != len(b.Env) ||
  25. len(a.PortSpecs) != len(b.PortSpecs) ||
  26. len(a.Entrypoint) != len(b.Entrypoint) {
  27. return false
  28. }
  29. for i := 0; i < len(a.Cmd); i++ {
  30. if a.Cmd[i] != b.Cmd[i] {
  31. return false
  32. }
  33. }
  34. for i := 0; i < len(a.Dns); i++ {
  35. if a.Dns[i] != b.Dns[i] {
  36. return false
  37. }
  38. }
  39. for i := 0; i < len(a.Env); i++ {
  40. if a.Env[i] != b.Env[i] {
  41. return false
  42. }
  43. }
  44. for i := 0; i < len(a.PortSpecs); i++ {
  45. if a.PortSpecs[i] != b.PortSpecs[i] {
  46. return false
  47. }
  48. }
  49. for i := 0; i < len(a.Entrypoint); i++ {
  50. if a.Entrypoint[i] != b.Entrypoint[i] {
  51. return false
  52. }
  53. }
  54. return true
  55. }
  56. func MergeConfig(userConf, imageConf *Config) {
  57. if userConf.User == "" {
  58. userConf.User = imageConf.User
  59. }
  60. if userConf.Memory == 0 {
  61. userConf.Memory = imageConf.Memory
  62. }
  63. if userConf.MemorySwap == 0 {
  64. userConf.MemorySwap = imageConf.MemorySwap
  65. }
  66. if userConf.CpuShares == 0 {
  67. userConf.CpuShares = imageConf.CpuShares
  68. }
  69. if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 {
  70. userConf.PortSpecs = imageConf.PortSpecs
  71. } else {
  72. for _, imagePortSpec := range imageConf.PortSpecs {
  73. found := false
  74. imageNat, _ := parseNat(imagePortSpec)
  75. for _, userPortSpec := range userConf.PortSpecs {
  76. userNat, _ := parseNat(userPortSpec)
  77. if imageNat.Proto == userNat.Proto && imageNat.Backend == userNat.Backend {
  78. found = true
  79. }
  80. }
  81. if !found {
  82. userConf.PortSpecs = append(userConf.PortSpecs, imagePortSpec)
  83. }
  84. }
  85. }
  86. if !userConf.Tty {
  87. userConf.Tty = imageConf.Tty
  88. }
  89. if !userConf.OpenStdin {
  90. userConf.OpenStdin = imageConf.OpenStdin
  91. }
  92. if !userConf.StdinOnce {
  93. userConf.StdinOnce = imageConf.StdinOnce
  94. }
  95. if userConf.Env == nil || len(userConf.Env) == 0 {
  96. userConf.Env = imageConf.Env
  97. } else {
  98. for _, imageEnv := range imageConf.Env {
  99. found := false
  100. imageEnvKey := strings.Split(imageEnv, "=")[0]
  101. for _, userEnv := range userConf.Env {
  102. userEnvKey := strings.Split(userEnv, "=")[0]
  103. if imageEnvKey == userEnvKey {
  104. found = true
  105. }
  106. }
  107. if !found {
  108. userConf.Env = append(userConf.Env, imageEnv)
  109. }
  110. }
  111. }
  112. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  113. userConf.Cmd = imageConf.Cmd
  114. }
  115. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  116. userConf.Dns = imageConf.Dns
  117. } else {
  118. //duplicates aren't an issue here
  119. userConf.Dns = append(userConf.Dns, imageConf.Dns...)
  120. }
  121. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  122. userConf.Entrypoint = imageConf.Entrypoint
  123. }
  124. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  125. userConf.Volumes = imageConf.Volumes
  126. } else {
  127. for k, v := range imageConf.Volumes {
  128. userConf.Volumes[k] = v
  129. }
  130. }
  131. }