diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2022-07-07 19:15:39 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2022-07-08 15:56:55 +0300 |
commit | d69450df85ab87a5075f53d2fbdefc5da8fd4a22 (patch) | |
tree | e8fd5a6ce3f157bcc55f12d42c995c415c5efcbc | |
parent | 94efb3dbb2aef7482bcd7b1be3c00d9b97febd9e (diff) |
Setup runtime directory after config has been loaded
Initially, the RuntimeDir contains the parent directory where all of
the runtime directories should be created in. The configuration is then
loaded, and the RuntimeDir is overwritten with the actual created runtime
directory. In order to prune the runtime directories, we need to have
access to the original parent directory where the prunable directories
are located. This commit defers the runtime directory setup to after
the config has been loaded so we can first prune the excess directories
and only then create the new runtime directory. Doing so, the validation
logic changes a bit as it's now fine to have unset runtime directory in
the config. SetupRuntimeDirectory knows how to handle this and will create
the temporary directory as done before.
-rw-r--r-- | cmd/gitaly/main.go | 6 | ||||
-rw-r--r-- | internal/gitaly/config/config.go | 10 | ||||
-rw-r--r-- | internal/gitaly/config/config_test.go | 27 |
3 files changed, 23 insertions, 20 deletions
diff --git a/cmd/gitaly/main.go b/cmd/gitaly/main.go index 43012ae2f..781f5fd67 100644 --- a/cmd/gitaly/main.go +++ b/cmd/gitaly/main.go @@ -136,6 +136,12 @@ func run(cfg config.Cfg) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + runtimeDir, err := config.SetupRuntimeDirectory(cfg, os.Getpid()) + if err != nil { + return fmt.Errorf("setup runtime directory: %w", err) + } + cfg.RuntimeDir = runtimeDir + defer func() { if err := os.RemoveAll(cfg.RuntimeDir); err != nil { log.Warn("could not clean up runtime dir") diff --git a/internal/gitaly/config/config.go b/internal/gitaly/config/config.go index e05ee173b..95214ce8f 100644 --- a/internal/gitaly/config/config.go +++ b/internal/gitaly/config/config.go @@ -231,12 +231,6 @@ func (cfg *Cfg) setDefaults() error { cfg.Hooks.CustomHooksDir = filepath.Join(cfg.GitlabShell.Dir, "hooks") } - runtimeDir, err := SetupRuntimeDirectory(*cfg, os.Getpid()) - if err != nil { - return err - } - cfg.RuntimeDir = runtimeDir - if reflect.DeepEqual(cfg.DailyMaintenance, DailyJob{}) { cfg.DailyMaintenance = defaultMaintenanceWindow(cfg.Storages) } @@ -377,8 +371,8 @@ func (cfg *Cfg) validateBinDir() error { } func (cfg *Cfg) validateRuntimeDir() error { - if len(cfg.RuntimeDir) == 0 { - return fmt.Errorf("runtime_dir: is not set") + if cfg.RuntimeDir == "" { + return nil } if err := validateIsDirectory(cfg.RuntimeDir, "runtime_dir"); err != nil { diff --git a/internal/gitaly/config/config_test.go b/internal/gitaly/config/config_test.go index ce73e9c8a..4351e421c 100644 --- a/internal/gitaly/config/config_test.go +++ b/internal/gitaly/config/config_test.go @@ -1193,15 +1193,15 @@ func TestValidateBinDir(t *testing.T) { } } -func TestCfg_RuntimeDir(t *testing.T) { +func TestSetupRuntimeDirectory(t *testing.T) { t.Run("defaults", func(t *testing.T) { t.Run("empty runtime directory", func(t *testing.T) { cfg := Cfg{} - require.NoError(t, cfg.setDefaults()) + runtimeDir, err := SetupRuntimeDirectory(cfg, os.Getpid()) + require.NoError(t, err) - require.Equal(t, os.TempDir(), filepath.Dir(cfg.RuntimeDir)) - require.True(t, strings.HasPrefix(filepath.Base(cfg.RuntimeDir), "gitaly-")) - require.DirExists(t, cfg.RuntimeDir) + require.DirExists(t, runtimeDir) + require.True(t, strings.HasPrefix(runtimeDir, filepath.Join(os.TempDir(), "gitaly-"))) }) t.Run("non-existent runtime directory", func(t *testing.T) { @@ -1209,7 +1209,8 @@ func TestCfg_RuntimeDir(t *testing.T) { RuntimeDir: "/does/not/exist", } - require.EqualError(t, cfg.setDefaults(), fmt.Sprintf("creating runtime directory: mkdir /does/not/exist/gitaly-%d: no such file or directory", os.Getpid())) + _, err := SetupRuntimeDirectory(cfg, os.Getpid()) + require.EqualError(t, err, fmt.Sprintf("creating runtime directory: mkdir /does/not/exist/gitaly-%d: no such file or directory", os.Getpid())) }) t.Run("existent runtime directory", func(t *testing.T) { @@ -1217,9 +1218,12 @@ func TestCfg_RuntimeDir(t *testing.T) { cfg := Cfg{ RuntimeDir: dir, } - require.NoError(t, cfg.setDefaults()) - require.Equal(t, filepath.Join(dir, fmt.Sprintf("gitaly-%d", os.Getpid())), cfg.RuntimeDir) - require.DirExists(t, cfg.RuntimeDir) + + runtimeDir, err := SetupRuntimeDirectory(cfg, os.Getpid()) + require.NoError(t, err) + + require.Equal(t, filepath.Join(dir, fmt.Sprintf("gitaly-%d", os.Getpid())), runtimeDir) + require.DirExists(t, runtimeDir) }) }) @@ -1238,9 +1242,8 @@ func TestCfg_RuntimeDir(t *testing.T) { runtimeDir: dirPath, }, { - desc: "unset", - runtimeDir: "", - expectedErr: fmt.Errorf("runtime_dir: is not set"), + desc: "unset", + runtimeDir: "", }, { desc: "path doesn't exist", |