Skip to content

Commit

Permalink
Fix invalide hcl file name (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Mar 2, 2023
1 parent 3cfa054 commit 7ede4e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/engine/runtime/terraform/terraform_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequ
return &runtime.ApplyResponse{Resource: nil, Status: status.NewErrorStatus(err)}
}

_, err := os.Stat(filepath.Join(tfCacheDir, tfops.HCLLOCKFILE))
_, err := os.Stat(filepath.Join(tfCacheDir, tfops.LockHCLFile))
if err != nil {
if os.IsNotExist(err) {
if err := t.WorkSpace.InitWorkSpace(ctx); err != nil {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (t *TerraformRuntime) Read(ctx context.Context, request *runtime.ReadReques
if err := t.WorkSpace.WriteHCL(); err != nil {
return &runtime.ReadResponse{Resource: nil, Status: status.NewErrorStatus(err)}
}
_, err := os.Stat(filepath.Join(tfCacheDir, tfops.HCLLOCKFILE))
_, err := os.Stat(filepath.Join(tfCacheDir, tfops.LockHCLFile))
if err != nil {
if os.IsNotExist(err) {
if err := t.WorkSpace.InitWorkSpace(ctx); err != nil {
Expand Down
48 changes: 22 additions & 26 deletions pkg/engine/runtime/terraform/tfops/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
)

const (
HCLMAINFILE = "main.tf.json"
HCLLOCKFILE = ".terraform.hcl.lock"
TFSTATEFILE = "terraform.tfstate"
MainTFFile = "main.tf.json"
LockHCLFile = ".terraform.lock.hcl"
TFStateFile = "terraform.tfstate"
envLog = "TF_LOG"
tfDebugLOG = "DEBUG"
envLogFile = "TF_LOG_PATH"
Expand Down Expand Up @@ -106,7 +106,7 @@ func (w *WorkSpace) WriteHCL() error {
return err
}
}
err = w.fs.WriteFile(filepath.Join(w.tfCacheDir, HCLMAINFILE), hclMain, 0o600)
err = w.fs.WriteFile(filepath.Join(w.tfCacheDir, MainTFFile), hclMain, 0o600)
if err != nil {
return fmt.Errorf("write hcl main.tf.json error: %v", err)
}
Expand Down Expand Up @@ -138,9 +138,9 @@ func (w *WorkSpace) WriteTFState(priorState *models.Resource) error {
return fmt.Errorf("marshal hcl state error: %v", err)
}

err = w.fs.WriteFile(filepath.Join(w.tfCacheDir, TFSTATEFILE), hclState, os.ModePerm)
err = w.fs.WriteFile(filepath.Join(w.tfCacheDir, TFStateFile), hclState, os.ModePerm)
if err != nil {
return fmt.Errorf("write hcl error: %v", err)
return fmt.Errorf("write hcl error: %v", err)
}
return nil
}
Expand All @@ -161,7 +161,7 @@ func (w *WorkSpace) InitWorkSpace(ctx context.Context) error {
// Apply with the terraform cli apply command
func (w *WorkSpace) Apply(ctx context.Context) (*TFState, error) {
chdir := fmt.Sprintf("-chdir=%s", w.tfCacheDir)
err := w.CleanAndInitWorkspace(ctx, chdir)
err := w.CleanAndInitWorkspace(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (w *WorkSpace) Read(ctx context.Context) (*TFState, error) {
// Refresh Sync Terraform State
func (w *WorkSpace) RefreshOnly(ctx context.Context) (*TFState, error) {
chdir := fmt.Sprintf("-chdir=%s", w.tfCacheDir)
err := w.CleanAndInitWorkspace(ctx, chdir)
err := w.CleanAndInitWorkspace(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (w *WorkSpace) Destroy(ctx context.Context) error {
// eg. registry.terraform.io/hashicorp/local/2.2.3
func (w *WorkSpace) GetProvider() (string, error) {
parser := hclparse.NewParser()
hclFile, diags := parser.ParseHCLFile(filepath.Join(w.tfCacheDir, ".terraform.lock.hcl"))
hclFile, diags := parser.ParseHCLFile(filepath.Join(w.tfCacheDir, LockHCLFile))
if diags != nil {
return "", errors.New(diags.Error())
}
Expand Down Expand Up @@ -282,37 +282,33 @@ func (w *WorkSpace) GetProvider() (string, error) {

// CleanAndInitWorkspace will clean up the provider cache and reinitialize the workspace
// when the provider version or hash is updated.
func (w *WorkSpace) CleanAndInitWorkspace(ctx context.Context, chdir string) error {
isHashUpdate := w.checkHashUpdate(ctx, chdir)
isVersionUpdate, err := w.checkVersionUpdate(ctx)
func (w *WorkSpace) CleanAndInitWorkspace(ctx context.Context) error {
isVersionUpdate, err := w.checkVersionUpdate()
if err != nil {
return fmt.Errorf("check provider version failed: %v", err)
}

// If the provider hash or version changes, delete the tf cache and reinitialize.
if isHashUpdate || isVersionUpdate {
if isVersionUpdate {
log.Info("provider hash or version change.")
os.Remove(filepath.Join(w.tfCacheDir, ".terraform.lock.hcl"))
os.Remove(filepath.Join(w.tfCacheDir, ".terraform"))
err := w.InitWorkSpace(ctx)
err = os.Remove(filepath.Join(w.tfCacheDir, LockHCLFile))
if err != nil {
return err
}
err = os.Remove(filepath.Join(w.tfCacheDir, ".terraform"))
if err != nil {
return err
}
err = w.InitWorkSpace(ctx)
if err != nil {
return fmt.Errorf("init terraform workspace failed: %v", err)
}
}
return nil
}

// checkHashUpdate checks whether the provider hash has changed, and returns true if changed
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")
}

// checkVersionUpdate checks whether the provider version has changed, and returns true if changed
func (w *WorkSpace) checkVersionUpdate(ctx context.Context) (bool, error) {
func (w *WorkSpace) checkVersionUpdate() (bool, error) {
providerAddr, err := w.GetProvider()
if err != nil {
return false, fmt.Errorf("provider get version failed: %v", err)
Expand Down

0 comments on commit 7ede4e1

Please sign in to comment.