opts.go 3.6 KB

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