compose.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/docker/cli/cli/command"
  6. "github.com/docker/ecs-plugin/pkg/amazon"
  7. "github.com/docker/ecs-plugin/pkg/compose"
  8. "github.com/docker/ecs-plugin/pkg/docker"
  9. "github.com/spf13/cobra"
  10. )
  11. func ComposeCommand(dockerCli command.Cli) *cobra.Command {
  12. cmd := &cobra.Command{
  13. Use: "compose",
  14. }
  15. opts := &compose.ProjectOptions{}
  16. opts.AddFlags(cmd.Flags())
  17. cmd.AddCommand(
  18. ConvertCommand(dockerCli, opts),
  19. UpCommand(dockerCli, opts),
  20. DownCommand(dockerCli, opts),
  21. )
  22. return cmd
  23. }
  24. type upOptions struct {
  25. loadBalancerArn string
  26. }
  27. func (o upOptions) LoadBalancerArn() *string {
  28. if o.loadBalancerArn == "" {
  29. return nil
  30. }
  31. return &o.loadBalancerArn
  32. }
  33. func ConvertCommand(dockerCli command.Cli, projectOpts *compose.ProjectOptions) *cobra.Command {
  34. cmd := &cobra.Command{
  35. Use: "convert",
  36. RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
  37. clusteropts, err := docker.GetAwsContext(dockerCli)
  38. if err != nil {
  39. return err
  40. }
  41. client, err := amazon.NewClient(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  42. if err != nil {
  43. return err
  44. }
  45. template, err := client.Convert(context.Background(), project)
  46. if err != nil {
  47. return err
  48. }
  49. j, err := template.JSON()
  50. if err != nil {
  51. fmt.Printf("Failed to generate JSON: %s\n", err)
  52. } else {
  53. fmt.Printf("%s\n", string(j))
  54. }
  55. return nil
  56. }),
  57. }
  58. return cmd
  59. }
  60. func UpCommand(dockerCli command.Cli, projectOpts *compose.ProjectOptions) *cobra.Command {
  61. opts := upOptions{}
  62. cmd := &cobra.Command{
  63. Use: "up",
  64. RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
  65. clusteropts, err := docker.GetAwsContext(dockerCli)
  66. if err != nil {
  67. return err
  68. }
  69. client, err := amazon.NewClient(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  70. if err != nil {
  71. return err
  72. }
  73. return client.ComposeUp(context.Background(), project)
  74. }),
  75. }
  76. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  77. return cmd
  78. }
  79. type downOptions struct {
  80. DeleteCluster bool
  81. }
  82. func DownCommand(dockerCli command.Cli, projectOpts *compose.ProjectOptions) *cobra.Command {
  83. opts := downOptions{}
  84. cmd := &cobra.Command{
  85. Use: "down",
  86. RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
  87. client, err := amazon.NewClient(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
  88. if err != nil {
  89. return err
  90. }
  91. if len(args) == 0 {
  92. project, err := compose.ProjectFromOptions(projectOpts)
  93. if err != nil {
  94. return err
  95. }
  96. return client.ComposeDown(context.Background(), project.Name, opts.DeleteCluster)
  97. }
  98. // project names passed as parameters
  99. for _, name := range args {
  100. err := client.ComposeDown(context.Background(), name, opts.DeleteCluster)
  101. if err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. }),
  107. }
  108. cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster")
  109. return cmd
  110. }