opts.go 3.7 KB

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