Skip to content

Commit fb37b23

Browse files
committed
chore: improve terraform logging
1 parent 856f178 commit fb37b23

File tree

3 files changed

+183
-26
lines changed

3 files changed

+183
-26
lines changed

main.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"os/signal"
1111
"path/filepath"
12+
"strings"
1213
"syscall"
1314

1415
"github.com/avast/retry-go"
@@ -29,15 +30,24 @@ import (
2930

3031
var (
3132
logger = hclog.New(&hclog.LoggerOptions{
32-
Name: "eggpack-terraform-plugin",
33-
Level: hclog.Info,
33+
Name: "eggpack-terraform-plugin",
34+
Level: hclog.Info,
35+
JSONFormat: true,
3436
})
3537
cliConfig = &config.TerraformPluginConfig{
3638
AgentAddress: ":4300",
3739
ConfigPath: "test/main",
40+
LogLevel: config.TerraformPluginConfig_LOG_LEVEL_INFO,
3841
}
3942
)
4043

44+
func tfLogStr() string {
45+
if cliConfig.LogAsJson {
46+
return "TF_LOG=JSON"
47+
}
48+
return "TF_LOG=" + strings.TrimPrefix(cliConfig.LogLevel.String(), "LOG_LEVEL_")
49+
}
50+
4151
func runTerraform(ctx context.Context, key string, tf *dynamic.Message) error {
4252
root, err := filepath.Abs(cliConfig.TerraformRoot)
4353
if err != nil {
@@ -58,13 +68,19 @@ func runTerraform(ctx context.Context, key string, tf *dynamic.Message) error {
5868
return err
5969
}
6070
l.Info("running terraform init")
61-
cmd := exec.Command("terraform", "-chdir="+workDir, "init")
71+
cmd := exec.CommandContext(ctx, "terraform", "-chdir="+workDir, "init")
72+
cmd.Env = os.Environ()
73+
cmd.Env = append(cmd.Env, tfLogStr())
74+
cmd.Stderr = os.Stderr
6275
if err = cmd.Run(); err != nil {
6376
l.Error("failed to run terraform init", "error", err)
6477
return err
6578
}
6679
l.Info("running terraform apply")
67-
cmd = exec.Command("terraform", "-chdir="+workDir, "apply", "-auto-approve")
80+
cmd = exec.CommandContext(ctx, "terraform", "-chdir="+workDir, "apply", "-auto-approve")
81+
cmd.Env = os.Environ()
82+
cmd.Env = append(cmd.Env, tfLogStr())
83+
cmd.Stderr = os.Stderr
6884
if err = cmd.Run(); err != nil {
6985
l.Error("failed to run terraform apply", "error", err)
7086
return err
@@ -87,6 +103,11 @@ func main() {
87103
if flagset.Parse(os.Args[1:]) != nil {
88104
logger.Error("failed to parse flag data")
89105
}
106+
logger = hclog.New(&hclog.LoggerOptions{
107+
Name: "eggpack-terraform-plugin",
108+
Level: hclog.Level(cliConfig.LogLevel),
109+
JSONFormat: cliConfig.LogAsJson,
110+
})
90111

91112
parser := &protoparse.Parser{ImportPaths: []string{"src", ""}}
92113
descriptors, err := parser.ParseFiles("terraform/v1/terraform.proto")

proto/protoconf_terraform/config/v1/config.pb.go

Lines changed: 130 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/protoconf_terraform/config/v1/config.proto

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@ message TerraformPluginConfig {
88
string agent_address = 1 [json_name = "agent-address"];
99
string config_path = 2 [json_name = "config-path"];
1010
string terraform_root = 3;
11+
LogLevel log_level = 4;
12+
bool log_as_json = 5;
13+
14+
enum LogLevel {
15+
// NoLevel is a special level used to indicate that no level has been
16+
// set and allow for a default to be used.
17+
LOG_LEVEL_UNSPECIFIED = 0;
18+
19+
// Trace is the most verbose level. Intended to be used for the tracing
20+
// of actions in code, such as function enters/exits, etc.
21+
LOG_LEVEL_TRACE = 1;
22+
23+
// Debug information for programmer low-level analysis.
24+
LOG_LEVEL_DEBUG = 2;
25+
26+
// Info information about steady state operations.
27+
LOG_LEVEL_INFO = 3;
28+
29+
// Warn information about rare but handled events.
30+
LOG_LEVEL_WARN = 4;
31+
32+
// Error information about unrecoverable events.
33+
LOG_LEVEL_ERROR = 5;
34+
35+
// Off disables all logging output.
36+
LOG_LEVEL_OFF = 6;
37+
38+
}
1139
}
1240

1341
message SubscriptionConfig {

0 commit comments

Comments
 (0)