create.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package context
  25. import (
  26. "context"
  27. "github.com/spf13/cobra"
  28. "github.com/docker/api/cli/mobycli"
  29. "github.com/docker/api/context/store"
  30. )
  31. type descriptionCreateOpts struct {
  32. description string
  33. }
  34. func createCommand() *cobra.Command {
  35. const longHelp = `Create a new context
  36. Create docker engine context:
  37. $ docker context create CONTEXT [flags]
  38. Create Azure Container Instances context:
  39. $ docker context create aci CONTEXT [flags]
  40. (see docker context create aci --help)
  41. Docker endpoint config:
  42. NAME DESCRIPTION
  43. from Copy named context's Docker endpoint configuration
  44. host Docker endpoint on which to connect
  45. ca Trust certs signed only by this CA
  46. cert Path to TLS certificate file
  47. key Path to TLS key file
  48. skip-tls-verify Skip TLS certificate validation
  49. Kubernetes endpoint config:
  50. NAME DESCRIPTION
  51. from Copy named context's Kubernetes endpoint configuration
  52. config-file Path to a Kubernetes config file
  53. context-override Overrides the context set in the kubernetes config file
  54. namespace-override Overrides the namespace set in the kubernetes config file
  55. Example:
  56. $ docker context create my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"`
  57. cmd := &cobra.Command{
  58. Use: "create CONTEXT",
  59. Short: "Create new context",
  60. RunE: func(cmd *cobra.Command, args []string) error {
  61. return mobycli.ExecCmd(cmd)
  62. },
  63. Long: longHelp,
  64. }
  65. cmd.AddCommand(
  66. createAciCommand(),
  67. createLocalCommand(),
  68. createExampleCommand(),
  69. )
  70. flags := cmd.Flags()
  71. flags.String("description", "", "Description of the context")
  72. flags.String(
  73. "default-stack-orchestrator", "",
  74. "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
  75. flags.StringToString("docker", nil, "set the docker endpoint")
  76. flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
  77. flags.String("from", "", "create context from a named context")
  78. return cmd
  79. }
  80. func createLocalCommand() *cobra.Command {
  81. var opts descriptionCreateOpts
  82. cmd := &cobra.Command{
  83. Use: "local CONTEXT",
  84. Short: "Create a context for accessing local engine",
  85. Args: cobra.ExactArgs(1),
  86. Hidden: true,
  87. RunE: func(cmd *cobra.Command, args []string) error {
  88. return createDockerContext(cmd.Context(), args[0], store.LocalContextType, opts.description, store.LocalContext{})
  89. },
  90. }
  91. addDescriptionFlag(cmd, &opts.description)
  92. return cmd
  93. }
  94. func createExampleCommand() *cobra.Command {
  95. var opts descriptionCreateOpts
  96. cmd := &cobra.Command{
  97. Use: "example CONTEXT",
  98. Short: "Create a test context returning fixed output",
  99. Args: cobra.ExactArgs(1),
  100. Hidden: true,
  101. RunE: func(cmd *cobra.Command, args []string) error {
  102. return createDockerContext(cmd.Context(), args[0], store.ExampleContextType, opts.description, store.ExampleContext{})
  103. },
  104. }
  105. addDescriptionFlag(cmd, &opts.description)
  106. return cmd
  107. }
  108. func createDockerContext(ctx context.Context, name string, contextType string, description string, data interface{}) error {
  109. s := store.ContextStore(ctx)
  110. result := s.Create(
  111. name,
  112. contextType,
  113. description,
  114. data,
  115. )
  116. return result
  117. }
  118. func addDescriptionFlag(cmd *cobra.Command, descriptionOpt *string) {
  119. cmd.Flags().StringVar(descriptionOpt, "description", "", "Description of the context")
  120. }