Skip to content

Commit

Permalink
Introduce --guest-builds-dir and --guest-cache-dir (#63)
Browse files Browse the repository at this point in the history
* Introduce --guest-builds-dir and --guest-cache-dir

* README.md: better explain the difference between config stage options

* Better clarify --guest-builds-dir and --guest-cache-dir
  • Loading branch information
edigaryev authored Feb 19, 2024
1 parent d8d241b commit 921eca7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ that required paid sponsorship upon exceeding a free limit.

### `config` stage

| Argument | Default | Description |
|----------------|---------|---------------------------------------------------------|
| `--builds-dir` | | Path to a directory on host to use for storing builds |
| `--cache-dir` | | Path to a directory on host to use for caching purposes |
| Argument | Default | Description |
|----------------------------------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--builds-dir` | | Path to a directory on host to use for storing builds, automatically mounts that directory to the guest VM (mutually exclusive with `--guest-builds-dir`) |
| `--cache-dir` | | Path to a directory on host to use for caching purposes, automatically mounts that directory to the guest VM (mutually exclusive with `--guest-cache-dir`) |
| `--guest-builds-dir`<sup>1</sup> | | Path to a directory in guest to use for storing builds, useful when mounting a block device (via [`--disk` command-line argument](#prepare-stage)) to the VM (mutually exclusive with `--builds-dir`) |
| `--guest-cache-dir`<sup>1</sup> | | Path to a directory in guest to use for caching purposes, useful when mounting a block device (via [`--disk` command-line argument](#prepare-stage) to the VM (mutually exclusive with `--cache-dir`) |

<sup>1</sup>: this is an advanced feature which should only be resorted to when the standard directory sharing via `--builds-dir` and `--cache-dir` is not sufficient for some reason.

### `prepare` stage

Expand Down
61 changes: 49 additions & 12 deletions internal/commands/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,35 @@ import (

var ErrConfigFailed = errors.New("configuration stage failed")

var buildsDir string
var cacheDir string
var (
buildsDir string
cacheDir string

guestBuildsDir string
guestCacheDir string
)

func NewCommand() *cobra.Command {
command := &cobra.Command{
cmd := &cobra.Command{
Use: "config",
Short: "Configure GitLab Runner",
RunE: runConfig,
}

command.PersistentFlags().StringVar(&buildsDir, "builds-dir", "",
"Path to a directory on host to use for storing builds")
command.PersistentFlags().StringVar(&cacheDir, "cache-dir", "",
"path to a directory on host to use for caching purposes")

return command
cmd.PersistentFlags().StringVar(&buildsDir, "builds-dir", "",
"path to a directory on host to use for storing builds, automatically mounts that directory "+
"to the guest VM, mutually exclusive with \"--guest-builds-dir\"")
cmd.PersistentFlags().StringVar(&cacheDir, "cache-dir", "",
"path to a directory on host to use for caching purposes, automatically mounts that directory "+
"to the guest VM, mutually exclusive with \"--guest-cache-dir\"")
cmd.PersistentFlags().StringVar(&guestBuildsDir, "guest-builds-dir", "",
"path to a directory in guest to use for storing builds, useful when mounting a block device "+
"via \"--disk\" command-line argument (mutually exclusive with \"--builds-dir\")")
cmd.PersistentFlags().StringVar(&guestCacheDir, "guest-cache-dir", "",
"path to a directory in guest to use for caching purposes, useful when mounting a block device "+
"via \"--disk\" command-line argument (mutually exclusive with \"--cache-dir\")")

return cmd
}

func runConfig(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -59,34 +72,58 @@ func runConfig(cmd *cobra.Command, args []string) error {
return err
}

// Validate environment variables and command-line arguments combinations
if tartConfig.HostDir && buildsDir != "" {
return fmt.Errorf("%w: --builds-dir and TART_EXECUTOR_HOST_DIR are mutually exclusive",
ErrConfigFailed)
}
if tartConfig.HostDir {
if tartConfig.HostDir && guestBuildsDir != "" {
return fmt.Errorf("%w: --guest-builds-dir and TART_EXECUTOR_HOST_DIR are mutually exclusive",
ErrConfigFailed)
}

if buildsDir != "" && guestBuildsDir != "" {
return fmt.Errorf("%w: --builds-dir and --guest-builds-dir are mutually exclusive",
ErrConfigFailed)
}

if cacheDir != "" && guestCacheDir != "" {
return fmt.Errorf("%w: --cache-dir and --guest-cache-dir are mutually exclusive",
ErrConfigFailed)
}

// Figure out the builds directory override to use
switch {
case tartConfig.HostDir:
gitlabRunnerConfig.BuildsDir = "/Volumes/My Shared Files/hostdir"

if err := os.MkdirAll(gitLabEnv.HostDirPath(), 0700); err != nil {
return err
}
} else if buildsDir != "" {
case buildsDir != "":
gitlabRunnerConfig.BuildsDir = "/Volumes/My Shared Files/buildsdir"
buildsDir = os.ExpandEnv(buildsDir)
gitlabRunnerConfig.JobEnv[tart.EnvTartExecutorInternalBuildsDir] = buildsDir

if err := os.MkdirAll(buildsDir, 0700); err != nil {
return err
}
case guestBuildsDir != "":
gitlabRunnerConfig.BuildsDir = guestBuildsDir
}

if cacheDir != "" {
// Figure out the cache directory override to use
switch {
case cacheDir != "":
gitlabRunnerConfig.CacheDir = "/Volumes/My Shared Files/cachedir"
cacheDir = os.ExpandEnv(cacheDir)
gitlabRunnerConfig.JobEnv[tart.EnvTartExecutorInternalCacheDir] = cacheDir

if err := os.MkdirAll(cacheDir, 0700); err != nil {
return err
}
case guestCacheDir != "":
gitlabRunnerConfig.CacheDir = guestCacheDir
}

jsonBytes, err := json.MarshalIndent(&gitlabRunnerConfig, "", " ")
Expand Down

0 comments on commit 921eca7

Please sign in to comment.