From 4596d2de1f0e003f20df50f112a87e9256a2b21e Mon Sep 17 00:00:00 2001 From: gurpalw <37112979+gurpalw@users.noreply.github.com> Date: Fri, 4 Oct 2024 23:36:04 +0100 Subject: [PATCH] rolecule: support volume mounts (#13) Co-authored-by: gurpalw --- README.md | 2 ++ cmd/create.go | 3 +++ pkg/config/config.go | 3 ++- pkg/instance/instance.go | 24 ++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ea1365..0618de3 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,8 @@ instances: - name: ubuntu-22.04 image: ubuntu-systemd:22.04 playbook: ubuntu/playbook.yml + volumes: + - /var/run/docker.sock:/var/run/docker.sock - name: ubuntu-22.04-build image: ubuntu-systemd:22.04 arch: amd64 diff --git a/cmd/create.go b/cmd/create.go index 3a72532..484dc06 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -1,6 +1,8 @@ package cmd import ( + "os" + "github.com/apex/log" "github.com/spf13/cobra" "github.com/z0mbix/rolecule/pkg/config" @@ -38,6 +40,7 @@ func create(cfg *config.Config) error { output, err := instance.Create() if err != nil { log.Error(err.Error()) + os.Exit(1) } log.Debug(output) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 11f373a..c208edb 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -59,7 +59,7 @@ func Get() (*Config, error) { log.Debugf("config file: %+v", configValues) if configValues.Engine.Name == "" { - log.Debugf("enginer not specified, using default engine: %s", defaultEngine) + log.Debugf("engine not specified, using default engine: %s", defaultEngine) configValues.Engine.Name = defaultEngine } engine, err := container.NewEngine(configValues.Engine.Name) @@ -150,6 +150,7 @@ func Get() (*Config, error) { Provisioner: iProvisioner, Verifier: iVerifier, RoleMounts: roleMounts, + Volumes: i.Volumes, } instances = append(instances, instanceConfig) diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 7dcab0f..c223f29 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -2,7 +2,9 @@ package instance import ( "fmt" + "os" "path/filepath" + "strings" "github.com/apex/log" "github.com/z0mbix/rolecule/pkg/container" @@ -15,6 +17,7 @@ type Instances []Instance type Config struct { Name string `mapstructure:"name"` Image string `mapstructure:"image"` + Volumes []string `mapstructure:"volumes"` Arch string `mapstructure:"arch"` Args []string `mapstructure:"args"` Playbook string `mapstructure:"playbook"` @@ -36,6 +39,7 @@ type Instance struct { RoleName string RoleDir string RoleMounts map[string]string + Volumes []string container.Engine Provisioner provisioner.Provisioner Verifier verifier.Verifier @@ -62,6 +66,26 @@ func (i *Instance) Create() (string, error) { instanceArgs = append(instanceArgs, "--volume", fmt.Sprintf("%s:%s", src, dst)) } + for _, volume := range i.Volumes { + volume = strings.TrimSpace(volume) + parts := strings.SplitN(volume, ":", 2) + + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return "", fmt.Errorf("invalid volume format, expected 'hostPath:containerPath', got: %s", volume) + } + + _, err := os.Stat(parts[0]) + if err != nil { + if os.IsNotExist(err) { + return "", fmt.Errorf("host path does not exist: %s", parts[0]) + } + return "", fmt.Errorf("error accessing path: %s, error: %w", parts[0], err) + } + + log.Debugf("mounting volume: %s -> %s", parts[0], parts[1]) + instanceArgs = append(instanceArgs, "--volume", fmt.Sprintf("%s:%s", parts[0], parts[1])) + } + if i.Arch != "" { instanceArgs = append(instanceArgs, "--platform", fmt.Sprintf("linux/%s", i.Arch)) }