opts.go 4.0 KB

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