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
This commit is contained in:
Miguel Prokop 2017-08-02 17:01:21 +02:00 committed by Frank Schröder
parent adf3fc0b3b
commit 6852dec3f2
4 changed files with 43 additions and 19 deletions

View File

@ -17,6 +17,7 @@ BUG FIXES:
* agent: Clean up temporary files during disk write errors when persisting services and checks. [GH-3207] * agent: Clean up temporary files during disk write errors when persisting services and checks. [GH-3207]
* agent: Fixed an issue where dns and client bind address templates were not being parsed via the go-sockaddr library. [GH-3322] * agent: Fixed an issue where dns and client bind address templates were not being parsed via the go-sockaddr library. [GH-3322]
* 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] * 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]
* agent: Fixed quoting issues in script health check on windows. [GH-1875]
## 0.9.0 (July 20, 2017) ## 0.9.0 (July 20, 2017)

View File

@ -6,9 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"os/exec"
"os/user" "os/user"
"runtime"
"strconv" "strconv"
"time" "time"
@ -45,23 +43,6 @@ func aeScale(interval time.Duration, n int) time.Duration {
return time.Duration(multiplier) * interval return time.Duration(multiplier) * interval
} }
// ExecScript returns a command to execute a script
func ExecScript(script string) (*exec.Cmd, error) {
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/C"
} else {
shell = "/bin/sh"
flag = "-c"
}
if other := os.Getenv("SHELL"); other != "" {
shell = other
}
cmd := exec.Command(shell, flag, script)
return cmd, nil
}
// decodeMsgPack is used to decode a MsgPack encoded object // decodeMsgPack is used to decode a MsgPack encoded object
func decodeMsgPack(buf []byte, out interface{}) error { func decodeMsgPack(buf []byte, out interface{}) error {
return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out) return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)

18
agent/util_other.go Normal file
View File

@ -0,0 +1,18 @@
// +build !windows
package agent
import (
"os"
"os/exec"
)
// ExecScript returns a command to execute a script
func ExecScript(script string) (*exec.Cmd, error) {
shell := "/bin/sh"
if other := os.Getenv("SHELL"); other != "" {
shell = other
}
return exec.Command(shell, "-c", script), nil
}

24
agent/util_windows.go Normal file
View File

@ -0,0 +1,24 @@
// +build windows
package agent
import (
"os"
"os/exec"
"strings"
"syscall"
)
// ExecScript returns a command to execute a script
func ExecScript(script string) (*exec.Cmd, error) {
shell := "cmd"
if other := os.Getenv("SHELL"); other != "" {
shell = other
}
script = "\"" + script + "\"";
cmd := exec.Command(shell, "/C", script)
cmd.SysProcAttr = &syscall.SysProcAttr{
CmdLine: strings.Join(cmd.Args, " "),
}
return cmd, nil
}