Skip to content

Commit 4c143b3

Browse files
packer: warn on bundled plugins usage
Since bundled plugins will be removed in an upcoming version of Packer, this commit adds a new warning message whenever a template uses one such plugin. This warning has been implemented on build, validate, console and the inspect subcommands. In addition to warning about the upcoming change and potential issue this will cause, this warning message proposes solutions to the user so they know what they'll have to do in order not to rely on those bundled plugins later.
1 parent a7562bb commit 4c143b3

12 files changed

+606
-15
lines changed

acctest/plugin/bundled_plugin_test.go

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package plugin
2+
3+
import (
4+
_ "embed"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
"testing"
11+
12+
"github.com/hashicorp/go-multierror"
13+
amazonacc "github.com/hashicorp/packer-plugin-amazon/builder/ebs/acceptance"
14+
"github.com/hashicorp/packer-plugin-sdk/acctest"
15+
"github.com/hashicorp/packer/hcl2template/addrs"
16+
)
17+
18+
//go:embed test-fixtures/basic_amazon_bundled.pkr.hcl
19+
var basicAmazonBundledEbsTemplate string
20+
21+
func TestAccBuildBundledPlugins(t *testing.T) {
22+
plugin := addrs.Plugin{
23+
Hostname: "github.com",
24+
Namespace: "hashicorp",
25+
Type: "amazon",
26+
}
27+
testCase := &acctest.PluginTestCase{
28+
Name: "amazon-ebs_bundled_test",
29+
Setup: func() error {
30+
return cleanupPluginInstallation(plugin)
31+
},
32+
Teardown: func() error {
33+
helper := amazonacc.AMIHelper{
34+
Region: "us-east-1",
35+
Name: "packer-plugin-bundled-amazon-ebs-test",
36+
}
37+
return helper.CleanUpAmi()
38+
},
39+
Template: basicAmazonBundledEbsTemplate,
40+
Type: "amazon-ebs",
41+
Init: false,
42+
Check: func(buildCommand *exec.Cmd, logfile string) error {
43+
if buildCommand.ProcessState != nil {
44+
if buildCommand.ProcessState.ExitCode() != 0 {
45+
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
46+
}
47+
}
48+
49+
rawLogs, err := os.ReadFile(logfile)
50+
if err != nil {
51+
return fmt.Errorf("failed to read logs: %s", err)
52+
}
53+
54+
var errs error
55+
56+
logs := string(rawLogs)
57+
58+
if !strings.Contains(logs, "Warning: Bundled plugins used") {
59+
errs = multierror.Append(errs, errors.New("expected warning about bundled plugins used, did not find it"))
60+
}
61+
62+
if !strings.Contains(logs, "Then run 'packer init' to manage installation of the plugins") {
63+
errs = multierror.Append(errs, errors.New("expected suggestion about packer init in logs, did not find it."))
64+
}
65+
66+
return errs
67+
},
68+
}
69+
70+
acctest.TestPlugin(t, testCase)
71+
}
72+
73+
//go:embed test-fixtures/basic_amazon_with_required_plugins.pkr.hcl
74+
var basicAmazonRequiredPluginEbsTemplate string
75+
76+
func TestAccBuildBundledPluginsWithRequiredPlugins(t *testing.T) {
77+
plugin := addrs.Plugin{
78+
Hostname: "github.com",
79+
Namespace: "hashicorp",
80+
Type: "amazon",
81+
}
82+
testCase := &acctest.PluginTestCase{
83+
Name: "amazon-ebs_with_required_plugins_test",
84+
Setup: func() error {
85+
return cleanupPluginInstallation(plugin)
86+
},
87+
Teardown: func() error {
88+
helper := amazonacc.AMIHelper{
89+
Region: "us-east-1",
90+
Name: "packer-plugin-required-plugin-amazon-ebs-test",
91+
}
92+
return helper.CleanUpAmi()
93+
},
94+
Template: basicAmazonRequiredPluginEbsTemplate,
95+
Type: "amazon-ebs",
96+
Init: false,
97+
Check: func(buildCommand *exec.Cmd, logfile string) error {
98+
if buildCommand.ProcessState != nil {
99+
if buildCommand.ProcessState.ExitCode() != 1 {
100+
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
101+
}
102+
}
103+
104+
rawLogs, err := os.ReadFile(logfile)
105+
if err != nil {
106+
return fmt.Errorf("failed to read logs: %s", err)
107+
}
108+
109+
var errs error
110+
111+
logs := string(rawLogs)
112+
113+
if strings.Contains(logs, "Warning: Bundled plugins used") {
114+
errs = multierror.Append(errs, errors.New("did not expect warning about bundled plugins used"))
115+
}
116+
117+
if !strings.Contains(logs, "Missing plugins") {
118+
errs = multierror.Append(errs, errors.New("expected error about plugins required and not installed, did not find it"))
119+
}
120+
121+
return errs
122+
},
123+
}
124+
125+
acctest.TestPlugin(t, testCase)
126+
}
127+
128+
//go:embed test-fixtures/basic_amazon_bundled.json
129+
var basicAmazonBundledEbsTemplateJSON string
130+
131+
func TestAccBuildBundledPluginsJSON(t *testing.T) {
132+
plugin := addrs.Plugin{
133+
Hostname: "github.com",
134+
Namespace: "hashicorp",
135+
Type: "amazon",
136+
}
137+
testCase := &acctest.PluginTestCase{
138+
Name: "amazon-ebs_bundled_test_json",
139+
Setup: func() error {
140+
return cleanupPluginInstallation(plugin)
141+
},
142+
Teardown: func() error {
143+
helper := amazonacc.AMIHelper{
144+
Region: "us-east-1",
145+
Name: "packer-plugin-bundled-amazon-ebs-test-json",
146+
}
147+
return helper.CleanUpAmi()
148+
},
149+
Template: basicAmazonBundledEbsTemplateJSON,
150+
Type: "amazon-ebs",
151+
Init: false,
152+
Check: func(buildCommand *exec.Cmd, logfile string) error {
153+
if buildCommand.ProcessState != nil {
154+
if buildCommand.ProcessState.ExitCode() != 0 {
155+
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
156+
}
157+
}
158+
159+
rawLogs, err := os.ReadFile(logfile)
160+
if err != nil {
161+
return fmt.Errorf("failed to read logs: %s", err)
162+
}
163+
164+
var errs error
165+
166+
logs := string(rawLogs)
167+
168+
if !strings.Contains(logs, "Warning: Bundled plugins used") {
169+
errs = multierror.Append(errs, errors.New("expected warning about bundled plugins, did not find it."))
170+
}
171+
172+
if !strings.Contains(logs, "plugins with the 'packer plugins install' command") {
173+
errs = multierror.Append(errs, errors.New("expected suggestion about packer plugins install in logs, did not find it."))
174+
}
175+
176+
return errs
177+
},
178+
}
179+
180+
acctest.TestPlugin(t, testCase)
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"builders": [{
3+
"type": "amazon-ebs",
4+
"region": "us-east-1",
5+
"instance_type": "m3.medium",
6+
"source_ami": "ami-76b2a71e",
7+
"ssh_username": "ubuntu",
8+
"ami_name": "packer-plugin-bundled-amazon-ebs-test-json"
9+
}]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source "amazon-ebs" "basic-test" {
2+
region = "us-east-1"
3+
instance_type = "m3.medium"
4+
source_ami = "ami-76b2a71e"
5+
ssh_username = "ubuntu"
6+
ami_name = "packer-plugin-bundled-amazon-ebs-test"
7+
}
8+
9+
build {
10+
sources = ["source.amazon-ebs.basic-test"]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packer {
2+
required_plugins {
3+
amazon = {
4+
source = "github.com/hashicorp/amazon",
5+
version = "~> 1"
6+
}
7+
}
8+
}
9+
10+
source "amazon-ebs" "basic-test" {
11+
region = "us-east-1"
12+
instance_type = "m3.medium"
13+
source_ami = "ami-76b2a71e"
14+
ssh_username = "ubuntu"
15+
ami_name = "packer-plugin-bundled-amazon-ebs-test"
16+
}
17+
18+
build {
19+
sources = ["source.amazon-ebs.basic-test"]
20+
}

command/build.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
8787
return ret
8888
}
8989

90-
diags := packerStarter.Initialize(packer.InitializeOptions{})
90+
diags := packerStarter.PreInitialize()
91+
ret = writeDiags(c.Ui, nil, diags)
92+
if ret != 0 {
93+
return ret
94+
}
95+
96+
diags = packerStarter.Initialize(packer.InitializeOptions{})
97+
bundledDiags := c.DetectBundledPlugins(packerStarter)
98+
diags = append(bundledDiags, diags...)
9199
ret = writeDiags(c.Ui, nil, diags)
92100
if ret != 0 {
93101
return ret

0 commit comments

Comments
 (0)