Skip to content

Commit

Permalink
Move AddMetadataToBuild to bucket methods
Browse files Browse the repository at this point in the history
  • Loading branch information
devashish-patel committed Apr 3, 2024
1 parent 6a3bcfb commit a8cb35c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
28 changes: 4 additions & 24 deletions internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,8 @@ func (h *HCLRegistry) PopulateVersion(ctx context.Context) error {

// StartBuild is invoked when one build for the configuration is starting to be processed
func (h *HCLRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) error {
name := build.Name()
cb, ok := build.(*packer.CoreBuild)
if ok {
name = cb.Type
}

metadata := cb.GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return err
}
return h.bucket.startBuild(ctx, name)
coreBuild := build.(*packer.CoreBuild)
return h.bucket.startBuild(ctx, coreBuild)
}

// CompleteBuild is invoked when one build for the configuration has finished
Expand All @@ -86,18 +76,8 @@ func (h *HCLRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
buildName := build.Name()
cb, ok := build.(*packer.CoreBuild)
if ok {
buildName = cb.Type
}

buildMetadata := cb.GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata)
if err != nil {
return nil, err
}
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
coreBuild := build.(*packer.CoreBuild)
return h.bucket.completeBuild(ctx, coreBuild, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
19 changes: 4 additions & 15 deletions internal/hcp/registry/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ func (h *JSONRegistry) PopulateVersion(ctx context.Context) error {

// StartBuild is invoked when one build for the configuration is starting to be processed
func (h *JSONRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) error {
name := build.Name()

metadata := build.(*packer.CoreBuild).GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, name, metadata)
if err != nil {
return err
}
return h.bucket.startBuild(ctx, name)
coreBuild := build.(*packer.CoreBuild)
return h.bucket.startBuild(ctx, coreBuild)
}

// CompleteBuild is invoked when one build for the configuration has finished
Expand All @@ -100,13 +94,8 @@ func (h *JSONRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
buildName := build.Name()
buildMetadata := build.(*packer.CoreBuild).GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata)
if err != nil {
return nil, err
}
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
coreBuild := build.(*packer.CoreBuild)
return h.bucket.completeBuild(ctx, coreBuild, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
23 changes: 20 additions & 3 deletions internal/hcp/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/packer/hcl2template"
hcpPackerAPI "github.com/hashicorp/packer/internal/hcp/api"
"github.com/hashicorp/packer/internal/hcp/env"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/mapstructure"
"google.golang.org/grpc/codes"
)
Expand Down Expand Up @@ -554,14 +555,22 @@ func (bucket *Bucket) HeartbeatBuild(ctx context.Context, build string) (func(),
}, nil
}

func (bucket *Bucket) startBuild(ctx context.Context, buildName string) error {
func (bucket *Bucket) startBuild(ctx context.Context, build *packer.CoreBuild) error {
buildName := build.Name()

metadata := build.GetMetadata()
err := bucket.Version.AddMetadataToBuild(ctx, buildName, metadata)
if err != nil {
return err
}

if !bucket.IsExpectingBuildForComponent(buildName) {
return &ErrBuildAlreadyDone{
Message: "build is already done",
}
}

err := bucket.UpdateBuildStatus(ctx, buildName, hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDRUNNING)
err = bucket.UpdateBuildStatus(ctx, buildName, hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDRUNNING)
if err != nil {
return fmt.Errorf("failed to update HCP Packer Build status for %q: %s", buildName, err)
}
Expand Down Expand Up @@ -604,10 +613,18 @@ type NotAHCPArtifactError struct {

func (bucket *Bucket) completeBuild(
ctx context.Context,
buildName string,
coreBuild *packer.CoreBuild,
packerSDKArtifacts []packerSDK.Artifact,
buildErr error,
) ([]packerSDK.Artifact, error) {
buildName := coreBuild.Name()

buildMetadata := coreBuild.GetMetadata()
err := bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata)
if err != nil {
return nil, err
}

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.")
Expand Down

0 comments on commit a8cb35c

Please sign in to comment.