-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathmail.go
164 lines (131 loc) · 4.01 KB
/
mail.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
package middlewares
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"os"
"strings"
"crypto/tls"
"gopkg.in/gomail.v2"
"github.com/mcuadros/ofelia/core"
)
// MailConfig configuration for the Mail middleware
type MailConfig struct {
SMTPHost string `gcfg:"smtp-host" mapstructure:"smtp-host"`
SMTPPort int `gcfg:"smtp-port" mapstructure:"smtp-port"`
SMTPUser string `gcfg:"smtp-user" mapstructure:"smtp-user" json:"-"`
SMTPPassword string `gcfg:"smtp-password" mapstructure:"smtp-password" json:"-"`
SMTPTLSSkipVerify bool `gcfg:"smtp-tls-skip-verify" mapstructure:"smtp-tls-skip-verify"`
EmailTo string `gcfg:"email-to" mapstructure:"email-to"`
EmailFrom string `gcfg:"email-from" mapstructure:"email-from"`
MailOnlyOnError bool `gcfg:"mail-only-on-error" mapstructure:"mail-only-on-error"`
}
// NewMail returns a Mail middleware if the given configuration is not empty
func NewMail(c *MailConfig) core.Middleware {
var m core.Middleware
if !IsEmpty(c) {
m = &Mail{*c}
}
return m
}
// Mail middleware delivers a email just after an execution finishes
type Mail struct {
MailConfig
}
// ContinueOnStop return allways true, we want always report the final status
func (m *Mail) ContinueOnStop() bool {
return true
}
// Run sents a email with the result of the execution
func (m *Mail) Run(ctx *core.Context) error {
err := ctx.Next()
ctx.Stop(err)
if ctx.Execution.Failed || !m.MailOnlyOnError {
err := m.sendMail(ctx)
if err != nil {
ctx.Logger.Errorf("Mail error: %q", err)
}
}
return err
}
func (m *Mail) sendMail(ctx *core.Context) error {
msg := gomail.NewMessage()
msg.SetHeader("From", m.from())
msg.SetHeader("To", strings.Split(m.EmailTo, ",")...)
msg.SetHeader("Subject", m.subject(ctx))
msg.SetBody("text/html", m.body(ctx))
base := fmt.Sprintf("%s_%s", ctx.Job.GetName(), ctx.Execution.ID)
msg.Attach(base+".stdout.log", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(ctx.Execution.OutputStream.Bytes())
return err
}))
msg.Attach(base+".stderr.log", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(ctx.Execution.ErrorStream.Bytes())
return err
}))
msg.Attach(base+".stderr.json", gomail.SetCopyFunc(func(w io.Writer) error {
js, _ := json.MarshalIndent(map[string]interface{}{
"Job": ctx.Job,
"Execution": ctx.Execution,
}, "", " ")
_, err := w.Write(js)
return err
}))
d := gomail.NewPlainDialer(m.SMTPHost, m.SMTPPort, m.SMTPUser, m.SMTPPassword)
// When TLSConfig.InsecureSkipVerify is true, mail server certificate authority is not validated
if m.SMTPTLSSkipVerify {
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
if err := d.DialAndSend(msg); err != nil {
return err
}
return nil
}
func (m *Mail) from() string {
if !strings.Contains(m.EmailFrom, "%") {
return m.EmailFrom
}
hostname, _ := os.Hostname()
return fmt.Sprintf(m.EmailFrom, hostname)
}
func (m *Mail) subject(ctx *core.Context) string {
buf := bytes.NewBuffer(nil)
mailSubjectTemplate.Execute(buf, ctx)
return buf.String()
}
func (m *Mail) body(ctx *core.Context) string {
buf := bytes.NewBuffer(nil)
mailBodyTemplate.Execute(buf, ctx)
return buf.String()
}
var mailBodyTemplate, mailSubjectTemplate *template.Template
func init() {
f := map[string]interface{}{
"status": executionLabel,
}
mailBodyTemplate = template.New("mail-body")
mailSubjectTemplate = template.New("mail-subject")
mailBodyTemplate.Funcs(f)
mailSubjectTemplate.Funcs(f)
template.Must(mailBodyTemplate.Parse(`
<p>
Job <b>{{.Job.GetName}}</b>,
Execution <b>{{status .Execution}}</b> in <b>{{.Execution.Duration}}</b>,
command: <pre>{{.Job.GetCommand}}</pre>
</p>
`))
template.Must(mailSubjectTemplate.Parse(
"[Execution {{status .Execution}}] Job {{.Job.GetName}} finished in {{.Execution.Duration}}",
))
}
func executionLabel(e *core.Execution) string {
status := "successful"
if e.Skipped {
status = "skipped"
} else if e.Failed {
status = "failed"
}
return status
}