Skip to content

Commit

Permalink
rolecule: support volume mounts (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: gurpalw <[email protected]>
  • Loading branch information
gurpalw and gurpalw authored Oct 4, 2024
1 parent 6b6fe34 commit 4596d2d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/apex/log"
"github.com/spf13/cobra"
"github.com/z0mbix/rolecule/pkg/config"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -150,6 +150,7 @@ func Get() (*Config, error) {
Provisioner: iProvisioner,
Verifier: iVerifier,
RoleMounts: roleMounts,
Volumes: i.Volumes,
}

instances = append(instances, instanceConfig)
Expand Down
24 changes: 24 additions & 0 deletions pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package instance

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/apex/log"
"github.com/z0mbix/rolecule/pkg/container"
Expand All @@ -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"`
Expand All @@ -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
Expand All @@ -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))
}
Expand Down

0 comments on commit 4596d2d

Please sign in to comment.