opts.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/compose-cli/utils"
  19. "github.com/docker/docker/pkg/namesgenerator"
  20. "github.com/docker/go-connections/nat"
  21. "github.com/docker/compose-cli/containers"
  22. "github.com/docker/compose-cli/formatter"
  23. )
  24. // Opts contain run command options
  25. type Opts struct {
  26. Name string
  27. Publish []string
  28. Labels []string
  29. Volumes []string
  30. Cpus float64
  31. Memory formatter.MemBytes
  32. Detach bool
  33. Environment []string
  34. RestartPolicyCondition string
  35. }
  36. // ToContainerConfig convert run options to a container configuration
  37. func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
  38. if r.Name == "" {
  39. r.Name = getRandomName()
  40. }
  41. publish, err := r.toPorts()
  42. if err != nil {
  43. return containers.ContainerConfig{}, err
  44. }
  45. labels, err := toLabels(r.Labels)
  46. if err != nil {
  47. return containers.ContainerConfig{}, err
  48. }
  49. restartPolicy, err := toRestartPolicy(r.RestartPolicyCondition)
  50. if err != nil {
  51. return containers.ContainerConfig{}, err
  52. }
  53. return containers.ContainerConfig{
  54. ID: r.Name,
  55. Image: image,
  56. Ports: publish,
  57. Labels: labels,
  58. Volumes: r.Volumes,
  59. MemLimit: r.Memory,
  60. CPULimit: r.Cpus,
  61. Environment: r.Environment,
  62. RestartPolicyCondition: restartPolicy,
  63. }, nil
  64. }
  65. func toRestartPolicy(value string) (string, error) {
  66. if value == "" {
  67. return containers.RestartPolicyNone, nil
  68. }
  69. if utils.StringContains(containers.RestartPolicyList, value) {
  70. return value, nil
  71. }
  72. return "", fmt.Errorf("invalid restart value, must be one of %s", strings.Join(containers.RestartPolicyList, ", "))
  73. }
  74. func (r *Opts) toPorts() ([]containers.Port, error) {
  75. _, bindings, err := nat.ParsePortSpecs(r.Publish)
  76. if err != nil {
  77. return nil, err
  78. }
  79. var result []containers.Port
  80. for port, bind := range bindings {
  81. for _, portbind := range bind {
  82. var hostPort uint32
  83. if portbind.HostPort != "" {
  84. hp, err := strconv.Atoi(portbind.HostPort)
  85. if err != nil {
  86. return nil, err
  87. }
  88. hostPort = uint32(hp)
  89. } else {
  90. hostPort = uint32(port.Int())
  91. }
  92. result = append(result, containers.Port{
  93. HostPort: hostPort,
  94. ContainerPort: uint32(port.Int()),
  95. Protocol: port.Proto(),
  96. HostIP: portbind.HostIP,
  97. })
  98. }
  99. }
  100. return result, nil
  101. }
  102. func toLabels(labels []string) (map[string]string, error) {
  103. result := map[string]string{}
  104. for _, label := range labels {
  105. parts := strings.Split(label, "=")
  106. if len(parts) != 2 {
  107. return nil, fmt.Errorf("wrong label format %q", label)
  108. }
  109. result[parts[0]] = parts[1]
  110. }
  111. return result, nil
  112. }
  113. func getRandomName() string {
  114. // Azure supports hyphen but not underscore in names
  115. return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
  116. }