feat: server add command
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Interacts with and stores infomaton in the docker store at ~/.docker Equivalent to docker context add
This commit is contained in:
parent
5e94050865
commit
fc7dade6f8
|
@ -58,6 +58,18 @@ var serverAddCommand = &cli.Command{
|
||||||
Usage: "Add a server, reachable on <host>.",
|
Usage: "Add a server, reachable on <host>.",
|
||||||
ArgsUsage: "<host> [<user>] [<port>]",
|
ArgsUsage: "<host> [<user>] [<port>]",
|
||||||
Description: "[<user>], [<port>] SSH connection details",
|
Description: "[<user>], [<port>] SSH connection details",
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
arg_len := c.Args().Len()
|
||||||
|
args := c.Args().Slice()
|
||||||
|
if arg_len < 3 {
|
||||||
|
args = append(args, make([]string, 3-arg_len)...)
|
||||||
|
}
|
||||||
|
if err := client.CreateContext(args[0], args[1], args[2]); err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println(args[0])
|
||||||
|
return nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverNewCommand = &cli.Command{
|
var serverNewCommand = &cli.Command{
|
||||||
|
|
|
@ -2,12 +2,14 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
dCliCommand "github.com/docker/cli/cli/command"
|
dCliCommand "github.com/docker/cli/cli/command"
|
||||||
dCliConfig "github.com/docker/cli/cli/config"
|
dCliConfig "github.com/docker/cli/cli/config"
|
||||||
dContext "github.com/docker/cli/cli/context"
|
dContext "github.com/docker/cli/cli/context"
|
||||||
|
"github.com/docker/cli/cli/context/docker"
|
||||||
dCliContextStore "github.com/docker/cli/cli/context/store"
|
dCliContextStore "github.com/docker/cli/cli/context/store"
|
||||||
dClient "github.com/docker/docker/client"
|
dClient "github.com/docker/docker/client"
|
||||||
"github.com/moby/term"
|
"github.com/moby/term"
|
||||||
|
@ -57,6 +59,79 @@ func NewClientWithContext(contextName string) (*dClient.Client, error) {
|
||||||
return cl, nil
|
return cl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateContext(contextName string, user string, port string) error {
|
||||||
|
host := contextName
|
||||||
|
if user != "" {
|
||||||
|
host = fmt.Sprintf("%s@%s", user, host)
|
||||||
|
}
|
||||||
|
if port != "" {
|
||||||
|
host = fmt.Sprintf("%s:%s", host, port)
|
||||||
|
}
|
||||||
|
host = fmt.Sprintf("ssh://%s", host)
|
||||||
|
if err := createNewContext(contextName, host); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createNewContext(name string, host string) error {
|
||||||
|
s := NewDefaultDockerContextStore()
|
||||||
|
contextMetadata := newContextMetadata(name)
|
||||||
|
contextTLSData := dCliContextStore.ContextTLSData{
|
||||||
|
Endpoints: make(map[string]dCliContextStore.EndpointTLSData),
|
||||||
|
}
|
||||||
|
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(host)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
contextMetadata.Endpoints[docker.DockerEndpoint] = dockerEP
|
||||||
|
if dockerTLS != nil {
|
||||||
|
contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerTLS
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.CreateOrUpdate(contextMetadata); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := s.ResetTLSMaterial(name, &contextTLSData); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDockerEndpoint(host string) (docker.Endpoint, error) {
|
||||||
|
skipTLSVerify := false
|
||||||
|
ep := docker.Endpoint{
|
||||||
|
EndpointMeta: docker.EndpointMeta{
|
||||||
|
Host: host,
|
||||||
|
SkipTLSVerify: skipTLSVerify,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// try to resolve a docker client, validating the configuration
|
||||||
|
opts, err := ep.ClientOpts()
|
||||||
|
if err != nil {
|
||||||
|
return docker.Endpoint{}, err
|
||||||
|
}
|
||||||
|
if _, err := dClient.NewClientWithOpts(opts...); err != nil {
|
||||||
|
return docker.Endpoint{}, err
|
||||||
|
}
|
||||||
|
return ep, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newContextMetadata(name string) dCliContextStore.Metadata {
|
||||||
|
return dCliContextStore.Metadata{
|
||||||
|
Endpoints: make(map[string]interface{}),
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDockerEndpointMetadataAndTLS(host string) (docker.EndpointMeta, *dCliContextStore.EndpointTLSData, error) {
|
||||||
|
ep, err := getDockerEndpoint(host)
|
||||||
|
if err != nil {
|
||||||
|
return docker.EndpointMeta{}, nil, err
|
||||||
|
}
|
||||||
|
return ep.EndpointMeta, ep.TLSData.ToStoreTLSData(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetContext(contextName string) (dCliContextStore.Metadata, error) {
|
func GetContext(contextName string) (dCliContextStore.Metadata, error) {
|
||||||
ctx, err := NewDefaultDockerContextStore().GetMetadata(contextName)
|
ctx, err := NewDefaultDockerContextStore().GetMetadata(contextName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Reference in New Issue