Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell cwd option & fix build on windows #436

Merged
merged 1 commit into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions builtin/bins/dkron-executor-shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"log"
"os"
"os/exec"
"os/user"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/armon/circbuf"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
39 changes: 39 additions & 0 deletions builtin/bins/dkron-executor-shell/shell_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions builtin/bins/dkron-executor-shell/shell_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build windows

package main

import (
"os/exec"
)

func setCmdAttr(cmd *exec.Cmd, config map[string]string) error {
return nil
}