From 9e4c0ff2adb2af2d590fc4d954d98d89346b16f3 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 25 May 2017 09:29:51 -0400 Subject: [PATCH] call PersistState immediately when cancelling When the backend operation is cancelled, immediately call PersistState. The is a high likelihood that the user is going to terminate the process early if the provider doesn't return in a timely manner, so persist as much state as possible. --- backend/local/backend_apply.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/backend/local/backend_apply.go b/backend/local/backend_apply.go index 27883d5b2..269095940 100644 --- a/backend/local/backend_apply.go +++ b/backend/local/backend_apply.go @@ -126,6 +126,17 @@ func (b *Local) opApply( b.CLI.Output("stopping apply operation...") } + // try to force a PersistState just in case the process is terminated + // before we can complete. + if err := opState.PersistState(); err != nil { + // We can't error out from here, but warn the user if there was an error. + // If this isn't transient, we will catch it again below, and + // attempt to save the state another way. + if b.CLI != nil { + b.CLI.Error(fmt.Sprintf(earlyStateWriteErrorFmt, err)) + } + } + // Stop execution go tfCtx.Stop() @@ -270,3 +281,10 @@ importing each resource using its id from the target system. This is a serious bug in Terraform and should be reported. ` + +const earlyStateWriteErrorFmt = `Error saving current state: %s + +Terraform encountered an error attempting to save the state before canceling +the current operation. Once the operation is complete another attempt will be +made to save the final state. +`