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

Packer tracks Version and Plugins Metadata #12860

Merged
merged 33 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7ab9915
packer: only support one directory for plugins
lbajolet-hashicorp Jan 15, 2024
b9947d1
packer: rm support for manually-installed plugins
lbajolet-hashicorp Jan 15, 2024
d564079
packer: remove directory slices from structs
lbajolet-hashicorp Jan 15, 2024
6c08b9a
packer: remove path argument to discover installed
lbajolet-hashicorp Jan 16, 2024
b14cfe0
plugin-getter: fix docs for Installation
lbajolet-hashicorp Jan 17, 2024
74bb620
plugin-getter: implement Sort interface for List
lbajolet-hashicorp Jan 17, 2024
b885760
plugin: consolidate loading logic
lbajolet-hashicorp Jan 17, 2024
7eaf94e
plugin-getter: reject plugin version mismatches
lbajolet-hashicorp Jan 17, 2024
fc2d255
command: move plugins remove tests outside of repo
lbajolet-hashicorp Jan 17, 2024
07e40e7
packer: fix plugin version sorting and pickup
lbajolet-hashicorp Feb 1, 2024
db8cebc
packer: support loading pre-release plugins
lbajolet-hashicorp Feb 1, 2024
f4d52bf
main: move Discover to DetectPluginBinaries
lbajolet-hashicorp Feb 2, 2024
4c22e48
plugins: add a release only flag to Discover
lbajolet-hashicorp Feb 2, 2024
d4b45cd
command: add --release-only flag to validate/build
lbajolet-hashicorp Feb 2, 2024
e65a6f5
command: add dev support for plugins install
lbajolet-hashicorp Feb 2, 2024
80b4f45
command: trim trailing spaces from help message
lbajolet-hashicorp Feb 2, 2024
4774675
command: migrate tests using the comment plugin
lbajolet-hashicorp Feb 23, 2024
ed03264
plugin-getter: fix typo in API mismatch error
lbajolet-hashicorp Feb 23, 2024
bf1895d
plugin: use API version for loading plugins
lbajolet-hashicorp Feb 23, 2024
0867ee1
POC Builds Metadata
devashish-patel Feb 28, 2024
aa61c9a
Add response structure in schema
devashish-patel Feb 28, 2024
526e3a5
Move implementation to packer package
devashish-patel Feb 28, 2024
ac44000
Track plugin metadata on the CoreBuild level
devashish-patel Feb 28, 2024
8f19797
Store and look up by the component type to prevent duplication
devashish-patel Feb 29, 2024
7343a37
Fix tests
devashish-patel Feb 29, 2024
24c673d
Add PR suggestions
devashish-patel Feb 29, 2024
b6c0b39
Some more changes
devashish-patel Feb 29, 2024
81bd454
Code clean up
devashish-patel Feb 29, 2024
16f8fd5
Remove out of scope changes
devashish-patel Feb 29, 2024
f45d550
Use GetMetadata method and change logs
devashish-patel Feb 29, 2024
225e4ab
Merge branch 'main' into plugin-metadata
devashish-patel Mar 5, 2024
acce102
Initialize PluginDetails struct only once and reuse it
devashish-patel Mar 5, 2024
3b4b1db
Lint and make GetPluginsMetadata method private
devashish-patel Mar 5, 2024
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
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{}
Loading