main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. cmd := &cobra.Command{
  80. Use: "convert",
  81. RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
  82. client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
  83. if err != nil {
  84. return err
  85. }
  86. template, err := client.Convert(project)
  87. if err != nil {
  88. return err
  89. }
  90. j, err := template.JSON()
  91. if err != nil {
  92. fmt.Printf("Failed to generate JSON: %s\n", err)
  93. } else {
  94. fmt.Printf("%s\n", string(j))
  95. }
  96. return nil
  97. }),
  98. }
  99. return cmd
  100. }
  101. func UpCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
  102. opts := upOptions{}
  103. cmd := &cobra.Command{
  104. Use: "up",
  105. RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
  106. client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
  107. if err != nil {
  108. return err
  109. }
  110. return client.ComposeUp(project)
  111. }),
  112. }
  113. cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
  114. return cmd
  115. }
  116. type downOptions struct {
  117. KeepLoadBalancer bool
  118. DeleteCluster bool
  119. }
  120. func DownCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
  121. opts := downOptions{}
  122. cmd := &cobra.Command{
  123. Use: "down",
  124. RunE: func(cmd *cobra.Command, args []string) error {
  125. client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
  126. if err != nil {
  127. return err
  128. }
  129. if len(args) == 0 {
  130. project, err := compose.ProjectFromOptions(projectOpts)
  131. if err != nil {
  132. return err
  133. }
  134. return client.ComposeDown(&project.Name, opts.KeepLoadBalancer, opts.DeleteCluster)
  135. }
  136. // project names passed as parameters
  137. for _, name := range args {
  138. err := client.ComposeDown(&name, opts.KeepLoadBalancer, opts.DeleteCluster)
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. },
  145. }
  146. cmd.Flags().BoolVar(&opts.KeepLoadBalancer, "keep-load-balancer", false, "Keep Load Balancer for further use")
  147. cmd.Flags().BoolVar(&opts.DeleteCluster, "delete-cluster", false, "Delete cluster")
  148. return cmd
  149. }