Skip to content

Commit

Permalink
Revert changes to docker implementation and add engine interface
Browse files Browse the repository at this point in the history
[no ci]
  • Loading branch information
lbeckman314 committed May 10, 2024
1 parent a3a3445 commit 8ec817b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
29 changes: 6 additions & 23 deletions worker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type DockerCommand struct {
Stdout io.Writer
Stderr io.Writer
Event *events.ExecutorWriter
Engine string
}

// Run runs the Docker command and blocks until done.
Expand All @@ -37,13 +36,13 @@ func (dcmd DockerCommand) Run(ctx context.Context) error {
dcmd.Event.Error("failed to sync docker client API version", err)
}

pullcmd := exec.Command(dcmd.Engine, "pull", dcmd.Image)
pullcmd := exec.Command("docker", "pull", dcmd.Image)
err = pullcmd.Run()
if err != nil {
dcmd.Event.Error("failed to pull docker image", err)
}

args := []string{dcmd.Engine, "run", "-i", "--read-only"}
args := []string{"run", "-i", "--read-only"}

if dcmd.RemoveContainer {
args = append(args, "--rm")
Expand All @@ -69,18 +68,11 @@ func (dcmd DockerCommand) Run(ctx context.Context) error {
}

args = append(args, dcmd.Image)
// if dcmd.Command contains the ["/bin/bash", "-ue"] string, replace it with "/bin/bash -ue"
c := strings.Join(dcmd.Command, " ")
if strings.Contains(c, "[/bin/bash, -ue]") {
dcmd.Command = transformCommandSlice(dcmd.Command)
}
args = append(args, dcmd.Command...)

// Roughly: `docker run --rm -i --read-only -w [workdir] -v [bindings] [imageName] [cmd]`
err = dcmd.Event.Info(fmt.Sprintf("args: %s", args))
dcmd.Event.Info("Running command", "cmd", dcmd.Engine+" "+strings.Join(args, " "))
strings.Split(dcmd.Engine, " ")
cmd := exec.Command("sudo", args...)
dcmd.Event.Info("Running command", "cmd", "docker "+strings.Join(args, " "))
cmd := exec.Command("docker", args...)

if dcmd.Stdin != nil {
cmd.Stdin = dcmd.Stdin
Expand All @@ -101,7 +93,7 @@ func (dcmd DockerCommand) Run(ctx context.Context) error {
func (dcmd DockerCommand) Stop() error {
dcmd.Event.Info("Stopping container", "container", dcmd.ContainerName)
// cmd := exec.Command("docker", "stop", dcmd.ContainerName)
cmd := exec.Command(dcmd.Engine, "rm", "-f", dcmd.ContainerName) //switching to this to be a bit more forceful
cmd := exec.Command("docker", "rm", "-f", dcmd.ContainerName) //switching to this to be a bit more forceful
return cmd.Run()
}

Expand All @@ -114,15 +106,6 @@ func formatVolumeArg(v Volume) string {
return fmt.Sprintf("%s:%s:%s", v.HostPath, v.ContainerPath, mode)
}

func transformCommandSlice(cmd []string) []string {
if len(cmd) == 0 {
return cmd // Handle empty slice
}

cmd[2] = "/bin/bash -ue .command.run &> .command.log"
return cmd
}

type metadata struct {
ID string
Name string
Expand All @@ -143,7 +126,7 @@ func (dcmd *DockerCommand) inspectContainer(ctx context.Context) {
case <-ctx.Done():
return
case <-ticker.C:
cmd := exec.CommandContext(ctx, "sudo", "/opt/acc/sbin/exadocker", "inspect", dcmd.ContainerName)
cmd := exec.CommandContext(ctx, "docker", "inspect", dcmd.ContainerName)
out, err := cmd.Output()
if err == nil {
meta := []metadata{}
Expand Down
27 changes: 27 additions & 0 deletions worker/engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package worker

import "context"

type Engine interface {
Pull(ctx context.Context, image string) error

Run(ctx context.Context, config ContainerConfig) error

Stop(ctx context.Context, containerID string) error

Inspect(ctx context.Context, containerID string) (ContainerInfo, error)
}

type ContainerConfig struct {
Image string
Command []string
Args []string
EnvVars map[string]string
Volumes []Volume
}

type ContainerInfo struct {
ID string
Name string
Image string
}

0 comments on commit 8ec817b

Please sign in to comment.