build endpoint is now working
This commit is contained in:
parent
6f0dbea56b
commit
15e6c51038
2 changed files with 166 additions and 118 deletions
|
@ -62,7 +62,7 @@ As you can see, you now have an identifier representing your job, you
|
|||
can use it to follow your logs (don't forget to urlencode it):
|
||||
|
||||
```
|
||||
$ curl http://localhost:8080/build?job=builder%2Fdispatch-1678866433-15aad86a
|
||||
$ curl http://localhost:8080/build?job=builder%2Fdispatch-1678866433-15aad86a&log=stderr
|
||||
<todo>
|
||||
```
|
||||
|
||||
|
|
66
main.go
66
main.go
|
@ -3,11 +3,11 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
nomad "github.com/hashicorp/nomad/api"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
"net/http"
|
||||
nomad "github.com/hashicorp/nomad/api"
|
||||
"strings"
|
||||
//"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
|
@ -79,7 +79,6 @@ func hook(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
flavor := "default"
|
||||
|
||||
|
||||
//@FIXME check for token in consul
|
||||
|
||||
var notification GiteaNotification
|
||||
|
@ -99,6 +98,8 @@ func hook(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// @FIXME logic on how to inject secrets securely
|
||||
// 1. Check senders
|
||||
// 2. Transform the consul object into a nomad payload
|
||||
|
||||
jobs := NomadClient.Jobs()
|
||||
dres, dmeta, err := jobs.Dispatch("builder", meta, []byte{}, "albatros", &nomad.WriteOptions{})
|
||||
|
@ -119,21 +120,68 @@ func build(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
logType, ok := q["log"]
|
||||
log.Printf("%+v\n", q)
|
||||
if !ok || len(logType) < 1 || !(logType[0] == "stdout" || logType[0] == "stderr") {
|
||||
http.Error(w, "Missing or wrong query parameter 'log'.\nTry adding '?log=stdout' or '?log=stderr'", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
logFilter := logType[0]
|
||||
|
||||
jobs := NomadClient.Jobs()
|
||||
allocList, query, err := jobs.Allocations(jobID[0], true, &nomad.QueryOptions{})
|
||||
allocList, _, err := jobs.Allocations(jobID[0], true, &nomad.QueryOptions{})
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to fetch your job on Nomad.", http.StatusInternalServerError)
|
||||
http.Error(w, "Unable to fetch your job on Nomad. Might be garbage collected.", 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)
|
||||
http.Error(w, "Job does not contain at least 1 allocation, can't display logs. Job might be garbage collected. ", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
alloc := allocList[0]
|
||||
log.Printf("Alloc: %+v\n", alloc)
|
||||
myAllocStub := allocList[0]
|
||||
|
||||
allocations := NomadClient.Allocations()
|
||||
myAlloc, _, err := allocations.Info(myAllocStub.ID, &nomad.QueryOptions{})
|
||||
if err != nil {
|
||||
http.Error(w, "Allocation does not exist anymore. Allocation might be garbage collected", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
log.Printf("Alloc: %+v\n", myAlloc)
|
||||
|
||||
allocFS := NomadClient.AllocFS()
|
||||
scancel := make(chan struct{})
|
||||
sframe, serr := allocFS.Logs(myAlloc, true, "runner", logFilter, "start", 0, scancel, &nomad.QueryOptions{})
|
||||
|
||||
build_loop:
|
||||
for {
|
||||
select {
|
||||
case <- r.Context().Done():
|
||||
// client disconnect, cleaning
|
||||
break build_loop
|
||||
case nomadErr := <-serr:
|
||||
// an error occured in nomad, inform user and clean
|
||||
_, _ = io.WriteString(w, fmt.Sprintf("\nBroken stream: %+v\n", nomadErr))
|
||||
break build_loop
|
||||
case chunk := <-sframe:
|
||||
// we get some data from nomad, send it to the client
|
||||
for i := 0; i < len(chunk.Data); {
|
||||
written, err := w.Write(chunk.Data[i:])
|
||||
w.(http.Flusher).Flush() // flush the buffer to send it right now
|
||||
i += written
|
||||
if err == io.EOF {
|
||||
_, _ = io.WriteString(w, "End of file :-)")
|
||||
break build_loop
|
||||
} else if err != nil {
|
||||
_, _ = io.WriteString(w, fmt.Sprintf("\nBroken stream: %+v\n", err))
|
||||
break build_loop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Cleaning %+v\n", myAlloc)
|
||||
scancel <- struct{}{}
|
||||
}
|
||||
|
||||
var NomadClient *nomad.Client
|
||||
|
|
Loading…
Reference in a new issue