albatros/main.go

159 lines
4.5 KiB
Go
Raw Normal View History

2023-03-14 17:51:31 +00:00
package main
import (
"encoding/json"
"fmt"
2023-03-15 07:35:37 +00:00
"io"
2023-03-14 17:51:31 +00:00
"log"
2023-03-15 07:51:13 +00:00
"strings"
2023-03-14 17:51:31 +00:00
"net/http"
2023-03-15 07:35:37 +00:00
nomad "github.com/hashicorp/nomad/api"
2023-03-14 17:51:31 +00:00
//"code.gitea.io/sdk/gitea"
)
type GitUser struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:username"`
}
type GiteaCommit struct {
Id string `json:"id"`
Message string `json:"message"`
Url string `json:"string"`
Author GitUser `json:"author"`
Committer GitUser `json:"committer"`
Timestamp string `json:"timestamp"`
}
type GiteaAccount struct {
Id int64 `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarUrl string `json:"avatar_url"`
Username string `json:"username"`
}
type GiteaRepository struct {
Id int64 `json:"id"`
Owner GiteaAccount `json:"owner"`
Description string `json:"description"`
Private bool `json:"private"`
Fork bool `json:"private"`
HtmlUrl string `json:"html_url"`
SshUrl string `json:"ssh_url"`
CloneUrl string `json:"clone_url"`
Website string `json:"website"`
StarsCount int64 `json:"stars_count"`
ForksCount int64 `json:"forks_count"`
WatchersCount int64 `json:"watchers_count"`
OpenIssuesCount int64 `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type GiteaNotification struct {
Secret string `json:"secret"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareUrl string `json:"compare_url"`
Commits []GiteaCommit `json:"commits"`
Repository GiteaRepository `json:"repository"`
Pusher GiteaAccount `json:"pusher"`
Sender GiteaAccount `json:"sender"`
}
func hook(w http.ResponseWriter, r *http.Request) {
2023-03-15 07:35:37 +00:00
if r.Method != http.MethodPost {
http.Error(w, "Hook only support POST requests", http.StatusBadRequest)
}
2023-03-15 09:28:09 +00:00
q := r.URL.Query()
token, ok := q["token"]
if !ok || len(token) < 1 {
http.Error(w, "Missing query parameter 'token'. Try adding '?token=xxx'", http.StatusBadRequest)
return
}
flavor := "default"
//@FIXME check for token in consul
2023-03-15 08:21:42 +00:00
2023-03-14 17:51:31 +00:00
var notification GiteaNotification
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&notification); err != nil {
2023-03-15 07:35:37 +00:00
http.Error(w, "Can't parse your request JSON", http.StatusBadRequest)
2023-03-14 17:51:31 +00:00
return
}
2023-03-15 07:35:37 +00:00
2023-03-15 07:51:13 +00:00
log.Printf("Gitea notification: %+v\n", notification)
2023-03-15 07:35:37 +00:00
meta := map[string]string{
2023-03-15 07:51:13 +00:00
"REPO_URL": notification.Repository.CloneUrl,
"COMMIT": notification.After,
2023-03-15 09:28:09 +00:00
"FLAVOR": flavor,
2023-03-15 07:51:13 +00:00
// @FIXME: this code is not correct, this is a hack
"BRANCH": strings.ReplaceAll(notification.Ref, "refs/heads/", ""),
2023-03-15 07:35:37 +00:00
}
2023-03-15 08:21:42 +00:00
// @FIXME logic on how to inject secrets securely
2023-03-15 07:35:37 +00:00
jobs := NomadClient.Jobs()
2023-03-15 07:51:13 +00:00
dres, dmeta, err := jobs.Dispatch("builder", meta, []byte{}, "albatros", &nomad.WriteOptions{})
if err != nil {
http.Error(w, "Can't submit your job to Nomad", http.StatusInternalServerError)
}
2023-03-15 08:21:42 +00:00
log.Printf("Query info: %+v\n", dmeta)
2023-03-15 07:51:13 +00:00
log.Printf("Job info: %+v\n", dres)
2023-03-15 07:35:37 +00:00
2023-03-15 09:08:18 +00:00
io.WriteString(w, dres.DispatchedJobID)
2023-03-14 17:51:31 +00:00
}
func build(w http.ResponseWriter, r *http.Request) {
2023-03-15 08:21:42 +00:00
q := r.URL.Query()
jobID, ok := q["job"]
if !ok || len(jobID) < 1 {
http.Error(w, "Missing query parameter 'job'. Try adding '?job=xxx'", http.StatusBadRequest)
return
}
jobs := NomadClient.Jobs()
allocList, query, err := jobs.Allocations(jobID[0], true, &nomad.QueryOptions{})
if err != nil {
http.Error(w, "Unable to fetch your job on Nomad.", http.StatusInternalServerError)
return
}
log.Printf("Query info: %+v\n", query)
if len(allocList) < 1 {
http.Error(w, "Job does not contain at least 1 allocation, can't display logs", http.StatusNotFound)
return
}
alloc := allocList[0]
log.Printf("Alloc: %+v\n", alloc)
2023-03-14 17:51:31 +00:00
}
2023-03-15 07:35:37 +00:00
var NomadClient *nomad.Client
2023-03-14 17:51:31 +00:00
func main() {
2023-03-15 07:35:37 +00:00
var err error
nomadConfig := nomad.DefaultConfig()
nomadConfig.Namespace = "ci"
2023-03-15 08:21:42 +00:00
// @TODO read env for encrypted parameters
2023-03-15 07:35:37 +00:00
NomadClient, err = nomad.NewClient(nomadConfig)
if err != nil {
log.Fatal("Unable to connect to Nomad, check your config and setup")
}
2023-03-14 17:51:31 +00:00
http.HandleFunc("/hook", hook)
http.HandleFunc("/build", build)
2023-03-15 07:35:37 +00:00
fmt.Println("albatros (:8080)")
if err = http.ListenAndServe(":8080", nil); err != nil {
2023-03-14 17:51:31 +00:00
log.Fatal("Can't start HTTP server")
}
}