mirror of https://github.com/docker/docs.git
add shellinit command for displaying environment for docker client
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
b0e329b53e
commit
a60f0e672e
87
commands.go
87
commands.go
|
|
@ -30,6 +30,13 @@ import (
|
||||||
"github.com/docker/machine/utils"
|
"github.com/docker/machine/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type machineConfig struct {
|
||||||
|
caCertPath string
|
||||||
|
clientCertPath string
|
||||||
|
clientKeyPath string
|
||||||
|
machineUrl string
|
||||||
|
}
|
||||||
|
|
||||||
type hostListItem struct {
|
type hostListItem struct {
|
||||||
Name string
|
Name string
|
||||||
Active bool
|
Active bool
|
||||||
|
|
@ -121,6 +128,11 @@ var Commands = []cli.Command{
|
||||||
Usage: "Remove a machine",
|
Usage: "Remove a machine",
|
||||||
Action: cmdRm,
|
Action: cmdRm,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "shellinit",
|
||||||
|
Usage: "Display the shell commands to set up the Docker client",
|
||||||
|
Action: cmdShellinit,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
|
|
@ -190,7 +202,7 @@ func cmdCreate(c *cli.Context) {
|
||||||
log.Fatal("You must specify a machine name")
|
log.Fatal("You must specify a machine name")
|
||||||
}
|
}
|
||||||
|
|
||||||
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key"))
|
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("tls-ca-cert"), c.GlobalString("tls-ca-key"))
|
||||||
|
|
||||||
host, err := store.Create(name, driver, c)
|
host, err := store.Create(name, driver, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -208,28 +220,12 @@ func cmdCreate(c *cli.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdConfig(c *cli.Context) {
|
func cmdConfig(c *cli.Context) {
|
||||||
name := c.Args().First()
|
cfg, err := getMachineConfig(c)
|
||||||
if name == "" {
|
|
||||||
cli.ShowCommandHelp(c, "config")
|
|
||||||
log.Fatal("You must specify a machine name")
|
|
||||||
}
|
|
||||||
|
|
||||||
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key"))
|
|
||||||
|
|
||||||
host, err := store.Load(name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error loading machine config: %s", err)
|
log.Fatal(err)
|
||||||
}
|
|
||||||
|
|
||||||
caCert := filepath.Join(utils.GetMachineClientCertDir(), "ca.pem")
|
|
||||||
clientCert := filepath.Join(utils.GetMachineClientCertDir(), "cert.pem")
|
|
||||||
clientKey := filepath.Join(utils.GetMachineClientCertDir(), "key.pem")
|
|
||||||
machineUrl, err := host.GetURL()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error getting machine url: %s", err)
|
|
||||||
}
|
}
|
||||||
fmt.Printf("--tls --tlscacert=%s --tlscert=%s --tlskey=%s -H %s",
|
fmt.Printf("--tls --tlscacert=%s --tlscert=%s --tlskey=%s -H %s",
|
||||||
caCert, clientCert, clientKey, machineUrl)
|
cfg.caCertPath, cfg.clientCertPath, cfg.clientKeyPath, cfg.machineUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdInspect(c *cli.Context) {
|
func cmdInspect(c *cli.Context) {
|
||||||
|
|
@ -258,7 +254,7 @@ func cmdKill(c *cli.Context) {
|
||||||
|
|
||||||
func cmdLs(c *cli.Context) {
|
func cmdLs(c *cli.Context) {
|
||||||
quiet := c.Bool("quiet")
|
quiet := c.Bool("quiet")
|
||||||
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key"))
|
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("tls-ca-cert"), c.GlobalString("tls-ca-key"))
|
||||||
|
|
||||||
hostList, err := store.List()
|
hostList, err := store.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -329,7 +325,7 @@ func cmdRm(c *cli.Context) {
|
||||||
|
|
||||||
isError := false
|
isError := false
|
||||||
|
|
||||||
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key"))
|
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("tls-ca-cert"), c.GlobalString("tls-ca-key"))
|
||||||
for _, host := range c.Args() {
|
for _, host := range c.Args() {
|
||||||
if err := store.Remove(host, force); err != nil {
|
if err := store.Remove(host, force); err != nil {
|
||||||
log.Errorf("Error removing machine %s: %s", host, err)
|
log.Errorf("Error removing machine %s: %s", host, err)
|
||||||
|
|
@ -341,9 +337,18 @@ func cmdRm(c *cli.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cmdShellinit(c *cli.Context) {
|
||||||
|
cfg, err := getMachineConfig(c)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("export DOCKER_TLS_VERIFY=yes\nexport DOCKER_CERT_PATH=%s\nexport DOCKER_URL=%s\n",
|
||||||
|
utils.GetMachineClientCertDir(), cfg.machineUrl)
|
||||||
|
}
|
||||||
|
|
||||||
func cmdSsh(c *cli.Context) {
|
func cmdSsh(c *cli.Context) {
|
||||||
name := c.Args().First()
|
name := c.Args().First()
|
||||||
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key"))
|
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("tls-ca-cert"), c.GlobalString("tls-ca-key"))
|
||||||
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
host, err := store.GetActive()
|
host, err := store.GetActive()
|
||||||
|
|
@ -421,7 +426,7 @@ func cmdNotFound(c *cli.Context, command string) {
|
||||||
|
|
||||||
func getHost(c *cli.Context) *Host {
|
func getHost(c *cli.Context) *Host {
|
||||||
name := c.Args().First()
|
name := c.Args().First()
|
||||||
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("auth-ca"), c.GlobalString("auth-key"))
|
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("tls-ca-cert"), c.GlobalString("tls-ca-key"))
|
||||||
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
host, err := store.GetActive()
|
host, err := store.GetActive()
|
||||||
|
|
@ -471,3 +476,37 @@ func getHostState(host Host, store Store, hostListItems chan<- hostListItem) {
|
||||||
URL: url,
|
URL: url,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getMachineConfig(c *cli.Context) (*machineConfig, error) {
|
||||||
|
name := c.Args().First()
|
||||||
|
store := NewStore(c.GlobalString("storage-path"), c.GlobalString("tls-ca-cert"), c.GlobalString("tls-ca-key"))
|
||||||
|
var machine *Host
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
m, err := store.GetActive()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error getting active host: %v", err)
|
||||||
|
}
|
||||||
|
machine = m
|
||||||
|
} else {
|
||||||
|
m, err := store.Load(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error loading machine config: %s", err)
|
||||||
|
}
|
||||||
|
machine = m
|
||||||
|
}
|
||||||
|
|
||||||
|
caCert := filepath.Join(utils.GetMachineClientCertDir(), "ca.pem")
|
||||||
|
clientCert := filepath.Join(utils.GetMachineClientCertDir(), "cert.pem")
|
||||||
|
clientKey := filepath.Join(utils.GetMachineClientCertDir(), "key.pem")
|
||||||
|
machineUrl, err := machine.GetURL()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error getting machine url: %s", err)
|
||||||
|
}
|
||||||
|
return &machineConfig{
|
||||||
|
caCertPath: caCert,
|
||||||
|
clientCertPath: clientCert,
|
||||||
|
clientKeyPath: clientKey,
|
||||||
|
machineUrl: machineUrl,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -43,7 +43,7 @@ func before(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(clientCertPath); os.IsNotExist(err) {
|
if _, err := os.Stat(clientCertPath); os.IsNotExist(err) {
|
||||||
log.Debugf("Creating client certificate: %s", clientCertPath)
|
log.Infof("Creating client certificate: %s", clientCertPath)
|
||||||
|
|
||||||
if _, err := os.Stat(utils.GetMachineClientCertDir()); err != nil {
|
if _, err := os.Stat(utils.GetMachineClientCertDir()); err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/machine/drivers"
|
|
||||||
_ "github.com/docker/machine/drivers/none"
|
_ "github.com/docker/machine/drivers/none"
|
||||||
|
"github.com/docker/machine/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DriverOptionsMock struct {
|
type DriverOptionsMock struct {
|
||||||
|
|
@ -27,7 +27,7 @@ func (d DriverOptionsMock) Bool(key string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearHosts() error {
|
func clearHosts() error {
|
||||||
return os.RemoveAll(path.Join(drivers.GetHomeDir(), ".docker", "machines"))
|
return os.RemoveAll(path.Join(utils.GetHomeDir(), ".docker", "machines"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStoreCreate(t *testing.T) {
|
func TestStoreCreate(t *testing.T) {
|
||||||
|
|
@ -50,7 +50,7 @@ func TestStoreCreate(t *testing.T) {
|
||||||
if host.Name != "test" {
|
if host.Name != "test" {
|
||||||
t.Fatal("Host name is incorrect")
|
t.Fatal("Host name is incorrect")
|
||||||
}
|
}
|
||||||
path := filepath.Join(drivers.GetHomeDir(), ".docker", "machines", "test")
|
path := filepath.Join(utils.GetHomeDir(), ".docker", "machines", "test")
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
t.Fatalf("Host path doesn't exist: %s", path)
|
t.Fatalf("Host path doesn't exist: %s", path)
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +72,7 @@ func TestStoreRemove(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
path := filepath.Join(drivers.GetHomeDir(), ".docker", "machines", "test")
|
path := filepath.Join(utils.GetHomeDir(), ".docker", "machines", "test")
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
t.Fatalf("Host path doesn't exist: %s", path)
|
t.Fatalf("Host path doesn't exist: %s", path)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue