90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package pipeline_api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
apiv1 "git.ohea.xyz/cursorius/server/proto/gen/api/v1"
|
|
"git.ohea.xyz/cursorius/server/proto/gen/api/v1/apiv1connect"
|
|
"git.ohea.xyz/cursorius/server/runnermanager"
|
|
"github.com/bufbuild/connect-go"
|
|
"github.com/google/uuid"
|
|
"github.com/op/go-logging"
|
|
)
|
|
|
|
var log = logging.MustGetLogger("cursorius-server")
|
|
|
|
type ApiServer struct {
|
|
getRunnerCh chan runnermanager.GetRunnerRequest
|
|
allocatedRunners map[uuid.UUID]RunnerWrapper
|
|
}
|
|
|
|
type RunnerWrapper struct {
|
|
runner runnermanager.Runner
|
|
mutex sync.Mutex
|
|
}
|
|
|
|
func (s *ApiServer) GetRunner(
|
|
ctx context.Context,
|
|
req *connect.Request[apiv1.GetRunnerRequest],
|
|
) (*connect.Response[apiv1.GetRunnerResponse], error) {
|
|
|
|
respChan := make(chan runnermanager.GetRunnerResponse)
|
|
s.getRunnerCh <- runnermanager.GetRunnerRequest{
|
|
Tags: req.Msg.Tags,
|
|
RespChan: respChan,
|
|
}
|
|
|
|
var runnerTagsStr strings.Builder
|
|
fmt.Fprintf(&runnerTagsStr, "%v", req.Msg.Tags[0])
|
|
for _, tag := range req.Msg.Tags[1:] {
|
|
fmt.Fprintf(&runnerTagsStr, " %v", tag)
|
|
}
|
|
|
|
response := <-respChan
|
|
if response.Err != nil {
|
|
log.Errorf("Could not get runner with tags \"%v\": %v", runnerTagsStr, response.Err)
|
|
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("Could not get runner"))
|
|
}
|
|
|
|
log.Info("Got runner with tags: %v", runnerTagsStr)
|
|
|
|
runnerUuid := uuid.New()
|
|
|
|
s.allocatedRunners[runnerUuid] = RunnerWrapper{runner: response.Runner}
|
|
|
|
res := connect.NewResponse(&apiv1.GetRunnerResponse{
|
|
Id: runnerUuid.String(),
|
|
})
|
|
res.Header().Set("GetRunner-Version", "v1")
|
|
return res, nil
|
|
}
|
|
|
|
func (s *ApiServer) RunCommand(
|
|
ctx context.Context,
|
|
req *connect.Request[apiv1.RunCommandRequest],
|
|
) (*connect.Response[apiv1.RunCommandResponse], error) {
|
|
|
|
res := connect.NewResponse(&apiv1.RunCommandResponse{
|
|
ReturnCode: 0,
|
|
Stdout: "",
|
|
Stderr: "",
|
|
})
|
|
res.Header().Set("RunCommand-Version", "v1")
|
|
return res, nil
|
|
}
|
|
|
|
func CreateHandler(mux *http.ServeMux, getRunnerCh chan runnermanager.GetRunnerRequest) {
|
|
api_server := &ApiServer{
|
|
getRunnerCh: getRunnerCh,
|
|
allocatedRunners: make(map[uuid.UUID]RunnerWrapper),
|
|
}
|
|
path, handler := apiv1connect.NewGetRunnerServiceHandler(api_server)
|
|
mux.Handle(path, handler)
|
|
path, handler = apiv1connect.NewRunCommandServiceHandler(api_server)
|
|
mux.Handle(path, handler)
|
|
}
|