merge.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package runconfig
  2. import (
  3. "github.com/dotcloud/docker/nat"
  4. "github.com/dotcloud/docker/utils"
  5. "strings"
  6. )
  7. func Merge(userConf, imageConf *Config) error {
  8. if userConf.User == "" {
  9. userConf.User = imageConf.User
  10. }
  11. if userConf.Memory == 0 {
  12. userConf.Memory = imageConf.Memory
  13. }
  14. if userConf.MemorySwap == 0 {
  15. userConf.MemorySwap = imageConf.MemorySwap
  16. }
  17. if userConf.CpuShares == 0 {
  18. userConf.CpuShares = imageConf.CpuShares
  19. }
  20. if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
  21. userConf.ExposedPorts = imageConf.ExposedPorts
  22. } else if imageConf.ExposedPorts != nil {
  23. if userConf.ExposedPorts == nil {
  24. userConf.ExposedPorts = make(nat.PortSet)
  25. }
  26. for port := range imageConf.ExposedPorts {
  27. if _, exists := userConf.ExposedPorts[port]; !exists {
  28. userConf.ExposedPorts[port] = struct{}{}
  29. }
  30. }
  31. }
  32. if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
  33. if userConf.ExposedPorts == nil {
  34. userConf.ExposedPorts = make(nat.PortSet)
  35. }
  36. ports, _, err := nat.ParsePortSpecs(userConf.PortSpecs)
  37. if err != nil {
  38. return err
  39. }
  40. for port := range ports {
  41. if _, exists := userConf.ExposedPorts[port]; !exists {
  42. userConf.ExposedPorts[port] = struct{}{}
  43. }
  44. }
  45. userConf.PortSpecs = nil
  46. }
  47. if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
  48. // FIXME: I think we can safely remove this. Leaving it for now for the sake of reverse-compat paranoia.
  49. utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
  50. if userConf.ExposedPorts == nil {
  51. userConf.ExposedPorts = make(nat.PortSet)
  52. }
  53. ports, _, err := nat.ParsePortSpecs(imageConf.PortSpecs)
  54. if err != nil {
  55. return err
  56. }
  57. for port := range ports {
  58. if _, exists := userConf.ExposedPorts[port]; !exists {
  59. userConf.ExposedPorts[port] = struct{}{}
  60. }
  61. }
  62. }
  63. if userConf.Env == nil || len(userConf.Env) == 0 {
  64. userConf.Env = imageConf.Env
  65. } else {
  66. for _, imageEnv := range imageConf.Env {
  67. found := false
  68. imageEnvKey := strings.Split(imageEnv, "=")[0]
  69. for _, userEnv := range userConf.Env {
  70. userEnvKey := strings.Split(userEnv, "=")[0]
  71. if imageEnvKey == userEnvKey {
  72. found = true
  73. }
  74. }
  75. if !found {
  76. userConf.Env = append(userConf.Env, imageEnv)
  77. }
  78. }
  79. }
  80. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  81. userConf.Cmd = imageConf.Cmd
  82. }
  83. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  84. userConf.Entrypoint = imageConf.Entrypoint
  85. }
  86. if userConf.WorkingDir == "" {
  87. userConf.WorkingDir = imageConf.WorkingDir
  88. }
  89. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  90. userConf.Volumes = imageConf.Volumes
  91. } else {
  92. for k, v := range imageConf.Volumes {
  93. userConf.Volumes[k] = v
  94. }
  95. }
  96. return nil
  97. }