add starter output handling, runner resource management

This commit is contained in:
root 2024-06-22 20:59:29 +05:00
parent 505431fedc
commit 4e0711fe8c
4 changed files with 39 additions and 29 deletions

View file

@ -1,27 +1,26 @@
package runner
import (
"errors"
//"errors"
"bytes"
"io"
"fmt"
"log"
"os"
"strings"
exec "os/exec"
cgroups "github.com/containerd/cgroups"
//cgroup2 "github.com/containerd/cgroups/v3/cgroup2"
//specs "github.com/opencontainers/runtime-spec/specs-go"
rand "math/rand"
"time"
"os/user"
)
const runner_username string = "gaterunner"
const runIdLength = 20
var coresIsolated []int
type Limits struct {
core int
memory int
Core int
Memory int
Time int
}
func extractNumbers(s string) (result []int) {
@ -68,22 +67,26 @@ func Init() error {
return fmt.Errorf("no free cores available")//TODO: trouble description
}
log.Println("running on cores:",coresIsolated)
//user setup:
_,err:=user.Lookup(runner_username)
if err!=nil{
if errors.As(err, new(user.UnknownUserError)) {
exec.Command("useradd", runner_username).Run();
} else {
return fmt.Errorf("user error:",err)
}
}
log.Println("Runner initialisation successful!")
return nil
}
func IsolatedRun(command []string,core int) {
func IsolatedRun(command []string,limits Limits) {
var runId string
for i:=0;i<runIdLength;i++{runId+=string('a'+byte(rand.Int31n(26)))}
os.mkdir("starter/"+runId,0777);
exec.Command("starter/starter", );
os.Mkdir("starter/"+runId,0777)
//log.Printf("%d",limits.Core)
//exec.Command("starter/starter", string(limits.Core),string(limits.Memory),string(limits.Time),runId,command...);
args:=[]string{string(limits.Core),string(limits.Memory),string(limits.Time),runId}
args=append(args,command...)
cmd:=exec.Command("/root/runner/starter/starter",args...)
//cmd:=exec.Command("/usr/bin/echo","123")
var stdBuffer bytes.Buffer
mw := io.MultiWriter(os.Stdout, &stdBuffer)
cmd.Stdout = mw
err := cmd.Run();
if err != nil { log.Fatal(err) }
//log.Println(stdBuffer.String())
os.RemoveAll("starter/"+runId)
cmd.Wait()
}