2017-02-22 09:46:42 +01:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
2017-03-07 09:35:14 +01:00
|
|
|
"bytes"
|
|
|
|
"html/template"
|
2017-03-22 08:22:09 +01:00
|
|
|
"net/http"
|
2017-02-22 09:46:42 +01:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
)
|
|
|
|
|
2017-03-07 09:35:14 +01:00
|
|
|
const emailTemplate = `Subject: Task '{{ .Alias }}' failed
|
|
|
|
|
2017-04-22 05:22:16 +02:00
|
|
|
Task {{ .TaskID }} with template '{{ .Alias }}' has failed!
|
|
|
|
Task log: <a href='{{ .TaskURL }}'>{{ .TaskURL }}</a>`
|
2017-03-07 09:35:14 +01:00
|
|
|
|
2017-04-22 05:22:16 +02:00
|
|
|
const telegramTemplate = `{"chat_id": "{{ .ChatID }}","text":"<b>Task {{ .TaskID }} with template '{{ .Alias }}' has failed!</b>\nTask log: <a href='{{ .TaskURL }}'>{{ .TaskURL }}</a>","parse_mode":"HTML"}`
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Alert represents an alert that will be templated and sent to the appropriate service
|
2017-03-07 09:35:14 +01:00
|
|
|
type Alert struct {
|
2017-04-18 16:36:09 +02:00
|
|
|
TaskID string
|
2017-03-07 09:35:14 +01:00
|
|
|
Alias string
|
2017-04-18 16:36:09 +02:00
|
|
|
TaskURL string
|
|
|
|
ChatID string
|
2017-03-07 09:35:14 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:46:42 +01:00
|
|
|
func (t *task) sendMailAlert() {
|
2018-03-27 22:12:47 +02:00
|
|
|
if !util.Config.EmailAlert || !t.alert {
|
2017-03-13 03:30:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mailHost := util.Config.EmailHost + ":" + util.Config.EmailPort
|
|
|
|
|
|
|
|
var mailBuffer bytes.Buffer
|
2017-04-18 16:36:09 +02:00
|
|
|
alert := Alert{
|
|
|
|
TaskID: strconv.Itoa(t.task.ID),
|
|
|
|
Alias: t.template.Alias,
|
|
|
|
TaskURL: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID),
|
|
|
|
}
|
2017-03-13 03:30:48 +01:00
|
|
|
tpl := template.New("mail body template")
|
|
|
|
tpl, err := tpl.Parse(emailTemplate)
|
2018-03-27 22:12:47 +02:00
|
|
|
util.LogError(err)
|
2017-03-13 03:30:48 +01:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
t.panicOnError(tpl.Execute(&mailBuffer, alert), "Can't generate alert template!")
|
2017-02-22 09:46:42 +01:00
|
|
|
|
2017-03-13 03:30:48 +01:00
|
|
|
for _, user := range t.users {
|
2020-12-04 23:22:05 +01:00
|
|
|
userObj, err := t.store.GetUser(user)
|
2017-03-13 03:30:48 +01:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
if !userObj.Alert {
|
2017-03-13 03:30:48 +01:00
|
|
|
return
|
|
|
|
}
|
2018-03-27 22:12:47 +02:00
|
|
|
t.panicOnError(err,"Can't find user Email!")
|
2017-03-13 03:30:48 +01:00
|
|
|
|
|
|
|
t.log("Sending email to " + userObj.Email + " from " + util.Config.EmailSender)
|
|
|
|
err = util.SendMail(mailHost, util.Config.EmailSender, userObj.Email, mailBuffer)
|
2018-03-27 22:12:47 +02:00
|
|
|
t.panicOnError(err, "Can't send email!")
|
2017-03-13 03:30:48 +01:00
|
|
|
}
|
2017-02-22 09:46:42 +01:00
|
|
|
}
|
2017-03-22 08:22:09 +01:00
|
|
|
|
|
|
|
func (t *task) sendTelegramAlert() {
|
2018-03-27 22:12:47 +02:00
|
|
|
if !util.Config.TelegramAlert || !t.alert {
|
2017-03-22 08:22:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
chatID := util.Config.TelegramChat
|
|
|
|
if t.alertChat != "" {
|
|
|
|
chatID = t.alertChat
|
2017-05-03 06:27:58 +02:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:22:09 +01:00
|
|
|
var telegramBuffer bytes.Buffer
|
2017-04-18 16:36:09 +02:00
|
|
|
alert := Alert{
|
|
|
|
TaskID: strconv.Itoa(t.task.ID),
|
|
|
|
Alias: t.template.Alias,
|
|
|
|
TaskURL: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID),
|
2018-03-27 22:12:47 +02:00
|
|
|
ChatID: chatID,
|
2017-04-18 16:36:09 +02:00
|
|
|
}
|
2017-03-22 08:22:09 +01:00
|
|
|
tpl := template.New("telegram body template")
|
|
|
|
tpl, err := tpl.Parse(telegramTemplate)
|
2018-03-27 22:12:47 +02:00
|
|
|
util.LogError(err)
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
t.panicOnError(tpl.Execute(&telegramBuffer, alert),"Can't generate alert template!")
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2017-03-24 08:50:26 +01:00
|
|
|
resp, err := http.Post("https://api.telegram.org/bot"+util.Config.TelegramToken+"/sendMessage", "application/json", &telegramBuffer)
|
2018-03-27 22:12:47 +02:00
|
|
|
t.panicOnError(err, "Can't send telegram alert!")
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2017-03-24 08:50:26 +01:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.log("Can't send telegram alert! Response code not 200!")
|
|
|
|
}
|
2017-03-22 08:22:09 +01:00
|
|
|
}
|