utils.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package daemon
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/docker/docker/nat"
  7. "github.com/docker/docker/runconfig"
  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) ([]string, error) {
  32. if hostConfig == nil {
  33. return nil, nil
  34. }
  35. out := []string{}
  36. // merge in the lxc conf options into the generic config map
  37. if lxcConf := hostConfig.LxcConf; lxcConf != nil {
  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. if !strings.Contains(pair.Key, ".") {
  42. return nil, errors.New("Illegal Key passed into LXC Configurations")
  43. }
  44. parts := strings.SplitN(pair.Key, ".", 2)
  45. out = append(out, fmt.Sprintf("%s=%s", parts[1], pair.Value))
  46. }
  47. }
  48. return out, nil
  49. }