opts.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package run
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "github.com/docker/docker/pkg/namesgenerator"
  19. "github.com/docker/go-connections/nat"
  20. "github.com/docker/api/containers"
  21. "github.com/docker/api/formatter"
  22. )
  23. // Opts contain run command options
  24. type Opts struct {
  25. Name string
  26. Publish []string
  27. Labels []string
  28. Volumes []string
  29. Cpus float64
  30. Memory formatter.MemBytes
  31. Detach bool
  32. Environment []string
  33. }
  34. // ToContainerConfig convert run options to a container configuration
  35. func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
  36. if r.Name == "" {
  37. r.Name = getRandomName()
  38. }
  39. publish, err := r.toPorts()
  40. if err != nil {
  41. return containers.ContainerConfig{}, err
  42. }
  43. labels, err := toLabels(r.Labels)
  44. if err != nil {
  45. return containers.ContainerConfig{}, err
  46. }
  47. return containers.ContainerConfig{
  48. ID: r.Name,
  49. Image: image,
  50. Ports: publish,
  51. Labels: labels,
  52. Volumes: r.Volumes,
  53. MemLimit: r.Memory,
  54. CPULimit: r.Cpus,
  55. Environment: r.Environment,
  56. }, nil
  57. }
  58. func (r *Opts) toPorts() ([]containers.Port, error) {
  59. _, bindings, err := nat.ParsePortSpecs(r.Publish)
  60. if err != nil {
  61. return nil, err
  62. }
  63. var result []containers.Port
  64. for port, bind := range bindings {
  65. for _, portbind := range bind {
  66. var hostPort uint32
  67. if portbind.HostPort != "" {
  68. hp, err := strconv.Atoi(portbind.HostPort)
  69. if err != nil {
  70. return nil, err
  71. }
  72. hostPort = uint32(hp)
  73. } else {
  74. hostPort = uint32(port.Int())
  75. }
  76. result = append(result, containers.Port{
  77. HostPort: hostPort,
  78. ContainerPort: uint32(port.Int()),
  79. Protocol: port.Proto(),
  80. HostIP: portbind.HostIP,
  81. })
  82. }
  83. }
  84. return result, nil
  85. }
  86. func toLabels(labels []string) (map[string]string, error) {
  87. result := map[string]string{}
  88. for _, label := range labels {
  89. parts := strings.Split(label, "=")
  90. if len(parts) != 2 {
  91. return nil, fmt.Errorf("wrong label format %q", label)
  92. }
  93. result[parts[0]] = parts[1]
  94. }
  95. return result, nil
  96. }
  97. func getRandomName() string {
  98. // Azure supports hyphen but not underscore in names
  99. return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
  100. }