compose.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, options compose.BuildOptions) error {
  28. return e.compose.Build(ctx, project, options)
  29. }
  30. func (e ecsLocalSimulation) Push(ctx context.Context, project *types.Project, options compose.PushOptions) error {
  31. return e.compose.Push(ctx, project, options)
  32. }
  33. func (e ecsLocalSimulation) Pull(ctx context.Context, project *types.Project, options compose.PullOptions) error {
  34. return e.compose.Pull(ctx, project, options)
  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) Restart(ctx context.Context, project *types.Project, options compose.RestartOptions) error {
  47. return e.compose.Restart(ctx, project, options)
  48. }
  49. func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
  50. return e.compose.Stop(ctx, project, options)
  51. }
  52. func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  53. return errdefs.ErrNotImplemented
  54. }
  55. func (e ecsLocalSimulation) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  56. return e.compose.Kill(ctx, project, options)
  57. }
  58. func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  59. enhanced, err := e.enhanceForLocalSimulation(project)
  60. if err != nil {
  61. return nil, err
  62. }
  63. delete(enhanced.Networks, "default")
  64. config := map[string]interface{}{
  65. "services": enhanced.Services,
  66. "networks": enhanced.Networks,
  67. "volumes": enhanced.Volumes,
  68. "secrets": enhanced.Secrets,
  69. "configs": enhanced.Configs,
  70. }
  71. switch options.Format {
  72. case "json":
  73. return json.MarshalIndent(config, "", " ")
  74. case "yaml":
  75. return yaml.Marshal(config)
  76. default:
  77. return nil, fmt.Errorf("unsupported format %q", options)
  78. }
  79. }
  80. func (e ecsLocalSimulation) enhanceForLocalSimulation(project *types.Project) (*types.Project, error) {
  81. project.Networks["credentials_network"] = types.NetworkConfig{
  82. Name: "credentials_network",
  83. Driver: "bridge",
  84. Ipam: types.IPAMConfig{
  85. Config: []*types.IPAMPool{
  86. {
  87. Subnet: "169.254.170.0/24",
  88. Gateway: "169.254.170.1",
  89. },
  90. },
  91. },
  92. }
  93. // On Windows, this directory can be found at "%UserProfile%\.aws"
  94. home, err := os.UserHomeDir()
  95. if err != nil {
  96. return nil, err
  97. }
  98. for i, service := range project.Services {
  99. service.Networks["credentials_network"] = &types.ServiceNetworkConfig{
  100. Ipv4Address: fmt.Sprintf("169.254.170.%d", i+3),
  101. }
  102. if service.DependsOn == nil {
  103. service.DependsOn = types.DependsOnConfig{}
  104. }
  105. service.DependsOn["ecs-local-endpoints"] = types.ServiceDependency{
  106. Condition: types.ServiceConditionStarted,
  107. }
  108. service.Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] = aws.String("/creds")
  109. service.Environment["ECS_CONTAINER_METADATA_URI"] = aws.String("http://169.254.170.2/v3")
  110. project.Services[i] = service
  111. }
  112. project.Services = append(project.Services, types.ServiceConfig{
  113. Name: "ecs-local-endpoints",
  114. Image: "amazon/amazon-ecs-local-container-endpoints",
  115. Volumes: []types.ServiceVolumeConfig{
  116. {
  117. Type: types.VolumeTypeBind,
  118. Source: "/var/run",
  119. Target: "/var/run",
  120. },
  121. {
  122. Type: types.VolumeTypeBind,
  123. Source: filepath.Join(home, ".aws"),
  124. Target: "/home/.aws",
  125. },
  126. },
  127. Environment: map[string]*string{
  128. "HOME": aws.String("/home"),
  129. "AWS_PROFILE": aws.String("default"),
  130. },
  131. Networks: map[string]*types.ServiceNetworkConfig{
  132. "credentials_network": {
  133. Ipv4Address: "169.254.170.2",
  134. },
  135. },
  136. })
  137. return project, nil
  138. }
  139. func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  140. options.RemoveOrphans = true
  141. return e.compose.Down(ctx, projectName, options)
  142. }
  143. func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  144. return e.compose.Logs(ctx, projectName, consumer, options)
  145. }
  146. func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  147. return e.compose.Ps(ctx, projectName, options)
  148. }
  149. func (e ecsLocalSimulation) List(ctx context.Context, opts compose.ListOptions) ([]compose.Stack, error) {
  150. return e.compose.List(ctx, opts)
  151. }
  152. func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  153. return 0, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
  154. }
  155. func (e ecsLocalSimulation) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
  156. return e.compose.Remove(ctx, project, options)
  157. }
  158. func (e ecsLocalSimulation) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  159. return errdefs.ErrNotImplemented
  160. }
  161. func (e ecsLocalSimulation) Pause(ctx context.Context, project *types.Project) error {
  162. return e.compose.Pause(ctx, project)
  163. }
  164. func (e ecsLocalSimulation) UnPause(ctx context.Context, project *types.Project) error {
  165. return e.compose.UnPause(ctx, project)
  166. }
  167. func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) {
  168. return e.compose.Top(ctx, projectName, services)
  169. }
  170. func (e ecsLocalSimulation) Events(ctx context.Context, project string, options compose.EventsOptions) error {
  171. return e.compose.Events(ctx, project, options)
  172. }
  173. func (e ecsLocalSimulation) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
  174. return "", 0, errdefs.ErrNotImplemented
  175. }
  176. func (e ecsLocalSimulation) Images(ctx context.Context, projectName string, options compose.ImagesOptions) ([]compose.ImageSummary, error) {
  177. return nil, errdefs.ErrNotImplemented
  178. }