Skip to content

Commit 80aaf4e

Browse files
graysidefebbraro
authored andcommitted
Display executed commands via Verbose Logging (#111)
* 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.
1 parent 7f9429d commit 80aaf4e

16 files changed

Lines changed: 174 additions & 132 deletions

commands/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package commands
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"strings"
87

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

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

5150
if cmd.machine.Exists() {
5251
// Setup new values if machine is running
53-
if output, err := exec.Command("docker-machine", "env", cmd.machine.Name).Output(); err == nil {
52+
if output, err := util.Command("docker-machine", "env", cmd.machine.Name).Output(); err == nil {
5453
os.Stdout.Write(output)
5554
}
5655
} else {

commands/dashboard.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package commands
22

33
import (
44
"fmt"
5-
"os/exec"
65

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

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

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

74-
util.ForceStreamCommand(exec.Command("docker", args...))
73+
util.ForceStreamCommand("docker", args...)
7574

7675
if util.IsMac() {
77-
exec.Command("open", "http://dashboard.outrigger.vm").Run()
76+
util.Command("open", "http://dashboard.outrigger.vm").Run()
7877
} else if util.IsWindows() {
79-
exec.Command("start", "http://dashboard.outrigger.vm").Run()
78+
util.Command("start", "http://dashboard.outrigger.vm").Run()
8079
} else {
8180
cmd.out.Info.Println("Outrigger Dashboard is now available at http://dashboard.outrigger.vm")
8281
}
@@ -86,6 +85,6 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) error {
8685

8786
// StopDashboard stops and removes the dashboard container
8887
func (cmd *Dashboard) StopDashboard() {
89-
exec.Command("docker", "stop", dashboardContainerName).Run()
90-
exec.Command("docker", "rm", dashboardContainerName).Run()
88+
util.Command("docker", "stop", dashboardContainerName).Run()
89+
util.Command("docker", "rm", dashboardContainerName).Run()
9190
}

commands/data_backup.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package commands
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76

8-
"github.com/fatih/color"
97
"github.com/phase2/rig/util"
108
"github.com/urfave/cli"
119
)
@@ -54,7 +52,7 @@ func (cmd *DataBackup) Run(c *cli.Context) error {
5452
backupFile := fmt.Sprintf("%s%c%s.tgz", backupDir, os.PathSeparator, cmd.machine.Name)
5553
if _, err := os.Stat(backupDir); err != nil {
5654
cmd.out.Info.Printf("Creating backup directory: %s...", backupDir)
57-
if mkdirErr := exec.Command("mkdir", "-p", backupDir).Run(); mkdirErr != nil {
55+
if mkdirErr := util.Command("mkdir", "-p", backupDir).Run(); mkdirErr != nil {
5856
cmd.out.Error.Println(mkdirErr)
5957
return cmd.Error(fmt.Sprintf("Could not create backup directory %s", backupDir), "BACKUP-DIR-CREATE-FAILED", 12)
6058
}
@@ -68,14 +66,7 @@ func (cmd *DataBackup) Run(c *cli.Context) error {
6866
// Stream the archive to stdout and capture it in a local file so we don't waste
6967
// space storing an archive on the VM filesystem. There may not be enough space.
7068
archiveCmd := fmt.Sprintf("sudo tar czf - -C %s .", dataDir)
71-
backup := exec.Command("docker-machine", "ssh", cmd.machine.Name, archiveCmd, ">", backupFile)
72-
backup.Stderr = os.Stderr
73-
74-
color.Set(color.FgCyan)
75-
err := backup.Run()
76-
color.Unset()
77-
78-
if err != nil {
69+
if err := util.StreamCommand("docker-machine", "ssh", cmd.machine.Name, archiveCmd, ">", backupFile); err != nil {
7970
return cmd.Error(err.Error(), "COMMAND-ERROR", 13)
8071
}
8172

commands/data_restore.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package commands
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"strings"
87

9-
"github.com/fatih/color"
108
"github.com/phase2/rig/util"
119
"github.com/urfave/cli"
1210
)
@@ -64,14 +62,7 @@ func (cmd *DataRestore) Run(c *cli.Context) error {
6462
// Send the archive via stdin and extract inline. Saves on disk & performance
6563
extractCmd := fmt.Sprintf("cat %s | docker-machine ssh %s \"sudo tar xzf - -C %s\"", backupFile, cmd.machine.Name, dataDir)
6664
cmd.out.Info.Printf(extractCmd)
67-
backup := exec.Command("bash", "-c", extractCmd)
68-
backup.Stderr = os.Stderr
69-
70-
color.Set(color.FgCyan)
71-
err := backup.Run()
72-
color.Unset()
73-
74-
if err != nil {
65+
if err := util.StreamCommand("bash", "-c", extractCmd); err != nil {
7566
return cmd.Error(err.Error(), "COMMAND-ERROR", 13)
7667
}
7768

commands/dns-records.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"fmt"
55
"io/ioutil"
66
"net/http"
7-
"os/exec"
87
"strings"
98

109
"github.com/bitly/go-simplejson"
1110
"github.com/urfave/cli"
11+
12+
"github.com/phase2/rig/util"
1213
)
1314

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

5253
// LoadRecords retrieves the records from DNSDock and processes/return them
5354
func (cmd *DNSRecords) LoadRecords() ([]map[string]interface{}, error) {
54-
ip, err := exec.Command("docker", "inspect", "--format", "{{.NetworkSettings.IPAddress}}", "dnsdock").Output()
55+
ip, err := util.Command("docker", "inspect", "--format", "{{.NetworkSettings.IPAddress}}", "dnsdock").Output()
5556
if err != nil {
5657
return nil, fmt.Errorf("failed to discover dnsdock IP address: %s", err)
5758
}

commands/dns.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package commands
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"regexp"
87
"strings"
98

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

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

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

110109
// #4: ifconfig <bridge> -hostfilter <member>
111-
util.StreamCommand(exec.Command("sudo", "ifconfig", iface, "-hostfilter", member))
110+
util.StreamCommand("sudo", "ifconfig", iface, "-hostfilter", member)
112111
}
113112

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

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

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

171-
if err := exec.Command("sudo", "mkdir", "-p", "/etc/resolver").Run(); err != nil {
170+
if err := util.Command("sudo", "mkdir", "-p", "/etc/resolver").Run(); err != nil {
172171
return err
173172
}
174-
if err := exec.Command("bash", "-c", fmt.Sprintf("echo 'nameserver %s' | sudo tee /etc/resolver/vm", bridgeIP)).Run(); err != nil {
173+
if err := util.Command("bash", "-c", fmt.Sprintf("echo 'nameserver %s' | sudo tee /etc/resolver/vm", bridgeIP)).Run(); err != nil {
175174
return err
176175
}
177176
if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
178177
// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
179178
cmd.out.Verbose.Println("Restarting discoveryutil to flush DNS caches")
180-
util.StreamCommand(exec.Command("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
181-
util.StreamCommand(exec.Command("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
179+
util.StreamCommand("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
180+
util.StreamCommand("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
182181
} else {
183182
// Reset DNS cache. We have seen this suddenly make /etc/resolver/vm work.
184183
cmd.out.Verbose.Println("Restarting mDNSResponder to flush DNS caches")
185-
util.StreamCommand(exec.Command("sudo", "killall", "-HUP", "mDNSResponder"))
184+
util.StreamCommand("sudo", "killall", "-HUP", "mDNSResponder")
186185
}
187186
return nil
188187
}
@@ -198,18 +197,18 @@ func (cmd *DNS) configureLinuxResolver() error {
198197
// Is NetworkManager in use
199198
if _, err := os.Stat("/etc/NetworkManager/dnsmasq.d"); err == nil {
200199
// Install for NetworkManager/dnsmasq connection to dnsdock
201-
util.StreamCommand(exec.Command("bash", "-c", fmt.Sprintf("echo 'server=/vm/%s' | sudo tee /etc/NetworkManager/dnsmasq.d/dnsdock.conf", bridgeIP)))
200+
util.StreamCommand("bash", "-c", fmt.Sprintf("echo 'server=/vm/%s' | sudo tee /etc/NetworkManager/dnsmasq.d/dnsdock.conf", bridgeIP))
202201

203202
// Restart NetworkManager if it is running
204-
if err := exec.Command("systemctl", "is-active", "NetworkManager").Run(); err != nil {
205-
util.StreamCommand(exec.Command("sudo", "systemctl", "restart", "NetworkManager"))
203+
if err := util.Command("systemctl", "is-active", "NetworkManager").Run(); err != nil {
204+
util.StreamCommand("sudo", "systemctl", "restart", "NetworkManager")
206205
}
207206
}
208207

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

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

225224
// StopDNS stops the dnsdock service and cleans up
226225
func (cmd *DNS) StopDNS() {
227-
exec.Command("docker", "stop", "dnsdock").Run()
228-
exec.Command("docker", "rm", "dnsdock").Run()
226+
util.Command("docker", "stop", "dnsdock").Run()
227+
util.Command("docker", "rm", "dnsdock").Run()
229228
}

commands/doctor.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package commands
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"strconv"
87
"strings"
98

@@ -33,19 +32,19 @@ func (cmd *Doctor) Commands() []cli.Command {
3332
// nolint: gocyclo
3433
func (cmd *Doctor) Run(c *cli.Context) error {
3534
// 0. Ensure all of rig's dependencies are available in the PATH.
36-
if err := exec.Command("docker", "-h").Start(); err == nil {
35+
if err := util.Command("docker", "-h").Start(); err == nil {
3736
cmd.out.Info.Println("Docker is installed.")
3837
} else {
3938
cmd.out.Error.Fatal("Docker (docker) is not installed.")
4039
}
4140
if !util.SupportsNativeDocker() {
42-
if err := exec.Command("docker-machine", "-h").Start(); err == nil {
41+
if err := util.Command("docker-machine", "-h").Start(); err == nil {
4342
cmd.out.Info.Println("Docker Machine is installed.")
4443
} else {
4544
cmd.out.Error.Fatal("Docker Machine (docker-machine) is not installed.")
4645
}
4746
}
48-
if err := exec.Command("docker-compose", "-h").Start(); err == nil {
47+
if err := util.Command("docker-compose", "-h").Start(); err == nil {
4948
cmd.out.Info.Println("Docker Compose is installed.")
5049
} else {
5150
cmd.out.Warning.Printf("Docker Compose (docker-compose) is not installed.")
@@ -61,7 +60,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {
6160
} else {
6261
cmd.out.Info.Printf("Docker Machine (%s) name matches your environment configuration.", cmd.machine.Name)
6362
}
64-
if output, err := exec.Command("docker-machine", "url", cmd.machine.Name).Output(); err == nil {
63+
if output, err := util.Command("docker-machine", "url", cmd.machine.Name).Output(); err == nil {
6564
hostURL := strings.TrimSpace(string(output))
6665
if hostURL != os.Getenv("DOCKER_HOST") {
6766
cmd.out.Error.Fatalf("Docker Host configuration should be '%s' but got '%s'. Please re-run 'eval \"$(rig config)\"'.", os.Getenv("DOCKER_HOST"), hostURL)
@@ -82,7 +81,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {
8281
cmd.out.Info.Printf("Docker Machine (%s) is running", cmd.machine.Name)
8382
}
8483
} else {
85-
if err := exec.Command("docker", "version").Run(); err != nil {
84+
if err := util.Command("docker", "version").Run(); err != nil {
8685
cmd.out.Error.Fatalf("Docker is not running. You may need to run 'systemctl start docker'")
8786
} else {
8887
cmd.out.Info.Println("Docker is running")
@@ -138,7 +137,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {
138137

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

148147
// 5. Check for storage on VM volume
149148
if !util.SupportsNativeDocker() {
150-
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()
149+
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()
151150
if err == nil {
152151
dataUsage := strings.TrimSpace(string(output))
153152
if i, e := strconv.Atoi(dataUsage); e == nil {
@@ -168,7 +167,7 @@ func (cmd *Doctor) Run(c *cli.Context) error {
168167

169168
// 6. Check for storage on /Users
170169
if !util.SupportsNativeDocker() {
171-
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()
170+
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()
172171
if err == nil {
173172
userUsage := strings.TrimSpace(string(output))
174173
if i, e := strconv.Atoi(userUsage); e == nil {

commands/kill.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package commands
22

33
import (
44
"fmt"
5-
"os/exec"
65

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

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

4746
// Ensure the underlying virtualization has stopped
4847
driver := cmd.machine.GetDriver()
4948
switch driver {
5049
case util.VirtualBox:
51-
util.StreamCommand(exec.Command("controlvm", cmd.machine.Name, "poweroff"))
50+
util.StreamCommand("controlvm", cmd.machine.Name, "poweroff")
5251
case util.VMWare:
5352
cmd.out.Warning.Println("Add vmrun suspend command.")
5453
case util.Xhyve:

0 commit comments

Comments
 (0)