@@ -2,6 +2,7 @@ package db_lib
2
2
3
3
import (
4
4
"fmt"
5
+ log "github.com/sirupsen/logrus"
5
6
"io"
6
7
"os"
7
8
"os/exec"
@@ -22,6 +23,7 @@ type TerraformApp struct {
22
23
reader terraformReader // reader
23
24
Name string // Name is the name of the terraform binary
24
25
PlanHasNoChanges bool // PlanHasNoChanges is true if terraform plan has no changes
26
+ backendFilename string // backendFilename is the name of the backend file
25
27
}
26
28
27
29
type terraformReader struct {
@@ -195,21 +197,34 @@ func (t *TerraformApp) selectWorkspace(workspace string, environmentVars []strin
195
197
}
196
198
197
199
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
+ }
198
214
}
199
215
200
216
func (t * TerraformApp ) InstallRequirements (environmentVars []string , tplParams any , params any ) (err error ) {
201
217
202
218
tpl := tplParams .(* db.TerraformTemplateParams )
203
219
p := params .(* db.TerraformTaskParams )
204
220
205
- backendFilename := "backend.tf"
206
- if tpl .BackendFilename != "" {
207
- backendFilename = tpl .BackendFilename
208
- }
209
-
210
- backendFile := path .Join (t .GetFullPath (), backendFilename )
211
-
212
221
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 )
213
228
err = os .WriteFile (backendFile , []byte ("terraform {\n backend \" http\" {\n }\n }\n " ), 0644 )
214
229
if err != nil {
215
230
return
@@ -220,16 +235,6 @@ func (t *TerraformApp) InstallRequirements(environmentVars []string, tplParams a
220
235
return
221
236
}
222
237
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
-
233
238
workspace := "default"
234
239
235
240
if t .Inventory .Inventory != "" {
0 commit comments