-
Notifications
You must be signed in to change notification settings - Fork 12
/
hook_script.go
38 lines (32 loc) · 873 Bytes
/
hook_script.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package dropship
import (
"errors"
"log"
"os"
"os/exec"
)
// ScriptHook defines a Hook that will run a specified command on a the
// machine.
type ScriptHook struct{}
// Execute is a Hook method to execut the hook with standard options
func (h ScriptHook) Execute(config HookConfig, service Config) error {
if c := config["command"]; c != "" {
// TODO(ChrisMcKenzie): Make this more secure by jailing it.
var cwd string
if len(service.Artifact) >= 1 {
cwd = service.Artifact["destination"]
}
out, err := executeCommand(c, cwd)
log.Printf("[INFO]: %s", out)
return err
}
return errors.New("Script: exiting no command was given")
}
func executeCommand(c, cwd string) (string, error) {
cmd := exec.Command("bash", "-c", c)
if _, err := os.Stat(cwd); !os.IsNotExist(err) {
cmd.Dir = cwd
}
out, err := cmd.Output()
return string(out), err
}