2017-02-22 09:46:42 +01:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
2017-03-07 09:35:14 +01:00
|
|
|
"bytes"
|
2024-02-27 11:47:22 +01:00
|
|
|
"embed"
|
|
|
|
"fmt"
|
2017-03-22 08:22:09 +01:00
|
|
|
"net/http"
|
2017-02-22 09:46:42 +01:00
|
|
|
"strconv"
|
2024-02-27 11:47:22 +01:00
|
|
|
"text/template"
|
2017-02-22 09:46:42 +01:00
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
|
2024-02-27 11:47:22 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
"github.com/ansible-semaphore/semaphore/util/mailer"
|
|
|
|
)
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
//go:embed templates/*.tmpl
|
|
|
|
var templates embed.FS
|
2022-04-11 10:29:48 +02: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 {
|
2024-02-27 11:47:22 +01:00
|
|
|
Name string
|
|
|
|
Author string
|
|
|
|
Color string
|
|
|
|
Task alertTask
|
|
|
|
Chat alertChat
|
|
|
|
}
|
|
|
|
|
|
|
|
type alertTask struct {
|
|
|
|
ID string
|
|
|
|
URL string
|
|
|
|
Result string
|
|
|
|
Desc string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
type alertChat struct {
|
|
|
|
ID string
|
2017-03-07 09:35:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
func (t *TaskRunner) sendMailAlert() {
|
2018-03-27 22:12:47 +02:00
|
|
|
if !util.Config.EmailAlert || !t.alert {
|
2017-03-13 03:30:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
body := bytes.NewBufferString("")
|
|
|
|
author, version := t.alertInfos()
|
2017-03-13 03:30:48 +01:00
|
|
|
|
2017-04-18 16:36:09 +02:00
|
|
|
alert := Alert{
|
2023-08-29 00:51:04 +02:00
|
|
|
Name: t.Template.Name,
|
2024-02-27 11:47:22 +01:00
|
|
|
Author: author,
|
|
|
|
Color: t.alertColor("email"),
|
|
|
|
Task: alertTask{
|
|
|
|
ID: strconv.Itoa(t.Task.ID),
|
|
|
|
URL: t.taskLink(),
|
2024-03-23 20:33:48 +01:00
|
|
|
Result: t.Task.Status.Format(),
|
2024-02-27 11:47:22 +01:00
|
|
|
Version: version,
|
|
|
|
Desc: t.Task.Message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.ParseFS(templates, "templates/email.tmpl")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't parse email alert template!")
|
|
|
|
panic(err)
|
2017-04-18 16:36:09 +02:00
|
|
|
}
|
2017-03-13 03:30:48 +01:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
if err := tpl.Execute(body, alert); err != nil {
|
|
|
|
t.Log("Can't generate email alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-02-22 09:46:42 +01:00
|
|
|
|
2024-03-03 20:26:28 +01:00
|
|
|
if body.Len() == 0 {
|
|
|
|
t.Log("Buffer for email alert is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
for _, uid := range t.users {
|
|
|
|
user, err := t.pool.store.GetUser(uid)
|
2017-03-13 03:30:48 +01:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
if !user.Alert {
|
2023-06-21 22:16:26 +02:00
|
|
|
continue
|
2017-03-13 03:30:48 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
if err != nil {
|
|
|
|
util.LogError(err)
|
2023-09-19 23:11:51 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-10-02 19:10:56 +02:00
|
|
|
|
2024-03-03 20:26:28 +01:00
|
|
|
t.Logf("Attempting to send email alert to %s", user.Email)
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
if err := mailer.Send(
|
|
|
|
util.Config.EmailSecure,
|
|
|
|
util.Config.EmailHost,
|
|
|
|
util.Config.EmailPort,
|
|
|
|
util.Config.EmailUsername,
|
|
|
|
util.Config.EmailPassword,
|
|
|
|
util.Config.EmailSender,
|
|
|
|
user.Email,
|
|
|
|
fmt.Sprintf("Task '%s' failed", t.Template.Name),
|
|
|
|
body.String(),
|
|
|
|
); err != nil {
|
|
|
|
util.LogError(err)
|
2024-03-03 20:26:28 +01:00
|
|
|
continue
|
2023-09-19 23:11:51 +02:00
|
|
|
}
|
2024-03-03 20:26:28 +01:00
|
|
|
|
|
|
|
t.Logf("Sent successfully email alert to %s", user.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
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
func (t *TaskRunner) sendTelegramAlert() {
|
2018-03-27 22:12:47 +02:00
|
|
|
if !util.Config.TelegramAlert || !t.alert {
|
2017-03-22 08:22:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus {
|
2022-02-12 13:15:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
chatID := util.Config.TelegramChat
|
2022-02-03 19:35:32 +01:00
|
|
|
if t.alertChat != nil && *t.alertChat != "" {
|
|
|
|
chatID = *t.alertChat
|
2017-05-03 06:27:58 +02:00
|
|
|
}
|
|
|
|
|
2023-09-19 23:11:51 +02:00
|
|
|
if chatID == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
body := bytes.NewBufferString("")
|
|
|
|
author, version := t.alertInfos()
|
2021-10-26 18:54:19 +02:00
|
|
|
|
2017-04-18 16:36:09 +02:00
|
|
|
alert := Alert{
|
2024-02-27 11:47:22 +01:00
|
|
|
Name: t.Template.Name,
|
|
|
|
Author: author,
|
|
|
|
Color: t.alertColor("telegram"),
|
|
|
|
Task: alertTask{
|
|
|
|
ID: strconv.Itoa(t.Task.ID),
|
|
|
|
URL: t.taskLink(),
|
2024-03-23 20:55:33 +01:00
|
|
|
Result: t.Task.Status.Format(),
|
2024-02-27 11:47:22 +01:00
|
|
|
Version: version,
|
|
|
|
Desc: t.Task.Message,
|
|
|
|
},
|
|
|
|
Chat: alertChat{
|
|
|
|
ID: chatID,
|
|
|
|
},
|
2017-04-18 16:36:09 +02:00
|
|
|
}
|
2021-08-31 14:03:52 +02:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
tpl, err := template.ParseFS(templates, "templates/telegram.tmpl")
|
2021-10-26 18:54:19 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2024-02-27 11:47:22 +01:00
|
|
|
t.Log("Can't parse telegram alert template!")
|
2021-10-26 18:54:19 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
if err := tpl.Execute(body, alert); err != nil {
|
|
|
|
t.Log("Can't generate telegram alert template!")
|
2021-10-26 18:54:19 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2024-03-03 20:26:28 +01:00
|
|
|
if body.Len() == 0 {
|
|
|
|
t.Log("Buffer for telegram alert is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Attempting to send telegram alert")
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
resp, err := http.Post(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"https://api.telegram.org/bot%s/sendMessage",
|
|
|
|
util.Config.TelegramToken,
|
|
|
|
),
|
|
|
|
"application/json",
|
|
|
|
body,
|
|
|
|
)
|
2017-03-22 08:22:09 +01:00
|
|
|
|
2021-10-26 18:54:19 +02:00
|
|
|
if err != nil {
|
2022-10-02 19:10:56 +02:00
|
|
|
t.Log("Can't send telegram alert! Error: " + err.Error())
|
2021-10-26 18:54:19 +02:00
|
|
|
} else if resp.StatusCode != 200 {
|
2022-10-02 19:10:56 +02:00
|
|
|
t.Log("Can't send telegram alert! Response code: " + strconv.Itoa(resp.StatusCode))
|
2017-03-24 08:50:26 +01:00
|
|
|
}
|
2024-03-03 20:26:28 +01:00
|
|
|
|
|
|
|
t.Log("Sent successfully telegram alert")
|
2017-03-22 08:22:09 +01:00
|
|
|
}
|
2022-04-11 10:29:48 +02:00
|
|
|
|
|
|
|
func (t *TaskRunner) sendSlackAlert() {
|
|
|
|
if !util.Config.SlackAlert || !t.alert {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus {
|
2022-04-11 10:29:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
body := bytes.NewBufferString("")
|
|
|
|
author, version := t.alertInfos()
|
2022-04-11 10:29:48 +02:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
alert := Alert{
|
|
|
|
Name: t.Template.Name,
|
|
|
|
Author: author,
|
|
|
|
Color: t.alertColor("slack"),
|
|
|
|
Task: alertTask{
|
|
|
|
ID: strconv.Itoa(t.Task.ID),
|
|
|
|
URL: t.taskLink(),
|
2024-03-23 20:55:33 +01:00
|
|
|
Result: t.Task.Status.Format(),
|
2024-02-27 11:47:22 +01:00
|
|
|
Version: version,
|
|
|
|
Desc: t.Task.Message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.ParseFS(templates, "templates/slack.tmpl")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't parse slack alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tpl.Execute(body, alert); err != nil {
|
|
|
|
t.Log("Can't generate slack alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-03-03 20:26:28 +01:00
|
|
|
if body.Len() == 0 {
|
|
|
|
t.Log("Buffer for slack alert is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Attempting to send slack alert")
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
resp, err := http.Post(
|
|
|
|
util.Config.SlackUrl,
|
|
|
|
"application/json",
|
|
|
|
body,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't send slack alert! Error: " + err.Error())
|
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
t.Log("Can't send slack alert! Response code: " + strconv.Itoa(resp.StatusCode))
|
2024-04-02 14:16:34 +02:00
|
|
|
} else {
|
|
|
|
t.Log("Sent successfully slack alert")
|
2024-02-27 11:47:22 +01:00
|
|
|
}
|
2024-03-03 20:26:28 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 18:20:54 +01:00
|
|
|
func (t *TaskRunner) sendRocketChatAlert() {
|
|
|
|
if !util.Config.RocketChatAlert || !t.alert {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus {
|
2024-03-15 18:20:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body := bytes.NewBufferString("")
|
|
|
|
author, version := t.alertInfos()
|
|
|
|
|
|
|
|
alert := Alert{
|
|
|
|
Name: t.Template.Name,
|
|
|
|
Author: author,
|
|
|
|
Color: t.alertColor("rocketchat"),
|
|
|
|
Task: alertTask{
|
|
|
|
ID: strconv.Itoa(t.Task.ID),
|
|
|
|
URL: t.taskLink(),
|
2024-03-23 20:55:33 +01:00
|
|
|
Result: t.Task.Status.Format(),
|
2024-03-15 18:20:54 +01:00
|
|
|
Version: version,
|
|
|
|
Desc: t.Task.Message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.ParseFS(templates, "templates/rocketchat.tmpl")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't parse rocketchat alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tpl.Execute(body, alert); err != nil {
|
|
|
|
t.Log("Can't generate rocketchat alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if body.Len() == 0 {
|
|
|
|
t.Log("Buffer for rocketchat alert is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Attempting to send rocketchat alert")
|
|
|
|
|
|
|
|
resp, err := http.Post(
|
|
|
|
util.Config.RocketChatUrl,
|
|
|
|
"application/json",
|
|
|
|
body,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't send rocketchat alert! Error: " + err.Error())
|
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
t.Log("Can't send rocketchat alert! Response code: " + strconv.Itoa(resp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Sent successfully rocketchat alert")
|
|
|
|
}
|
|
|
|
|
2024-03-03 20:26:28 +01:00
|
|
|
func (t *TaskRunner) sendMicrosoftTeamsAlert() {
|
|
|
|
if !util.Config.MicrosoftTeamsAlert || !t.alert {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
if t.Template.SuppressSuccessAlerts && t.Task.Status == task_logger.TaskSuccessStatus {
|
2024-03-03 20:26:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body := bytes.NewBufferString("")
|
|
|
|
author, version := t.alertInfos()
|
|
|
|
|
|
|
|
alert := Alert{
|
|
|
|
Name: t.Template.Name,
|
|
|
|
Author: author,
|
|
|
|
Color: t.alertColor("micorsoft-teams"),
|
|
|
|
Task: alertTask{
|
|
|
|
ID: strconv.Itoa(t.Task.ID),
|
|
|
|
URL: t.taskLink(),
|
2024-03-23 20:55:33 +01:00
|
|
|
Result: t.Task.Status.Format(),
|
2024-03-03 20:26:28 +01:00
|
|
|
Version: version,
|
|
|
|
Desc: t.Task.Message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := template.ParseFS(templates, "templates/microsoft-teams.tmpl")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't parse microsoft teams alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tpl.Execute(body, alert); err != nil {
|
|
|
|
t.Log("Can't generate microsoft teams alert template!")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if body.Len() == 0 {
|
|
|
|
t.Log("Buffer for microsoft teams alert is empty")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Attempting to send microsoft teams alert")
|
|
|
|
|
|
|
|
resp, err := http.Post(
|
|
|
|
util.Config.MicrosoftTeamsUrl,
|
|
|
|
"application/json",
|
|
|
|
body,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't send microsoft teams alert! Error: " + err.Error())
|
|
|
|
} else if resp.StatusCode != 200 {
|
|
|
|
t.Log("Can't send microsoft teams alert! Response code: " + strconv.Itoa(resp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Sent successfully microsoft teams alert")
|
2024-02-27 11:47:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TaskRunner) alertInfos() (string, string) {
|
|
|
|
version := ""
|
2022-04-11 10:29:48 +02:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Task.Version != nil {
|
|
|
|
version = *t.Task.Version
|
|
|
|
} else if t.Task.BuildTaskID != nil {
|
|
|
|
version = "build " + strconv.Itoa(*t.Task.BuildTaskID)
|
2022-04-11 10:29:48 +02:00
|
|
|
} else {
|
|
|
|
version = ""
|
|
|
|
}
|
|
|
|
|
2024-04-03 00:28:36 +02:00
|
|
|
author := "—"
|
2022-04-11 10:29:48 +02:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Task.UserID != nil {
|
|
|
|
user, err := t.pool.store.GetUser(*t.Task.UserID)
|
2024-02-27 11:47:22 +01:00
|
|
|
|
2022-04-11 10:29:48 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
author = user.Name
|
2022-04-11 10:29:48 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 12:06:11 +02:00
|
|
|
return author, version
|
2024-02-27 11:47:22 +01:00
|
|
|
}
|
2022-04-11 10:29:48 +02:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
func (t *TaskRunner) alertColor(kind string) string {
|
|
|
|
switch kind {
|
|
|
|
case "slack":
|
|
|
|
switch t.Task.Status {
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskSuccessStatus:
|
2024-02-27 11:47:22 +01:00
|
|
|
return "good"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskFailStatus:
|
2024-02-27 11:47:22 +01:00
|
|
|
return "danger"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskRunningStatus:
|
2024-02-27 11:47:22 +01:00
|
|
|
return "#333CFF"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskWaitingStatus:
|
2024-02-27 11:47:22 +01:00
|
|
|
return "#FFFC33"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskStoppingStatus:
|
2024-02-27 11:47:22 +01:00
|
|
|
return "#BEBEBE"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskStoppedStatus:
|
2024-02-27 11:47:22 +01:00
|
|
|
return "#5B5B5B"
|
|
|
|
}
|
2024-03-15 18:20:54 +01:00
|
|
|
case "rocketchat":
|
|
|
|
switch t.Task.Status {
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskSuccessStatus:
|
2024-03-15 18:20:54 +01:00
|
|
|
return "#00EE00"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskFailStatus:
|
2024-03-15 18:20:54 +01:00
|
|
|
return "#EE0000"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskRunningStatus:
|
2024-03-15 18:20:54 +01:00
|
|
|
return "#333CFF"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskWaitingStatus:
|
2024-03-15 18:20:54 +01:00
|
|
|
return "#FFFC33"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskStoppingStatus:
|
2024-03-15 18:20:54 +01:00
|
|
|
return "#BEBEBE"
|
2024-04-12 12:32:54 +02:00
|
|
|
case task_logger.TaskStoppedStatus:
|
2024-03-15 18:20:54 +01:00
|
|
|
return "#5B5B5B"
|
|
|
|
}
|
2022-04-11 10:29:48 +02:00
|
|
|
}
|
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
return ""
|
|
|
|
}
|
2022-04-11 10:29:48 +02:00
|
|
|
|
2024-02-27 11:47:22 +01:00
|
|
|
func (t *TaskRunner) taskLink() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s/project/%d/templates/%d?t=%d",
|
|
|
|
util.Config.WebHost,
|
|
|
|
t.Template.ProjectID,
|
|
|
|
t.Template.ID,
|
|
|
|
t.Task.ID,
|
|
|
|
)
|
2022-04-11 10:29:48 +02:00
|
|
|
}
|