compose.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 compose
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "strings"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/pkg/errors"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/docker/cli/cli/config/configfile"
  24. moby "github.com/docker/docker/api/types"
  25. "github.com/docker/docker/client"
  26. "github.com/sanathkr/go-yaml"
  27. )
  28. // Separator is used for naming components
  29. var Separator = "-"
  30. // NewComposeService create a local implementation of the compose.Service API
  31. func NewComposeService(apiClient client.APIClient, configFile *configfile.ConfigFile) api.Service {
  32. return &composeService{
  33. apiClient: apiClient,
  34. configFile: configFile,
  35. }
  36. }
  37. type composeService struct {
  38. apiClient client.APIClient
  39. configFile *configfile.ConfigFile
  40. }
  41. func getCanonicalContainerName(c moby.Container) string {
  42. // Names return container canonical name /foo + link aliases /linked_by/foo
  43. for _, name := range c.Names {
  44. if strings.LastIndex(name, "/") == 0 {
  45. return name[1:]
  46. }
  47. }
  48. return c.Names[0][1:]
  49. }
  50. func getContainerNameWithoutProject(c moby.Container) string {
  51. name := getCanonicalContainerName(c)
  52. project := c.Labels[api.ProjectLabel]
  53. prefix := fmt.Sprintf("%s_%s_", project, c.Labels[api.ServiceLabel])
  54. if strings.HasPrefix(name, prefix) {
  55. return name[len(project)+1:]
  56. }
  57. return name
  58. }
  59. func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
  60. switch options.Format {
  61. case "json":
  62. marshal, err := json.MarshalIndent(project, "", " ")
  63. if err != nil {
  64. return nil, err
  65. }
  66. return escapeDollarSign(marshal), nil
  67. case "yaml":
  68. marshal, err := yaml.Marshal(project)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return escapeDollarSign(marshal), nil
  73. default:
  74. return nil, fmt.Errorf("unsupported format %q", options)
  75. }
  76. }
  77. func escapeDollarSign(marshal []byte) []byte {
  78. dollar := []byte{'$'}
  79. escDollar := []byte{'$', '$'}
  80. return bytes.ReplaceAll(marshal, dollar, escDollar)
  81. }
  82. // projectFromName builds a types.Project based on actual resources with compose labels set
  83. func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) {
  84. project := &types.Project{
  85. Name: projectName,
  86. }
  87. if len(containers) == 0 {
  88. return project, errors.New("no such project: " + projectName)
  89. }
  90. set := map[string]*types.ServiceConfig{}
  91. for _, c := range containers {
  92. serviceLabel := c.Labels[api.ServiceLabel]
  93. _, ok := set[serviceLabel]
  94. if !ok {
  95. set[serviceLabel] = &types.ServiceConfig{
  96. Name: serviceLabel,
  97. Image: c.Image,
  98. Labels: c.Labels,
  99. }
  100. }
  101. set[serviceLabel].Scale++
  102. }
  103. for _, service := range set {
  104. dependencies := service.Labels[api.DependenciesLabel]
  105. if len(dependencies) > 0 {
  106. service.DependsOn = types.DependsOnConfig{}
  107. for _, dc := range strings.Split(dependencies, ",") {
  108. dcArr := strings.Split(dc, ":")
  109. condition := ServiceConditionRunningOrHealthy
  110. dependency := dcArr[0]
  111. // backward compatibility
  112. if len(dcArr) > 1 {
  113. condition = dcArr[1]
  114. }
  115. service.DependsOn[dependency] = types.ServiceDependency{Condition: condition}
  116. }
  117. }
  118. project.Services = append(project.Services, *service)
  119. }
  120. SERVICES:
  121. for _, qs := range services {
  122. for _, es := range project.Services {
  123. if es.Name == qs {
  124. continue SERVICES
  125. }
  126. }
  127. return project, errors.New("no such service: " + qs)
  128. }
  129. err := project.ForServices(services)
  130. if err != nil {
  131. return project, err
  132. }
  133. return project, nil
  134. }