From 6fc9af8280135ff6ad873c60c25776c941d2bcb7 Mon Sep 17 00:00:00 2001 From: restitux Date: Sun, 1 Jan 2023 13:44:09 -0700 Subject: [PATCH] Implement skeleton of RunCommand --- pipeline_api/pipeline_api.go | 47 ++++++++++++++++++++++---- proto/api/v1/run_command.proto | 2 +- proto/gen/api/v1/run_command.pb.go | 53 +++++++++++++++--------------- 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/pipeline_api/pipeline_api.go b/pipeline_api/pipeline_api.go index 2a8babb..96190f3 100644 --- a/pipeline_api/pipeline_api.go +++ b/pipeline_api/pipeline_api.go @@ -18,8 +18,9 @@ import ( var log = logging.MustGetLogger("cursorius-server") type ApiServer struct { - getRunnerCh chan runnermanager.GetRunnerRequest - allocatedRunners map[uuid.UUID]RunnerWrapper + getRunnerCh chan runnermanager.GetRunnerRequest + allocatedRunners map[uuid.UUID]*RunnerWrapper + allocatedRunnersMutex sync.RWMutex } type RunnerWrapper struct { @@ -27,6 +28,23 @@ type RunnerWrapper struct { mutex sync.Mutex } +func (r *RunnerWrapper) RunCommand(cmd string) (int64, string, string, error) { + r.mutex.Unlock() + defer r.mutex.Lock() + + return_code, stdout, stderr, err := r.runner.RunCommand(cmd) + + // TODO: run command by sending websocket packet + // TODO: get stdout and stderr response + return return_code, stdout, stderr, err +} + +func (s *ApiServer) GetRunnerFromMap(uuid uuid) *RunnerWrapper { + s.allocatedRunnersMutex.RLock() + defer s.allocatedRunnersMutex.RUnlock() + return s.allocatedRunners[uuid] +} + func (s *ApiServer) GetRunner( ctx context.Context, req *connect.Request[apiv1.GetRunnerRequest], @@ -54,7 +72,9 @@ func (s *ApiServer) GetRunner( runnerUuid := uuid.New() - s.allocatedRunners[runnerUuid] = RunnerWrapper{runner: response.Runner} + s.allocatedRunnersMutex.Lock() + s.allocatedRunners[runnerUuid] = &RunnerWrapper{runner: response.Runner} + s.allocatedRunnersMutex.Unlock() res := connect.NewResponse(&apiv1.GetRunnerResponse{ Id: runnerUuid.String(), @@ -68,10 +88,23 @@ func (s *ApiServer) RunCommand( req *connect.Request[apiv1.RunCommandRequest], ) (*connect.Response[apiv1.RunCommandResponse], error) { + uuid, err := uuid.Parse(req.Msg.Id) + if err != nil { + return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Invalid runner id")) + } + + runner := s.GetRunnerFromMap(uuid) + + return_code, stdout, stderr, err := runner.RunCommand(req.Msg.Command) + if err != nil { + log.Errorf("Could not run command on runner \"%v\", %v", runner.runner.Id(), err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Could not run command")) + } + res := connect.NewResponse(&apiv1.RunCommandResponse{ - ReturnCode: 0, - Stdout: "", - Stderr: "", + ReturnCode: return_code, + Stdout: stdout, + Stderr: stderr, }) res.Header().Set("RunCommand-Version", "v1") return res, nil @@ -80,7 +113,7 @@ func (s *ApiServer) RunCommand( func CreateHandler(mux *http.ServeMux, getRunnerCh chan runnermanager.GetRunnerRequest) { api_server := &ApiServer{ getRunnerCh: getRunnerCh, - allocatedRunners: make(map[uuid.UUID]RunnerWrapper), + allocatedRunners: make(map[uuid.UUID]*RunnerWrapper), } path, handler := apiv1connect.NewGetRunnerServiceHandler(api_server) mux.Handle(path, handler) diff --git a/proto/api/v1/run_command.proto b/proto/api/v1/run_command.proto index b3b67bb..d684d3d 100644 --- a/proto/api/v1/run_command.proto +++ b/proto/api/v1/run_command.proto @@ -5,7 +5,7 @@ package api.v1; option go_package = "git.ohea.xyz/cursorius/server/proto/gen/api/v1;apiv1"; message RunCommandRequest { - int64 runner_id = 1; + string id = 1; string command = 2; } diff --git a/proto/gen/api/v1/run_command.pb.go b/proto/gen/api/v1/run_command.pb.go index e3dfd41..a7a9a0e 100644 --- a/proto/gen/api/v1/run_command.pb.go +++ b/proto/gen/api/v1/run_command.pb.go @@ -25,8 +25,8 @@ type RunCommandRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - RunnerId int64 `protobuf:"varint,1,opt,name=runner_id,json=runnerId,proto3" json:"runner_id,omitempty"` - Command string `protobuf:"bytes,2,opt,name=command,proto3" json:"command,omitempty"` + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + Command string `protobuf:"bytes,2,opt,name=command,proto3" json:"command,omitempty"` } func (x *RunCommandRequest) Reset() { @@ -61,11 +61,11 @@ func (*RunCommandRequest) Descriptor() ([]byte, []int) { return file_api_v1_run_command_proto_rawDescGZIP(), []int{0} } -func (x *RunCommandRequest) GetRunnerId() int64 { +func (x *RunCommandRequest) GetId() string { if x != nil { - return x.RunnerId + return x.Id } - return 0 + return "" } func (x *RunCommandRequest) GetCommand() string { @@ -143,28 +143,27 @@ var File_api_v1_run_command_proto protoreflect.FileDescriptor var file_api_v1_run_command_proto_rawDesc = []byte{ 0x0a, 0x18, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x72, 0x75, 0x6e, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x61, 0x70, 0x69, 0x2e, - 0x76, 0x31, 0x22, 0x4a, 0x0a, 0x11, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1b, 0x0a, 0x09, 0x72, 0x75, 0x6e, 0x6e, 0x65, - 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x72, 0x75, 0x6e, 0x6e, - 0x65, 0x72, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x22, 0x65, - 0x0a, 0x12, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x5f, 0x63, - 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x72, 0x65, 0x74, 0x75, 0x72, - 0x6e, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x64, 0x6f, 0x75, 0x74, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x64, 0x6f, 0x75, 0x74, 0x12, 0x16, 0x0a, - 0x06, 0x73, 0x74, 0x64, 0x65, 0x72, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, - 0x74, 0x64, 0x65, 0x72, 0x72, 0x32, 0x5a, 0x0a, 0x11, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, - 0x61, 0x6e, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x45, 0x0a, 0x0a, 0x52, 0x75, - 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x19, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, - 0x31, 0x2e, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x75, 0x6e, - 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, - 0x00, 0x42, 0x36, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x2e, 0x6f, 0x68, 0x65, 0x61, 0x2e, 0x78, 0x79, - 0x7a, 0x2f, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x69, 0x75, 0x73, 0x2f, 0x73, 0x65, 0x72, 0x76, - 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x61, 0x70, 0x69, - 0x2f, 0x76, 0x31, 0x3b, 0x61, 0x70, 0x69, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x76, 0x31, 0x22, 0x3d, 0x0a, 0x11, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, + 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, + 0x64, 0x22, 0x65, 0x0a, 0x12, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x72, 0x65, 0x74, 0x75, 0x72, + 0x6e, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x72, 0x65, + 0x74, 0x75, 0x72, 0x6e, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x64, 0x6f, + 0x75, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x64, 0x6f, 0x75, 0x74, + 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x64, 0x65, 0x72, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x06, 0x73, 0x74, 0x64, 0x65, 0x72, 0x72, 0x32, 0x5a, 0x0a, 0x11, 0x52, 0x75, 0x6e, 0x43, + 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x45, 0x0a, + 0x0a, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x19, 0x2e, 0x61, 0x70, + 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, + 0x52, 0x75, 0x6e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x42, 0x36, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x2e, 0x6f, 0x68, 0x65, 0x61, + 0x2e, 0x78, 0x79, 0x7a, 0x2f, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x69, 0x75, 0x73, 0x2f, 0x73, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x65, 0x6e, 0x2f, + 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x3b, 0x61, 0x70, 0x69, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var (