compose.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "github.com/docker/compose-cli/compose"
  25. "github.com/docker/compose-cli/errdefs"
  26. "github.com/aws/aws-sdk-go/aws"
  27. "github.com/compose-spec/compose-go/types"
  28. "github.com/pkg/errors"
  29. "github.com/sanathkr/go-yaml"
  30. "golang.org/x/mod/semver"
  31. )
  32. func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project) error {
  33. cmd := exec.Command("docker-compose", "version", "--short")
  34. b := bytes.Buffer{}
  35. b.WriteString("v")
  36. cmd.Stdout = bufio.NewWriter(&b)
  37. err := cmd.Run()
  38. if err != nil {
  39. return errors.Wrap(err, "ECS simulation mode require Docker-compose 1.27")
  40. }
  41. version := semver.MajorMinor(strings.TrimSpace(b.String()))
  42. if version == "" {
  43. return fmt.Errorf("can't parse docker-compose version: %s", b.String())
  44. }
  45. if semver.Compare(version, "v1.27") < 0 {
  46. return fmt.Errorf("ECS simulation mode require Docker-compose 1.27, found %s", version)
  47. }
  48. converted, err := e.Convert(ctx, project)
  49. if err != nil {
  50. return err
  51. }
  52. cmd = exec.Command("docker-compose", "--context", "default", "--project-directory", project.WorkingDir, "--project-name", project.Name, "-f", "-", "up")
  53. cmd.Stdin = strings.NewReader(string(converted))
  54. cmd.Stdout = os.Stdout
  55. cmd.Stderr = os.Stderr
  56. return cmd.Run()
  57. }
  58. func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project) ([]byte, error) {
  59. project.Networks["credentials_network"] = types.NetworkConfig{
  60. Driver: "bridge",
  61. Ipam: types.IPAMConfig{
  62. Config: []*types.IPAMPool{
  63. {
  64. Subnet: "169.254.170.0/24",
  65. Gateway: "169.254.170.1",
  66. },
  67. },
  68. },
  69. }
  70. // On Windows, this directory can be found at "%UserProfile%\.aws"
  71. home, err := os.UserHomeDir()
  72. if err != nil {
  73. return nil, err
  74. }
  75. for i, service := range project.Services {
  76. service.Networks["credentials_network"] = &types.ServiceNetworkConfig{
  77. Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3),
  78. }
  79. service.DependsOn = append(service.DependsOn, "ecs-local-endpoints")
  80. service.Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = aws.String("/creds")
  81. service.Environment["ECS_CONTAINER_METADATA_URI"] = aws.String("http://169.254.170.2/v3")
  82. project.Services[i] = service
  83. }
  84. project.Services = append(project.Services, types.ServiceConfig{
  85. Name: "ecs-local-endpoints",
  86. Image: "amazon/amazon-ecs-local-container-endpoints",
  87. Volumes: []types.ServiceVolumeConfig{
  88. {
  89. Type: types.VolumeTypeBind,
  90. Source: "/var/run",
  91. Target: "/var/run",
  92. },
  93. {
  94. Type: types.VolumeTypeBind,
  95. Source: filepath.Join(home, ".aws"),
  96. Target: "/home/.aws",
  97. },
  98. },
  99. Environment: map[string]*string{
  100. "HOME": aws.String("/home"),
  101. "AWS_PROFILE": aws.String("default"),
  102. },
  103. Networks: map[string]*types.ServiceNetworkConfig{
  104. "credentials_network": {
  105. Ipv4Address: "169.254.170.2",
  106. },
  107. },
  108. })
  109. delete(project.Networks, "default")
  110. config := map[string]interface{}{
  111. "services": project.Services,
  112. "networks": project.Networks,
  113. "volumes": project.Volumes,
  114. "secrets": project.Secrets,
  115. "configs": project.Configs,
  116. }
  117. return yaml.Marshal(config)
  118. }
  119. func (e ecsLocalSimulation) Down(ctx context.Context, projectName string) error {
  120. return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose down")
  121. }
  122. func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, w io.Writer) error {
  123. return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose logs")
  124. }
  125. func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string) ([]compose.ServiceStatus, error) {
  126. return nil, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose ps")
  127. }