hcp: wrap completeBuild to mark as failed on error

When a build cannot be completed without errors, the build state was
left as running, unless the build explicitly failed, which meant that
HCP Packer would be responsible for changing the status after the
heartbeats for the build stopped being sent for two 5m periods.

This commit changes this behaviour, by explicitly marking the build as
failed if something did not work while trying to complete a build on HCP
Packer, even if the local Packer core build succeeded before that.
poc/with-api-imported
Lucas Bajolet 2 years ago
parent 9d4b6836db
commit 255874c021

@ -639,17 +639,6 @@ func (bucket *Bucket) completeBuild(
packerSDKArtifacts []packerSDK.Artifact,
buildErr error,
) ([]packerSDK.Artifact, error) {
doneCh, ok := bucket.RunningBuilds[buildName]
if !ok {
log.Print("[ERROR] done build does not have an entry in the heartbeat table, state will be inconsistent.")
} else {
log.Printf("[TRACE] signal stopping heartbeats")
// Stop heartbeating
doneCh <- struct{}{}
log.Printf("[TRACE] stopped heartbeats")
}
if buildErr != nil {
status := hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDFAILED
if ctx.Err() != nil {
@ -662,6 +651,33 @@ func (bucket *Bucket) completeBuild(
return packerSDKArtifacts, fmt.Errorf("build failed, not uploading artifacts")
}
artifacts, err := bucket.doCompleteBuild(ctx, buildName, packerSDKArtifacts, buildErr)
if err != nil {
err := bucket.UpdateBuildStatus(ctx, buildName, hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDFAILED)
if err != nil {
log.Printf("[ERROR] failed to update build %q status to FAILED: %s", buildName, err)
}
}
return artifacts, err
}
func (bucket *Bucket) doCompleteBuild(
ctx context.Context,
buildName string,
packerSDKArtifacts []packerSDK.Artifact,
buildErr error,
) ([]packerSDK.Artifact, error) {
doneCh, ok := bucket.RunningBuilds[buildName]
if !ok {
log.Print("[ERROR] done build does not have an entry in the heartbeat table, state will be inconsistent.")
} else {
log.Printf("[TRACE] signal stopping heartbeats")
// Stop heartbeating
doneCh <- struct{}{}
log.Printf("[TRACE] stopped heartbeats")
}
for _, art := range packerSDKArtifacts {
var sdkImages []packerSDKRegistry.Image
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{

Loading…
Cancel
Save