forked from clee/channon
/
notifications.go
51 lines (45 loc) · 1.29 KB
/
notifications.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
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"os"
"fmt"
"log"
"os/exec"
)
func (n *Notification) Execute(run *Run) {
/*
* Bail out early if this trigger doesn't need to execute for this run.
*/
switch n.Target {
case "every-failure":
if run.Status != "failure" {
log.Printf("not running notification! %s trigger not met", n.Target)
return
}
case "result-changed":
if run.Id == 0 {
log.Printf("not running notification! %s trigger not met", n.Target)
return
}
if run.Status == run.plan.Runs[run.Id - 1].Status {
log.Printf("not running notification! %s trigger not met", n.Target)
return
}
}
path, err := os.Getwd()
notificationPath := fmt.Sprintf("%s/plans/%s/notify-%s", path, run.plan.Name, n.Target)
cmd := exec.Command(notificationPath)
cmd.Stdout = nil
cmd.Stderr = nil
env := os.Environ()
env = append(env, fmt.Sprintf("CHANNON_TRIGGER=%s/trigger", run.path))
for index := range run.plan.Steps {
stepPath := fmt.Sprintf("%s/step%d", run.path, index)
env = append(env, fmt.Sprintf("CHANNON_STEP%d_STDOUT='%s.out'", index, stepPath))
env = append(env, fmt.Sprintf("CHANNON_STEP%d_STDERR='%s.err'", index, stepPath))
}
err = cmd.Run()
if err != nil {
log.Printf("err was not nil! the %s notification for plan %s failed.", n.Target, run.plan.Name)
log.Printf(err.Error())
}
}