compose.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. "github.com/compose-spec/compose-go/cli"
  9. "github.com/docker/cli/cli/command"
  10. amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
  11. "github.com/docker/ecs-plugin/pkg/amazon/cloudformation"
  12. "github.com/docker/ecs-plugin/pkg/docker"
  13. "github.com/docker/ecs-plugin/pkg/progress"
  14. "github.com/spf13/cobra"
  15. )
  16. func ComposeCommand(dockerCli command.Cli) *cobra.Command {
  17. cmd := &cobra.Command{
  18. Use: "compose",
  19. }
  20. opts := &composeOptions{}
  21. AddFlags(opts, cmd.Flags())
  22. cmd.AddCommand(
  23. ConvertCommand(dockerCli, opts),
  24. UpCommand(dockerCli, opts),
  25. DownCommand(dockerCli, opts),
  26. LogsCommand(dockerCli, opts),
  27. PsCommand(dockerCli, opts),
  28. )
  29. return cmd
  30. }
  31. type upOptions struct {
  32. loadBalancerArn string
  33. }
  34. func (o upOptions) LoadBalancerArn() *string {
  35. if o.loadBalancerArn == "" {
  36. return nil
  37. }
  38. return &o.loadBalancerArn
  39. }
  40. func ConvertCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  41. cmd := &cobra.Command{
  42. Use: "convert",
  43. RunE: WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
  44. opts, err := options.toProjectOptions()
  45. if err != nil {
  46. return err
  47. }
  48. project, err := cli.ProjectFromOptions(opts)
  49. if err != nil {
  50. return err
  51. }
  52. template, err := backend.Convert(project)
  53. if err != nil {
  54. return err
  55. }
  56. json, err := cloudformation.Marshall(template)
  57. if err != nil {
  58. return err
  59. }
  60. fmt.Printf("%s\n", string(json))
  61. return nil
  62. }),
  63. }
  64. return cmd
  65. }
  66. func UpCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  67. opts := upOptions{}
  68. cmd := &cobra.Command{
  69. Use: "up",
  70. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  71. opts, err := options.toProjectOptions()
  72. if err != nil {
  73. return err
  74. }
  75. return progress.Run(context.Background(), func(ctx context.Context) error {
  76. backend.SetWriter(ctx)
  77. return backend.Up(ctx, opts)
  78. })
  79. }),
  80. }
  81. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  82. return cmd
  83. }
  84. func PsCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  85. opts := upOptions{}
  86. cmd := &cobra.Command{
  87. Use: "ps",
  88. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  89. opts, err := options.toProjectOptions()
  90. if err != nil {
  91. return err
  92. }
  93. status, err := backend.Ps(context.Background(), opts)
  94. if err != nil {
  95. return err
  96. }
  97. printSection(os.Stdout, len(status), func(w io.Writer) {
  98. for _, service := range status {
  99. fmt.Fprintf(w, "%s\t%s\t%d/%d\t%s\n", service.ID, service.Name, service.Replicas, service.Desired, strings.Join(service.Ports, ", "))
  100. }
  101. }, "ID", "NAME", "REPLICAS", "PORTS")
  102. return nil
  103. }),
  104. }
  105. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  106. return cmd
  107. }
  108. type downOptions struct {
  109. DeleteCluster bool
  110. }
  111. func DownCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  112. opts := downOptions{}
  113. cmd := &cobra.Command{
  114. Use: "down",
  115. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  116. opts, err := options.toProjectOptions()
  117. if err != nil {
  118. return err
  119. }
  120. return progress.Run(context.Background(), func(ctx context.Context) error {
  121. backend.SetWriter(ctx)
  122. return backend.Down(ctx, opts)
  123. })
  124. }),
  125. }
  126. cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster")
  127. return cmd
  128. }
  129. func LogsCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  130. cmd := &cobra.Command{
  131. Use: "logs [PROJECT NAME]",
  132. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  133. opts, err := options.toProjectOptions()
  134. if err != nil {
  135. return err
  136. }
  137. return backend.Logs(context.Background(), opts, os.Stdout)
  138. }),
  139. }
  140. return cmd
  141. }