Matrix notifications (fix #3)
This commit is contained in:
parent
7ff174fab9
commit
16e20f496b
@ -1,6 +1,10 @@
|
|||||||
// package main is the main package for the LapisDeploy program
|
// package main is the main package for the LapisDeploy program
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
// DeployError contains important information about an error if something went wrong.
|
// DeployError contains important information about an error if something went wrong.
|
||||||
type DeployError struct {
|
type DeployError struct {
|
||||||
code int
|
code int
|
||||||
@ -9,6 +13,13 @@ type DeployError struct {
|
|||||||
command_output string
|
command_output string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendOverMatrix provides an easy way to send the contents of a DeployError over Matrix
|
||||||
|
func (deploy_error *DeployError) SendOverMatrix() {
|
||||||
|
sendMessage(MatrixMessage{text: fmt.Sprintf("🔴 **Error in **`%s`**!**\n- *%s*\n- Code: %d",
|
||||||
|
deploy_error.where, deploy_error.details, deploy_error.code),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// newDeployError creates a DeployError
|
// newDeployError creates a DeployError
|
||||||
func newDeployError(code int, where string, details string, command_output string) DeployError {
|
func newDeployError(code int, where string, details string, command_output string) DeployError {
|
||||||
return DeployError{ code: code, where: where, details: details, command_output: command_output }
|
return DeployError{ code: code, where: where, details: details, command_output: command_output }
|
||||||
|
1
go.mod
1
go.mod
@ -13,6 +13,7 @@ require (
|
|||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.1 // indirect
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
github.com/tidwall/sjson v1.2.5 // indirect
|
github.com/tidwall/sjson v1.2.5 // indirect
|
||||||
|
github.com/yuin/goldmark v1.6.0 // indirect
|
||||||
go.mau.fi/util v0.4.0 // indirect
|
go.mau.fi/util v0.4.0 // indirect
|
||||||
golang.org/x/crypto v0.21.0 // indirect
|
golang.org/x/crypto v0.21.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
|
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -35,6 +35,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
|||||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
||||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||||
|
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
|
||||||
|
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
go.mau.fi/util v0.4.0 h1:S2X3qU4pUcb/vxBRfAuZjbrR9xVMAXSjQojNBLPBbhs=
|
go.mau.fi/util v0.4.0 h1:S2X3qU4pUcb/vxBRfAuZjbrR9xVMAXSjQojNBLPBbhs=
|
||||||
go.mau.fi/util v0.4.0/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw=
|
go.mau.fi/util v0.4.0/go.mod h1:leeiHtgVBuN+W9aDii3deAXnfC563iN3WK6BF8/AjNw=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
11
main.go
11
main.go
@ -25,16 +25,19 @@ func fileExists(path string) (bool, error) {
|
|||||||
func handler(data map[string]interface{}) {
|
func handler(data map[string]interface{}) {
|
||||||
repository := data["repository"].(map[string]interface{})
|
repository := data["repository"].(map[string]interface{})
|
||||||
log.Default().Printf("Repo: %s", repository["full_name"])
|
log.Default().Printf("Repo: %s", repository["full_name"])
|
||||||
|
sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])})
|
||||||
|
|
||||||
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
|
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
|
||||||
if deploy_error.code != 0 {
|
if deploy_error.code != 0 {
|
||||||
log.Default().Printf("Error in %s! [Code: %d] (%s)", deploy_error.where, deploy_error.code, deploy_error.details)
|
deploy_error.SendOverMatrix()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
deploy_error = restartServer(repository["name"].(string))
|
deploy_error = restartServer(repository["name"].(string))
|
||||||
if deploy_error.code != 0 {
|
if deploy_error.code != 0 {
|
||||||
log.Printf("Error in %s! [Code: %d] (%s) {Command output: '%s'}",
|
deploy_error.SendOverMatrix()
|
||||||
deploy_error.where, deploy_error.code, deploy_error.details, deploy_error.command_output)
|
return
|
||||||
}
|
}
|
||||||
|
sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// main is the starting point of the program
|
// main is the starting point of the program
|
||||||
|
49
matrix.go
49
matrix.go
@ -3,14 +3,29 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
//"errors"
|
|
||||||
"log"
|
"log"
|
||||||
//"sync"
|
"time"
|
||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
|
"maunium.net/go/mautrix/id"
|
||||||
|
"maunium.net/go/mautrix/format"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MatrixMessage struct {
|
||||||
|
text string
|
||||||
|
}
|
||||||
|
|
||||||
|
// I/O queues
|
||||||
|
var MatrixIn []MatrixMessage
|
||||||
|
var MatrixOut []MatrixMessage
|
||||||
|
|
||||||
|
// sendMessage sends a message through Matrix
|
||||||
|
func sendMessage(msg MatrixMessage) {
|
||||||
|
MatrixOut = append(MatrixOut, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// initMatrix starts a Matrix bot.
|
||||||
func initMatrix() {
|
func initMatrix() {
|
||||||
client, err := mautrix.NewClient(configuration.matrix.homeserver, "", "")
|
client, err := mautrix.NewClient(configuration.matrix.homeserver, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -41,24 +56,26 @@ func initMatrix() {
|
|||||||
log.Printf("[Matrix] Rejected invite to room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
log.Printf("[Matrix] Rejected invite to room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go func () {
|
||||||
|
for range time.Tick(time.Second * 3) {
|
||||||
|
log.Printf("Scanning for queued messages...")
|
||||||
|
for _, msg := range MatrixOut {
|
||||||
|
_, err := client.SendMessageEvent(context.Background(), id.RoomID(configuration.matrix.room_id), event.EventMessage, format.RenderMarkdown(msg.text, true, false))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[Matrix] ERROR : %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Print("[Matrix] Sent queued message successfully.")
|
||||||
|
}
|
||||||
|
MatrixOut = []MatrixMessage{}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
log.Print("[Matrix] Running!")
|
log.Print("[Matrix] Running!")
|
||||||
err = client.Sync()
|
err = client.Sync()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
//syncCtx, cancelSync := context.WithCancel(context.Background())
|
|
||||||
//var syncStopWait sync.WaitGroup
|
|
||||||
//syncStopWait.Add(1)
|
|
||||||
|
|
||||||
//go func() {
|
|
||||||
// err = client.SyncWithContext(syncCtx)
|
|
||||||
// defer syncStopWait.Done()
|
|
||||||
// if err != nil && !errors.Is(err, context.Canceled) {
|
|
||||||
// panic(err)
|
|
||||||
// }
|
|
||||||
//}()
|
|
||||||
//cancelSync()
|
|
||||||
//syncStopWait.Wait()
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user