Skip to content

Commit

Permalink
Display executed commands via Verbose Logging (#111)
Browse files Browse the repository at this point in the history
* Display commands executed by StreamCommand and rig dashboard.

* Add executor: command wrapper.

* Simplify calling signature of *StreamCommand

* Continue re-wrapping exec commands.

* Executorize all commands.

* Correct lint standards and account for API changes in git conflict resolution.
  • Loading branch information
grayside authored and febbraro committed Nov 10, 2017
1 parent 7f9429d commit 80aaf4e
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 132 deletions.
5 changes: 2 additions & 3 deletions commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/phase2/rig/util"
Expand Down Expand Up @@ -44,13 +43,13 @@ func (cmd *Config) Run(c *cli.Context) error {
}

// Clear out any previous environment variables
if output, err := exec.Command("docker-machine", "env", "-u").Output(); err == nil {
if output, err := util.Command("docker-machine", "env", "-u").Output(); err == nil {
os.Stdout.Write(output)
}

if cmd.machine.Exists() {
// Setup new values if machine is running
if output, err := exec.Command("docker-machine", "env", cmd.machine.Name).Output(); err == nil {
if output, err := util.Command("docker-machine", "env", cmd.machine.Name).Output(); err == nil {
os.Stdout.Write(output)
}
} else {
Expand Down
13 changes: 6 additions & 7 deletions commands/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"os/exec"

"github.com/phase2/rig/util"
"github.com/urfave/cli"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) error {
}

cmd.out.Verbose.Printf("Attempting to update %s", dashboardImageName)
if err := util.StreamCommand(exec.Command("docker", "pull", dashboardImageName)); err != nil {
if err := util.StreamCommand("docker", "pull", dashboardImageName); err != nil {
cmd.out.Verbose.Println("Failed to update dashboard image. Will use local cache if available.")
}

Expand All @@ -71,12 +70,12 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) error {
dashboardImageName,
}

util.ForceStreamCommand(exec.Command("docker", args...))
util.ForceStreamCommand("docker", args...)

if util.IsMac() {
exec.Command("open", "http://dashboard.outrigger.vm").Run()
util.Command("open", "http://dashboard.outrigger.vm").Run()
} else if util.IsWindows() {
exec.Command("start", "http://dashboard.outrigger.vm").Run()
util.Command("start", "http://dashboard.outrigger.vm").Run()
} else {
cmd.out.Info.Println("Outrigger Dashboard is now available at http://dashboard.outrigger.vm")
}
Expand All @@ -86,6 +85,6 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) error {

// StopDashboard stops and removes the dashboard container
func (cmd *Dashboard) StopDashboard() {
exec.Command("docker", "stop", dashboardContainerName).Run()
exec.Command("docker", "rm", dashboardContainerName).Run()
util.Command("docker", "stop", dashboardContainerName).Run()
util.Command("docker", "rm", dashboardContainerName).Run()
}
13 changes: 2 additions & 11 deletions commands/data_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package commands
import (
"fmt"
"os"
"os/exec"

"github.com/fatih/color"
"github.com/phase2/rig/util"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -54,7 +52,7 @@ func (cmd *DataBackup) Run(c *cli.Context) error {
backupFile := fmt.Sprintf("%s%c%s.tgz", backupDir, os.PathSeparator, cmd.machine.Name)
if _, err := os.Stat(backupDir); err != nil {
cmd.out.Info.Printf("Creating backup directory: %s...", backupDir)
if mkdirErr := exec.Command("mkdir", "-p", backupDir).Run(); mkdirErr != nil {
if mkdirErr := util.Command("mkdir", "-p", backupDir).Run(); mkdirErr != nil {
cmd.out.Error.Println(mkdirErr)
return cmd.Error(fmt.Sprintf("Could not create backup directory %s", backupDir), "BACKUP-DIR-CREATE-FAILED", 12)
}
Expand All @@ -68,14 +66,7 @@ func (cmd *DataBackup) Run(c *cli.Context) error {
// Stream the archive to stdout and capture it in a local file so we don't waste
// space storing an archive on the VM filesystem. There may not be enough space.
archiveCmd := fmt.Sprintf("sudo tar czf - -C %s .", dataDir)
backup := exec.Command("docker-machine", "ssh", cmd.machine.Name, archiveCmd, ">", backupFile)
backup.Stderr = os.Stderr

color.Set(color.FgCyan)
err := backup.Run()
color.Unset()

if err != nil {
if err := util.StreamCommand("docker-machine", "ssh", cmd.machine.Name, archiveCmd, ">", backupFile); err != nil {
return cmd.Error(err.Error(), "COMMAND-ERROR", 13)
}

Expand Down
11 changes: 1 addition & 10 deletions commands/data_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package commands
import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/fatih/color"
"github.com/phase2/rig/util"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -64,14 +62,7 @@ func (cmd *DataRestore) Run(c *cli.Context) error {
// Send the archive via stdin and extract inline. Saves on disk & performance
extractCmd := fmt.Sprintf("cat %s | docker-machine ssh %s \"sudo tar xzf - -C %s\"", backupFile, cmd.machine.Name, dataDir)
cmd.out.Info.Printf(extractCmd)
backup := exec.Command("bash", "-c", extractCmd)
backup.Stderr = os.Stderr

color.Set(color.FgCyan)
err := backup.Run()
color.Unset()

if err != nil {
if err := util.StreamCommand("bash", "-c", extractCmd); err != nil {
return cmd.Error(err.Error(), "COMMAND-ERROR", 13)
}

Expand Down
5 changes: 3 additions & 2 deletions commands/dns-records.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strings"

"github.com/bitly/go-simplejson"
"github.com/urfave/cli"

"github.com/phase2/rig/util"
)

// DNSRecords is the command for exporting all DNS Records in Outrigger DNS in `hosts` file format
Expand Down Expand Up @@ -51,7 +52,7 @@ func (cmd *DNSRecords) Run(c *cli.Context) error {

// LoadRecords retrieves the records from DNSDock and processes/return them
func (cmd *DNSRecords) LoadRecords() ([]map[string]interface{}, error) {
ip, err := exec.Command("docker", "inspect", "--format", "{{.NetworkSettings.IPAddress}}", "dnsdock").Output()
ip, err := util.Command("docker", "inspect", "--format", "{{.NetworkSettings.IPAddress}}", "dnsdock").Output()
if err != nil {
return nil, fmt.Errorf("failed to discover dnsdock IP address: %s", err)
}
Expand Down
45 changes: 22 additions & 23 deletions commands/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"

Expand Down Expand Up @@ -73,24 +72,24 @@ func (cmd *DNS) configureMacRoutes(machine Machine) {
if machine.IsXhyve() {
cmd.removeHostFilter(machineIP)
}
exec.Command("sudo", "route", "-n", "delete", "-net", "172.17.0.0").Run()
util.StreamCommand(exec.Command("sudo", "route", "-n", "add", "172.17.0.0/16", machineIP))
util.Command("sudo", "route", "-n", "delete", "-net", "172.17.0.0").Run()
util.StreamCommand("sudo", "route", "-n", "add", "172.17.0.0/16", machineIP)
if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
cmd.out.Verbose.Println("Restarting discoveryutil to flush DNS caches")
util.StreamCommand(exec.Command("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
util.StreamCommand(exec.Command("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
util.StreamCommand("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
util.StreamCommand("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
} else {
// Reset DNS cache. We have seen this suddenly make /etc/resolver/vm work.
cmd.out.Verbose.Println("Restarting mDNSResponder to flush DNS caches")
util.StreamCommand(exec.Command("sudo", "killall", "-HUP", "mDNSResponder"))
util.StreamCommand("sudo", "killall", "-HUP", "mDNSResponder")
}
}

// removeHostFilter removes the host filter from the xhyve bridge interface
func (cmd *DNS) removeHostFilter(ipAddr string) {
// #1: route -n get <machineIP> to find the interface name
routeData, err := exec.Command("route", "-n", "get", ipAddr).CombinedOutput()
routeData, err := util.Command("route", "-n", "get", ipAddr).CombinedOutput()
if err != nil {
cmd.out.Warning.Println("Unable to determine bridge interface to remove hostfilter")
return
Expand All @@ -99,7 +98,7 @@ func (cmd *DNS) removeHostFilter(ipAddr string) {
iface := ifaceRegexp.FindStringSubmatch(string(routeData))[1]

// #2: ifconfig <interface name> to get the details
ifaceData, err := exec.Command("ifconfig", iface).CombinedOutput()
ifaceData, err := util.Command("ifconfig", iface).CombinedOutput()
if err != nil {
cmd.out.Warning.Println("Unable to determine member to remove hostfilter")
return
Expand All @@ -108,13 +107,13 @@ func (cmd *DNS) removeHostFilter(ipAddr string) {
member := memberRegexp.FindStringSubmatch(string(ifaceData))[1]

// #4: ifconfig <bridge> -hostfilter <member>
util.StreamCommand(exec.Command("sudo", "ifconfig", iface, "-hostfilter", member))
util.StreamCommand("sudo", "ifconfig", iface, "-hostfilter", member)
}

// ConfigureWindowsRoutes configures network routing
func (cmd *DNS) configureWindowsRoutes(machine Machine) {
exec.Command("runas", "/noprofile", "/user:Administrator", "route", "DELETE", "172.17.0.0").Run()
util.StreamCommand(exec.Command("runas", "/noprofile", "/user:Administrator", "route", "-p", "ADD", "172.17.0.0/16", machine.GetIP()))
util.Command("runas", "/noprofile", "/user:Administrator", "route", "DELETE", "172.17.0.0").Run()
util.StreamCommand("runas", "/noprofile", "/user:Administrator", "route", "-p", "ADD", "172.17.0.0/16", machine.GetIP())
}

// StartDNS will start the dnsdock service
Expand Down Expand Up @@ -149,7 +148,7 @@ func (cmd *DNS) StartDNS(machine Machine, nameservers string) error {
for _, server := range dnsServers {
args = append(args, "--nameserver="+server)
}
util.ForceStreamCommand(exec.Command("docker", args...))
util.ForceStreamCommand("docker", args...)

// Configure the resolvers based on platform
var resolverReturn error
Expand All @@ -168,21 +167,21 @@ func (cmd *DNS) configureMacResolver(machine Machine) error {
cmd.out.Verbose.Print("Configuring DNS resolution for macOS")
bridgeIP := machine.GetBridgeIP()

if err := exec.Command("sudo", "mkdir", "-p", "/etc/resolver").Run(); err != nil {
if err := util.Command("sudo", "mkdir", "-p", "/etc/resolver").Run(); err != nil {
return err
}
if err := exec.Command("bash", "-c", fmt.Sprintf("echo 'nameserver %s' | sudo tee /etc/resolver/vm", bridgeIP)).Run(); err != nil {
if err := util.Command("bash", "-c", fmt.Sprintf("echo 'nameserver %s' | sudo tee /etc/resolver/vm", bridgeIP)).Run(); err != nil {
return err
}
if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
cmd.out.Verbose.Println("Restarting discoveryutil to flush DNS caches")
util.StreamCommand(exec.Command("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
util.StreamCommand(exec.Command("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
util.StreamCommand("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
util.StreamCommand("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
} else {
// Reset DNS cache. We have seen this suddenly make /etc/resolver/vm work.
cmd.out.Verbose.Println("Restarting mDNSResponder to flush DNS caches")
util.StreamCommand(exec.Command("sudo", "killall", "-HUP", "mDNSResponder"))
util.StreamCommand("sudo", "killall", "-HUP", "mDNSResponder")
}
return nil
}
Expand All @@ -198,18 +197,18 @@ func (cmd *DNS) configureLinuxResolver() error {
// Is NetworkManager in use
if _, err := os.Stat("/etc/NetworkManager/dnsmasq.d"); err == nil {
// Install for NetworkManager/dnsmasq connection to dnsdock
util.StreamCommand(exec.Command("bash", "-c", fmt.Sprintf("echo 'server=/vm/%s' | sudo tee /etc/NetworkManager/dnsmasq.d/dnsdock.conf", bridgeIP)))
util.StreamCommand("bash", "-c", fmt.Sprintf("echo 'server=/vm/%s' | sudo tee /etc/NetworkManager/dnsmasq.d/dnsdock.conf", bridgeIP))

// Restart NetworkManager if it is running
if err := exec.Command("systemctl", "is-active", "NetworkManager").Run(); err != nil {
util.StreamCommand(exec.Command("sudo", "systemctl", "restart", "NetworkManager"))
if err := util.Command("systemctl", "is-active", "NetworkManager").Run(); err != nil {
util.StreamCommand("sudo", "systemctl", "restart", "NetworkManager")
}
}

// Is libnss-resolver in use
if _, err := os.Stat("/etc/resolver"); err == nil {
// Install for libnss-resolver connection to dnsdock
exec.Command("bash", "-c", fmt.Sprintf("echo 'nameserver %s:53' | sudo tee /etc/resolver/vm", bridgeIP)).Run()
util.Command("bash", "-c", fmt.Sprintf("echo 'nameserver %s:53' | sudo tee /etc/resolver/vm", bridgeIP)).Run()
}

return nil
Expand All @@ -224,6 +223,6 @@ func (cmd *DNS) configureWindowsResolver(machine Machine) error {

// StopDNS stops the dnsdock service and cleans up
func (cmd *DNS) StopDNS() {
exec.Command("docker", "stop", "dnsdock").Run()
exec.Command("docker", "rm", "dnsdock").Run()
util.Command("docker", "stop", "dnsdock").Run()
util.Command("docker", "rm", "dnsdock").Run()
}
17 changes: 8 additions & 9 deletions commands/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"

Expand Down Expand Up @@ -33,19 +32,19 @@ func (cmd *Doctor) Commands() []cli.Command {
// nolint: gocyclo
func (cmd *Doctor) Run(c *cli.Context) error {
// 0. Ensure all of rig's dependencies are available in the PATH.
if err := exec.Command("docker", "-h").Start(); err == nil {
if err := util.Command("docker", "-h").Start(); err == nil {
cmd.out.Info.Println("Docker is installed.")
} else {
cmd.out.Error.Fatal("Docker (docker) is not installed.")
}
if !util.SupportsNativeDocker() {
if err := exec.Command("docker-machine", "-h").Start(); err == nil {
if err := util.Command("docker-machine", "-h").Start(); err == nil {
cmd.out.Info.Println("Docker Machine is installed.")
} else {
cmd.out.Error.Fatal("Docker Machine (docker-machine) is not installed.")
}
}
if err := exec.Command("docker-compose", "-h").Start(); err == nil {
if err := util.Command("docker-compose", "-h").Start(); err == nil {
cmd.out.Info.Println("Docker Compose is installed.")
} else {
cmd.out.Warning.Printf("Docker Compose (docker-compose) is not installed.")
Expand All @@ -61,7 +60,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {
} else {
cmd.out.Info.Printf("Docker Machine (%s) name matches your environment configuration.", cmd.machine.Name)
}
if output, err := exec.Command("docker-machine", "url", cmd.machine.Name).Output(); err == nil {
if output, err := util.Command("docker-machine", "url", cmd.machine.Name).Output(); err == nil {
hostURL := strings.TrimSpace(string(output))
if hostURL != os.Getenv("DOCKER_HOST") {
cmd.out.Error.Fatalf("Docker Host configuration should be '%s' but got '%s'. Please re-run 'eval \"$(rig config)\"'.", os.Getenv("DOCKER_HOST"), hostURL)
Expand All @@ -82,7 +81,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {
cmd.out.Info.Printf("Docker Machine (%s) is running", cmd.machine.Name)
}
} else {
if err := exec.Command("docker", "version").Run(); err != nil {
if err := util.Command("docker", "version").Run(); err != nil {
cmd.out.Error.Fatalf("Docker is not running. You may need to run 'systemctl start docker'")
} else {
cmd.out.Info.Println("Docker is running")
Expand Down Expand Up @@ -138,7 +137,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {

// 4. Ensure that docker-machine-nfs script is available for our NFS mounts (Mac ONLY)
if util.IsMac() {
if err := exec.Command("which", "docker-machine-nfs").Run(); err != nil {
if err := util.Command("which", "docker-machine-nfs").Run(); err != nil {
cmd.out.Error.Println("Docker Machine NFS is not installed.")
} else {
cmd.out.Info.Println("Docker Machine NFS is installed.")
Expand All @@ -147,7 +146,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {

// 5. Check for storage on VM volume
if !util.SupportsNativeDocker() {
output, err := exec.Command("docker-machine", "ssh", cmd.machine.Name, "df -h 2> /dev/null | grep /dev/sda1 | head -1 | awk '{print $5}' | sed 's/%//'").Output()
output, err := util.Command("docker-machine", "ssh", cmd.machine.Name, "df -h 2> /dev/null | grep /dev/sda1 | head -1 | awk '{print $5}' | sed 's/%//'").Output()
if err == nil {
dataUsage := strings.TrimSpace(string(output))
if i, e := strconv.Atoi(dataUsage); e == nil {
Expand All @@ -168,7 +167,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {

// 6. Check for storage on /Users
if !util.SupportsNativeDocker() {
output, err := exec.Command("docker-machine", "ssh", cmd.machine.Name, "df -h 2> /dev/null | grep /Users | head -1 | awk '{print $5}' | sed 's/%//'").Output()
output, err := util.Command("docker-machine", "ssh", cmd.machine.Name, "df -h 2> /dev/null | grep /Users | head -1 | awk '{print $5}' | sed 's/%//'").Output()
if err == nil {
userUsage := strings.TrimSpace(string(output))
if i, e := strconv.Atoi(userUsage); e == nil {
Expand Down
5 changes: 2 additions & 3 deletions commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"os/exec"

"github.com/phase2/rig/util"
"github.com/urfave/cli"
Expand Down Expand Up @@ -42,13 +41,13 @@ func (cmd *Kill) Run(c *cli.Context) error {
}

cmd.out.Info.Printf("Killing machine '%s'", cmd.machine.Name)
util.StreamCommand(exec.Command("docker-machine", "kill", cmd.machine.Name))
util.StreamCommand("docker-machine", "kill", cmd.machine.Name)

// Ensure the underlying virtualization has stopped
driver := cmd.machine.GetDriver()
switch driver {
case util.VirtualBox:
util.StreamCommand(exec.Command("controlvm", cmd.machine.Name, "poweroff"))
util.StreamCommand("controlvm", cmd.machine.Name, "poweroff")
case util.VMWare:
cmd.out.Warning.Println("Add vmrun suspend command.")
case util.Xhyve:
Expand Down
Loading

0 comments on commit 80aaf4e

Please sign in to comment.