@@ -53,13 +53,6 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
53
53
}
54
54
}()
55
55
start := request .GetStart ()
56
- statefilePath := filepath .Join (start .Directory , "terraform.tfstate" )
57
- if len (start .State ) > 0 {
58
- err := os .WriteFile (statefilePath , start .State , 0600 )
59
- if err != nil {
60
- return xerrors .Errorf ("write statefile %q: %w" , statefilePath , err )
61
- }
62
- }
63
56
64
57
terraform , err := tfexec .NewTerraform (start .Directory , t .binaryPath )
65
58
if err != nil {
@@ -244,14 +237,18 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
244
237
errorMessage := err .Error ()
245
238
// Terraform can fail and apply and still need to store it's state.
246
239
// 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
+ if err != nil {
242
+ return xerrors .Errorf ("show state: %w" , err )
243
+ }
244
+ stateData , err := json .Marshal (state )
248
245
if err != nil {
249
- return xerrors .Errorf ("read file %q : %w" , statefilePath , err )
246
+ return xerrors .Errorf ("marshal state : %w" , err )
250
247
}
251
248
return stream .Send (& proto.Provision_Response {
252
249
Type : & proto.Provision_Response_Complete {
253
250
Complete : & proto.Provision_Complete {
254
- State : statefileContent ,
251
+ State : stateData ,
255
252
Error : errorMessage ,
256
253
},
257
254
},
@@ -264,7 +261,7 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
264
261
if start .DryRun {
265
262
resp , err = parseTerraformPlan (stream .Context (), terraform , planfilePath )
266
263
} else {
267
- resp , err = parseTerraformApply (stream .Context (), terraform , statefilePath )
264
+ resp , err = parseTerraformApply (stream .Context (), terraform )
268
265
}
269
266
if err != nil {
270
267
return err
@@ -358,14 +355,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
358
355
}, nil
359
356
}
360
357
361
- func parseTerraformApply (ctx context.Context , terraform * tfexec.Terraform , statefilePath string ) (* proto.Provision_Response , error ) {
362
- statefileContent , err := os . ReadFile ( statefilePath )
358
+ func parseTerraformApply (ctx context.Context , terraform * tfexec.Terraform ) (* proto.Provision_Response , error ) {
359
+ state , err := terraform . Show ( ctx )
363
360
if err != nil {
364
- return nil , xerrors .Errorf ("read file %q: %w" , statefilePath , err )
365
- }
366
- state , err := terraform .ShowStateFile (ctx , statefilePath )
367
- if err != nil {
368
- return nil , xerrors .Errorf ("show state file %q: %w" , statefilePath , err )
361
+ return nil , xerrors .Errorf ("show state file: %w" , err )
369
362
}
370
363
resources := make ([]* proto.Resource , 0 )
371
364
if state .Values != nil {
@@ -498,6 +491,11 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
498
491
}
499
492
}
500
493
494
+ statefileContent , err := json .Marshal (state )
495
+ if err != nil {
496
+ return nil , xerrors .Errorf ("marshal state: %w" , err )
497
+ }
498
+
501
499
return & proto.Provision_Response {
502
500
Type : & proto.Provision_Response_Complete {
503
501
Complete : & proto.Provision_Complete {
0 commit comments