Skip to content

Commit

Permalink
support provider log output (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
markliby committed Feb 2, 2023
1 parent a5397fd commit e4db076
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions pkg/engine/runtime/terraform/tfops/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ import (

"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/util/kfile"
)

const (
HCLMAINFILE = "main.tf.json"
HCLLOCKFILE = ".terraform.hcl.lock"
TFSTATEFILE = "terraform.tfstate"
HCLMAINFILE = "main.tf.json"
HCLLOCKFILE = ".terraform.hcl.lock"
TFSTATEFILE = "terraform.tfstate"
envLog = "TF_LOG"
tfDebugLOG = "DEBUG"
envLogFile = "TF_LOG_PATH"
tfProviderPrefix = "terraform-provider"
)

var envTFLog = fmt.Sprintf("%s=%s", envLog, tfDebugLOG)

type WorkSpace struct {
resource *models.Resource
fs afero.Afero
Expand Down Expand Up @@ -143,6 +150,7 @@ func (w *WorkSpace) InitWorkSpace(ctx context.Context) error {
chdir := fmt.Sprintf("-chdir=%s", w.tfCacheDir)
cmd := exec.CommandContext(ctx, "terraform", chdir, "init")
cmd.Dir = w.stackDir
cmd.Env = append(os.Environ(), envTFLog, w.getEnvProviderLogPath())
_, err := cmd.Output()
if e, ok := err.(*exec.ExitError); ok {
return errors.New(string(e.Stderr))
Expand All @@ -160,6 +168,7 @@ func (w *WorkSpace) Apply(ctx context.Context) (*TFState, error) {

cmd := exec.CommandContext(ctx, "terraform", chdir, "apply", "-auto-approve", "-json", "-lock=false")
cmd.Dir = w.stackDir
cmd.Env = append(os.Environ(), envTFLog, w.getEnvProviderLogPath())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, TFError(out)
Expand Down Expand Up @@ -204,6 +213,7 @@ func (w *WorkSpace) RefreshOnly(ctx context.Context) (*TFState, error) {
}
cmd := exec.CommandContext(ctx, "terraform", chdir, "apply", "-auto-approve", "-json", "--refresh-only", "-lock=false")
cmd.Dir = w.stackDir
cmd.Env = append(os.Environ(), envTFLog, w.getEnvProviderLogPath())
out, err := cmd.CombinedOutput()
if err != nil {
return nil, TFError(out)
Expand All @@ -220,6 +230,7 @@ func (w *WorkSpace) Destroy(ctx context.Context) error {
chdir := fmt.Sprintf("-chdir=%s", w.tfCacheDir)
cmd := exec.CommandContext(ctx, "terraform", chdir, "destroy", "-auto-approve")
cmd.Dir = w.stackDir
cmd.Env = append(os.Environ(), envTFLog, w.getEnvProviderLogPath())
out, err := cmd.CombinedOutput()
if err != nil {
return TFError(out)
Expand Down Expand Up @@ -295,6 +306,7 @@ func (w *WorkSpace) CleanAndInitWorkspace(ctx context.Context, chdir string) err
func (w *WorkSpace) checkHashUpdate(ctx context.Context, chdir string) bool {
cmd := exec.CommandContext(ctx, "terraform", chdir, "providers", "lock")
cmd.Dir = w.stackDir
cmd.Env = append(os.Environ(), envTFLog, w.getEnvProviderLogPath())
output, _ := cmd.Output()
return strings.Contains(string(output), "Terraform has updated the lock file")
}
Expand All @@ -307,3 +319,16 @@ func (w *WorkSpace) checkVersionUpdate(ctx context.Context) (bool, error) {
}
return providerAddr != w.resource.Extensions["provider"].(string), nil
}

// getProviderLogPath returns the provider log path environmental variable,
// the environmental variables that determine the provider log go to a file.
func (w *WorkSpace) getEnvProviderLogPath() string {
kusionDataDir, _ := kfile.KusionDataFolder()
if v := os.Getenv("LOG_DIR"); v != "" {
kusionDataDir = v
}
provider := strings.Split(w.resource.Extensions["provider"].(string), "/")
providerLogPath := filepath.Join(kusionDataDir, "logs", fmt.Sprintf("%s-%s.log", tfProviderPrefix, provider[len(provider)-2]))
envTFLogPath := fmt.Sprintf("%s=%s", envLogFile, providerLogPath)
return envTFLogPath
}

0 comments on commit e4db076

Please sign in to comment.