Skip to content

Commit

Permalink
Merge branch 'ansible-semaphore:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mwxp06 committed Oct 26, 2021
2 parents 738ab2d + 4410a39 commit a9d210b
Show file tree
Hide file tree
Showing 15 changed files with 920 additions and 243 deletions.
31 changes: 31 additions & 0 deletions .dredd/dredd.windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
dry-run: null
hookfiles: ./.dredd/compiled_hooks.exe
language: go
#server: context=dev task dc:up
server-wait: 240
init: false
custom: {}
names: false
only: []
reporter: []
output: []
header: "Authorization: bearer h4a_i4qslpnxyyref71rk5nqbwxccrs7enwvggx0vfs="
sorted: false
user: null
inline-errors: false
details: false
method: []
color: true
loglevel: debug
path: []
hooks-worker-timeout: 5000
hooks-worker-connect-timeout: 1500
hooks-worker-connect-retry: 500
hooks-worker-after-connect-wait: 100
hooks-worker-term-timeout: 5000
hooks-worker-term-retry: 500
hooks-worker-handler-host: 0.0.0.0
hooks-worker-handler-port: 61321
config: ./.dredd/dredd.yml
blueprint: api-docs.yml
endpoint: 'http://localhost:3000'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ util/version.go
!.gitkeep

.dredd/compiled_hooks
.dredd/compiled_hooks.exe

.vscode
__debug_bin*
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ tasks:
compile:api:hooks:
dir: ./.dredd/hooks
cmds:
- go build -o ../compiled_hooks
- go build -o ../compiled_hooks{{ if eq OS "windows" }}.exe{{ end }}

build:
desc: Build a full set of release binaries and packages
Expand Down
70 changes: 56 additions & 14 deletions api/tasks/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"net/http"
"strconv"
"strings"

"github.com/ansible-semaphore/semaphore/util"
)
Expand All @@ -14,14 +15,18 @@ const emailTemplate = `Subject: Task '{{ .Alias }}' failed
Task {{ .TaskID }} with template '{{ .Alias }}' has failed!
Task log: <a href='{{ .TaskURL }}'>{{ .TaskURL }}</a>`

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"}`
const telegramTemplate = `{"chat_id": "{{ .ChatID }}","parse_mode":"HTML","text":"<code>{{ .Alias }}</code>\n#{{ .TaskID }} <b>{{ .TaskResult }}</b> <code>{{ .TaskVersion }}</code> {{ .TaskDescription }}\nby {{ .Author }}\nLink: {{ .TaskURL }}"}`

// Alert represents an alert that will be templated and sent to the appropriate service
type Alert struct {
TaskID string
Alias string
TaskURL string
ChatID string
TaskID string
Alias string
TaskURL string
ChatID string
TaskResult string
TaskDescription string
TaskVersion string
Author string
}

func (t *task) sendMailAlert() {
Expand Down Expand Up @@ -49,7 +54,7 @@ func (t *task) sendMailAlert() {
if !userObj.Alert {
return
}
t.panicOnError(err,"Can't find user Email!")
t.panicOnError(err, "Can't find user Email!")

t.log("Sending email to " + userObj.Email + " from " + util.Config.EmailSender)
if util.Config.EmailSecure {
Expand All @@ -72,23 +77,60 @@ func (t *task) sendTelegramAlert() {
}

var telegramBuffer bytes.Buffer

var version string
if t.task.Version != nil {
version = *t.task.Version
} else if t.task.BuildTaskID != nil {
version = "build " + strconv.Itoa(*t.task.BuildTaskID)
} else {
version = ""
}

var message string
if t.task.Message != "" {
message = "- " + t.task.Message
}

var author string
if t.task.UserID != nil {
user, err := t.store.GetUser(*t.task.UserID)
if err != nil {
panic(err)
}
author = user.Name
}

alert := Alert{
TaskID: strconv.Itoa(t.task.ID),
Alias: t.template.Alias,
TaskURL: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID) + "/templates/" + strconv.Itoa(t.template.ID) + "?t=" + strconv.Itoa(t.task.ID),
ChatID: chatID,
TaskID: strconv.Itoa(t.task.ID),
Alias: t.template.Alias,
TaskURL: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID) + "/templates/" + strconv.Itoa(t.template.ID) + "?t=" + strconv.Itoa(t.task.ID),
ChatID: chatID,
TaskResult: strings.ToUpper(t.task.Status),
TaskVersion: version,
TaskDescription: message,
Author: author,
}

tpl := template.New("telegram body template")

tpl, err := tpl.Parse(telegramTemplate)
util.LogError(err)
if err != nil {
t.log("Can't parse telegram template!")
panic(err)
}

t.panicOnError(tpl.Execute(&telegramBuffer, alert),"Can't generate alert template!")
err = tpl.Execute(&telegramBuffer, alert)
if err != nil {
t.log("Can't generate alert template!")
panic(err)
}

resp, err := http.Post("https://api.telegram.org/bot"+util.Config.TelegramToken+"/sendMessage", "application/json", &telegramBuffer)
t.panicOnError(err, "Can't send telegram alert!")

if resp.StatusCode != 200 {
if err != nil {
t.log("Can't send telegram alert! Response code not 200!")
} else if resp.StatusCode != 200 {
t.log("Can't send telegram alert! Response code not 200!")
}
}
9 changes: 7 additions & 2 deletions api/tasks/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ func (t *task) setStatus(status string) {
panic("stopping task cannot be " + status)
}
}

t.task.Status = status

t.updateStatus()

if status == taskSuccessStatus || status == taskFailStatus {
t.sendMailAlert()
t.sendTelegramAlert()
}
}

func (t *task) updateStatus() {
Expand Down Expand Up @@ -96,8 +103,6 @@ func (t *task) updateStatus() {

func (t *task) fail() {
t.setStatus(taskFailStatus)
t.sendMailAlert()
t.sendTelegramAlert()
}

func (t *task) destroyKeys() {
Expand Down
7 changes: 4 additions & 3 deletions db/sql/Version.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ func (version *Version) GetErrPath() string {
}

// GetSQL takes a path to an SQL file and returns it from packr as a slice of strings separated by newlines
func (version *Version) GetSQL(path string) []string {
func (version *Version) GetSQL(path string) (queries []string) {
sql, err := dbAssets.MustString(path)
if err != nil {
panic(err)
}
return strings.Split(sql, ";\n")
queries = strings.Split(strings.ReplaceAll(sql, ";\r\n", ";\n"), ";\n")
return
}

func init() {
Expand Down Expand Up @@ -82,8 +83,8 @@ func init() {
{Major: 2, Minor: 7, Patch: 10},
{Major: 2, Minor: 7, Patch: 12},
{Major: 2, Minor: 7, Patch: 13},
{Major: 2, Minor: 7, Patch: 17},
{Major: 2, Minor: 8, Patch: 0},
{Major: 2, Minor: 8, Patch: 1},
{Major: 2, Minor: 8, Patch: 7},
}
}
3 changes: 2 additions & 1 deletion db/sql/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (d *SqlDb) applyMigration(version *Version) error {
_, err = tx.Exec(q)
if err != nil {
handleRollbackError(tx.Rollback())
log.Warnf("\n ERR! Query: %v\n\n", q)
log.Warnf("\n ERR! Query: %s\n\n", q)
log.Fatalf(err.Error())
return err
}
}
Expand Down
8 changes: 4 additions & 4 deletions db/sql/migrations/v1.0.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ alter table `project__inventory` add `ssh_key_id` int null references access_key
alter table `task__output` rename to `task__output_backup`;
create table `task__output`
(
task_id int not null
references task
on delete cascade,
task_id int not null,
task varchar(255) not null,
time datetime not null,
output longtext not null
output longtext not null,

foreign key (`task_id`) references task(`id`) on delete cascade
);
insert into `task__output` select * from `task__output_backup`;
drop table `task__output_backup`;
8 changes: 4 additions & 4 deletions db/sql/migrations/v2.2.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ alter table task__output rename to task__output_backup;
create table task__output
(
id integer primary key autoincrement,
task_id int not null
references task
on delete cascade,
task_id int not null,
task varchar(255) not null,
time datetime not null,
output longtext not null
output longtext not null,

foreign key (`task_id`) references task(`id`) on delete cascade
);

insert into task__output(task_id, task, time, output) select * from task__output_backup;
Expand Down
6 changes: 3 additions & 3 deletions db/sql/migrations/v2.3.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ create table user__token
id varchar(44) not null primary key,
created datetime not null,
expired boolean default false not null,
user_id int not null
references `user`
on delete cascade
user_id int not null,

foreign key (`user_id`) references `user`(`id`) on delete cascade
);

insert into user__token select * from user__token_backup;
Expand Down
2 changes: 1 addition & 1 deletion db/sql/migrations/v2.7.12.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alter table `project__inventory` add `become_key_id` int references access_key(`id`);
alter table `project__template` add `vault_pass_id` int references access_key(`id`);
alter table `project__template` add `vault_key_id` int references access_key(`id`);
3 changes: 0 additions & 3 deletions db/sql/migrations/v2.7.17.sql

This file was deleted.

1 change: 1 addition & 0 deletions db/sql/migrations/v2.8.7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table `task` drop column `arguments`;
Loading

0 comments on commit a9d210b

Please sign in to comment.