Skip to content

Commit

Permalink
Merge pull request #20 from BrunnerLivio/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
BrunnerLivio authored Nov 13, 2023
2 parents 50ce4a4 + 3e0cd52 commit 272172e
Show file tree
Hide file tree
Showing 21 changed files with 346 additions and 320 deletions.
3 changes: 1 addition & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ func Run(args args.TspWebArgs) error {
})
})

LabelController(args, api.PathPrefix("/label").Subrouter())
ConfigController(args, api.PathPrefix("/config").Subrouter())
TaskSpoolerController(args, api.PathPrefix("/task-spooler").Subrouter())
CommandController(args, api.PathPrefix("/command").Subrouter())

// SPA
spa := util.SpaHandler{StaticFS: Static, StaticPath: "web", IndexPath: "index.html"}
Expand Down
35 changes: 0 additions & 35 deletions api/commandController.go

This file was deleted.

25 changes: 25 additions & 0 deletions api/confController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api

import (
"encoding/json"
"net/http"
"tsp-web/internal/args"
userconf "tsp-web/internal/user-conf"

"github.com/gorilla/mux"
)

func ConfigController(args args.TspWebArgs, r *mux.Router) {
r.HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
conf := userconf.GetUserConf(args)
res, err := json.Marshal(conf)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Write(res)

}).Methods("GET")
}
87 changes: 0 additions & 87 deletions api/labelController.go

This file was deleted.

104 changes: 52 additions & 52 deletions api/taskSpoolerController.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package api

import (
"encoding/json"
"errors"
"net/http"
"os/exec"
"tsp-web/internal/args"
taskspooler "tsp-web/internal/task-spooler"
userconf "tsp-web/internal/user-conf"
Expand All @@ -24,22 +24,40 @@ type ExecRes struct {

func TaskSpoolerController(args args.TspWebArgs, r *mux.Router) {
r.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
GetList(args, w, r)
res, err := GetList(args, getEnv(r), w, r)

if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Write(res)
}).Methods("GET")

r.HandleFunc("/clear", func(w http.ResponseWriter, r *http.Request) {
PostClear(args, w, r)
err := taskspooler.ClearFinishedTasks(args, getEnv(r))
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}).Methods("POST")

r.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) {
PostExec(args, w, r)
res, err := PostExec(args, w, r, getEnv(r))
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(res)
}).Methods("POST")

r.HandleFunc("/kill/{id}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

err := taskspooler.Kill(args, id)
err := taskspooler.Kill(args, id, getEnv(r))
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -48,15 +66,12 @@ func TaskSpoolerController(args args.TspWebArgs, r *mux.Router) {
}).Methods("POST")
}

func GetList(args args.TspWebArgs, w http.ResponseWriter, r *http.Request) {
labels, _ := userconf.GetLabels(args)

currentTasks, err := taskspooler.List(args)
func GetList(args args.TspWebArgs, env map[string]string, w http.ResponseWriter, r *http.Request) (res []byte, err error) {
labels := userconf.GetUserConf(args).Labels
currentTasks, err := taskspooler.List(args, env)

if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
return nil, err
}

for i, task := range currentTasks {
Expand All @@ -79,46 +94,26 @@ func GetList(args args.TspWebArgs, w http.ResponseWriter, r *http.Request) {
}

if !hasFoundCachedDetail {
currentTasks[i].Detail, err = taskspooler.Detail(args, task.ID)
currentTasks[i].Detail, err = taskspooler.Detail(args, task.ID, env)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
return nil, err
}
}
}

cachedTasks = currentTasks

res, err := json.Marshal(currentTasks)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Write(res)
}

func PostClear(args args.TspWebArgs, w http.ResponseWriter, r *http.Request) {
err := taskspooler.ClearFinishedTasks(args)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
return json.Marshal(currentTasks)
}

func PostExec(args args.TspWebArgs, w http.ResponseWriter, r *http.Request) {
func PostExec(args args.TspWebArgs, w http.ResponseWriter, r *http.Request, env map[string]string) (res []byte, erro error) {
var command ExecArg
err := json.NewDecoder(r.Body).Decode(&command)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
return nil, err
}

allCommands := userconf.GetCommands(args)
allCommands := userconf.GetUserConf(args).Commands

var foundCommand *userconf.Command
for _, c := range allCommands {
Expand All @@ -129,22 +124,27 @@ func PostExec(args args.TspWebArgs, w http.ResponseWriter, r *http.Request) {
}

if foundCommand == nil {
log.Error("Command not found: ", command.Name)
w.WriteHeader(http.StatusNotFound)
return
return nil, errors.New("Command not found")
}

log.Info("Executing command from user request with args: ", args.TsBin, foundCommand.Args)
cmd := exec.Command(args.TsBin, foundCommand.Args...)
out, err := cmd.Output()
if err != nil {
log.Error("Error executing command: ", err)
w.WriteHeader(http.StatusInternalServerError)
}
arguments := append([]string{args.TsBin}, foundCommand.Args...)
out, err := taskspooler.Execute(env, arguments...)

var result ExecRes
result.ID = int(out[0])
resJson, _ := json.Marshal(result)
return resJson, nil
}

func getEnv(r *http.Request) map[string]string {
socket := r.URL.Query().Get("socket")

var res ExecRes
res.ID = int(out[0])
env := map[string]string{}

if socket != "default" && socket != "" {
env["TS_SOCKET"] = socket
}

resJson, _ := json.Marshal(res)
w.Write(resJson)
return env
}
37 changes: 25 additions & 12 deletions internal/task-spooler/commands.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package taskspooler

import (
"os"
"os/exec"
"tsp-web/internal/args"
userconf "tsp-web/internal/user-conf"

log "github.com/sirupsen/logrus"
)

type Task struct {
Expand All @@ -29,26 +32,36 @@ type TaskDetail struct {
TimeRun string
}

func List(args args.TspWebArgs) ([]Task, error) {
cmd := exec.Command(args.TsBin)
out, err := cmd.Output()
func List(args args.TspWebArgs, envVars map[string]string) ([]Task, error) {
out, err := Execute(envVars, args.TsBin)
return parseListOutput(string(out)), err
}

func Detail(args args.TspWebArgs, id string) (TaskDetail, error) {
cmd := exec.Command(args.TsBin, "-i", id)
out, err := cmd.Output()
func Detail(args args.TspWebArgs, id string, envVars map[string]string) (TaskDetail, error) {
out, err := Execute(envVars, args.TsBin, "-i", id)
return parseDetailOutput(string(out)), err
}

func ClearFinishedTasks(args args.TspWebArgs) error {
cmd := exec.Command(args.TsBin, "-C")
err := cmd.Run()
func ClearFinishedTasks(args args.TspWebArgs, envVars map[string]string) error {
_, err := Execute(envVars, args.TsBin, "-C")
return err
}

func Kill(args args.TspWebArgs, id string) error {
cmd := exec.Command(args.TsBin, "-k", id)
err := cmd.Run()
func Kill(args args.TspWebArgs, id string, envVars map[string]string) error {
_, err := Execute(envVars, args.TsBin, "-k", id)
return err
}

func cmdEnv(cmd *exec.Cmd, envVars map[string]string) {
cmd.Env = os.Environ()
for k, v := range envVars {
cmd.Env = append(cmd.Env, k+"="+v)
}
}

func Execute(envVars map[string]string, args ...string) ([]byte, error) {
cmd := exec.Command(args[0], args[1:]...)
log.Debug(cmd.Args)
cmdEnv(cmd, envVars)
return cmd.Output()
}
Loading

0 comments on commit 272172e

Please sign in to comment.