Skip to content
This repository was archived by the owner on Feb 27, 2018. It is now read-only.

Commit ab39714

Browse files
Sven DowideitSven Dowideit
authored andcommitted
Merge pull request #226 from SvenDowideit/add-b2d-shellinit-cmd
Add b2d shellinit cmd
2 parents dd7a88d + 51821eb commit ab39714

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

cmds.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func cmdUp() error {
7373
certPath, err := RequestCertsUsingSSH(m)
7474
if err != nil && B2D.Verbose {
7575
// These errors are not fatal
76-
fmt.Printf("Error copying Certificates: %s\n", err)
76+
fmt.Fprintf(os.Stderr, "Error copying Certificates: %s\n", err)
7777
}
7878
switch runtime.GOOS {
7979
case "windows":
@@ -89,15 +89,7 @@ func cmdUp() error {
8989
socket := RequestSocketFromSSH(m)
9090
if os.Getenv("DOCKER_HOST") != socket || os.Getenv("DOCKER_CERT_PATH") != certPath {
9191
fmt.Printf("\nTo connect the Docker client to the Docker daemon, please set:\n")
92-
fmt.Printf(" export DOCKER_HOST=%s\n", socket)
93-
if certPath == "" {
94-
if os.Getenv("DOCKER_CERT_PATH") != "" {
95-
fmt.Println(" unset DOCKER_CERT_PATH")
96-
}
97-
} else {
98-
// Assume Docker 1.2.0 with TLS on...
99-
fmt.Printf(" export DOCKER_CERT_PATH=%s\n", certPath)
100-
}
92+
printExport(socket, certPath)
10193
} else {
10294
fmt.Printf("Your DOCKER_HOST env variable is already set correctly.\n")
10395
}
@@ -107,6 +99,41 @@ func cmdUp() error {
10799
return nil
108100
}
109101

102+
// Give the user the exact command to run to set the env.
103+
func cmdShellInit() error {
104+
m, err := driver.GetMachine(&B2D)
105+
if err != nil {
106+
return fmt.Errorf("Failed to get machine %q: %s", B2D.VM, err)
107+
}
108+
109+
if m.GetState() != driver.Running {
110+
return fmt.Errorf("VM %q is not running.", B2D.VM)
111+
}
112+
113+
socket := RequestSocketFromSSH(m)
114+
115+
certPath, err := RequestCertsUsingSSH(m)
116+
if err != nil && B2D.Verbose {
117+
// These errors are not fatal
118+
fmt.Fprintf(os.Stderr, "Error copying Certificates: %s\n", err)
119+
}
120+
printExport(socket, certPath)
121+
122+
return nil
123+
}
124+
125+
func printExport(socket, certPath string) {
126+
fmt.Printf(" export DOCKER_HOST=%s\n", socket)
127+
if certPath == "" {
128+
if os.Getenv("DOCKER_CERT_PATH") != "" {
129+
fmt.Println(" unset DOCKER_CERT_PATH")
130+
}
131+
} else {
132+
// Assume Docker 1.2.0 with TLS on...
133+
fmt.Printf(" export DOCKER_CERT_PATH=%s\n", certPath)
134+
}
135+
}
136+
110137
// Tell the user the config (and later let them set it?)
111138
func cmdConfig() error {
112139
dir, err := cfgDir(".boot2docker")
@@ -243,10 +270,10 @@ func cmdSocket() error {
243270
return fmt.Errorf("VM %q is not running.", B2D.VM)
244271
}
245272

246-
Socket := RequestSocketFromSSH(m)
273+
socket := RequestSocketFromSSH(m)
247274

248275
fmt.Fprintf(os.Stderr, "\n\t export DOCKER_HOST=")
249-
fmt.Printf("%s", Socket)
276+
fmt.Printf("%s", socket)
250277
fmt.Fprintf(os.Stderr, "\n\n")
251278

252279
return nil

config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func config() (*flag.FlagSet, error) {
175175
}
176176

177177
func usageShort() {
178-
fmt.Fprintf(os.Stderr, "Usage: %s [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|socket|delete|destroy|download|version} [<args>]\n", os.Args[0])
178+
fmt.Fprintf(os.Stderr, "Usage: %s [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|socket|shellinit|delete|destroy|download|version} [<args>]\n", os.Args[0])
179179
}
180180

181181
func usageLong(flags *flag.FlagSet) {
@@ -198,6 +198,7 @@ Commands:
198198
info Display detailed information of VM.
199199
ip Display the IP address of the VM's Host-only network.
200200
socket Display the DOCKER_HOST socket to connect to.
201+
shellinit Display the Environment export shell command needed to set up the Docker client.
201202
status Display current state of VM.
202203
download Download boot2docker ISO image.
203204
upgrade Upgrade the boot2docker ISO image (if vm is running it will be stopped and started).

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func run() error {
6767
return cmdInfo()
6868
case "socket":
6969
return cmdSocket()
70+
case "shellinit":
71+
return cmdShellInit()
7072
case "status":
7173
return cmdStatus()
7274
case "ssh":

util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ func RequestIPFromSerialPort(socket string) string {
297297
}
298298

299299
// TODO: need to add or abstract to get a Serial coms version
300+
// RequestCertsUsingSSH requests certs using SSH.
301+
// The assumption is that if the certs are in b2d:/home/docker/.docker
302+
// then the daemon is using TLS. We can't assume that because there are
303+
// certs in the local host's user dir, that the server is using them, so
304+
// for now, make sure things are updated from the server. (for `docker shellinit`)
300305
func RequestCertsUsingSSH(m driver.Machine) (string, error) {
301306
cmd := getSSHCommand(m, "tar c /home/docker/.docker/*.pem")
302307

0 commit comments

Comments
 (0)