From b4fb59c6da55ca7172b9e4548fad98285a537cd6 Mon Sep 17 00:00:00 2001 From: fedor Date: Wed, 6 Mar 2024 10:31:03 -0500 Subject: [PATCH 1/4] Mount directories directly into `$HOME` After introducing of https://github.com/cirruslabs/tart/pull/733 we can use different VirtioFS tags for different directories and precisely mount them into exact locations. `/Volumes/My Shared Files/` default path is error-prone since some tools don't like whitespaces in paths. With this change directories will be directly mounted into `$HOME`. Fixes #64 --- internal/commands/config/config.go | 6 ++--- internal/commands/prepare/prepare.go | 33 ++++++++++++++++++++++++++++ internal/tart/vm.go | 6 ++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/internal/commands/config/config.go b/internal/commands/config/config.go index 62556ad..fb0ce67 100644 --- a/internal/commands/config/config.go +++ b/internal/commands/config/config.go @@ -95,13 +95,13 @@ func runConfig(cmd *cobra.Command, args []string) error { // Figure out the builds directory override to use switch { case tartConfig.HostDir: - gitlabRunnerConfig.BuildsDir = "/Volumes/My Shared Files/hostdir" + gitlabRunnerConfig.BuildsDir = fmt.Sprintf("/Users/%s/hostdir", tartConfig.SSHUsername) if err := os.MkdirAll(gitLabEnv.HostDirPath(), 0700); err != nil { return err } case buildsDir != "": - gitlabRunnerConfig.BuildsDir = "/Volumes/My Shared Files/buildsdir" + gitlabRunnerConfig.BuildsDir = fmt.Sprintf("/Users/%s/buildsdir", tartConfig.SSHUsername) buildsDir = os.ExpandEnv(buildsDir) gitlabRunnerConfig.JobEnv[tart.EnvTartExecutorInternalBuildsDir] = buildsDir @@ -115,7 +115,7 @@ func runConfig(cmd *cobra.Command, args []string) error { // Figure out the cache directory override to use switch { case cacheDir != "": - gitlabRunnerConfig.CacheDir = "/Volumes/My Shared Files/cachedir" + gitlabRunnerConfig.CacheDir = fmt.Sprintf("/Users/%s/cachedir", tartConfig.SSHUsername) cacheDir = os.ExpandEnv(cacheDir) gitlabRunnerConfig.JobEnv[tart.EnvTartExecutorInternalCacheDir] = cacheDir diff --git a/internal/commands/prepare/prepare.go b/internal/commands/prepare/prepare.go index dfc0da2..ce3d8c1 100644 --- a/internal/commands/prepare/prepare.go +++ b/internal/commands/prepare/prepare.go @@ -181,6 +181,39 @@ func runPrepareVM(cmd *cobra.Command, args []string) error { log.Printf("Timezone was set to %s!\n", tz) } + dirsToMount := []string{} + if config.HostDir { + dirsToMount = append(dirsToMount, "hostdir") + } + if _, ok := os.LookupEnv(tart.EnvTartExecutorInternalBuildsDir); ok { + dirsToMount = append(dirsToMount, "buildsdir") + } + if _, ok := os.LookupEnv(tart.EnvTartExecutorInternalCacheDir); ok { + dirsToMount = append(dirsToMount, "cachedir") + } + + for _, dirToMount := range dirsToMount { + log.Printf("Mounting %s...\n", dirToMount) + + session, err := ssh.NewSession() + if err != nil { + return err + } + defer session.Close() + + session.Stdin = bytes.NewBufferString(fmt.Sprintf("mount_virtiofs tart.virtiofs.%s /Users/%s/%s\n", dirToMount, config.SSHUsername, dirToMount)) + session.Stdout = os.Stdout + session.Stderr = os.Stderr + + if err := session.Shell(); err != nil { + return err + } + + if err := session.Wait(); err != nil { + return err + } + } + log.Println("VM is ready.") return ssh.Close() diff --git a/internal/tart/vm.go b/internal/tart/vm.go index 95457fc..20e7b97 100644 --- a/internal/tart/vm.go +++ b/internal/tart/vm.go @@ -124,13 +124,13 @@ func (vm *VM) Start( } if config.HostDir { - runArgs = append(runArgs, "--dir", fmt.Sprintf("hostdir:%s", gitLabEnv.HostDirPath())) + runArgs = append(runArgs, "--dir", fmt.Sprintf("%s:tag=tart.virtiofs.hostdir", gitLabEnv.HostDirPath())) } else if buildsDir, ok := os.LookupEnv(EnvTartExecutorInternalBuildsDir); ok { - runArgs = append(runArgs, "--dir", fmt.Sprintf("buildsdir:%s", buildsDir)) + runArgs = append(runArgs, "--dir", fmt.Sprintf("%s:tag=tart.virtiofs.buildsdir", buildsDir)) } if cacheDir, ok := os.LookupEnv(EnvTartExecutorInternalCacheDir); ok { - runArgs = append(runArgs, "--dir", fmt.Sprintf("cachedir:%s", cacheDir)) + runArgs = append(runArgs, "--dir", fmt.Sprintf("%s:tag=tart.virtiofs.cachedir", cacheDir)) } runArgs = append(runArgs, vm.id) From b76eef39093767f7f7e126a5ba862fc4fcfd899c Mon Sep 17 00:00:00 2001 From: fedor Date: Wed, 6 Mar 2024 10:34:05 -0500 Subject: [PATCH 2/4] Fixed lint issue --- internal/commands/prepare/prepare.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/commands/prepare/prepare.go b/internal/commands/prepare/prepare.go index ce3d8c1..6f8a65a 100644 --- a/internal/commands/prepare/prepare.go +++ b/internal/commands/prepare/prepare.go @@ -201,7 +201,11 @@ func runPrepareVM(cmd *cobra.Command, args []string) error { } defer session.Close() - session.Stdin = bytes.NewBufferString(fmt.Sprintf("mount_virtiofs tart.virtiofs.%s /Users/%s/%s\n", dirToMount, config.SSHUsername, dirToMount)) + mountScript := fmt.Sprintf( + "mount_virtiofs tart.virtiofs.%s /Users/%s/%s\n", + dirToMount, config.SSHUsername, dirToMount, + ) + session.Stdin = bytes.NewBufferString(mountScript) session.Stdout = os.Stdout session.Stderr = os.Stderr From 012a73f1532e27a395a41afb3d4b9747c4e83f6c Mon Sep 17 00:00:00 2001 From: fedor Date: Mon, 11 Mar 2024 09:34:29 -0400 Subject: [PATCH 3/4] Don't forget to `mkdir -p` --- internal/commands/prepare/prepare.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/commands/prepare/prepare.go b/internal/commands/prepare/prepare.go index 6f8a65a..0953174 100644 --- a/internal/commands/prepare/prepare.go +++ b/internal/commands/prepare/prepare.go @@ -201,11 +201,15 @@ func runPrepareVM(cmd *cobra.Command, args []string) error { } defer session.Close() + mkdirScript := fmt.Sprintf( + "mkdir -p /Users/%s/%s", + config.SSHUsername, dirToMount, + ) mountScript := fmt.Sprintf( - "mount_virtiofs tart.virtiofs.%s /Users/%s/%s\n", + "mount_virtiofs tart.virtiofs.%s /Users/%s/%s", dirToMount, config.SSHUsername, dirToMount, ) - session.Stdin = bytes.NewBufferString(mountScript) + session.Stdin = bytes.NewBufferString(strings.Join([]string{mkdirScript, mountScript, ""}, "\n") session.Stdout = os.Stdout session.Stderr = os.Stderr From 9e2887a03087482b9ae96e2bf8628ec59d28cd06 Mon Sep 17 00:00:00 2001 From: fedor Date: Mon, 11 Mar 2024 09:56:46 -0400 Subject: [PATCH 4/4] Introduce `mountPoint` --- internal/commands/prepare/prepare.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/internal/commands/prepare/prepare.go b/internal/commands/prepare/prepare.go index 0953174..916bcaa 100644 --- a/internal/commands/prepare/prepare.go +++ b/internal/commands/prepare/prepare.go @@ -201,15 +201,10 @@ func runPrepareVM(cmd *cobra.Command, args []string) error { } defer session.Close() - mkdirScript := fmt.Sprintf( - "mkdir -p /Users/%s/%s", - config.SSHUsername, dirToMount, - ) - mountScript := fmt.Sprintf( - "mount_virtiofs tart.virtiofs.%s /Users/%s/%s", - dirToMount, config.SSHUsername, dirToMount, - ) - session.Stdin = bytes.NewBufferString(strings.Join([]string{mkdirScript, mountScript, ""}, "\n") + mountPoint := fmt.Sprintf("/Users/%s/%s", config.SSHUsername, dirToMount) + mkdirScript := fmt.Sprintf("mkdir -p %s", mountPoint) + mountScript := fmt.Sprintf("mount_virtiofs tart.virtiofs.%s %s", dirToMount, mountPoint) + session.Stdin = bytes.NewBufferString(strings.Join([]string{mkdirScript, mountScript, ""}, "\n")) session.Stdout = os.Stdout session.Stderr = os.Stderr