Implement runners execute route

Co-authored-by: Tobias Kantusch <tobias.kantusch@student.hpi.uni-potsdam.de>
This commit is contained in:
Konrad Hanff
2021-04-30 16:21:44 +02:00
parent c571d4635d
commit 6a00ea3165
7 changed files with 177 additions and 362 deletions

View File

@ -1,7 +1,6 @@
package api
import (
"encoding/json"
"github.com/gorilla/mux"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/auth"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
@ -47,30 +46,3 @@ func newRouterV1(router *mux.Router) *mux.Router {
return v1
}
func writeInternalServerError(writer http.ResponseWriter, err error, errorCode dto.ErrorCode) {
sendJson(writer, &dto.InternalServerError{Message: err.Error(), ErrorCode: errorCode}, http.StatusInternalServerError)
}
func writeBadRequest(writer http.ResponseWriter, err error) {
sendJson(writer, &dto.ClientError{Message: err.Error()}, http.StatusBadRequest)
}
func writeNotFound(writer http.ResponseWriter, err error) {
sendJson(writer, &dto.ClientError{Message: err.Error()}, http.StatusNotFound)
}
func sendJson(writer http.ResponseWriter, content interface{}, httpStatusCode int) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(httpStatusCode)
response, err := json.Marshal(content)
if err != nil {
// cannot produce infinite recursive loop, since json.Marshal of dto.InternalServerError won't return an error
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
if _, err := writer.Write(response); err != nil {
log.WithError(err).Warn("Error writing JSON to response")
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
}

View File

@ -1,28 +1,40 @@
package dto
// RequestRunner is the expected json structure of the request body for the ProvideRunner function
type RequestRunner struct {
// RunnerRequest is the expected json structure of the request body for the ProvideRunner function.
type RunnerRequest struct {
ExecutionEnvironmentId int `json:"executionEnvironmentId"`
InactivityTimeout int `json:"inactivityTimeout"`
}
// ResponseRunner is the expected result from the api server
type ResponseRunner struct {
// ExecutionRequest is the expected json structure of the request body for the ExecuteCommand function.
type ExecutionRequest struct {
Command string
TimeLimit int
Environment map[string]string
}
// RunnerResponse is the expected response when providing a runner.
type RunnerResponse struct {
Id string `json:"runnerId"`
}
// ClientError is the response interface if the request is not valid
// WebsocketResponse is the expected response when creating an execution for a runner.
type WebsocketResponse struct {
WebsocketUrl string
}
// ClientError is the response interface if the request is not valid.
type ClientError struct {
Message string `json:"message"`
}
// InternalServerError is the response interface that is returned when an error occurs
// InternalServerError is the response interface that is returned when an error occurs.
type InternalServerError struct {
Message string `json:"message"`
ErrorCode ErrorCode `json:"errorCode"`
}
// ErrorCode is the type for error codes expected by CodeOcean
// ErrorCode is the type for error codes expected by CodeOcean.
type ErrorCode string
const (

43
api/helpers.go Normal file
View File

@ -0,0 +1,43 @@
package api
import (
"encoding/json"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"log"
"net/http"
)
func writeInternalServerError(writer http.ResponseWriter, err error, errorCode dto.ErrorCode) {
sendJson(writer, &dto.InternalServerError{Message: err.Error(), ErrorCode: errorCode}, http.StatusInternalServerError)
}
func writeBadRequest(writer http.ResponseWriter, err error) {
sendJson(writer, &dto.ClientError{Message: err.Error()}, http.StatusBadRequest)
}
func writeNotFound(writer http.ResponseWriter, err error) {
sendJson(writer, &dto.ClientError{Message: err.Error()}, http.StatusNotFound)
}
func sendJson(writer http.ResponseWriter, content interface{}, httpStatusCode int) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(httpStatusCode)
response, err := json.Marshal(content)
if err != nil {
// cannot produce infinite recursive loop, since json.Marshal of dto.InternalServerError won't return an error
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
if _, err = writer.Write(response); err != nil {
log.Printf("Error writing JSON to response: %v\n", err)
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
}
func parseRequestBodyJSON(writer http.ResponseWriter, request *http.Request, structure interface{}) error {
if err := json.NewDecoder(request.Body).Decode(structure); err != nil {
writeBadRequest(writer, err)
return err
}
return nil
}

View File

@ -1,19 +1,24 @@
package api
import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/config"
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment"
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
"log"
"net/http"
"sync"
)
// ProvideRunner tries to respond with the id of a runner.
// This runner is then reserved for future use.
func ProvideRunner(writer http.ResponseWriter, request *http.Request) {
runnerRequest := new(dto.RequestRunner)
if err := json.NewDecoder(request.Body).Decode(runnerRequest); err != nil {
writeBadRequest(writer, err)
// ProvideRunner tries to respond with the id of a runner
// This runner is then reserved for future use
func provideRunner(writer http.ResponseWriter, request *http.Request) {
runnerRequest := new(dto.RunnerRequest)
if err := parseRequestBodyJSON(writer, request, runnerRequest); err != nil {
return
}
environment, err := environment.GetExecutionEnvironment(runnerRequest.ExecutionEnvironmentId)
@ -26,10 +31,92 @@ func ProvideRunner(writer http.ResponseWriter, request *http.Request) {
writeInternalServerError(writer, err, dto.ErrorNomadOverload)
return
}
executionsLock.Lock()
executions[runner.Id] = make(map[string]dto.ExecutionRequest)
executionsLock.Unlock()
sendJson(writer, &dto.RunnerResponse{Id: runner.Id}, http.StatusOK)
}
sendJson(writer, &dto.ResponseRunner{Id: runner.Id()}, http.StatusOK)
var (
executions = make(map[string]map[string]dto.ExecutionRequest)
executionsLock = sync.Mutex{}
)
func executeCommand(writer http.ResponseWriter, request *http.Request) {
executionRequest := new(dto.ExecutionRequest)
if err := parseRequestBodyJSON(writer, request, executionRequest); err != nil {
return
}
var scheme string
if config.Config.Server.TLS {
scheme = "wss"
} else {
scheme = "ws"
}
r, ok := runner.FromContext(request.Context())
if !ok {
log.Fatal("Expected runner in context! Something must be broken ...")
}
id, err := uuid.NewRandom()
if err != nil {
log.Printf("Error creating new execution id: %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
executionsLock.Lock()
runnerExecutions, ok := executions[r.Id]
if !ok {
writeNotFound(writer, errors.New("runner has not been provided"))
return
}
runnerExecutions[id.String()] = *executionRequest
executionsLock.Unlock()
path, err := router.Get("runner-websocket").URL("runnerId", r.Id)
if err != nil {
log.Printf("Error creating runner websocket URL %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
websocketUrl := fmt.Sprintf("%s://%s%s?executionId=%s", scheme, request.Host, path, id)
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl}, http.StatusOK)
}
func connectToRunner(writer http.ResponseWriter, request *http.Request) {
// Upgrade the connection to websocket
executionId := request.URL.Query()["executionId"]
log.Printf("Websocket for execution %s requested", executionId)
writer.WriteHeader(http.StatusNotImplemented)
}
func findRunnerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Find runner
runnerId := mux.Vars(request)["runnerId"]
// TODO: Get runner from runner store using runnerId
env, err := execution_environment.GetExecutionEnvironment(1)
if err != nil {
writeNotFound(writer, err)
return
}
r, ok := env.Runners[runnerId]
if !ok {
writer.WriteHeader(http.StatusNotFound)
return
}
ctx := runner.NewContext(request.Context(), r)
requestWithRunner := request.WithContext(ctx)
next.ServeHTTP(writer, requestWithRunner)
})
}
func registerRunnerRoutes(router *mux.Router) {
router.HandleFunc("", ProvideRunner).Methods(http.MethodPost)
router.HandleFunc("", provideRunner).Methods(http.MethodPost)
runnerRouter := router.PathPrefix("/{runnerId}").Subrouter()
runnerRouter.Use(findRunnerMiddleware)
runnerRouter.HandleFunc("/execute", executeCommand).Methods(http.MethodPost).Name("runner-execute")
runnerRouter.HandleFunc("/websocket", connectToRunner).Methods(http.MethodGet).Name("runner-websocket")
}