compose.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "context"
  16. "encoding/json"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "github.com/aws/aws-sdk-go/aws"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/pkg/errors"
  23. "github.com/sanathkr/go-yaml"
  24. "github.com/docker/compose-cli/api/compose"
  25. "github.com/docker/compose-cli/api/errdefs"
  26. )
  27. func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project) error {
  28. return e.compose.Build(ctx, project)
  29. }
  30. func (e ecsLocalSimulation) Push(ctx context.Context, project *types.Project) error {
  31. return e.compose.Push(ctx, project)
  32. }
  33. func (e ecsLocalSimulation) Pull(ctx context.Context, project *types.Project) error {
  34. return e.compose.Pull(ctx, project)
  35. }
  36. func (e ecsLocalSimulation) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  37. enhanced, err := e.enhanceForLocalSimulation(project)
  38. if err != nil {
  39. return err
  40. }
  41. return e.compose.Create(ctx, enhanced, opts)
  42. }
  43. func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
  44. return e.compose.Start(ctx, project, options)
  45. }
  46. func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error {
  47. return e.compose.Stop(ctx, project)
  48. }
  49. func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  50. return errdefs.ErrNotImplemented
  51. }
  52. func (e ecsLocalSimulation) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  53. return e.compose.Kill(ctx, project, options)
  54. }
  55. func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  56. enhanced, err := e.enhanceForLocalSimulation(project)
  57. if err != nil {
  58. return nil, err
  59. }
  60. delete(enhanced.Networks, "default")
  61. config := map[string]interface{}{
  62. "services": enhanced.Services,
  63. "networks": enhanced.Networks,
  64. "volumes": enhanced.Volumes,
  65. "secrets": enhanced.Secrets,
  66. "configs": enhanced.Configs,
  67. }
  68. switch options.Format {
  69. case "json":
  70. return json.MarshalIndent(config, "", " ")
  71. case "yaml":
  72. return yaml.Marshal(config)
  73. default:
  74. return nil, fmt.Errorf("unsupported format %q", options)
  75. }
  76. }
  77. func (e ecsLocalSimulation) enhanceForLocalSimulation(project *types.Project) (*types.Project, error) {
  78. project.Networks["credentials_network"] = types.NetworkConfig{
  79. Name: "credentials_network",
  80. Driver: "bridge",
  81. Ipam: types.IPAMConfig{
  82. Config: []*types.IPAMPool{
  83. {
  84. Subnet: "169.254.170.0/24",
  85. Gateway: "169.254.170.1",
  86. },
  87. },
  88. },
  89. }
  90. // On Windows, this directory can be found at "%UserProfile%\.aws"
  91. home, err := os.UserHomeDir()
  92. if err != nil {
  93. return nil, err
  94. }
  95. for i, service := range project.Services {
  96. service.Networks["credentials_network"] = &types.ServiceNetworkConfig{
  97. Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3),
  98. }
  99. if service.DependsOn == nil {
  100. service.DependsOn = types.DependsOnConfig{}
  101. }
  102. service.DependsOn["ecs-local-endpoints"] = types.ServiceDependency{
  103. Condition: types.ServiceConditionStarted,
  104. }
  105. service.Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = aws.String("/creds")
  106. service.Environment["ECS_CONTAINER_METADATA_URI"] = aws.String("http://169.254.170.2/v3")
  107. project.Services[i] = service
  108. }
  109. project.Services = append(project.Services, types.ServiceConfig{
  110. Name: "ecs-local-endpoints",
  111. Image: "amazon/amazon-ecs-local-container-endpoints",
  112. Volumes: []types.ServiceVolumeConfig{
  113. {
  114. Type: types.VolumeTypeBind,
  115. Source: "/var/run",
  116. Target: "/var/run",
  117. },
  118. {
  119. Type: types.VolumeTypeBind,
  120. Source: filepath.Join(home, ".aws"),
  121. Target: "/home/.aws",
  122. },
  123. },
  124. Environment: map[string]*string{
  125. "HOME": aws.String("/home"),
  126. "AWS_PROFILE": aws.String("default"),
  127. },
  128. Networks: map[string]*types.ServiceNetworkConfig{
  129. "credentials_network": {
  130. Ipv4Address: "169.254.170.2",
  131. },
  132. },
  133. })
  134. return project, nil
  135. }
  136. func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  137. options.RemoveOrphans = true
  138. return e.compose.Down(ctx, projectName, options)
  139. }
  140. func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  141. return e.compose.Logs(ctx, projectName, consumer, options)
  142. }
  143. func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  144. return e.compose.Ps(ctx, projectName, options)
  145. }
  146. func (e ecsLocalSimulation) List(ctx context.Context) ([]compose.Stack, error) {
  147. return e.compose.List(ctx)
  148. }
  149. func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  150. return 0, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
  151. }
  152. func (e ecsLocalSimulation) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
  153. return e.compose.Remove(ctx, project, options)
  154. }