utils.go 8.4 KB

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