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"
|
|
|
|
|
2017-04-18 15:58:48 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2017-02-22 09:46:42 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
)
|
|
|
|
|
2017-03-07 09:35:14 +01:00
|
|
|
const emailTemplate = `Subject: Task '{{ .Alias }}' failed
|
|
|
|
|
2017-03-11 08:54:52 +01:00
|
|
|
Task {{ .TaskId }} with template '{{ .Alias }}' has failed!
|
2017-03-07 09:35:14 +01:00
|
|
|
Task log: <a href='{{ .TaskUrl }}'>{{ .TaskUrl }}</a>`
|
|
|
|
|
2017-03-22 08:22:09 +01: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-07 09:35:14 +01:00
|
|
|
type Alert struct {
|
|
|
|
TaskId string
|
|
|
|
Alias string
|
|
|
|
TaskUrl string
|
2017-03-22 08:22:09 +01:00
|
|
|
ChatId string
|
2017-03-07 09:35:14 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:46:42 +01:00
|
|
|
func (t *task) sendMailAlert() {
|
|
|
|
|
2017-03-13 03:30:48 +01:00
|
|
|
if util.Config.EmailAlert != true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.alert != true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mailHost := util.Config.EmailHost + ":" + util.Config.EmailPort
|
|
|
|
|
|
|
|
var mailBuffer bytes.Buffer
|
|
|
|
alert := Alert{TaskId: strconv.Itoa(t.task.ID), Alias: t.template.Alias, TaskUrl: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID)}
|
|
|
|
tpl := template.New("mail body template")
|
|
|
|
tpl, err := tpl.Parse(emailTemplate)
|
|
|
|
err = tpl.Execute(&mailBuffer, alert)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.log("Can't generate alert template!")
|
|
|
|
panic(err)
|
2017-02-22 09:46:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-13 03:30:48 +01:00
|
|
|
for _, user := range t.users {
|
2017-04-18 15:58:48 +02:00
|
|
|
userObj, err := db.FetchUser(user)
|
2017-03-13 03:30:48 +01:00
|
|
|
|
|
|
|
if userObj.Alert != true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.log("Can't find user Email!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.log("Sending email to " + userObj.Email + " from " + util.Config.EmailSender)
|
|
|
|
err = util.SendMail(mailHost, util.Config.EmailSender, userObj.Email, mailBuffer)
|
|
|
|
if err != nil {
|
|
|
|
t.log("Can't send email!")
|
|
|
|
t.log("Error: " + err.Error())
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-02-22 09:46:42 +01:00
|
|
|
}
|
2017-03-22 08:22:09 +01:00
|
|
|
|
|
|
|
func (t *task) sendTelegramAlert() {
|
|
|
|
|
|
|
|
if util.Config.TelegramAlert != true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.alert != true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var telegramBuffer bytes.Buffer
|
|
|
|
alert := Alert{TaskId: strconv.Itoa(t.task.ID), Alias: t.template.Alias, TaskUrl: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID), ChatId: util.Config.TelegramChat}
|
|
|
|
tpl := template.New("telegram body template")
|
|
|
|
tpl, err := tpl.Parse(telegramTemplate)
|
|
|
|
err = tpl.Execute(&telegramBuffer, alert)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.log("Can't generate alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-03-24 08:50:26 +01:00
|
|
|
resp, err := http.Post("https://api.telegram.org/bot"+util.Config.TelegramToken+"/sendMessage", "application/json", &telegramBuffer)
|
2017-03-22 08:22:09 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.log("Can't send telegram alert!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|