Skip to content

Commit 6852dec

Browse files
mprokopmagiconair
authored andcommitted
agent: Fix script quoting on windows (#1875)
This patch fixes the quoting for executing scripts on windows and splits the platform dependent code. Fixes #1875
1 parent adf3fc0 commit 6852dec

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BUG FIXES:
1717
* agent: Clean up temporary files during disk write errors when persisting services and checks. [GH-3207]
1818
* agent: Fixed an issue where dns and client bind address templates were not being parsed via the go-sockaddr library. [GH-3322]
1919
* agent: Fixed status code on all KV store operations that fail due to an ACL issue. They now return a 403 status code, rather than a 404. [GH-2637]
20+
* agent: Fixed quoting issues in script health check on windows. [GH-1875]
2021

2122
## 0.9.0 (July 20, 2017)
2223

agent/util.go

-19
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"fmt"
77
"math"
88
"os"
9-
"os/exec"
109
"os/user"
11-
"runtime"
1210
"strconv"
1311
"time"
1412

@@ -45,23 +43,6 @@ func aeScale(interval time.Duration, n int) time.Duration {
4543
return time.Duration(multiplier) * interval
4644
}
4745

48-
// ExecScript returns a command to execute a script
49-
func ExecScript(script string) (*exec.Cmd, error) {
50-
var shell, flag string
51-
if runtime.GOOS == "windows" {
52-
shell = "cmd"
53-
flag = "/C"
54-
} else {
55-
shell = "/bin/sh"
56-
flag = "-c"
57-
}
58-
if other := os.Getenv("SHELL"); other != "" {
59-
shell = other
60-
}
61-
cmd := exec.Command(shell, flag, script)
62-
return cmd, nil
63-
}
64-
6546
// decodeMsgPack is used to decode a MsgPack encoded object
6647
func decodeMsgPack(buf []byte, out interface{}) error {
6748
return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)

agent/util_other.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// +build !windows
2+
3+
package agent
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
)
9+
10+
11+
// ExecScript returns a command to execute a script
12+
func ExecScript(script string) (*exec.Cmd, error) {
13+
shell := "/bin/sh"
14+
if other := os.Getenv("SHELL"); other != "" {
15+
shell = other
16+
}
17+
return exec.Command(shell, "-c", script), nil
18+
}

agent/util_windows.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// +build windows
2+
3+
package agent
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
"strings"
9+
"syscall"
10+
)
11+
12+
// ExecScript returns a command to execute a script
13+
func ExecScript(script string) (*exec.Cmd, error) {
14+
shell := "cmd"
15+
if other := os.Getenv("SHELL"); other != "" {
16+
shell = other
17+
}
18+
script = "\"" + script + "\"";
19+
cmd := exec.Command(shell, "/C", script)
20+
cmd.SysProcAttr = &syscall.SysProcAttr{
21+
CmdLine: strings.Join(cmd.Args, " "),
22+
}
23+
return cmd, nil
24+
}

0 commit comments

Comments
 (0)