compose.go 7.0 KB

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