Skip to content

Commit

Permalink
Packer tracks Version and Plugins Metadata (#12860)
Browse files Browse the repository at this point in the history
  • Loading branch information
devashish-patel committed Apr 8, 2024
1 parent 1c7930b commit 3ed317a
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 21 deletions.
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
12 changes: 12 additions & 0 deletions internal/hcp/registry/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func (h *HCLRegistry) CompleteBuild(
if ok {
name = cb.Type
}

metadata := cb.GetMetadata()
log.Printf(
"[TRACE] HCL 'Packer Version' Metadata for build name %q: %q\n",
name, metadata.PackerVersion,
)
for k, pluginDetails := range metadata.Plugins {
log.Printf(
"[TRACE] HCL 'Plugin' Metadata for build name %q: %q -- %q\n",
name, k, pluginDetails.Description.Version,
)
}
return h.bucket.completeBuild(ctx, name, artifacts, buildErr)
}

Expand Down
15 changes: 14 additions & 1 deletion internal/hcp/registry/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,20 @@ func (h *JSONRegistry) CompleteBuild(
artifacts []sdkpacker.Artifact,
buildErr error,
) ([]sdkpacker.Artifact, error) {
return h.bucket.completeBuild(ctx, build.Name(), artifacts, buildErr)
name := build.Name()

metadata := build.(*packer.CoreBuild).GetMetadata()
log.Printf(
"[TRACE] JSON 'Packer Version' Metadata for build name %q: %q\n",
name, metadata.PackerVersion,
)
for k, pluginDetails := range metadata.Plugins {
log.Printf(
"[TRACE] JSON 'Plugin' Metadata for build name %q: %q -- %q\n",
name, k, pluginDetails.Description.Version,
)
}
return h.bucket.completeBuild(ctx, name, artifacts, buildErr)
}

// VersionStatusSummary prints a status report in the UI if the version is not yet done
Expand Down
40 changes: 40 additions & 0 deletions packer/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ type CoreBuild struct {
prepareCalled bool
}

type BuildMetadata struct {
PackerVersion string
Plugins map[string]PluginDetails
}

func (b *CoreBuild) getPluginsMetadata() map[string]PluginDetails {
resp := map[string]PluginDetails{}

builderPlugin, builderPluginOk := PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentBuilder, b.BuilderType)]
if builderPluginOk {
resp[builderPlugin.Name] = builderPlugin
}

for _, pp := range b.PostProcessors {
for _, p := range pp {
postprocessorsPlugin, postprocessorsPluginOk := PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentPostProcessor, p.PType)]
if postprocessorsPluginOk {
resp[postprocessorsPlugin.Name] = postprocessorsPlugin
}
}
}

for _, pv := range b.Provisioners {
provisionerPlugin, provisionerPluginOk := PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentProvisioner, pv.PType)]
if provisionerPluginOk {
resp[provisionerPlugin.Name] = provisionerPlugin
}
}

return resp
}

func (b *CoreBuild) GetMetadata() BuildMetadata {
metadata := BuildMetadata{
PackerVersion: version.FormattedVersion(),
Plugins: b.getPluginsMetadata(),
}
return metadata
}

// CoreBuildPostProcessor Keeps track of the post-processor and the
// configuration of the post-processor used within a build.
type CoreBuildPostProcessor struct {
Expand Down
29 changes: 29 additions & 0 deletions packer/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package packer
import (
"crypto/sha256"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -135,6 +136,11 @@ func (c *PluginConfig) DiscoverMultiPlugin(pluginName, pluginPath string) error
}

pluginPrefix := pluginName + "-"
pluginDetails := PluginDetails{
Name: pluginName,
Description: desc,
PluginPath: pluginPath,
}

for _, builderName := range desc.Builders {
builderName := builderName // copy to avoid pointer overwrite issue
Expand All @@ -145,6 +151,8 @@ func (c *PluginConfig) DiscoverMultiPlugin(pluginName, pluginPath string) error
c.Builders.Set(key, func() (packersdk.Builder, error) {
return c.Client(pluginPath, "start", "builder", builderName).Builder()
})
PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentBuilder, key)] = pluginDetails

}

if len(desc.Builders) > 0 {
Expand All @@ -160,6 +168,7 @@ func (c *PluginConfig) DiscoverMultiPlugin(pluginName, pluginPath string) error
c.PostProcessors.Set(key, func() (packersdk.PostProcessor, error) {
return c.Client(pluginPath, "start", "post-processor", postProcessorName).PostProcessor()
})
PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentPostProcessor, key)] = pluginDetails
}

if len(desc.PostProcessors) > 0 {
Expand All @@ -175,6 +184,8 @@ func (c *PluginConfig) DiscoverMultiPlugin(pluginName, pluginPath string) error
c.Provisioners.Set(key, func() (packersdk.Provisioner, error) {
return c.Client(pluginPath, "start", "provisioner", provisionerName).Provisioner()
})
PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentProvisioner, key)] = pluginDetails

}
if len(desc.Provisioners) > 0 {
log.Printf("found external %v provisioner from %s plugin", desc.Provisioners, pluginName)
Expand All @@ -189,6 +200,7 @@ func (c *PluginConfig) DiscoverMultiPlugin(pluginName, pluginPath string) error
c.DataSources.Set(key, func() (packersdk.Datasource, error) {
return c.Client(pluginPath, "start", "datasource", datasourceName).Datasource()
})
PluginsDetailsStorage[fmt.Sprintf("%q-%q", PluginComponentDataSource, key)] = pluginDetails
}
if len(desc.Datasources) > 0 {
log.Printf("found external %v datasource from %s plugin", desc.Datasources, pluginName)
Expand Down Expand Up @@ -240,3 +252,20 @@ func (c *PluginConfig) Client(path string, args ...string) *PluginClient {
config.MaxPort = c.PluginMaxPort
return NewClient(&config)
}

type PluginComponentType string

const (
PluginComponentBuilder PluginComponentType = "builder"
PluginComponentPostProcessor PluginComponentType = "post-processor"
PluginComponentProvisioner PluginComponentType = "provisioner"
PluginComponentDataSource PluginComponentType = "data-source"
)

type PluginDetails struct {
Name string
Description pluginsdk.SetDescription
PluginPath string
}

var PluginsDetailsStorage = map[string]PluginDetails{}

0 comments on commit 3ed317a

Please sign in to comment.