Add docker container logic to runner
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.ohea.xyz/cursorius/server/config"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/op/go-logging"
|
||||
)
|
||||
|
||||
@@ -14,6 +23,67 @@ type Run struct {
|
||||
func runJob(job config.Job) {
|
||||
if job.Folder != nil {
|
||||
log.Debugf("Job configured with folder \"%v\"", *job.Folder)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
resp, err := cli.ContainerCreate(ctx,
|
||||
&container.Config{
|
||||
Image: "cursorius:latest",
|
||||
Cmd: []string{"/launcher.sh"},
|
||||
Tty: false,
|
||||
Env: []string{fmt.Sprintf("CURSORIUS_SRC_DIR=%s", *job.Folder)},
|
||||
},
|
||||
&container.HostConfig{
|
||||
VolumesFrom: []string{hostname},
|
||||
},
|
||||
nil, nil, "",
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Could not create container: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||
log.Errorf("Could not start container: %v", err)
|
||||
return
|
||||
}
|
||||
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
log.Errorf("Container returned error: %v", err)
|
||||
return
|
||||
}
|
||||
case retCode := <-statusCh:
|
||||
log.Debugf("Container finished running with return code: %v", retCode)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var stdOut bytes.Buffer
|
||||
var stdErr bytes.Buffer
|
||||
|
||||
stdcopy.StdCopy(&stdOut, &stdErr, out)
|
||||
|
||||
log.Debugf("%s", stdOut.Bytes())
|
||||
log.Debugf("%s", stdErr.Bytes())
|
||||
|
||||
} else if job.URL != nil {
|
||||
log.Debugf("Job configured with URL \"%v\"", *job.URL)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user