Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phase 1: Packer Build Metadata #12866

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hcl2template/types.build.hcp_packer_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) {
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
BuilderType: "virtualbox-iso",
},
},
false,
Expand Down Expand Up @@ -110,6 +111,7 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) {
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
BuilderType: "virtualbox-iso",
},
},
false,
Expand Down Expand Up @@ -241,6 +243,7 @@ func Test_ParseHCPPackerRegistryBlock(t *testing.T) {
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
BuilderType: "null",
},
},
false,
Expand Down
34 changes: 21 additions & 13 deletions hcl2template/types.build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func TestParse_build(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
Expand Down Expand Up @@ -317,6 +318,7 @@ func TestParse_build(t *testing.T) {
},
&packer.CoreBuild{
Type: "amazon-ebs.aws-ubuntu-16.04",
BuilderType: "amazon-ebs",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
Expand Down Expand Up @@ -397,9 +399,10 @@ func TestParse_build(t *testing.T) {
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: emptyMockBuilder,
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{
{
PType: "shell",
Expand Down Expand Up @@ -427,9 +430,10 @@ func TestParse_build(t *testing.T) {
PostProcessors: [][]packer.CoreBuildPostProcessor{},
},
&packer.CoreBuild{
Type: "amazon-ebs.aws-ubuntu-16.04",
Prepared: true,
Builder: emptyMockBuilder,
Type: "amazon-ebs.aws-ubuntu-16.04",
BuilderType: "amazon-ebs",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{
{
PType: "file",
Expand Down Expand Up @@ -486,9 +490,10 @@ func TestParse_build(t *testing.T) {
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: emptyMockBuilder,
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{
{
PType: "shell",
Expand Down Expand Up @@ -550,6 +555,7 @@ func TestParse_build(t *testing.T) {
&packer.CoreBuild{
BuildName: "build-name",
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
Expand Down Expand Up @@ -604,6 +610,7 @@ func TestParse_build(t *testing.T) {
&packer.CoreBuild{
BuildName: "test-build",
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
Expand Down Expand Up @@ -659,10 +666,11 @@ func TestParse_build(t *testing.T) {
false, false,
[]packersdk.Build{
&packer.CoreBuild{
BuildName: "build-name-test",
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: emptyMockBuilder,
BuildName: "build-name-test",
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{
{
PName: "build-name-test",
Expand Down
2 changes: 2 additions & 0 deletions hcl2template/types.datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestParse_datasource(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down Expand Up @@ -134,6 +135,7 @@ func TestParse_datasource(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down
1 change: 1 addition & 0 deletions hcl2template/types.packer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packersdk.Bu

decoded, _ := decodeHCL2Spec(srcUsage.Body, cfg.EvalContext(BuildContext, nil), builder)
pcb.HCLConfig = decoded
pcb.BuilderType = srcUsage.Type

// If the builder has provided a list of to-be-generated variables that
// should be made accessible to provisioners, pass that list into
Expand Down
10 changes: 6 additions & 4 deletions hcl2template/types.packer_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ func TestParser_complete(t *testing.T) {
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Type: "virtualbox-iso.ubuntu-1204",
BuilderType: "virtualbox-iso",
Prepared: true,
Builder: &MockBuilder{
Config: MockConfig{
NestedMockConfig: NestedMockConfig{
Expand Down Expand Up @@ -319,8 +320,9 @@ func TestParser_complete(t *testing.T) {
},
},
&packer.CoreBuild{
Type: "amazon-ebs.ubuntu-1604",
Prepared: true,
Type: "amazon-ebs.ubuntu-1604",
BuilderType: "amazon-ebs",
Prepared: true,
Builder: &MockBuilder{
Config: MockConfig{
NestedMockConfig: NestedMockConfig{
Expand Down
1 change: 1 addition & 0 deletions hcl2template/types.source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestParse_source(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down
13 changes: 10 additions & 3 deletions hcl2template/types.variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestParse_variables(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down Expand Up @@ -293,6 +294,7 @@ func TestParse_variables(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down Expand Up @@ -379,6 +381,7 @@ func TestParse_variables(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down Expand Up @@ -442,6 +445,7 @@ func TestParse_variables(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down Expand Up @@ -483,6 +487,7 @@ func TestParse_variables(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down Expand Up @@ -538,9 +543,10 @@ func TestParse_variables(t *testing.T) {
},
false, false,
[]packersdk.Build{&packer.CoreBuild{
Type: "null.null-builder",
Prepared: true,
Builder: &null.Builder{},
Type: "null.null-builder",
BuilderType: "null",
Prepared: true,
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{
{
PType: "shell",
Expand Down Expand Up @@ -627,6 +633,7 @@ func TestParse_variables(t *testing.T) {
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
BuilderType: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Expand Down
2 changes: 2 additions & 0 deletions internal/hcp/api/service_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (c *Client) UpdateBuild(
buildLabels map[string]string,
buildStatus hcpPackerModels.HashicorpCloudPacker20230101BuildStatus,
artifacts []*hcpPackerModels.HashicorpCloudPacker20230101ArtifactCreateBody,
metadata *hcpPackerModels.HashicorpCloudPacker20230101BuildMetadata,
) (string, error) {

params := hcpPackerAPI.NewPackerServiceUpdateBuildParamsWithContext(ctx)
Expand All @@ -76,6 +77,7 @@ func (c *Client) UpdateBuild(
Platform: platform,
SourceExternalIdentifier: sourceExternalIdentifier,
Status: &buildStatus,
Metadata: metadata,
}

resp, err := c.Packer.PackerServiceUpdateBuild(params, nil)
Expand Down
18 changes: 15 additions & 3 deletions internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func (h *HCLRegistry) StartBuild(ctx context.Context, build sdkpacker.Build) err
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)
}

Expand All @@ -80,12 +86,18 @@ func (h *HCLRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
name := build.Name()
buildName := build.Name()
cb, ok := build.(*packer.CoreBuild)
if ok {
name = cb.Type
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, name, artifacts, buildErr)
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
17 changes: 15 additions & 2 deletions internal/hcp/registry/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ 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 {
return h.bucket.startBuild(ctx, build.Name())
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)
}

// CompleteBuild is invoked when one build for the configuration has finished
Expand All @@ -93,7 +100,13 @@ func (h *JSONRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
return h.bucket.completeBuild(ctx, build.Name(), artifacts, buildErr)
buildName := build.Name()
buildMetadata := build.(*packer.CoreBuild).GetMetadata()
err := h.bucket.Version.AddMetadataToBuild(ctx, buildName, buildMetadata)
if err != nil {
return nil, err
}
lbajolet-hashicorp marked this conversation as resolved.
Show resolved Hide resolved
return h.bucket.completeBuild(ctx, buildName, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
3 changes: 3 additions & 0 deletions internal/hcp/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func (bucket *Bucket) UpdateBuildStatus(
nil,
status,
nil,
&buildToUpdate.Metadata,
)
if err != nil {
return err
Expand Down Expand Up @@ -285,6 +286,7 @@ func (bucket *Bucket) markBuildComplete(ctx context.Context, name string) error
buildToUpdate.Labels,
status,
artifacts,
&buildToUpdate.Metadata,
)
if err != nil {
return err
Expand Down Expand Up @@ -535,6 +537,7 @@ func (bucket *Bucket) HeartbeatBuild(ctx context.Context, build string) (func(),
nil,
hcpPackerModels.HashicorpCloudPacker20230101BuildStatusBUILDRUNNING,
nil,
nil,
)
if err != nil {
log.Printf("[ERROR] failed to send heartbeat for build %q: %s", build, err)
Expand Down
1 change: 1 addition & 0 deletions internal/hcp/registry/types.builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Build struct {
Labels map[string]string
Artifacts map[string]packerSDKRegistry.Image
Status hcpPackerModels.HashicorpCloudPacker20230101BuildStatus
Metadata hcpPackerModels.HashicorpCloudPacker20230101BuildMetadata
}

// NewBuildFromCloudPackerBuild converts a HashicorpCloudPackerBuild to a local build that can be tracked and
Expand Down
28 changes: 28 additions & 0 deletions internal/hcp/registry/types.version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package registry

import (
"context"
"errors"
"fmt"
"math/rand"
Expand All @@ -16,6 +17,7 @@ import (
sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
packerSDKRegistry "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
"github.com/hashicorp/packer/internal/hcp/env"
"github.com/hashicorp/packer/packer"
"github.com/oklog/ulid"
)

Expand Down Expand Up @@ -174,3 +176,29 @@ func (version *Version) statusSummary(ui sdkpacker.Ui) {

ui.Say(buf.String())
}

// AddMetadataToBuild adds metadata to a build in the HCP Packer registry.
func (version *Version) AddMetadataToBuild(
ctx context.Context, buildName string, metadata packer.BuildMetadata,
) error {
buildToUpdate, err := version.Build(buildName)
if err != nil {
return err
}

packerMetadata := make(map[string]interface{})
packerMetadata["version"] = metadata.PackerVersion

var pluginsMetadata []map[string]interface{}
for _, plugin := range metadata.Plugins {
pluginMetadata := map[string]interface{}{
"version": plugin.Description.Version,
"name": plugin.Name,
}
pluginsMetadata = append(pluginsMetadata, pluginMetadata)
}
packerMetadata["plugins"] = pluginsMetadata

buildToUpdate.Metadata.Packer = packerMetadata
return nil
}
Loading
Loading