compose.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 local
  14. import (
  15. "bufio"
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "io"
  20. "os"
  21. "os/exec"
  22. "path/filepath"
  23. "strings"
  24. types2 "github.com/docker/docker/api/types"
  25. "github.com/docker/docker/api/types/filters"
  26. "github.com/docker/compose-cli/api/compose"
  27. "github.com/docker/compose-cli/errdefs"
  28. "github.com/aws/aws-sdk-go/aws"
  29. "github.com/compose-spec/compose-go/types"
  30. "github.com/pkg/errors"
  31. "github.com/sanathkr/go-yaml"
  32. "golang.org/x/mod/semver"
  33. )
  34. func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, detach bool) error {
  35. cmd := exec.Command("docker-compose", "version", "--short")
  36. b := bytes.Buffer{}
  37. b.WriteString("v")
  38. cmd.Stdout = bufio.NewWriter(&b)
  39. err := cmd.Run()
  40. if err != nil {
  41. return errors.Wrap(err, "ECS simulation mode require Docker-compose 1.27")
  42. }
  43. version := semver.MajorMinor(strings.TrimSpace(b.String()))
  44. if version == "" {
  45. return fmt.Errorf("can't parse docker-compose version: %s", b.String())
  46. }
  47. if semver.Compare(version, "v1.27") < 0 {
  48. return fmt.Errorf("ECS simulation mode require Docker-compose 1.27, found %s", version)
  49. }
  50. converted, err := e.Convert(ctx, project)
  51. if err != nil {
  52. return err
  53. }
  54. cmd = exec.Command("docker-compose", "--context", "default", "--project-directory", project.WorkingDir, "--project-name", project.Name, "-f", "-", "up")
  55. cmd.Stdin = strings.NewReader(string(converted))
  56. cmd.Stdout = os.Stdout
  57. cmd.Stderr = os.Stderr
  58. return cmd.Run()
  59. }
  60. func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project) ([]byte, error) {
  61. project.Networks["credentials_network"] = types.NetworkConfig{
  62. Driver: "bridge",
  63. Ipam: types.IPAMConfig{
  64. Config: []*types.IPAMPool{
  65. {
  66. Subnet: "169.254.170.0/24",
  67. Gateway: "169.254.170.1",
  68. },
  69. },
  70. },
  71. }
  72. // On Windows, this directory can be found at "%UserProfile%\.aws"
  73. home, err := os.UserHomeDir()
  74. if err != nil {
  75. return nil, err
  76. }
  77. for i, service := range project.Services {
  78. service.Networks["credentials_network"] = &types.ServiceNetworkConfig{
  79. Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3),
  80. }
  81. if service.DependsOn == nil {
  82. service.DependsOn = types.DependsOnConfig{}
  83. }
  84. service.DependsOn["ecs-local-endpoints"] = types.ServiceDependency{
  85. Condition: types.ServiceConditionStarted,
  86. }
  87. service.Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = aws.String("/creds")
  88. service.Environment["ECS_CONTAINER_METADATA_URI"] = aws.String("http://169.254.170.2/v3")
  89. project.Services[i] = service
  90. }
  91. project.Services = append(project.Services, types.ServiceConfig{
  92. Name: "ecs-local-endpoints",
  93. Image: "amazon/amazon-ecs-local-container-endpoints",
  94. Volumes: []types.ServiceVolumeConfig{
  95. {
  96. Type: types.VolumeTypeBind,
  97. Source: "/var/run",
  98. Target: "/var/run",
  99. },
  100. {
  101. Type: types.VolumeTypeBind,
  102. Source: filepath.Join(home, ".aws"),
  103. Target: "/home/.aws",
  104. },
  105. },
  106. Environment: map[string]*string{
  107. "HOME": aws.String("/home"),
  108. "AWS_PROFILE": aws.String("default"),
  109. },
  110. Networks: map[string]*types.ServiceNetworkConfig{
  111. "credentials_network": {
  112. Ipv4Address: "169.254.170.2",
  113. },
  114. },
  115. })
  116. delete(project.Networks, "default")
  117. config := map[string]interface{}{
  118. "services": project.Services,
  119. "networks": project.Networks,
  120. "volumes": project.Volumes,
  121. "secrets": project.Secrets,
  122. "configs": project.Configs,
  123. }
  124. return yaml.Marshal(config)
  125. }
  126. func (e ecsLocalSimulation) Down(ctx context.Context, projectName string) error {
  127. cmd := exec.Command("docker-compose", "--context", "default", "--project-name", projectName, "-f", "-", "down", "--remove-orphans")
  128. cmd.Stdin = strings.NewReader(string(`
  129. services:
  130. ecs-local-endpoints:
  131. image: "amazon/amazon-ecs-local-container-endpoints"
  132. `))
  133. cmd.Stdout = os.Stdout
  134. cmd.Stderr = os.Stderr
  135. return cmd.Run()
  136. }
  137. func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, w io.Writer) error {
  138. list, err := e.moby.ContainerList(ctx, types2.ContainerListOptions{
  139. Filters: filters.NewArgs(filters.Arg("label", "com.docker.compose.project="+projectName)),
  140. })
  141. if err != nil {
  142. return err
  143. }
  144. services := map[string]types.ServiceConfig{}
  145. for _, c := range list {
  146. services[c.Labels["com.docker.compose.service"]] = types.ServiceConfig{
  147. Image: "unused",
  148. }
  149. }
  150. marshal, err := yaml.Marshal(map[string]interface{}{
  151. "services": services,
  152. })
  153. if err != nil {
  154. return err
  155. }
  156. cmd := exec.Command("docker-compose", "--context", "default", "--project-name", projectName, "-f", "-", "logs", "-f")
  157. cmd.Stdin = strings.NewReader(string(marshal))
  158. cmd.Stdout = os.Stdout
  159. cmd.Stderr = os.Stderr
  160. return cmd.Run()
  161. }
  162. func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) {
  163. return nil, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose ps")
  164. }
  165. func (e ecsLocalSimulation) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
  166. return nil, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose ls")
  167. }