Fetch alloc + some info
This commit is contained in:
parent
0baf1efabc
commit
09752bf4d6
3 changed files with 120 additions and 2 deletions
19
README.md
Normal file
19
README.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Albatros
|
||||
|
||||
A lightweight and (quasi-)stateless CI built on top of Nomad.
|
||||
Our main principle: offload as much work to Nomad as possible.
|
||||
When we can't, offload it to Gitea, Consul, and the others.
|
||||
At a last resort, we might do it in Albatros...
|
||||
|
||||
## Deploy
|
||||
|
||||
```
|
||||
nomad namespace apply -description "Continuous Integration" ci
|
||||
nomad run hcl/builder.hcl
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## Ideas
|
||||
|
||||
- [ ] Register the builder programatically
|
||||
- [ ] Allow user to define it's own set of builders (ones with more CPU+RAM, etc.)
|
74
hcl/builder.hcl
Normal file
74
hcl/builder.hcl
Normal file
|
@ -0,0 +1,74 @@
|
|||
job "builder" {
|
||||
namespace = "ci"
|
||||
type = "batch"
|
||||
|
||||
datacenters = ["neptune", "jupiter", "corrin", "bespin"]
|
||||
priority = 100
|
||||
|
||||
parameterized {
|
||||
payload = "forbidden"
|
||||
meta_required = [ "REPO_URL", "COMMIT", "BRANCH" ]
|
||||
}
|
||||
|
||||
reschedule {
|
||||
attempts = 0
|
||||
unlimited = false
|
||||
}
|
||||
|
||||
task "runner" {
|
||||
driver = "docker"
|
||||
config {
|
||||
image = "nixpkgs/nix:nixos-22.11"
|
||||
args = [ "/tmp/builder.sh" ]
|
||||
volumes = [
|
||||
"local/builder.sh:/tmp/builder.sh",
|
||||
"local/nix.conf:/etc/nix/nix.conf"
|
||||
]
|
||||
}
|
||||
|
||||
template {
|
||||
data = <<EOH
|
||||
#!/usr/bin/env bash
|
||||
set -euxo pipefail
|
||||
|
||||
mkdir /workspace
|
||||
cd /workspace
|
||||
git init
|
||||
git remote add origin ${NOMAD_META_REPO_URL}
|
||||
git fetch origin ${NOMAD_META_COMMIT}
|
||||
git checkout ${NOMAD_META_COMMIT} -b ${NOMAD_META_BRANCH}
|
||||
export COMMIT=${NOMAD_META_COMMIT}
|
||||
export BRANCH=${NOMAD_META_BRANCH}
|
||||
./build.sh
|
||||
|
||||
EOH
|
||||
destination = "local/builder.sh"
|
||||
perms = "555"
|
||||
}
|
||||
|
||||
template {
|
||||
data = <<EOH
|
||||
substituters = https://cache.nixos.org https://nix.web.deuxfleurs.fr
|
||||
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nix.web.deuxfleurs.fr:eTGL6kvaQn6cDR/F9lDYUIP9nCVR/kkshYfLDJf1yKs=
|
||||
max-jobs = auto
|
||||
cores = 0
|
||||
log-lines = 200
|
||||
filter-syscalls = false
|
||||
sandbox = false
|
||||
keep-outputs = true
|
||||
keep-derivations = true
|
||||
EOH
|
||||
destination = "local/nix.conf"
|
||||
}
|
||||
|
||||
restart {
|
||||
attempts = 0
|
||||
}
|
||||
|
||||
resources {
|
||||
cpu = 1000
|
||||
memory = 4000
|
||||
}
|
||||
}
|
||||
}
|
||||
|
29
main.go
29
main.go
|
@ -71,6 +71,8 @@ func hook(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, "Hook only support POST requests", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
//@FIXME check for key (in consul?)
|
||||
|
||||
var notification GiteaNotification
|
||||
dec := json.NewDecoder(r.Body)
|
||||
if err := dec.Decode(¬ification); err != nil {
|
||||
|
@ -86,12 +88,14 @@ func hook(w http.ResponseWriter, r *http.Request) {
|
|||
"BRANCH": strings.ReplaceAll(notification.Ref, "refs/heads/", ""),
|
||||
}
|
||||
|
||||
// @FIXME logic on how to inject secrets securely
|
||||
|
||||
jobs := NomadClient.Jobs()
|
||||
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)
|
||||
}
|
||||
log.Printf("Dispatch info: %+v\n", dmeta)
|
||||
log.Printf("Query info: %+v\n", dmeta)
|
||||
log.Printf("Job info: %+v\n", dres)
|
||||
|
||||
fmt.Println(notification.CompareUrl)
|
||||
|
@ -99,6 +103,27 @@ func hook(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func build(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
|
@ -108,7 +133,7 @@ func main() {
|
|||
var err error
|
||||
nomadConfig := nomad.DefaultConfig()
|
||||
nomadConfig.Namespace = "ci"
|
||||
// @TODO read env
|
||||
// @TODO read env for encrypted parameters
|
||||
NomadClient, err = nomad.NewClient(nomadConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to connect to Nomad, check your config and setup")
|
||||
|
|
Loading…
Reference in a new issue