2020-05-14 21:13:07 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2020 Docker Inc.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
obtaining a copy of this software and associated documentation
|
|
|
|
files (the "Software"), to deal in the Software without
|
|
|
|
restriction, including without limitation the rights to use, copy,
|
|
|
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED,
|
|
|
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY,
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
TORT OR OTHERWISE,
|
|
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
|
|
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-05-14 20:55:40 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-06-17 17:57:44 +02:00
|
|
|
"github.com/docker/api/cli/mobycli"
|
2020-05-14 20:55:40 +02:00
|
|
|
"github.com/docker/api/context/store"
|
|
|
|
)
|
|
|
|
|
2020-06-11 10:12:41 +02:00
|
|
|
type descriptionCreateOpts struct {
|
|
|
|
description string
|
2020-05-14 20:55:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createCommand() *cobra.Command {
|
2020-06-11 10:12:41 +02:00
|
|
|
const longHelp = `Create a new context
|
|
|
|
|
|
|
|
Create docker engine context:
|
|
|
|
$ docker context create CONTEXT [flags]
|
|
|
|
|
|
|
|
Create Azure Container Instances context:
|
|
|
|
$ docker context create aci CONTEXT [flags]
|
|
|
|
(see docker context create aci --help)
|
|
|
|
|
|
|
|
Docker endpoint config:
|
|
|
|
|
|
|
|
NAME DESCRIPTION
|
|
|
|
from Copy named context's Docker endpoint configuration
|
|
|
|
host Docker endpoint on which to connect
|
|
|
|
ca Trust certs signed only by this CA
|
|
|
|
cert Path to TLS certificate file
|
|
|
|
key Path to TLS key file
|
|
|
|
skip-tls-verify Skip TLS certificate validation
|
|
|
|
|
|
|
|
Kubernetes endpoint config:
|
|
|
|
|
|
|
|
NAME DESCRIPTION
|
|
|
|
from Copy named context's Kubernetes endpoint configuration
|
|
|
|
config-file Path to a Kubernetes config file
|
|
|
|
context-override Overrides the context set in the kubernetes config file
|
|
|
|
namespace-override Overrides the namespace set in the kubernetes config file
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
$ docker context create my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"`
|
|
|
|
|
2020-05-14 20:55:40 +02:00
|
|
|
cmd := &cobra.Command{
|
2020-06-11 10:12:41 +02:00
|
|
|
Use: "create CONTEXT",
|
|
|
|
Short: "Create new context",
|
2020-05-14 20:55:40 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-06-17 17:57:44 +02:00
|
|
|
return mobycli.ExecCmd(cmd)
|
2020-05-14 20:55:40 +02:00
|
|
|
},
|
2020-06-11 10:12:41 +02:00
|
|
|
Long: longHelp,
|
2020-05-14 20:55:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 10:12:41 +02:00
|
|
|
cmd.AddCommand(
|
|
|
|
createAciCommand(),
|
2020-06-12 15:00:30 +02:00
|
|
|
createLocalCommand(),
|
2020-06-11 10:12:41 +02:00
|
|
|
createExampleCommand(),
|
|
|
|
)
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.String("description", "", "Description of the context")
|
|
|
|
flags.String(
|
|
|
|
"default-stack-orchestrator", "",
|
|
|
|
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
|
|
|
|
flags.StringToString("docker", nil, "set the docker endpoint")
|
|
|
|
flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
|
|
|
|
flags.String("from", "", "create context from a named context")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2020-05-14 20:55:40 +02:00
|
|
|
|
2020-06-12 15:00:30 +02:00
|
|
|
func createLocalCommand() *cobra.Command {
|
2020-06-11 10:12:41 +02:00
|
|
|
var opts descriptionCreateOpts
|
|
|
|
cmd := &cobra.Command{
|
2020-06-12 15:00:30 +02:00
|
|
|
Use: "local CONTEXT",
|
|
|
|
Short: "Create a context for accessing local engine",
|
2020-06-11 10:12:41 +02:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Hidden: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-06-12 15:00:30 +02:00
|
|
|
return createDockerContext(cmd.Context(), args[0], store.LocalContextType, opts.description, store.LocalContext{})
|
2020-06-11 10:12:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
addDescriptionFlag(cmd, &opts.description)
|
2020-05-14 20:55:40 +02:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-06-11 10:12:41 +02:00
|
|
|
func createExampleCommand() *cobra.Command {
|
|
|
|
var opts descriptionCreateOpts
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "example CONTEXT",
|
|
|
|
Short: "Create a test context returning fixed output",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Hidden: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return createDockerContext(cmd.Context(), args[0], store.ExampleContextType, opts.description, store.ExampleContext{})
|
|
|
|
},
|
2020-06-03 19:01:20 +02:00
|
|
|
}
|
2020-06-11 10:12:41 +02:00
|
|
|
|
|
|
|
addDescriptionFlag(cmd, &opts.description)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDockerContext(ctx context.Context, name string, contextType string, description string, data interface{}) error {
|
2020-06-03 19:01:20 +02:00
|
|
|
s := store.ContextStore(ctx)
|
2020-06-11 10:12:41 +02:00
|
|
|
result := s.Create(
|
2020-06-03 19:01:20 +02:00
|
|
|
name,
|
|
|
|
contextType,
|
|
|
|
description,
|
2020-06-11 10:12:41 +02:00
|
|
|
data,
|
2020-06-03 19:01:20 +02:00
|
|
|
)
|
2020-06-11 10:12:41 +02:00
|
|
|
return result
|
2020-06-03 19:01:20 +02:00
|
|
|
}
|
2020-06-02 09:49:30 +02:00
|
|
|
|
2020-06-11 10:12:41 +02:00
|
|
|
func addDescriptionFlag(cmd *cobra.Command, descriptionOpt *string) {
|
|
|
|
cmd.Flags().StringVar(descriptionOpt, "description", "", "Description of the context")
|
2020-05-14 20:55:40 +02:00
|
|
|
}
|