From 9444827cd50d6f6cf8101f6287f919857fe6ec72 Mon Sep 17 00:00:00 2001 From: Sven Dowideit Date: Thu, 29 May 2014 21:10:34 +1000 Subject: [PATCH] boot2docker ip outputs the host only network IP All the nice words are in stderr, and only the IP address goes to stdout, so its simple to use in an ENV / script. --- VERSION | 2 +- cmds.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- config.go | 3 ++- main.go | 2 ++ util.go | 22 ++++++++++++++-------- 5 files changed, 73 insertions(+), 12 deletions(-) diff --git a/VERSION b/VERSION index 4f7638f..8978f1d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.11.1 +v0.11.1-pre1 diff --git a/cmds.go b/cmds.go index f13ff4e..4d796c4 100644 --- a/cmds.go +++ b/cmds.go @@ -51,7 +51,7 @@ func cmdInit() int { logf("Something wrong with SSH Key file %q: %s", B2D.SSHKey, err) return 1 } - if err := cmd(B2D.SSHGen, "-t", "rsa", "-N", "", "-f", B2D.SSHKey); err != nil { + if err := cmdInteractive(B2D.SSHGen, "-t", "rsa", "-N", "", "-f", B2D.SSHKey); err != nil { logf("Error generating new SSH Key into %s: %s", B2D.SSHKey, err) return 1 } @@ -190,6 +190,15 @@ func cmdInit() int { logf("Error making tarfile: %s", err) return 1 } + file = &tar.Header{Name: ".ssh/authorized_keys2", Size: int64(len(pubKey)), Mode: 0644} + if err := tw.WriteHeader(file); err != nil { + logf("Error making tarfile: %s", err) + return 1 + } + if _, err := tw.Write([]byte(pubKey)); err != nil { + logf("Error making tarfile: %s", err) + return 1 + } if err := tw.Close(); err != nil { logf("Error making tarfile: %s", err) return 1 @@ -397,7 +406,8 @@ func cmdSSH() int { i++ } - if err := cmd(B2D.SSH, + if err := cmdInteractive(B2D.SSH, + //"-vvv", //TODO: add if its boot2docker -v "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-p", fmt.Sprintf("%d", m.SSHPort), @@ -411,6 +421,48 @@ func cmdSSH() int { return 0 } +func cmdIP() int { + m, err := vbx.GetMachine(B2D.VM) + if err != nil { + logf("Failed to get machine %q: %s", B2D.VM, err) + return 2 + } + + if m.State != vbx.Running { + logf("VM %q is not running.", B2D.VM) + return 1 + } + + out, err := cmd(B2D.SSH, + //"-vvv", //TODO: add if its boot2docker -v + "-o", "StrictHostKeyChecking=no", + "-o", "UserKnownHostsFile=/dev/null", + "-p", fmt.Sprintf("%d", m.SSHPort), + "-i", B2D.SSHKey, + "docker@localhost", + "ip addr show dev eth1", + ) + if err != nil { + logf("%s", err) + return 1 + } + // parse to find: inet 192.168.59.103/24 brd 192.168.59.255 scope global eth1 + lines := strings.Split(out, "\n") + for _, line := range lines { + vals := strings.Split(strings.TrimSpace(line), " ") + if vals[0] == "inet" { + ip := vals[1][:strings.Index(vals[1], "/")] + errf("\nThe VM's Host only interface IP address is: ") + fmt.Printf("%s", ip) + errf("\n\n") + return 0 + } + } + errf("\nFailed to get VM Host only IP address.\n") + errf("\tWas the VM initilized using boot2docker?\n") + return 0 +} + // Download the boot2docker ISO image. func cmdDownload() int { logf("Downloading boot2docker ISO image...") diff --git a/config.go b/config.go index 8f9799e..33e7ad6 100644 --- a/config.go +++ b/config.go @@ -162,7 +162,7 @@ func config() (*flag.FlagSet, error) { } func usageShort() { - errf("Usage: %s [] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|delete|download|version} []\n", os.Args[0]) + errf("Usage: %s [] {help|init|up|ssh|save|down|poweroff|reset|restart|config|status|info|ip|delete|download|version} []\n", os.Args[0]) } @@ -184,6 +184,7 @@ Commands: delete Delete boot2docker VM and its disk image. config|cfg Show selected profile file settings. info Display detailed information of VM. + ip Display the IP address of the VM's Host-only network. status Display current state of VM. download Download boot2docker ISO image. version Display version information. diff --git a/main.go b/main.go index 968c530..4d67534 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,8 @@ func run() int { return cmdStatus() case "ssh": return cmdSSH() + case "ip": + return cmdIP() case "version": outf("Client version: %s\nGit commit: %s\n", Version, GitSHA) return 0 diff --git a/util.go b/util.go index 583dce2..e37cbca 100644 --- a/util.go +++ b/util.go @@ -125,19 +125,25 @@ func getLatestReleaseName(url string) (string, error) { } // Convenient function to exec a command. -func cmd(name string, args ...string) error { +func cmd(name string, args ...string) (string, error) { cmd := exec.Command(name, args...) if B2D.Verbose { - logf("executing: %v %v", name, strings.Join(args, " ")) - cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + log.Printf("executing: %v %v", name, strings.Join(args, " ")) } - // TODO: replace this hardcoded test with something more generic - if name == B2D.SSHGen || name == B2D.SSH { - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + + b, err := cmd.Output() + return string(b), err +} + +func cmdInteractive(name string, args ...string) error { + cmd := exec.Command(name, args...) + if B2D.Verbose { + logf("executing: %v %v", name, strings.Join(args, " ")) } + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr return cmd.Run() }