utils.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "strconv"
  6. "strings"
  7. )
  8. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  9. // If OpenStdin is set, then it differs
  10. func CompareConfig(a, b *Config) bool {
  11. if a == nil || b == nil ||
  12. a.OpenStdin || b.OpenStdin {
  13. return false
  14. }
  15. if a.AttachStdout != b.AttachStdout ||
  16. a.AttachStderr != b.AttachStderr ||
  17. a.User != b.User ||
  18. a.Memory != b.Memory ||
  19. a.MemorySwap != b.MemorySwap ||
  20. a.CpuShares != b.CpuShares ||
  21. a.OpenStdin != b.OpenStdin ||
  22. a.Tty != b.Tty ||
  23. a.VolumesFrom != b.VolumesFrom {
  24. return false
  25. }
  26. if len(a.Cmd) != len(b.Cmd) ||
  27. len(a.Dns) != len(b.Dns) ||
  28. len(a.Env) != len(b.Env) ||
  29. len(a.PortSpecs) != len(b.PortSpecs) ||
  30. len(a.ExposedPorts) != len(b.ExposedPorts) ||
  31. len(a.Entrypoint) != len(b.Entrypoint) ||
  32. len(a.Volumes) != len(b.Volumes) {
  33. return false
  34. }
  35. for i := 0; i < len(a.Cmd); i++ {
  36. if a.Cmd[i] != b.Cmd[i] {
  37. return false
  38. }
  39. }
  40. for i := 0; i < len(a.Dns); i++ {
  41. if a.Dns[i] != b.Dns[i] {
  42. return false
  43. }
  44. }
  45. for i := 0; i < len(a.Env); i++ {
  46. if a.Env[i] != b.Env[i] {
  47. return false
  48. }
  49. }
  50. for i := 0; i < len(a.PortSpecs); i++ {
  51. if a.PortSpecs[i] != b.PortSpecs[i] {
  52. return false
  53. }
  54. }
  55. for k := range a.ExposedPorts {
  56. if _, exists := b.ExposedPorts[k]; !exists {
  57. return false
  58. }
  59. }
  60. for i := 0; i < len(a.Entrypoint); i++ {
  61. if a.Entrypoint[i] != b.Entrypoint[i] {
  62. return false
  63. }
  64. }
  65. for key := range a.Volumes {
  66. if _, exists := b.Volumes[key]; !exists {
  67. return false
  68. }
  69. }
  70. return true
  71. }
  72. func MergeConfig(userConf, imageConf *Config) error {
  73. if userConf.User == "" {
  74. userConf.User = imageConf.User
  75. }
  76. if userConf.Memory == 0 {
  77. userConf.Memory = imageConf.Memory
  78. }
  79. if userConf.MemorySwap == 0 {
  80. userConf.MemorySwap = imageConf.MemorySwap
  81. }
  82. if userConf.CpuShares == 0 {
  83. userConf.CpuShares = imageConf.CpuShares
  84. }
  85. if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
  86. userConf.ExposedPorts = imageConf.ExposedPorts
  87. }
  88. if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
  89. if userConf.ExposedPorts == nil {
  90. userConf.ExposedPorts = make(map[Port]struct{})
  91. }
  92. ports, _, err := parsePortSpecs(userConf.PortSpecs)
  93. if err != nil {
  94. return err
  95. }
  96. for port := range ports {
  97. if _, exists := userConf.ExposedPorts[port]; !exists {
  98. userConf.ExposedPorts[port] = struct{}{}
  99. }
  100. }
  101. userConf.PortSpecs = nil
  102. }
  103. if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
  104. utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
  105. if userConf.ExposedPorts == nil {
  106. userConf.ExposedPorts = make(map[Port]struct{})
  107. }
  108. ports, _, err := parsePortSpecs(imageConf.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. }
  118. if !userConf.Tty {
  119. userConf.Tty = imageConf.Tty
  120. }
  121. if !userConf.OpenStdin {
  122. userConf.OpenStdin = imageConf.OpenStdin
  123. }
  124. if !userConf.StdinOnce {
  125. userConf.StdinOnce = imageConf.StdinOnce
  126. }
  127. if userConf.Env == nil || len(userConf.Env) == 0 {
  128. userConf.Env = imageConf.Env
  129. } else {
  130. for _, imageEnv := range imageConf.Env {
  131. found := false
  132. imageEnvKey := strings.Split(imageEnv, "=")[0]
  133. for _, userEnv := range userConf.Env {
  134. userEnvKey := strings.Split(userEnv, "=")[0]
  135. if imageEnvKey == userEnvKey {
  136. found = true
  137. }
  138. }
  139. if !found {
  140. userConf.Env = append(userConf.Env, imageEnv)
  141. }
  142. }
  143. }
  144. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  145. userConf.Cmd = imageConf.Cmd
  146. }
  147. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  148. userConf.Dns = imageConf.Dns
  149. } else {
  150. //duplicates aren't an issue here
  151. userConf.Dns = append(userConf.Dns, imageConf.Dns...)
  152. }
  153. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  154. userConf.Entrypoint = imageConf.Entrypoint
  155. }
  156. if userConf.WorkingDir == "" {
  157. userConf.WorkingDir = imageConf.WorkingDir
  158. }
  159. if userConf.VolumesFrom == "" {
  160. userConf.VolumesFrom = imageConf.VolumesFrom
  161. }
  162. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  163. userConf.Volumes = imageConf.Volumes
  164. } else {
  165. for k, v := range imageConf.Volumes {
  166. userConf.Volumes[k] = v
  167. }
  168. }
  169. return nil
  170. }
  171. func parseLxcConfOpts(opts ListOpts) ([]KeyValuePair, error) {
  172. out := make([]KeyValuePair, len(opts))
  173. for i, o := range opts {
  174. k, v, err := parseLxcOpt(o)
  175. if err != nil {
  176. return nil, err
  177. }
  178. out[i] = KeyValuePair{Key: k, Value: v}
  179. }
  180. return out, nil
  181. }
  182. func parseLxcOpt(opt string) (string, string, error) {
  183. parts := strings.SplitN(opt, "=", 2)
  184. if len(parts) != 2 {
  185. return "", "", fmt.Errorf("Unable to parse lxc conf option: %s", opt)
  186. }
  187. return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
  188. }
  189. // We will receive port specs in the format of ip:public:private/proto and these need to be
  190. // parsed in the internal types
  191. func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
  192. exposedPorts := make(map[Port]struct{}, len(ports))
  193. bindings := make(map[Port][]PortBinding)
  194. for _, rawPort := range ports {
  195. proto := "tcp"
  196. if i := strings.LastIndex(rawPort, "/"); i != -1 {
  197. proto = rawPort[i+1:]
  198. rawPort = rawPort[:i]
  199. }
  200. if !strings.Contains(rawPort, ":") {
  201. rawPort = fmt.Sprintf("::%s", rawPort)
  202. } else if len(strings.Split(rawPort, ":")) == 2 {
  203. rawPort = fmt.Sprintf(":%s", rawPort)
  204. }
  205. parts, err := utils.PartParser("ip:hostPort:containerPort", rawPort)
  206. if err != nil {
  207. return nil, nil, err
  208. }
  209. containerPort := parts["containerPort"]
  210. rawIp := parts["ip"]
  211. hostPort := parts["hostPort"]
  212. if containerPort == "" {
  213. return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
  214. }
  215. port := NewPort(proto, containerPort)
  216. if _, exists := exposedPorts[port]; !exists {
  217. exposedPorts[port] = struct{}{}
  218. }
  219. binding := PortBinding{
  220. HostIp: rawIp,
  221. HostPort: hostPort,
  222. }
  223. bslice, exists := bindings[port]
  224. if !exists {
  225. bslice = []PortBinding{}
  226. }
  227. bindings[port] = append(bslice, binding)
  228. }
  229. return exposedPorts, bindings, nil
  230. }
  231. // Splits a port in the format of port/proto
  232. func splitProtoPort(rawPort string) (string, string) {
  233. parts := strings.Split(rawPort, "/")
  234. l := len(parts)
  235. if l == 0 {
  236. return "", ""
  237. }
  238. if l == 1 {
  239. return "tcp", rawPort
  240. }
  241. return parts[0], parts[1]
  242. }
  243. func parsePort(rawPort string) (int, error) {
  244. port, err := strconv.ParseUint(rawPort, 10, 16)
  245. if err != nil {
  246. return 0, err
  247. }
  248. return int(port), nil
  249. }
  250. func migratePortMappings(config *Config) error {
  251. if config.PortSpecs != nil {
  252. // We don't have to worry about migrating the bindings to the host
  253. // This is our breaking change
  254. ports, _, err := parsePortSpecs(config.PortSpecs)
  255. if err != nil {
  256. return err
  257. }
  258. config.PortSpecs = nil
  259. if config.ExposedPorts == nil {
  260. config.ExposedPorts = make(map[Port]struct{}, len(ports))
  261. }
  262. for k, v := range ports {
  263. config.ExposedPorts[k] = v
  264. }
  265. }
  266. return nil
  267. }
  268. // Links come in the format of
  269. // name:alias
  270. func parseLink(rawLink string) (map[string]string, error) {
  271. return utils.PartParser("name:alias", rawLink)
  272. }