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: support loading pre-release plugins #12828

Merged
merged 19 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 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
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
5 changes: 5 additions & 0 deletions command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func writeDiags(ui packersdk.Ui, files map[string]*hcl.File, diags hcl.Diagnosti
}

func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int {
// Set the release only flag if specified as argument
//
// This deactivates the capacity for Packer to load development binaries.
c.CoreConfig.Components.PluginConfig.ReleasesOnly = cla.ReleaseOnly

packerStarter, ret := c.GetConfig(&cla.MetaArgs)
if ret != 0 {
return ret
Expand Down
6 changes: 6 additions & 0 deletions command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) {
flags.Var(flagOnError, "on-error", "")

flags.BoolVar(&ba.MetaArgs.WarnOnUndeclaredVar, "warn-on-undeclared-var", false, "Show warnings for variable files containing undeclared variables.")

flags.BoolVar(&ba.ReleaseOnly, "ignore-prerelease-plugins", false, "Disable the loading of prerelease plugin binaries (x.y.z-<prerelease-name>.")

ba.MetaArgs.AddFlagSets(flags)
}

Expand All @@ -100,6 +103,7 @@ type BuildArgs struct {
Color, TimestampUi, MachineReadable bool
ParallelBuilds int64
OnError string
ReleaseOnly bool
}

func (ia *InitArgs) AddFlagSets(flags *flag.FlagSet) {
Expand Down Expand Up @@ -142,6 +146,7 @@ func (va *ValidateArgs) AddFlagSets(flags *flag.FlagSet) {
flags.BoolVar(&va.SyntaxOnly, "syntax-only", false, "check syntax only")
flags.BoolVar(&va.NoWarnUndeclaredVar, "no-warn-undeclared-var", false, "Ignore warnings for variable files containing undeclared variables.")
flags.BoolVar(&va.EvaluateDatasources, "evaluate-datasources", false, "evaluate datasources for validation (HCL2 only, may incur costs)")
flags.BoolVar(&va.ReleaseOnly, "ignore-prerelease-plugins", false, "Disable the loading of prerelease plugin binaries (x.y.z-<prerelease-name>.")

va.MetaArgs.AddFlagSets(flags)
}
Expand All @@ -151,6 +156,7 @@ type ValidateArgs struct {
MetaArgs
SyntaxOnly, NoWarnUndeclaredVar bool
EvaluateDatasources bool
ReleaseOnly bool
}

func (va *InspectArgs) AddFlagSets(flags *flag.FlagSet) {
Expand Down
4 changes: 2 additions & 2 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ for more info.`)
}

opts := plugingetter.ListInstallationsOptions{
FromFolders: c.Meta.CoreConfig.Components.PluginConfig.KnownPluginFolders,
PluginDirectory: c.Meta.CoreConfig.Components.PluginConfig.PluginDirectory,
BinaryInstallationOptions: plugingetter.BinaryInstallationOptions{
OS: runtime.GOOS,
ARCH: runtime.GOARCH,
Expand Down Expand Up @@ -132,7 +132,7 @@ for more info.`)
}

newInstall, err := pluginRequirement.InstallLatest(plugingetter.InstallOptions{
InFolders: opts.FromFolders,
PluginDirectory: opts.PluginDirectory,
BinaryInstallationOptions: opts.BinaryInstallationOptions,
Getters: getters,
Force: cla.Force,
Expand Down
146 changes: 74 additions & 72 deletions command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,71 +61,75 @@ func TestInitCommand_Run(t *testing.T) {
cfg := &configDirSingleton{map[string]string{}}

tests := []testCaseInit{
// {
// // here we pre-write plugins with valid checksums, Packer will
// // see those as valid installations it did.
// // the directory hash before and after init should be the same,
// // that's a no-op. This also should do no GH query, so it is best
// // to always run it.
// //
// // Note: cannot work with plugin changes since the fake binary
// // isn't recognised as a potential plugin, so Packer always
// // installs it.
// "already-installed-no-op",
// nil,
// TestMetaFile(t),
// map[string]string{
// "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_darwin_amd64": "1",
// "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_darwin_amd64_SHA256SUM": "a23e48324f2d9b912a89354945562b21b0ae99133b31d3132e2e6671aba8e085",
// "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_windows_amd64.exe": "1.exe",
// "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_windows_amd64.exe_SHA256SUM": "f1cf5865b35933b8e5195625ac8be44487b64007f223912cc5c1784e493e62b2",
// "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_linux_amd64": "1.out",
// "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_linux_amd64_SHA256SUM": "0a4e4e1d6de28054f64946782a5eb92edc663e980ae0780fcb3a614d27c58506",
// },
// "h1:jQchMpyaQhkZYn0iguw6E6O4VCWxacYx2aR/RJJNLmo=",
// map[string]string{
// `cfg.pkr.hcl`: `
// packer {
// required_plugins {
// comment = {
// source = "github.com/hashicorp/hashicups"
// version = "v1.0.1"
// }
// }
// }`,
// },
// cfg.dir("1_pkr_config"),
// cfg.dir("1_pkr_user_folder"),
// 0,
// nil,
// "h1:jQchMpyaQhkZYn0iguw6E6O4VCWxacYx2aR/RJJNLmo=",
// []func(t *testing.T, tc testCaseInit){
// // test that a build will not work since plugins are broken for
// // this tests (they are not binaries).
// testBuild{want: 1}.fn,
// },
// },
{
// here we pre-write plugins with valid checksums, Packer will
// see those as valid installations it did.
// the directory hash before and after init should be the same,
// that's a no-op. This also should do no GH query, so it is best
// to always run it.
"already-installed-no-op",
nil,
TestMetaFile(t),
map[string]string{
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_windows_amd64.exe": "1.exe",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_windows_amd64.exe_SHA256SUM": "07d8453027192ee0c4120242e6e84e2ca2328b8e0f506e2f818a1a5b82790a0b",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64": "1.out",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM": "59031c50e0dfeedfde2b4e9445754804dce3f29e4efa737eead0ca9b4f5b85a5",
},
"h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=",
map[string]string{
`cfg.pkr.hcl`: `
packer {
required_plugins {
comment = {
source = "github.com/sylviamoss/comment"
version = "v0.2.018"
}
}
}`,
},
cfg.dir("1_pkr_config"),
cfg.dir("1_pkr_user_folder"),
0,
nil,
"h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=",
[]func(t *testing.T, tc testCaseInit){
// test that a build will not work since plugins are broken for
// this tests (they are not binaries).
testBuild{want: 1}.fn,
},
},
{
// here we pre-write plugins with valid checksums, Packer will
// see those as valid installations it did.
// But because we require version 0.2.19, we will upgrade.
// But because we require version 1.0.2, we will upgrade.
"already-installed-upgrade",
[]func(t *testing.T, tc testCaseInit){
skipInitTestUnlessEnVar(acctest.TestEnvVar).fn,
},
TestMetaFile(t),
map[string]string{
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_windows_amd64.exe": "1.exe",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_windows_amd64.exe_SHA256SUM": "07d8453027192ee0c4120242e6e84e2ca2328b8e0f506e2f818a1a5b82790a0b",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64": "1.out",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM": "59031c50e0dfeedfde2b4e9445754804dce3f29e4efa737eead0ca9b4f5b85a5",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_darwin_amd64": "1",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_darwin_amd64_SHA256SUM": "a23e48324f2d9b912a89354945562b21b0ae99133b31d3132e2e6671aba8e085",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_windows_amd64.exe": "1.exe",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_windows_amd64.exe_SHA256SUM": "f1cf5865b35933b8e5195625ac8be44487b64007f223912cc5c1784e493e62b2",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_linux_amd64": "1.out",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_linux_amd64_SHA256SUM": "0a4e4e1d6de28054f64946782a5eb92edc663e980ae0780fcb3a614d27c58506",
},
"h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=",
"h1:jQchMpyaQhkZYn0iguw6E6O4VCWxacYx2aR/RJJNLmo=",
map[string]string{
`cfg.pkr.hcl`: `
packer {
required_plugins {
comment = {
source = "github.com/sylviamoss/comment"
version = "v0.2.019"
hashicups = {
source = "github.com/hashicorp/hashicups"
version = "v1.0.2"
}
}
}`,
Expand All @@ -136,11 +140,9 @@ func TestInitCommand_Run(t *testing.T) {
`,
`build.pkr.hcl`: `
build {
sources = ["source.null.test"]
provisioner "comment" {
comment = "Begin ¡"
ui = true
bubble_text = true
sources = ["null.test"]
provisioner "hashicups-toppings" {
toppings = ["sugar"] # Takes 5 seconds in the current state
}
}
`,
Expand All @@ -149,27 +151,27 @@ func TestInitCommand_Run(t *testing.T) {
cfg.dir("2_pkr_user_folder"),
0,
[]string{
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_windows_amd64.exe",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_windows_amd64.exe_SHA256SUM",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_darwin_amd64",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_darwin_amd64_SHA256SUM",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_linux_amd64",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_linux_amd64_SHA256SUM",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_windows_amd64.exe",
"github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.1_x5.0_windows_amd64.exe_SHA256SUM",
map[string]string{
"darwin": "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.19_x5.0_darwin_amd64_SHA256SUM",
"linux": "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.19_x5.0_linux_amd64_SHA256SUM",
"windows": "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.19_x5.0_windows_amd64.exe_SHA256SUM",
"darwin": "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.2_x5.0_darwin_amd64_SHA256SUM",
"linux": "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.2_x5.0_linux_amd64_SHA256SUM",
"windows": "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.2_x5.0_windows_amd64.exe_SHA256SUM",
}[runtime.GOOS],
map[string]string{
"darwin": "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.19_x5.0_darwin_amd64",
"linux": "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.19_x5.0_linux_amd64",
"windows": "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.19_x5.0_windows_amd64.exe",
"darwin": "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.2_x5.0_darwin_amd64",
"linux": "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.2_x5.0_linux_amd64",
"windows": "github.com/hashicorp/hashicups/packer-plugin-hashicups_v1.0.2_x5.0_windows_amd64.exe",
}[runtime.GOOS],
},
map[string]string{
"darwin": "h1:ORwcCYUx8z/5n/QvuTJo2vrgKpfJA4AxlNg1G9/BCDI=",
"linux": "h1:CGym0+Nd0LEANgzqL0wx/LDjRL8bYwlpZ0HajPJo/hs=",
"windows": "h1:ag0/C1YjP7KoEDYOiJHE0K+lhFgs0tVgjriWCXVT1fg=",
"darwin": "h1:ptsMLvUeLsMMeXDJP2PWKAKIkE+kWVhOkhNYOYPJbSE=",
"linux": "h1:ivCmyQ+/qNXfBsyeccGsa7P5232q7MUZk83B3yl80Ms=",
"windows": "h1:BeqAUnyGiBg9fVuf9Cn9a4h91bgdZ2U4kV7EuQKefcM=",
}[runtime.GOOS],
[]func(t *testing.T, tc testCaseInit){
// test that a build will work as the plugin was just installed
Expand Down Expand Up @@ -267,7 +269,7 @@ func TestInitCommand_Run(t *testing.T) {
t.Fatalf("Failed to discover plugins: %s", err)
}

c.CoreConfig.Components.PluginConfig.KnownPluginFolders = []string{tt.packerConfigDir}
c.CoreConfig.Components.PluginConfig.PluginDirectory = tt.packerConfigDir
if got := c.Run(args); got != tt.want {
t.Errorf("InitCommand.Run() = %v, want %v", got, tt.want)
}
Expand Down
19 changes: 8 additions & 11 deletions command/plugins_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ Usage: packer plugins install [OPTIONS...] <plugin> [<version constraint>]
packer plugins install --path ./packer-plugin-happycloud "github.com/hashicorp/happycloud"

Options:
-path <path> Install the plugin from a locally-sourced plugin binary.
This installs the plugin where a normal invocation would, but will
-path <path> Install the plugin from a locally-sourced plugin binary.
This installs the plugin where a normal invocation would, but will
not try to download it from a remote location, and instead
install the binary in the Packer plugins path. This option cannot
install the binary in the Packer plugins path. This option cannot
be specified with a version constraint.
-force Forces reinstallation of plugins, even if already installed.
`
Expand Down Expand Up @@ -121,7 +121,7 @@ func (c *PluginsInstallCommand) ParseArgs(args []string) (*PluginsInstallArgs, i

func (c *PluginsInstallCommand) RunContext(buildCtx context.Context, args *PluginsInstallArgs) int {
opts := plugingetter.ListInstallationsOptions{
FromFolders: c.Meta.CoreConfig.Components.PluginConfig.KnownPluginFolders,
PluginDirectory: c.Meta.CoreConfig.Components.PluginConfig.PluginDirectory,
BinaryInstallationOptions: plugingetter.BinaryInstallationOptions{
OS: runtime.GOOS,
ARCH: runtime.GOARCH,
Expand Down Expand Up @@ -176,7 +176,7 @@ func (c *PluginsInstallCommand) RunContext(buildCtx context.Context, args *Plugi
}

newInstall, err := pluginRequirement.InstallLatest(plugingetter.InstallOptions{
InFolders: opts.FromFolders,
PluginDirectory: opts.PluginDirectory,
BinaryInstallationOptions: opts.BinaryInstallationOptions,
Getters: getters,
Force: args.Force,
Expand All @@ -201,10 +201,7 @@ func (c *PluginsInstallCommand) RunContext(buildCtx context.Context, args *Plugi
}

func (c *PluginsInstallCommand) InstallFromBinary(opts plugingetter.ListInstallationsOptions, pluginIdentifier *addrs.Plugin, args *PluginsInstallArgs) int {
// As with the other commands, we get the last plugin directory as it
// has precedence over the others, and is where we'll install the
// plugins to.
pluginDir := opts.FromFolders[len(opts.FromFolders)-1]
pluginDir := opts.PluginDirectory

var err error

Expand Down Expand Up @@ -260,11 +257,11 @@ func (c *PluginsInstallCommand) InstallFromBinary(opts plugingetter.ListInstalla
Detail: fmt.Sprintf("Plugin's reported version (%q) is not semver-compatible: %s", desc.Version, err),
}})
}
if semver.Prerelease() != "" {
if semver.Prerelease() != "" && semver.Prerelease() != "dev" {
return writeDiags(c.Ui, nil, hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid version",
Detail: fmt.Sprintf("Packer can only install plugin releases with this command (ex: 1.0.0), the binary's reported version is %q", desc.Version),
Detail: fmt.Sprintf("Packer can only install plugin releases with this command (ex: 1.0.0) or development pre-releases (ex: 1.0.0-dev), the binary's reported version is %q", desc.Version),
}})
}

Expand Down
Loading
Loading