utils.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package docker
  2. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  3. // If OpenStdin is set, then it differs
  4. func CompareConfig(a, b *Config) bool {
  5. if a == nil || b == nil ||
  6. a.OpenStdin || b.OpenStdin {
  7. return false
  8. }
  9. if a.AttachStdout != b.AttachStdout ||
  10. a.AttachStderr != b.AttachStderr ||
  11. a.User != b.User ||
  12. a.Memory != b.Memory ||
  13. a.MemorySwap != b.MemorySwap ||
  14. a.CpuShares != b.CpuShares ||
  15. a.OpenStdin != b.OpenStdin ||
  16. a.Tty != b.Tty {
  17. return false
  18. }
  19. if len(a.Cmd) != len(b.Cmd) ||
  20. len(a.Dns) != len(b.Dns) ||
  21. len(a.Env) != len(b.Env) ||
  22. len(a.PortSpecs) != len(b.PortSpecs) {
  23. return false
  24. }
  25. for i := 0; i < len(a.Cmd); i++ {
  26. if a.Cmd[i] != b.Cmd[i] {
  27. return false
  28. }
  29. }
  30. for i := 0; i < len(a.Dns); i++ {
  31. if a.Dns[i] != b.Dns[i] {
  32. return false
  33. }
  34. }
  35. for i := 0; i < len(a.Env); i++ {
  36. if a.Env[i] != b.Env[i] {
  37. return false
  38. }
  39. }
  40. for i := 0; i < len(a.PortSpecs); i++ {
  41. if a.PortSpecs[i] != b.PortSpecs[i] {
  42. return false
  43. }
  44. }
  45. for i := 0; i < len(a.Entrypoint); i++ {
  46. if a.Entrypoint[i] != b.Entrypoint[i] {
  47. return false
  48. }
  49. }
  50. return true
  51. }
  52. func MergeConfig(userConf, imageConf *Config) {
  53. if userConf.User == "" {
  54. userConf.User = imageConf.User
  55. }
  56. if userConf.Memory == 0 {
  57. userConf.Memory = imageConf.Memory
  58. }
  59. if userConf.MemorySwap == 0 {
  60. userConf.MemorySwap = imageConf.MemorySwap
  61. }
  62. if userConf.CpuShares == 0 {
  63. userConf.CpuShares = imageConf.CpuShares
  64. }
  65. if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 {
  66. userConf.PortSpecs = imageConf.PortSpecs
  67. }
  68. if !userConf.Tty {
  69. userConf.Tty = imageConf.Tty
  70. }
  71. if !userConf.OpenStdin {
  72. userConf.OpenStdin = imageConf.OpenStdin
  73. }
  74. if !userConf.StdinOnce {
  75. userConf.StdinOnce = imageConf.StdinOnce
  76. }
  77. if userConf.Env == nil || len(userConf.Env) == 0 {
  78. userConf.Env = imageConf.Env
  79. }
  80. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  81. userConf.Cmd = imageConf.Cmd
  82. }
  83. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  84. userConf.Dns = imageConf.Dns
  85. }
  86. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  87. userConf.Entrypoint = imageConf.Entrypoint
  88. }
  89. }