create_ecs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "context"
  16. "fmt"
  17. "github.com/pkg/errors"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/context/store"
  21. "github.com/docker/compose-cli/ecs"
  22. "github.com/docker/compose-cli/errdefs"
  23. )
  24. func init() {
  25. extraCommands = append(extraCommands, createEcsCommand)
  26. extraHelp = append(extraHelp, `
  27. Create Amazon ECS context:
  28. $ docker context create ecs CONTEXT [flags]
  29. (see docker context create ecs --help)
  30. `)
  31. }
  32. func createEcsCommand() *cobra.Command {
  33. var localSimulation bool
  34. var opts ecs.ContextParams
  35. cmd := &cobra.Command{
  36. Use: "ecs CONTEXT [flags]",
  37. Short: "Create a context for Amazon ECS",
  38. Args: cobra.ExactArgs(1),
  39. RunE: func(cmd *cobra.Command, args []string) error {
  40. opts.Name = args[0]
  41. if opts.CredsFromEnv && opts.Profile != "" {
  42. return fmt.Errorf("--profile and --from-env flags cannot be set at the same time")
  43. }
  44. if localSimulation {
  45. return runCreateLocalSimulation(cmd.Context(), args[0], opts)
  46. }
  47. return runCreateEcs(cmd.Context(), args[0], opts)
  48. },
  49. }
  50. addDescriptionFlag(cmd, &opts.Description)
  51. cmd.Flags().BoolVar(&localSimulation, "local-simulation", false, "Create context for ECS local simulation endpoints")
  52. cmd.Flags().StringVar(&opts.Profile, "profile", "", "Use an existing AWS profile")
  53. cmd.Flags().BoolVar(&opts.CredsFromEnv, "from-env", false, "Use AWS environment variables for profile, or credentials and region")
  54. return cmd
  55. }
  56. func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  57. if contextExists(ctx, contextName) {
  58. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  59. }
  60. cs, err := client.GetCloudService(ctx, store.EcsLocalSimulationContextType)
  61. if err != nil {
  62. return errors.Wrap(err, "cannot connect to ECS backend")
  63. }
  64. data, description, err := cs.CreateContextData(ctx, opts)
  65. if err != nil {
  66. return err
  67. }
  68. return createDockerContext(ctx, contextName, store.EcsLocalSimulationContextType, description, data)
  69. }
  70. func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
  71. if contextExists(ctx, contextName) {
  72. return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
  73. }
  74. contextData, description, err := getEcsContextData(ctx, opts)
  75. if err != nil {
  76. return err
  77. }
  78. return createDockerContext(ctx, contextName, store.EcsContextType, description, contextData)
  79. }
  80. func getEcsContextData(ctx context.Context, opts ecs.ContextParams) (interface{}, string, error) {
  81. cs, err := client.GetCloudService(ctx, store.EcsContextType)
  82. if err != nil {
  83. return nil, "", errors.Wrap(err, "cannot connect to ECS backend")
  84. }
  85. return cs.CreateContextData(ctx, opts)
  86. }