up.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 ecs
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "os/signal"
  19. "syscall"
  20. "github.com/compose-spec/compose-go/types"
  21. "github.com/sirupsen/logrus"
  22. "github.com/docker/compose-cli/pkg/api"
  23. "github.com/docker/compose-cli/pkg/progress"
  24. )
  25. func (b *ecsAPIService) Build(ctx context.Context, project *types.Project, options api.BuildOptions) error {
  26. return api.ErrNotImplemented
  27. }
  28. func (b *ecsAPIService) Push(ctx context.Context, project *types.Project, options api.PushOptions) error {
  29. return api.ErrNotImplemented
  30. }
  31. func (b *ecsAPIService) Pull(ctx context.Context, project *types.Project, options api.PullOptions) error {
  32. return api.ErrNotImplemented
  33. }
  34. func (b *ecsAPIService) Create(ctx context.Context, project *types.Project, opts api.CreateOptions) error {
  35. return api.ErrNotImplemented
  36. }
  37. func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, options api.StartOptions) error {
  38. return api.ErrNotImplemented
  39. }
  40. func (b *ecsAPIService) Restart(ctx context.Context, project *types.Project, options api.RestartOptions) error {
  41. return api.ErrNotImplemented
  42. }
  43. func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project, options api.StopOptions) error {
  44. return api.ErrNotImplemented
  45. }
  46. func (b *ecsAPIService) Pause(ctx context.Context, project string, options api.PauseOptions) error {
  47. return api.ErrNotImplemented
  48. }
  49. func (b *ecsAPIService) UnPause(ctx context.Context, project string, options api.PauseOptions) error {
  50. return api.ErrNotImplemented
  51. }
  52. func (b *ecsAPIService) Events(ctx context.Context, project string, options api.EventsOptions) error {
  53. return api.ErrNotImplemented
  54. }
  55. func (b *ecsAPIService) Port(ctx context.Context, project string, service string, port int, options api.PortOptions) (string, int, error) {
  56. return "", 0, api.ErrNotImplemented
  57. }
  58. func (b *ecsAPIService) Copy(ctx context.Context, project *types.Project, options api.CopyOptions) error {
  59. return api.ErrNotImplemented
  60. }
  61. func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
  62. return progress.Run(ctx, func(ctx context.Context) error {
  63. return b.up(ctx, project, options)
  64. })
  65. }
  66. func (b *ecsAPIService) up(ctx context.Context, project *types.Project, options api.UpOptions) error {
  67. logrus.Debugf("deploying on AWS with region=%q", b.Region)
  68. err := b.aws.CheckRequirements(ctx, b.Region)
  69. if err != nil {
  70. return err
  71. }
  72. template, err := b.Convert(ctx, project, api.ConvertOptions{
  73. Format: "yaml",
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. update, err := b.aws.StackExists(ctx, project.Name)
  79. if err != nil {
  80. return err
  81. }
  82. var previousEvents []string
  83. if update {
  84. var err error
  85. previousEvents, err = b.previousStackEvents(ctx, project.Name)
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. operation := stackCreate
  91. if update {
  92. operation = stackUpdate
  93. changeset, err := b.aws.CreateChangeSet(ctx, project.Name, b.Region, template)
  94. if err != nil {
  95. return err
  96. }
  97. err = b.aws.UpdateStack(ctx, changeset)
  98. if err != nil {
  99. return err
  100. }
  101. } else {
  102. err = b.aws.CreateStack(ctx, project.Name, b.Region, template)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. if options.Start.Attach == nil {
  108. return nil
  109. }
  110. signalChan := make(chan os.Signal, 1)
  111. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  112. go func() {
  113. <-signalChan
  114. fmt.Println("user interrupted deployment. Deleting stack...")
  115. b.Down(ctx, project.Name, api.DownOptions{}) // nolint:errcheck
  116. }()
  117. err = b.WaitStackCompletion(ctx, project.Name, operation, previousEvents...)
  118. return err
  119. }