diff --git a/README.md b/README.md index 3a81e622..8f57501c 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,8 @@ repositories: - `--git-ssh-known-hosts-file (GIT_SSH_KNOWN_HOSTS_FILE)` - (default: `/etc/git-secret/known_hosts`) The local path to the known hosts file used to setup GIT_SSH_COMMAND env. - `--git-verify-known-hosts (GIT_VERIFY_KNOWN_HOSTS)` - (default: `true`) The local path to the known hosts file used to setup GIT_SSH_COMMAND env. - `--controller-runtime-env (CONTROLLER_RUNTIME_ENV)` - (default: `""`) The comma separated list of ENVs which will be passed from controller to all terraform run process. The envs should be set on the controller. +- `--cleanup-temp-dir` - (default: `false`) If set, the OS temporary directory will be removed and re-created. This can help removing redundant terraform binaries and avoiding temp directory growing in size with every restart. + --- - `--module-label-selector (MODULE_LABEL_SELECTOR)` - (default: `""`) If present controller will only watch and process modules with this label. diff --git a/main.go b/main.go index 129e9700..edaf30c9 100644 --- a/main.go +++ b/main.go @@ -151,7 +151,12 @@ var ( Usage: "The comma separated list of ENVs which will be passed from controller to its managed modules during terraform run. " + "The values should be set on the controller.", }, - + &cli.BoolFlag{ + Name: "cleanup-temp-dir", + Value: false, + Usage: "If set, the OS temporary directory will be removed and re-created. This can help removing redundant terraform" + + "binaries and avoiding temp directory growing in size with every restart.", + }, &cli.StringFlag{ Name: "module-label-selector", EnvVars: []string{"MODULE_LABEL_SELECTOR"}, @@ -368,6 +373,11 @@ func setupGlobalEnv(c *cli.Context) { globalRunEnv[env] = os.Getenv(env) } + // Cleanup temp directory if the corresponding flag is set + if c.Bool("cleanup-temp-dir") { + cleanupTmpDir() + } + if c.Bool("set-git-ssh-command-global-env") { cmdStr, err := git.GitSSHCommand( c.String("git-ssh-key-file"), c.String("git-ssh-known-hosts-file"), c.Bool("git-verify-known-hosts"), @@ -411,6 +421,20 @@ preferences: {} } +func cleanupTmpDir() { + tmpDir := os.TempDir() + + err := os.RemoveAll(tmpDir) + if err != nil { + logger.Error("Error removing temporary directory:", err) + } + + err = os.Mkdir(tmpDir, 0777) + if err != nil { + logger.Error("Error creating temporary directory:", err) + } +} + func main() { app := &cli.App{ Name: "terraform-applier",