main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/docker/cli/cli-plugins/manager"
  5. "github.com/docker/cli/cli-plugins/plugin"
  6. "github.com/docker/cli/cli/command"
  7. "github.com/docker/ecs-plugin/pkg/amazon"
  8. "github.com/docker/ecs-plugin/pkg/compose"
  9. "github.com/spf13/cobra"
  10. )
  11. const version = "0.0.1"
  12. func main() {
  13. plugin.Run(func(dockerCli command.Cli) *cobra.Command {
  14. cmd := NewRootCmd("ecs", dockerCli)
  15. return cmd
  16. }, manager.Metadata{
  17. SchemaVersion: "0.1.0",
  18. Vendor: "Docker Inc.",
  19. Version: version,
  20. Experimental: true,
  21. })
  22. }
  23. type clusterOptions struct {
  24. profile string
  25. region string
  26. cluster string
  27. }
  28. // NewRootCmd returns the base root command.
  29. func NewRootCmd(name string, dockerCli command.Cli) *cobra.Command {
  30. var opts clusterOptions
  31. cmd := &cobra.Command{
  32. Short: "Docker ECS",
  33. Long: `run multi-container applications on Amazon ECS.`,
  34. Use: name,
  35. Annotations: map[string]string{"experimentalCLI": "true"},
  36. }
  37. cmd.AddCommand(
  38. VersionCommand(),
  39. ComposeCommand(&opts),
  40. )
  41. cmd.Flags().StringVarP(&opts.profile, "profile", "p", "default", "AWS Profile")
  42. cmd.Flags().StringVarP(&opts.cluster, "cluster", "c", "default", "ECS cluster")
  43. cmd.Flags().StringVarP(&opts.region, "region", "r", "", "AWS region")
  44. return cmd
  45. }
  46. func VersionCommand() *cobra.Command {
  47. return &cobra.Command{
  48. Use: "version",
  49. Short: "Show version.",
  50. RunE: func(cmd *cobra.Command, args []string) error {
  51. fmt.Printf("Docker ECS plugin %s\n", version)
  52. return nil
  53. },
  54. }
  55. }
  56. func ComposeCommand(clusteropts *clusterOptions) *cobra.Command {
  57. cmd := &cobra.Command{
  58. Use: "compose",
  59. }
  60. opts := &compose.ProjectOptions{}
  61. opts.AddFlags(cmd.Flags())
  62. cmd.AddCommand(
  63. ConvertCommand(clusteropts, opts),
  64. UpCommand(clusteropts, opts),
  65. DownCommand(clusteropts, opts),
  66. )
  67. return cmd
  68. }
  69. type upOptions struct {
  70. loadBalancerArn string
  71. }
  72. func (o upOptions) LoadBalancerArn() *string {
  73. if o.loadBalancerArn == "" {
  74. return nil
  75. }
  76. return &o.loadBalancerArn
  77. }
  78. func ConvertCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
  79. opts := upOptions{}
  80. cmd := &cobra.Command{
  81. Use: "convert",
  82. RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
  83. client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
  84. if err != nil {
  85. return err
  86. }
  87. template, err := client.Convert(project, opts.LoadBalancerArn())
  88. if err != nil {
  89. return err
  90. }
  91. j, err := template.JSON()
  92. if err != nil {
  93. fmt.Printf("Failed to generate JSON: %s\n", err)
  94. } else {
  95. fmt.Printf("%s\n", string(j))
  96. }
  97. return nil
  98. }),
  99. }
  100. return cmd
  101. }
  102. func UpCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
  103. opts := upOptions{}
  104. cmd := &cobra.Command{
  105. Use: "up",
  106. RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
  107. client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
  108. if err != nil {
  109. return err
  110. }
  111. return client.ComposeUp(project, opts.LoadBalancerArn())
  112. }),
  113. }
  114. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  115. return cmd
  116. }
  117. type downOptions struct {
  118. KeepLoadBalancer bool
  119. DeleteCluster bool
  120. }
  121. func DownCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
  122. opts := downOptions{}
  123. cmd := &cobra.Command{
  124. Use: "down",
  125. RunE: func(cmd *cobra.Command, args []string) error {
  126. client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
  127. if err != nil {
  128. return err
  129. }
  130. if len(args) == 0 {
  131. project, err := compose.ProjectFromOptions(projectOpts)
  132. if err != nil {
  133. return err
  134. }
  135. return client.ComposeDown(&project.Name, opts.KeepLoadBalancer, opts.DeleteCluster)
  136. }
  137. // project names passed as parameters
  138. for _, name := range args {
  139. err := client.ComposeDown(&name, opts.KeepLoadBalancer, opts.DeleteCluster)
  140. if err != nil {
  141. return err
  142. }
  143. }
  144. return nil
  145. },
  146. }
  147. cmd.Flags().BoolVar(&opts.KeepLoadBalancer, "keep-load-balancer", false, "Keep Load Balancer for further use")
  148. cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster")
  149. return cmd
  150. }