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