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