/
install.go
70 lines (57 loc) · 1.52 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package opentofu
import (
"context"
"encoding/json"
"fmt"
"get.porter.sh/porter/pkg/exec/builder"
)
// defaultOpentofuVarFilename is the default name for terrafrom tfvars json file
const defaultOpentofuVarsFilename = "terraform.tfvars.json"
// Install runs a tofu apply
func (m *Mixin) Install(ctx context.Context) error {
action, err := m.loadAction(ctx)
if err != nil {
return err
}
step := action.Steps[0]
err = m.commandPreRun(ctx, &step)
if err != nil {
return err
}
// Update step fields that exec/builder works with
step.Arguments = []string{"apply"}
// Always run in non-interactive mode
step.Flags = append(step.Flags, builder.NewFlag("auto-approve"))
step.Flags = append(step.Flags, builder.NewFlag("input=false"))
vbs, err := json.Marshal(step.Vars)
if err != nil {
return err
}
// Only create a tf var file for install
if !step.DisableVarFile && action.Name == "install" {
vf, err := m.FileSystem.Create(defaultOpentofuVarsFilename)
if err != nil {
return err
}
defer vf.Close()
// If the vars block is empty, set vbs to an empty JSON object
// to prevent tofu from erroring out
if len(step.Vars) == 0 {
vbs = []byte("{}")
}
_, err = vf.Write(vbs)
if err != nil {
return err
}
if m.DebugMode {
fmt.Fprintf(m.Err, "DEBUG: TF var file created:\n%s\n", string(vbs))
}
}
applyVarsToStepFlags(&step)
action.Steps[0] = step
_, err = builder.ExecuteSingleStepAction(ctx, m.RuntimeConfig, action)
if err != nil {
return err
}
return m.handleOutputs(ctx, step.Outputs)
}