From 63ce832c1f31d63871c83e36421a9b14572ccd3e Mon Sep 17 00:00:00 2001 From: tengattack Date: Wed, 12 Sep 2018 23:19:33 +0800 Subject: [PATCH] Add shell cwd option & fix build on windows --- builtin/bins/dkron-executor-shell/shell.go | 43 ++++++------------- .../bins/dkron-executor-shell/shell_unix.go | 39 +++++++++++++++++ .../dkron-executor-shell/shell_windows.go | 11 +++++ 3 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 builtin/bins/dkron-executor-shell/shell_unix.go create mode 100644 builtin/bins/dkron-executor-shell/shell_windows.go diff --git a/builtin/bins/dkron-executor-shell/shell.go b/builtin/bins/dkron-executor-shell/shell.go index 9caa38193..de52b1d0c 100644 --- a/builtin/bins/dkron-executor-shell/shell.go +++ b/builtin/bins/dkron-executor-shell/shell.go @@ -4,11 +4,9 @@ import ( "log" "os" "os/exec" - "os/user" "runtime" "strconv" "strings" - "syscall" "time" "github.com/armon/circbuf" @@ -38,9 +36,13 @@ func (s *Shell) Execute(args *dkron.ExecuteRequest) ([]byte, error) { } command := args.Config["command"] env := strings.Split(args.Config["env"], ",") - su, _ := args.Config["su"] + cwd, _ := args.Config["cwd"] - cmd, err := buildCmd(command, shell, env, su) + cmd, err := buildCmd(command, shell, env, cwd) + if err != nil { + return nil, err + } + err = setCmdAttr(cmd, args.Config) if err != nil { return nil, err } @@ -65,17 +67,19 @@ func (s *Shell) Execute(args *dkron.ExecuteRequest) ([]byte, error) { } err = cmd.Wait() + + // Always log output + log.Printf("shell: Command output %s", output) + if err != nil { return nil, err } - log.Printf("shell: Command output %s", output) - return output.Bytes(), nil } // Determine the shell invocation based on OS -func buildCmd(command string, useShell bool, env []string, su string) (cmd *exec.Cmd, err error) { +func buildCmd(command string, useShell bool, env []string, cwd string) (cmd *exec.Cmd, err error) { var shell, flag string if useShell { @@ -97,29 +101,6 @@ func buildCmd(command string, useShell bool, env []string, su string) (cmd *exec if env != nil { cmd.Env = append(os.Environ(), env...) } - if su != "" && runtime.GOOS != windows { - var uid, gid int - parts := strings.Split(su, ":") - u, err := user.Lookup(parts[0]) - if err != nil { - return nil, err - } - uid, _ = strconv.Atoi(u.Uid) - if len(parts) > 1 { - g, err := user.LookupGroup(parts[1]) - if err != nil { - return nil, err - } - gid, _ = strconv.Atoi(g.Gid) - } else { - gid, _ = strconv.Atoi(u.Gid) - } - cmd.SysProcAttr = &syscall.SysProcAttr{} - cmd.SysProcAttr.Credential = &syscall.Credential{ - Uid: uint32(uid), - Gid: uint32(gid), - } - } - + cmd.Dir = cwd return } diff --git a/builtin/bins/dkron-executor-shell/shell_unix.go b/builtin/bins/dkron-executor-shell/shell_unix.go new file mode 100644 index 000000000..f1d671598 --- /dev/null +++ b/builtin/bins/dkron-executor-shell/shell_unix.go @@ -0,0 +1,39 @@ +// +build !windows + +package main + +import ( + "os/exec" + "os/user" + "strconv" + "strings" + "syscall" +) + +func setCmdAttr(cmd *exec.Cmd, config map[string]string) error { + su, _ := config["su"] + if su != "" { + var uid, gid int + parts := strings.Split(su, ":") + u, err := user.Lookup(parts[0]) + if err != nil { + return err + } + uid, _ = strconv.Atoi(u.Uid) + if len(parts) > 1 { + g, err := user.LookupGroup(parts[1]) + if err != nil { + return err + } + gid, _ = strconv.Atoi(g.Gid) + } else { + gid, _ = strconv.Atoi(u.Gid) + } + cmd.SysProcAttr = &syscall.SysProcAttr{} + cmd.SysProcAttr.Credential = &syscall.Credential{ + Uid: uint32(uid), + Gid: uint32(gid), + } + } + return nil +} diff --git a/builtin/bins/dkron-executor-shell/shell_windows.go b/builtin/bins/dkron-executor-shell/shell_windows.go new file mode 100644 index 000000000..417de4030 --- /dev/null +++ b/builtin/bins/dkron-executor-shell/shell_windows.go @@ -0,0 +1,11 @@ +// +build windows + +package main + +import ( + "os/exec" +) + +func setCmdAttr(cmd *exec.Cmd, config map[string]string) error { + return nil +}