create_ecs.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package context
  14. import (
  15. "bufio"
  16. "context"
  17. "fmt"
  18. "os"
  19. "strings"
  20. "github.com/pkg/errors"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose-cli/api/client"
  23. "github.com/docker/compose-cli/api/context/store"
  24. "github.com/docker/compose-cli/ecs"
  25. "github.com/docker/compose-cli/errdefs"
  26. )
  27. func init() {
  28. extraCommands = append(extraCommands, createEcsCommand)
  29. extraHelp = append(extraHelp, `
  30. Create Amazon ECS context:
  31. $ docker context create ecs CONTEXT [flags]
  32. (see docker context create ecs --help)
  33. `)
  34. }
  35. func createEcsCommand() *cobra.Command {
  36. var localSimulation bool
  37. var opts ecs.ContextParams
  38. var accessKeysFile string
  39. cmd := &cobra.Command{
  40. Use: "ecs CONTEXT [flags]",
  41. Short: "Create a context for Amazon ECS",
  42. Args: cobra.ExactArgs(1),
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. opts.Name = args[0]
  45. if accessKeysFile != "" {
  46. err := parseAccessKeysFile(accessKeysFile, &opts)
  47. if err != nil {
  48. return err
  49. }
  50. }
  51. if opts.CredsFromEnv && opts.Profile != "" {
  52. return fmt.Errorf("--profile and --from-env flags cannot be set at the same time")
  53. }
  54. if accessKeysFile != "" && opts.Profile != "" {
  55. return fmt.Errorf("--profile and --access-keys flags cannot be set at the same time")
  56. }
  57. if opts.CredsFromEnv && accessKeysFile != "" {
  58. return fmt.Errorf("--access-keys and --from-env flags cannot be set at the same time")
  59. }
  60. if localSimulation {
  61. return runCreateLocalSimulation(cmd.Context(), args[0], opts)
  62. }
  63. return runCreateEcs(cmd.Context(), args[0], opts)
  64. },
  65. }
  66. addDescriptionFlag(cmd, &opts.Description)
  67. cmd.Flags().BoolVar(&localSimulation, "local-simulation", false, "Create context for ECS local simulation endpoints")
  68. cmd.Flags().StringVar(&opts.Profile, "profile", "", "Use an existing AWS profile")
  69. cmd.Flags().StringVar(&accessKeysFile, "access-keys", "", "Use AWS access keys from file")
  70. cmd.Flags().BoolVar(&opts.CredsFromEnv, "from-env", false, "Use AWS environment variables for profile, or credentials and region")
  71. return cmd
  72. }
  73. func parseAccessKeysFile(file string, opts *ecs.ContextParams) error {
  74. f, err := os.Open(file)
  75. if err != nil {
  76. return err
  77. }
  78. defer f.Close() // nolint:errcheck
  79. scanner := bufio.NewScanner(f)
  80. scanner.Split(bufio.ScanLines)
  81. values := map[string]string{}
  82. for scanner.Scan() {
  83. line := scanner.Text()
  84. parts := strings.SplitN(line, "=", 2)
  85. values[parts[0]] = parts[1]
  86. }
  87. var ok bool
  88. opts.AccessKey, ok = values["AWSAccessKeyId"]
  89. if !ok {
  90. return fmt.Errorf("%s is missing AWSAccessKeyId", file)
  91. }
  92. opts.SecretKey, ok = values["AWSSecretKey"]
  93. if !ok {
  94. return fmt.Errorf("%s is missing AWSSecretKey", file)
  95. }
  96. return nil
  97. }
  98. func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  99. if contextExists(ctx, contextName) {
  100. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  101. }
  102. cs, err := client.GetCloudService(ctx, store.EcsLocalSimulationContextType)
  103. if err != nil {
  104. return errors.Wrap(err, "cannot connect to ECS backend")
  105. }
  106. data, description, err := cs.CreateContextData(ctx, opts)
  107. if err != nil {
  108. return err
  109. }
  110. return createDockerContext(ctx, contextName, store.EcsLocalSimulationContextType, description, data)
  111. }
  112. func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  113. if contextExists(ctx, contextName) {
  114. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  115. }
  116. contextData, description, err := getEcsContextData(ctx, opts)
  117. if err != nil {
  118. return err
  119. }
  120. return createDockerContext(ctx, contextName, store.EcsContextType, description, contextData)
  121. }
  122. func getEcsContextData(ctx context.Context, opts ecs.ContextParams) (interface{}, string, error) {
  123. cs, err := client.GetCloudService(ctx, store.EcsContextType)
  124. if err != nil {
  125. return nil, "", errors.Wrap(err, "cannot connect to ECS backend")
  126. }
  127. return cs.CreateContextData(ctx, opts)
  128. }