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

Commit a088ba4

Browse files
Sven DowideitSven Dowideit
authored andcommitted
Merge pull request #136 from SvenDowideit/use-actual-vbox-ports
boot2docker ip outputs the host only network IP
2 parents 749e187 + 9444827 commit a088ba4

5 files changed

Lines changed: 73 additions & 12 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.11.1
1+
v0.11.1-pre1

cmds.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func cmdInit() int {
5151
logf("Something wrong with SSH Key file %q: %s", B2D.SSHKey, err)
5252
return 1
5353
}
54-
if err := cmd(B2D.SSHGen, "-t", "rsa", "-N", "", "-f", B2D.SSHKey); err != nil {
54+
if err := cmdInteractive(B2D.SSHGen, "-t", "rsa", "-N", "", "-f", B2D.SSHKey); err != nil {
5555
logf("Error generating new SSH Key into %s: %s", B2D.SSHKey, err)
5656
return 1
5757
}
@@ -190,6 +190,15 @@ func cmdInit() int {
190190
logf("Error making tarfile: %s", err)
191191
return 1
192192
}
193+
file = &tar.Header{Name: ".ssh/authorized_keys2", Size: int64(len(pubKey)), Mode: 0644}
194+
if err := tw.WriteHeader(file); err != nil {
195+
logf("Error making tarfile: %s", err)
196+
return 1
197+
}
198+
if _, err := tw.Write([]byte(pubKey)); err != nil {
199+
logf("Error making tarfile: %s", err)
200+
return 1
201+
}
193202
if err := tw.Close(); err != nil {
194203
logf("Error making tarfile: %s", err)
195204
return 1
@@ -397,7 +406,8 @@ func cmdSSH() int {
397406
i++
398407
}
399408

400-
if err := cmd(B2D.SSH,
409+
if err := cmdInteractive(B2D.SSH,
410+
//"-vvv", //TODO: add if its boot2docker -v
401411
"-o", "StrictHostKeyChecking=no",
402412
"-o", "UserKnownHostsFile=/dev/null",
403413
"-p", fmt.Sprintf("%d", m.SSHPort),
@@ -411,6 +421,48 @@ func cmdSSH() int {
411421
return 0
412422
}
413423

424+
func cmdIP() int {
425+
m, err := vbx.GetMachine(B2D.VM)
426+
if err != nil {
427+
logf("Failed to get machine %q: %s", B2D.VM, err)
428+
return 2
429+
}
430+
431+
if m.State != vbx.Running {
432+
logf("VM %q is not running.", B2D.VM)
433+
return 1
434+
}
435+
436+
out, err := cmd(B2D.SSH,
437+
//"-vvv", //TODO: add if its boot2docker -v
438+
"-o", "StrictHostKeyChecking=no",
439+
"-o", "UserKnownHostsFile=/dev/null",
440+
"-p", fmt.Sprintf("%d", m.SSHPort),
441+
"-i", B2D.SSHKey,
442+
"docker@localhost",
443+
"ip addr show dev eth1",
444+
)
445+
if err != nil {
446+
logf("%s", err)
447+
return 1
448+
}
449+
// parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1
450+
lines := strings.Split(out, "\n")
451+
for _, line := range lines {
452+
vals := strings.Split(strings.TrimSpace(line), " ")
453+
if vals[0] == "inet" {
454+
ip := vals[1][:strings.Index(vals[1], "/")]
455+
errf("\nThe VM's Host only interface IP address is: ")
456+
fmt.Printf("%s", ip)
457+
errf("\n\n")
458+
return 0
459+
}
460+
}
461+
errf("\nFailed to get VM Host only IP address.\n")
462+
errf("\tWas the VM initilized using boot2docker?\n")
463+
return 0
464+
}
465+
414466
// Download the boot2docker ISO image.
415467
func cmdDownload() int {
416468
logf("Downloading boot2docker ISO image...")

config.go

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

164164
func usageShort() {
165-
errf("Usage: %s [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|delete|download|version} [<args>]\n", os.Args[0])
165+
errf("Usage: %s [<options>] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|delete|download|version} [<args>]\n", os.Args[0])
166166

167167
}
168168

@@ -184,6 +184,7 @@ Commands:
184184
delete Delete boot2docker VM and its disk image.
185185
config|cfg Show selected profile file settings.
186186
info Display detailed information of VM.
187+
ip Display the IP address of the VM's Host-only network.
187188
status Display current state of VM.
188189
download Download boot2docker ISO image.
189190
version Display version information.

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func run() int {
5353
return cmdStatus()
5454
case "ssh":
5555
return cmdSSH()
56+
case "ip":
57+
return cmdIP()
5658
case "version":
5759
outf("Client version: %s\nGit commit: %s\n", Version, GitSHA)
5860
return 0

util.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,25 @@ func getLatestReleaseName(url string) (string, error) {
125125
}
126126

127127
// Convenient function to exec a command.
128-
func cmd(name string, args ...string) error {
128+
func cmd(name string, args ...string) (string, error) {
129129
cmd := exec.Command(name, args...)
130130
if B2D.Verbose {
131-
logf("executing: %v %v", name, strings.Join(args, " "))
132-
cmd.Stdout = os.Stdout
133131
cmd.Stderr = os.Stderr
132+
log.Printf("executing: %v %v", name, strings.Join(args, " "))
134133
}
135-
// TODO: replace this hardcoded test with something more generic
136-
if name == B2D.SSHGen || name == B2D.SSH {
137-
cmd.Stdin = os.Stdin
138-
cmd.Stdout = os.Stdout
139-
cmd.Stderr = os.Stderr
134+
135+
b, err := cmd.Output()
136+
return string(b), err
137+
}
138+
139+
func cmdInteractive(name string, args ...string) error {
140+
cmd := exec.Command(name, args...)
141+
if B2D.Verbose {
142+
logf("executing: %v %v", name, strings.Join(args, " "))
140143
}
144+
cmd.Stdin = os.Stdin
145+
cmd.Stdout = os.Stdout
146+
cmd.Stderr = os.Stderr
141147
return cmd.Run()
142148
}
143149

0 commit comments

Comments
 (0)