Skip to content

Commit

Permalink
Create volumes directories as the current user
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Sep 15, 2023
1 parent 9067a5c commit d9cde17
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions adapter/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"encoding/json"
"errors"
"io"
"os"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"

dockertypes "github.com/docker/docker/api/types"
Expand Down Expand Up @@ -127,13 +129,52 @@ func (a RunnerDockerAdapter) Start(instance *types.Instance, onLog func(msg stri

// binds
if instance.Methods.Docker.Volumes != nil {
currentUser, err := user.Current()
if err != nil {
return err
}
uidString := currentUser.Uid
gidString := currentUser.Gid

uid, err := strconv.Atoi(uidString)
if err != nil {
return err
}
gid, err := strconv.Atoi(gidString)
if err != nil {
return err
}

volumesPath := path.Join(instancePath, "volumes")

err = os.MkdirAll(volumesPath, os.ModePerm)
if err != nil {
return err
}

err = os.Chown(volumesPath, uid, gid)
if err != nil {
return err
}

for source, target := range *instance.Methods.Docker.Volumes {
if !strings.HasPrefix(source, "/") {
source, err = filepath.Abs(path.Join(instancePath, "volumes", source))
if err != nil {
return err
}
}

err = os.MkdirAll(source, os.ModePerm)
if err != nil {
return err
}

err = os.Chown(source, uid, gid)
if err != nil {
return err
}

options.binds = append(options.binds, source+":"+target)
}
}
Expand All @@ -157,10 +198,10 @@ func (a RunnerDockerAdapter) Start(instance *types.Instance, onLog func(msg stri
}

if instance.Methods.Docker.Dockerfile != nil {
id, err = a.createContainer(options)
id, err = a.createContainer(instancePath, options)
} else if instance.Methods.Docker.Image != nil {
options.imageName = *instance.Methods.Docker.Image
id, err = a.createContainer(options)
id, err = a.createContainer(instancePath, options)
}
if err != nil {
return err
Expand Down Expand Up @@ -380,7 +421,7 @@ type createContainerOptions struct {
sysctls map[string]string
}

func (a RunnerDockerAdapter) createContainer(options createContainerOptions) (string, error) {
func (a RunnerDockerAdapter) createContainer(instancePath string, options createContainerOptions) (string, error) {
config := container.Config{
Image: options.imageName,
ExposedPorts: options.exposedPorts,
Expand Down

0 comments on commit d9cde17

Please sign in to comment.