90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
|
package runner
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"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 {
|
||
|
core int
|
||
|
memory int
|
||
|
}
|
||
|
|
||
|
func extractNumbers(s string) (result []int) {
|
||
|
lastNumber,isNumber := false,false
|
||
|
var curNumber=0
|
||
|
for _,char :=range s {
|
||
|
isNumber=(char>='0' && char<='9')
|
||
|
if(isNumber) {
|
||
|
curNumber*=10
|
||
|
curNumber+=int(char-'0')
|
||
|
}
|
||
|
if(!isNumber && lastNumber) {
|
||
|
result=append(result,curNumber)
|
||
|
curNumber=0
|
||
|
}
|
||
|
lastNumber=isNumber
|
||
|
}
|
||
|
if(lastNumber) {
|
||
|
result=append(result,curNumber)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func Init() error {
|
||
|
rand.Seed(time.Now().UnixNano())
|
||
|
//croup initialisation:
|
||
|
if cgroups.Mode() == cgroups.Unified {
|
||
|
log.Println("cgroups v2 usage approved")
|
||
|
} else {
|
||
|
return fmt.Errorf("cgroups v2 are not enabled")//TODO: trouble description
|
||
|
}
|
||
|
//isolated cores initialisation:
|
||
|
cmdlineBytes := make([]byte, 400)
|
||
|
cmdlineFile,_:=os.Open("/proc/cmdline");
|
||
|
countCmdlineBytes,_:=cmdlineFile.Read(cmdlineBytes);
|
||
|
cmdline:=string(cmdlineBytes[:countCmdlineBytes])
|
||
|
kernelParams:=strings.Split(cmdline," ")
|
||
|
for _,param := range kernelParams{
|
||
|
if(len(param)>=9 && param[:9]=="isolcpus=") {
|
||
|
coresIsolated=append(coresIsolated, extractNumbers(param[9:])...)
|
||
|
}
|
||
|
}
|
||
|
if(len(coresIsolated)==0) {
|
||
|
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) {
|
||
|
var runId string
|
||
|
for i:=0;i<runIdLength;i++{
|
||
|
runId+=string('a'+byte(rand.Int31n(26)))
|
||
|
}
|
||
|
}
|