-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrundeck-failed-jobs-mailgun.go
194 lines (149 loc) · 4.93 KB
/
rundeck-failed-jobs-mailgun.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
m "github.com/mailgun/mailgun-go"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)
type Recipient struct {
Email string
Name string
SendType string
}
type Configuration struct {
RundeckServerUrl string
RundeckApiVersion string
RundeckAuthToken string
MailgunDomain string
MailgunPublicKey string
MailgunPrivateKey string
MailgunFromEmail string
MailgunFromName string
MailgunRecipients []Recipient
}
type Job struct {
XMLName xml.Name `xml:"job"`
Name string `xml:"name"`
Group string `xml:"group"`
Project string `xml:"project"`
Description string `xml:"description"`
}
type Node struct {
XMLName xml.Name `xml:"node"`
Name string `xml:"name,attr"`
}
type FailedNodes struct {
XMLName xml.Name `xml:"failedNodes"`
Nodes []Node `xml:"node"`
}
type Execution struct {
XMLName xml.Name `xml:"execution"`
Href string `xml:"href,attr"`
User string `xml:"user"`
Started string `xml:"date-started"`
Ended string `xml:"date-ended"`
Jobs []Job `xml:"job"`
FailedNodes FailedNodes `xml:"failedNodes"`
}
type QueryExecutions struct {
XMLName xml.Name `xml:"executions"`
Executions []Execution `xml:"execution"`
}
func main() {
// Path
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
// flag (Params)
projectPtr := flag.String("project", "", "the project name")
groupPtr := flag.String("group", "", "specify a group or partial group path to include all jobs within that group path")
recentFilterPtr := flag.String("recentfilter", "1h", "Use a simple text format to filter executions that completed within a period of time")
max := flag.String("max", "20", "Indicate the maximum number of results to return. If max = 0, all results will be returned. Default = 20.")
flag.Parse()
if len(*projectPtr) == 0 {
log.Fatal("Missing required [project] param!")
}
// Config
file, err := os.Open(dir + "/conf.json")
if err != nil {
log.Fatal(err)
}
decoder := json.NewDecoder(file)
configuration := Configuration{}
err = decoder.Decode(&configuration)
// Send get request to Rundeck api
client := &http.Client{}
req, err := http.NewRequest("GET", configuration.RundeckServerUrl+"/api/"+configuration.RundeckApiVersion+"/executions?project="+*projectPtr+"&groupPath="+*groupPtr+"&statusFilter=failed&recentFilter="+*recentFilterPtr+"&max="+*max, nil)
req.Header.Set("X-Rundeck-Auth-Token", configuration.RundeckAuthToken)
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
response_body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if res.StatusCode != 200 {
fmt.Printf("HTTP Status: %d - %s\n", res.StatusCode, http.StatusText(res.StatusCode))
fmt.Printf("Response: %s", response_body)
os.Exit(1)
}
var query QueryExecutions
xml.Unmarshal(response_body, &query)
failed_executions := len(query.Executions)
if failed_executions > 0 {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%v Failed Executions from project [%v]", failed_executions, *projectPtr))
if len(*groupPtr) != 0 {
buffer.WriteString(fmt.Sprintf(" group [%v]", *groupPtr))
}
buffer.WriteString(".\n\n")
buffer.WriteString("Executions:\n")
for _, execution := range query.Executions {
for _, job := range execution.Jobs {
if len(*groupPtr) > 0 {
buffer.WriteString("\t" + job.Name + "\n")
} else {
buffer.WriteString("\t" + job.Group + "/" + job.Name + "\n")
}
}
buffer.WriteString("\t\t" + execution.Href + "\n")
buffer.WriteString("\t\tStarted: " + execution.Started + " | User:" + execution.User + "\n")
buffer.WriteString("\t\tNodes: ")
for i, node := range execution.FailedNodes.Nodes {
if i > 0 {
buffer.WriteString(" / ")
}
buffer.WriteString(node.Name)
}
buffer.WriteString("\n\n")
}
mail_client := m.NewMailgun(configuration.MailgunDomain, configuration.MailgunPrivateKey, configuration.MailgunPublicKey)
var subject string
if len(*groupPtr) != 0 {
subject = fmt.Sprintf("[RunDeck] [%v] [%v] %v failures!", *projectPtr, *groupPtr, failed_executions)
} else {
subject = fmt.Sprintf("[RunDeck] [%v] %v failures!", *projectPtr, failed_executions)
}
message := mail_client.NewMessage(fmt.Sprintf("%v <%v>", configuration.MailgunFromName, configuration.MailgunFromEmail), subject, buffer.String())
for _, recipient := range configuration.MailgunRecipients {
message.AddRecipient(fmt.Sprintf("%v <%v>", recipient.Name, recipient.Email))
}
fmt.Println(fmt.Sprintf("%v failed jobs found.", failed_executions))
response, id, _ := mail_client.Send(message)
fmt.Printf("Response ID: %s\n", id)
fmt.Printf("Message from server: %s\n", response)
} else {
fmt.Println(fmt.Sprintf("No failed jobs found in the period [%v].", *recentFilterPtr))
}
os.Exit(0)
}