Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4790fd5

Browse files
committed
fix: Allow remote state to be used with Terraform
The Terraform Provisioner depended on the statefile contentbeing at a specific path, which disallowed the use of externalstate providers. This fixes it!
1 parentb948f2d commit4790fd5

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

‎provisioner/terraform/provision.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
5353
}
5454
}()
5555
start:=request.GetStart()
56-
statefilePath:=filepath.Join(start.Directory,"terraform.tfstate")
57-
iflen(start.State)>0 {
58-
err:=os.WriteFile(statefilePath,start.State,0600)
59-
iferr!=nil {
60-
returnxerrors.Errorf("write statefile %q: %w",statefilePath,err)
61-
}
62-
}
6356

6457
terraform,err:=tfexec.NewTerraform(start.Directory,t.binaryPath)
6558
iferr!=nil {
@@ -244,14 +237,18 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
244237
errorMessage:=err.Error()
245238
// Terraform can fail and apply and still need to store it's state.
246239
// In this case, we return Complete with an explicit error message.
247-
statefileContent,err:=os.ReadFile(statefilePath)
240+
state,err:=terraform.Show(stream.Context())
241+
iferr!=nil {
242+
returnxerrors.Errorf("show state: %w",err)
243+
}
244+
stateData,err:=json.Marshal(state)
248245
iferr!=nil {
249-
returnxerrors.Errorf("read file %q: %w",statefilePath,err)
246+
returnxerrors.Errorf("marshal state: %w",err)
250247
}
251248
returnstream.Send(&proto.Provision_Response{
252249
Type:&proto.Provision_Response_Complete{
253250
Complete:&proto.Provision_Complete{
254-
State:statefileContent,
251+
State:stateData,
255252
Error:errorMessage,
256253
},
257254
},
@@ -264,7 +261,7 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
264261
ifstart.DryRun {
265262
resp,err=parseTerraformPlan(stream.Context(),terraform,planfilePath)
266263
}else {
267-
resp,err=parseTerraformApply(stream.Context(),terraform,statefilePath)
264+
resp,err=parseTerraformApply(stream.Context(),terraform)
268265
}
269266
iferr!=nil {
270267
returnerr
@@ -358,14 +355,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
358355
},nil
359356
}
360357

361-
funcparseTerraformApply(ctx context.Context,terraform*tfexec.Terraform,statefilePathstring) (*proto.Provision_Response,error) {
362-
statefileContent,err:=os.ReadFile(statefilePath)
358+
funcparseTerraformApply(ctx context.Context,terraform*tfexec.Terraform) (*proto.Provision_Response,error) {
359+
state,err:=terraform.Show(ctx)
363360
iferr!=nil {
364-
returnnil,xerrors.Errorf("read file %q: %w",statefilePath,err)
365-
}
366-
state,err:=terraform.ShowStateFile(ctx,statefilePath)
367-
iferr!=nil {
368-
returnnil,xerrors.Errorf("show state file %q: %w",statefilePath,err)
361+
returnnil,xerrors.Errorf("show state file: %w",err)
369362
}
370363
resources:=make([]*proto.Resource,0)
371364
ifstate.Values!=nil {
@@ -498,6 +491,11 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
498491
}
499492
}
500493

494+
statefileContent,err:=json.Marshal(state)
495+
iferr!=nil {
496+
returnnil,xerrors.Errorf("marshal state: %w",err)
497+
}
498+
501499
return&proto.Provision_Response{
502500
Type:&proto.Provision_Response_Complete{
503501
Complete:&proto.Provision_Complete{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp