utils.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package daemon
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/nat"
  5. "github.com/dotcloud/docker/pkg/namesgenerator"
  6. "github.com/dotcloud/docker/runconfig"
  7. "strings"
  8. )
  9. func migratePortMappings(config *runconfig.Config, hostConfig *runconfig.HostConfig) error {
  10. if config.PortSpecs != nil {
  11. ports, bindings, err := nat.ParsePortSpecs(config.PortSpecs)
  12. if err != nil {
  13. return err
  14. }
  15. config.PortSpecs = nil
  16. if len(bindings) > 0 {
  17. if hostConfig == nil {
  18. hostConfig = &runconfig.HostConfig{}
  19. }
  20. hostConfig.PortBindings = bindings
  21. }
  22. if config.ExposedPorts == nil {
  23. config.ExposedPorts = make(nat.PortSet, len(ports))
  24. }
  25. for k, v := range ports {
  26. config.ExposedPorts[k] = v
  27. }
  28. }
  29. return nil
  30. }
  31. func mergeLxcConfIntoOptions(hostConfig *runconfig.HostConfig, driverConfig map[string][]string) {
  32. if hostConfig == nil {
  33. return
  34. }
  35. // merge in the lxc conf options into the generic config map
  36. if lxcConf := hostConfig.LxcConf; lxcConf != nil {
  37. lxc := driverConfig["lxc"]
  38. for _, pair := range lxcConf {
  39. // because lxc conf gets the driver name lxc.XXXX we need to trim it off
  40. // and let the lxc driver add it back later if needed
  41. parts := strings.SplitN(pair.Key, ".", 2)
  42. lxc = append(lxc, fmt.Sprintf("%s=%s", parts[1], pair.Value))
  43. }
  44. driverConfig["lxc"] = lxc
  45. }
  46. }
  47. type checker struct {
  48. daemon *Daemon
  49. }
  50. func (c *checker) Exists(name string) bool {
  51. return c.daemon.containerGraph.Exists("/" + name)
  52. }
  53. // Generate a random and unique name
  54. func generateRandomName(daemon *Daemon) (string, error) {
  55. return namesgenerator.GenerateRandomName(&checker{daemon})
  56. }