Semaphore/api/runners.go

153 lines
3.3 KiB
Go
Raw Normal View History

2024-09-25 20:43:55 +02:00
package api
import (
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
2024-09-26 12:54:03 +02:00
log "github.com/sirupsen/logrus"
2024-09-25 20:43:55 +02:00
"net/http"
"github.com/gorilla/context"
)
//type minimalGlobalRunner struct {
// ID int `json:"id"`
// Name string `json:"name"`
// Active bool `json:"active"`
// Webhook string `db:"webhook" json:"webhook"`
// MaxParallelTasks int `db:"max_parallel_tasks" json:"max_parallel_tasks"`
//}
2024-09-25 20:43:55 +02:00
2024-09-26 12:54:03 +02:00
func getGlobalRunners(w http.ResponseWriter, r *http.Request) {
runners, err := helpers.Store(r).GetGlobalRunners(false)
2024-09-25 20:43:55 +02:00
if err != nil {
panic(err)
}
var result = make([]db.Runner, 0)
2024-09-25 20:43:55 +02:00
for _, runner := range runners {
result = append(result, runner)
2024-09-25 20:43:55 +02:00
}
helpers.WriteJSON(w, http.StatusOK, result)
}
2024-09-26 12:54:03 +02:00
func addGlobalRunner(w http.ResponseWriter, r *http.Request) {
var runner db.Runner
2024-09-26 12:54:03 +02:00
if !helpers.Bind(w, r, &runner) {
return
}
2024-09-25 20:43:55 +02:00
2024-09-26 12:54:03 +02:00
editor := context.Get(r, "user").(*db.User)
if !editor.Admin {
log.Warn(editor.Username + " is not permitted to create users")
w.WriteHeader(http.StatusUnauthorized)
return
}
newRunner, err := helpers.Store(r).CreateRunner(db.Runner{
Webhook: runner.Webhook,
MaxParallelTasks: runner.MaxParallelTasks,
})
if err != nil {
log.Warn("Runner is not created: " + err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
helpers.WriteJSON(w, http.StatusCreated, newRunner)
}
func globalRunnerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
runnerID, err := helpers.GetIntParam("runner_id", w, r)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "runner_id required",
})
return
}
store := helpers.Store(r)
runner, err := store.GetGlobalRunner(runnerID)
if err != nil {
helpers.WriteJSON(w, http.StatusNotFound, map[string]string{
"error": "Runner not found",
})
return
}
context.Set(r, "runner", &runner)
2024-09-26 12:54:03 +02:00
next.ServeHTTP(w, r)
})
}
func getGlobalRunner(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
helpers.WriteJSON(w, http.StatusOK, runner)
2024-09-26 12:54:03 +02:00
}
func updateGlobalRunner(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
store := helpers.Store(r)
runner.ProjectID = nil
err := store.UpdateRunner(*runner)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}
func deleteGlobalRunner(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
store := helpers.Store(r)
err := store.DeleteGlobalRunner(runner.ID)
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}
func setGlobalRunnerActive(w http.ResponseWriter, r *http.Request) {
runner := context.Get(r, "runner").(*db.Runner)
store := helpers.Store(r)
var body struct {
Active bool `json:"active"`
}
if !helpers.Bind(w, r, &body) {
helpers.WriteErrorStatus(w, "Invalid request body", http.StatusBadRequest)
return
}
runner.Active = body.Active
err := store.UpdateRunner(*runner)
2024-09-26 12:54:03 +02:00
if err != nil {
helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}