contextStore.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws/awserr"
  5. "github.com/docker/cli/cli/command"
  6. cliconfig "github.com/docker/cli/cli/config"
  7. "github.com/docker/cli/cli/context/store"
  8. amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
  9. "github.com/mitchellh/mapstructure"
  10. "github.com/spf13/cobra"
  11. )
  12. const contextType = "aws"
  13. type TypeContext struct {
  14. Type string
  15. }
  16. func getter() interface{} {
  17. return &TypeContext{}
  18. }
  19. type AwsContext struct {
  20. Profile string
  21. Cluster string
  22. Region string
  23. }
  24. func NewContext(name string, awsContext *AwsContext) error {
  25. _, err := NewContextWithStore(name, awsContext, cliconfig.ContextStoreDir())
  26. return err
  27. }
  28. func NewContextWithStore(name string, awsContext *AwsContext, contextDirectory string) (store.Store, error) {
  29. contextStore := initContextStore(contextDirectory)
  30. endpoints := map[string]interface{}{
  31. "aws": awsContext,
  32. "docker": awsContext,
  33. }
  34. metadata := store.Metadata{
  35. Name: name,
  36. Endpoints: endpoints,
  37. Metadata: TypeContext{Type: contextType},
  38. }
  39. return contextStore, contextStore.CreateOrUpdate(metadata)
  40. }
  41. func initContextStore(contextDir string) store.Store {
  42. config := store.NewConfig(getter)
  43. return store.New(contextDir, config)
  44. }
  45. func checkAwsContextExists(contextName string) (*AwsContext, error) {
  46. if contextName == command.DefaultContextName {
  47. return nil, fmt.Errorf("can't use \"%s\" with ECS command because it is not an AWS context", contextName)
  48. }
  49. contextStore := initContextStore(cliconfig.ContextStoreDir())
  50. metadata, err := contextStore.GetMetadata(contextName)
  51. if err != nil {
  52. return nil, err
  53. }
  54. endpoint := metadata.Endpoints["aws"]
  55. awsContext := AwsContext{}
  56. err = mapstructure.Decode(endpoint, &awsContext)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if awsContext == (AwsContext{}) {
  61. return nil, fmt.Errorf("can't use \"%s\" with ECS command because it is not an AWS context", contextName)
  62. }
  63. return &awsContext, nil
  64. }
  65. type ContextFunc func(ctx AwsContext, backend *amazon.Backend, args []string) error
  66. func WithAwsContext(dockerCli command.Cli, f ContextFunc) func(cmd *cobra.Command, args []string) error {
  67. return func(cmd *cobra.Command, args []string) error {
  68. ctx, err := GetAwsContext(dockerCli)
  69. if err != nil {
  70. return err
  71. }
  72. backend, err := amazon.NewBackend(ctx.Profile, ctx.Cluster, ctx.Region)
  73. if err != nil {
  74. return err
  75. }
  76. err = f(*ctx, backend, args)
  77. if e, ok := err.(awserr.Error); ok {
  78. return fmt.Errorf(e.Message())
  79. }
  80. return err
  81. }
  82. }
  83. func GetAwsContext(dockerCli command.Cli) (*AwsContext, error) {
  84. contextName := dockerCli.CurrentContext()
  85. return checkAwsContextExists(contextName)
  86. }