utils.go 7.8 KB

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