Skip to content

Commit d842941

Browse files
committed
feat(tf): remote tmp files
1 parent 2f5e245 commit d842941

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

db_lib/TerraformApp.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package db_lib
22

33
import (
44
"fmt"
5+
log "github.com/sirupsen/logrus"
56
"io"
67
"os"
78
"os/exec"
@@ -22,6 +23,7 @@ type TerraformApp struct {
2223
reader terraformReader // reader
2324
Name string // Name is the name of the terraform binary
2425
PlanHasNoChanges bool // PlanHasNoChanges is true if terraform plan has no changes
26+
backendFilename string // backendFilename is the name of the backend file
2527
}
2628

2729
type terraformReader struct {
@@ -195,21 +197,34 @@ func (t *TerraformApp) selectWorkspace(workspace string, environmentVars []strin
195197
}
196198

197199
func (t *TerraformApp) Clear() {
200+
if t.backendFilename == "" {
201+
return
202+
}
203+
204+
err := os.Remove(path.Join(t.GetFullPath(), t.backendFilename))
205+
if os.IsNotExist(err) {
206+
err = nil
207+
}
208+
if err != nil {
209+
log.WithError(err).WithFields(log.Fields{
210+
"context": "terraform",
211+
"task_id": t.Template.ID,
212+
}).Warn("Unable to remove backend file")
213+
}
198214
}
199215

200216
func (t *TerraformApp) InstallRequirements(environmentVars []string, tplParams any, params any) (err error) {
201217

202218
tpl := tplParams.(*db.TerraformTemplateParams)
203219
p := params.(*db.TerraformTaskParams)
204220

205-
backendFilename := "backend.tf"
206-
if tpl.BackendFilename != "" {
207-
backendFilename = tpl.BackendFilename
208-
}
209-
210-
backendFile := path.Join(t.GetFullPath(), backendFilename)
211-
212221
if tpl.OverrideBackend {
222+
t.backendFilename = "backend.tf"
223+
if tpl.BackendFilename != "" {
224+
t.backendFilename = tpl.BackendFilename
225+
}
226+
227+
backendFile := path.Join(t.GetFullPath(), t.backendFilename)
213228
err = os.WriteFile(backendFile, []byte("terraform {\n backend \"http\" {\n }\n}\n"), 0644)
214229
if err != nil {
215230
return
@@ -220,16 +235,6 @@ func (t *TerraformApp) InstallRequirements(environmentVars []string, tplParams a
220235
return
221236
}
222237

223-
//if tpl.OverrideBackend {
224-
// err = os.Remove(backendFile)
225-
// if os.IsNotExist(err) {
226-
// err = nil
227-
// }
228-
// if err != nil {
229-
// return
230-
// }
231-
//}
232-
233238
workspace := "default"
234239

235240
if t.Inventory.Inventory != "" {

0 commit comments

Comments
 (0)