-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatus.go
98 lines (84 loc) · 2.27 KB
/
status.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"os"
"os/exec"
"log"
"io"
"encoding/json"
"time"
)
type StatusReporter func(jobSets map[string]*JobSet)
type GodoitInfo struct {
Time string `json:"time"`
Hostname string `json:"hostname"`
JobInfo []JobCollection `json:"jobInfo"`
Environment map[string]string `json:"environment"`
}
type JobCollection struct {
Path string `json:"path"`
Jobs []JobInfo `json:"jobs"`
}
type JobInfo struct {
Name string `json:"name"`
Spec string `json:"spec"`
Timezone string `json:"timezone"`
Path string `json:"path"`
Timeout int `json:"timeout"`
Enabled bool `json:"enabled"`
Errors []string `json:"errors"`
}
func ToJson(jobSets map[string]*JobSet, statusEnvironment []string) []byte {
jobCollections := make([]JobCollection, len(jobSets))
i := 0
for _, jobSet := range jobSets {
jobs := make([]JobInfo, len(jobSet.jobs))
j := 0
for _, job := range jobSet.jobs {
jobs[j] =
JobInfo{
job.Name,
job.Spec,
job.Timezone.String(),
job.Filepath,
int(job.Timeout.Seconds()),
job.Enabled,
job.Errors}
j++
}
jobCollections[i] = JobCollection{jobSet.directory, jobs}
i++
}
hostname, _ := os.Hostname()
time := time.Now().UTC().Format("20060102T15:04:05Z")
// Setup environment variables
environment := make(map[string]string)
for _, environmentVariable := range statusEnvironment {
environment[environmentVariable] = os.Getenv(environmentVariable)
}
godoitInfo := &GodoitInfo{time,hostname,jobCollections, environment}
info, _ := json.Marshal(godoitInfo)
return info
}
func StatusReporterFromScript(statusScript string, statusEnvironment []string, output io.Writer) StatusReporter {
if len(statusScript) == 0 {
log.Fatalf("Status script is not defined")
}
statusScript = os.ExpandEnv(statusScript)
return func(jobSets map[string]*JobSet) {
cmd := exec.Command(statusScript)
log.Printf("Running status script: %s", statusScript)
cmd.Stdout = output
cmd.Stderr = output
pipe, _ := cmd.StdinPipe()
err := cmd.Start()
pipe.Write(ToJson(jobSets, statusEnvironment))
pipe.Close()
if err != nil {
log.Printf("ERROR: Failed to execute status script %s", statusScript)
}
cmd.Wait()
if err != nil {
log.Printf("ERROR: Status script completed with error code %s", statusScript)
}
}
}