utils.go 8.8 KB

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