compose.go 3.8 KB

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