Fix runner logic and add git clone step
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
type Config struct {
|
||||
ServerUrl string
|
||||
Id string
|
||||
WorkingDir string
|
||||
Secret string
|
||||
Tags []string
|
||||
}
|
||||
@@ -20,6 +21,7 @@ func GetConfig() (config.Config[Config], bool, error) {
|
||||
ServerUrl: "FILL IN",
|
||||
Id: "FILL IN",
|
||||
Secret: "FILL IN",
|
||||
WorkingDir: "FILL IN",
|
||||
Tags: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.ohea.xyz/cursorius/runner/config"
|
||||
"git.ohea.xyz/cursorius/runner/runner"
|
||||
"github.com/op/go-logging"
|
||||
"nhooyr.io/websocket"
|
||||
"nhooyr.io/websocket/wsjson"
|
||||
@@ -19,11 +20,6 @@ type Register struct {
|
||||
Tags []string
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
URL *string
|
||||
Folder *string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var format = logging.MustStringFormatter(
|
||||
@@ -60,26 +56,31 @@ func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
|
||||
c, _, err := websocket.Dial(ctx, connectString, nil)
|
||||
conn, _, err := websocket.Dial(ctx, connectString, nil)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not connect to server at \"%v\", %v", connectString, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer c.Close(websocket.StatusInternalError, "the sky is falling")
|
||||
defer conn.Close(websocket.StatusInternalError, "the sky is falling")
|
||||
|
||||
err = wsjson.Write(ctx, c, registrationData)
|
||||
err = wsjson.Write(ctx, conn, registrationData)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not send registration information to server: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for {
|
||||
var job Job
|
||||
err = wsjson.Read(ctx, c, &job)
|
||||
var job runner.Job
|
||||
err = wsjson.Read(ctx, conn, &job)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not receive from connection: %v", err)
|
||||
continue
|
||||
}
|
||||
log.Infof("Received job from server: %v", *job.URL)
|
||||
log.Infof("Received job from server: id: %v, URL: %v", job.Id, job.URL)
|
||||
|
||||
err = runner.RunJob(job, configData.Config.WorkingDir)
|
||||
if err != nil {
|
||||
log.Errorf("Could not run job: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+45
-25
@@ -4,30 +4,48 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/op/go-logging"
|
||||
"os"
|
||||
)
|
||||
|
||||
var log = logging.MustGetLogger("cursorius-server")
|
||||
|
||||
func runJob(job config.Job) {
|
||||
if job.Folder != nil {
|
||||
log.Debugf("Job configured with folder \"%v\"", *job.Folder)
|
||||
type Job struct {
|
||||
Id string
|
||||
URL string
|
||||
}
|
||||
|
||||
func RunJob(job Job, workingDir string) error {
|
||||
jobFolder := filepath.Join(workingDir, job.Id)
|
||||
|
||||
log.Debugf("Job %v configured with URL \"%v\"", job.Id, job.URL)
|
||||
|
||||
log.Debugf("Job %v configured with folder \"%v\"", job.Id, jobFolder)
|
||||
|
||||
err := os.MkdirAll(jobFolder, 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create working directory for job %v: %v", job.Id, err)
|
||||
}
|
||||
|
||||
log.Infof("Cloning source from URL %v", job.URL)
|
||||
cloneCmd := exec.Command("git", "clone", job.URL, jobFolder)
|
||||
output, err := cloneCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Debugf("%s", output)
|
||||
return fmt.Errorf("could not clone source: %v", err)
|
||||
}
|
||||
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
log.Errorf("Could not create docker clien: %va", err)
|
||||
return
|
||||
}
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
log.Errorf("Could not get hostname: %v", err)
|
||||
return
|
||||
return fmt.Errorf("Could not create docker client: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -37,28 +55,33 @@ func runJob(job config.Job) {
|
||||
Image: "cursorius:latest",
|
||||
Cmd: []string{"/launcher.sh"},
|
||||
Tty: false,
|
||||
Env: []string{fmt.Sprintf("CURSORIUS_SRC_DIR=%s", *job.Folder)},
|
||||
Env: []string{fmt.Sprintf("CURSORIUS_SRC_DIR=/job")},
|
||||
},
|
||||
// TODO: fix running the runner in docker (add VolumesFrom to HostConfig)
|
||||
&container.HostConfig{
|
||||
VolumesFrom: []string{hostname},
|
||||
Mounts: []mount.Mount{{
|
||||
Source: jobFolder,
|
||||
Target: "/job",
|
||||
ReadOnly: false,
|
||||
Consistency: mount.ConsistencyDefault,
|
||||
}},
|
||||
},
|
||||
nil, nil, "",
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Could not create container: %v", err)
|
||||
return
|
||||
return fmt.Errorf("could not create container: %v", err)
|
||||
}
|
||||
|
||||
log.Info("Launching container")
|
||||
|
||||
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||
log.Errorf("Could not start container: %v", err)
|
||||
return
|
||||
return fmt.Errorf("could not start container: %v", err)
|
||||
}
|
||||
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
log.Errorf("Container returned error: %v", err)
|
||||
return
|
||||
return fmt.Errorf("container returned error: %v", err)
|
||||
}
|
||||
case retCode := <-statusCh:
|
||||
log.Debugf("Container finished running with return code: %v", retCode)
|
||||
@@ -66,8 +89,7 @@ func runJob(job config.Job) {
|
||||
|
||||
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
|
||||
if err != nil {
|
||||
log.Errorf("Could not get container logs: %v", err)
|
||||
return
|
||||
return fmt.Errorf("could not get container logs: %v", err)
|
||||
}
|
||||
|
||||
var stdOut bytes.Buffer
|
||||
@@ -78,7 +100,5 @@ func runJob(job config.Job) {
|
||||
log.Debugf("%s", stdOut.Bytes())
|
||||
log.Debugf("%s", stdErr.Bytes())
|
||||
|
||||
} else if job.URL != nil {
|
||||
log.Debugf("Job configured with URL \"%v\"", *job.URL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user