1
0

compose.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. return backend.Up(ctx, opts)
  77. })
  78. }),
  79. }
  80. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  81. return cmd
  82. }
  83. func PsCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  84. opts := upOptions{}
  85. cmd := &cobra.Command{
  86. Use: "ps",
  87. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  88. opts, err := options.toProjectOptions()
  89. if err != nil {
  90. return err
  91. }
  92. status, err := backend.Ps(context.Background(), opts)
  93. if err != nil {
  94. return err
  95. }
  96. printSection(os.Stdout, len(status), func(w io.Writer) {
  97. for _, service := range status {
  98. fmt.Fprintf(w, "%s\t%s\t%d/%d\t%s\n", service.ID, service.Name, service.Replicas, service.Desired, strings.Join(service.Ports, ", "))
  99. }
  100. }, "ID", "NAME", "REPLICAS", "PORTS")
  101. return nil
  102. }),
  103. }
  104. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  105. return cmd
  106. }
  107. type downOptions struct {
  108. DeleteCluster bool
  109. }
  110. func DownCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  111. opts := downOptions{}
  112. cmd := &cobra.Command{
  113. Use: "down",
  114. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  115. opts, err := options.toProjectOptions()
  116. if err != nil {
  117. return err
  118. }
  119. return progress.Run(context.Background(), func(ctx context.Context) error {
  120. return backend.Down(ctx, opts)
  121. })
  122. }),
  123. }
  124. cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster")
  125. return cmd
  126. }
  127. func LogsCommand(dockerCli command.Cli, options *composeOptions) *cobra.Command {
  128. cmd := &cobra.Command{
  129. Use: "logs [PROJECT NAME]",
  130. RunE: WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
  131. opts, err := options.toProjectOptions()
  132. if err != nil {
  133. return err
  134. }
  135. return backend.Logs(context.Background(), opts, os.Stdout)
  136. }),
  137. }
  138. return cmd
  139. }