Skip to content

Commit

Permalink
command: add --force option to init/install
Browse files Browse the repository at this point in the history
The --force option for packer init and packer plugins install enforces
installation of a plugin, even if it is already locally installed.

This will become useful if for some reason a pre-existing plugin
binary/version is already installed, and we want to overwrite it.
  • Loading branch information
lbajolet-hashicorp committed Oct 26, 2023
1 parent 6f2bf64 commit acf3ef9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type BuildArgs struct {

func (ia *InitArgs) AddFlagSets(flags *flag.FlagSet) {
flags.BoolVar(&ia.Upgrade, "upgrade", false, "upgrade any present plugin to the highest allowed version.")
flags.BoolVar(&ia.Force, "force", false, "force installation of a plugin, even if already installed")

ia.MetaArgs.AddFlagSets(flags)
}
Expand All @@ -112,6 +113,7 @@ func (ia *InitArgs) AddFlagSets(flags *flag.FlagSet) {
type InitArgs struct {
MetaArgs
Upgrade bool
Force bool
}

// PluginsRequiredArgs represents a parsed cli line for a `packer plugins required <path>`
Expand Down
3 changes: 3 additions & 0 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ for more info.`)
InFolders: opts.FromFolders,
BinaryInstallationOptions: opts.BinaryInstallationOptions,
Getters: getters,
Force: cla.Force,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed getting the %q plugin:", pluginRequirement.Identifier))
Expand Down Expand Up @@ -163,6 +164,8 @@ Options:
version, if there is a new higher one. Note that
this still takes into consideration the version
constraint of the config.
-force Forces installation of plugins, even if already
installed.
`

return strings.TrimSpace(helpText)
Expand Down
4 changes: 4 additions & 0 deletions command/plugins_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Options:
installs the plugin where a normal invocation would, but will
not try to download it from a web server, but instead directly
install the binary for Packer to be able to load it later on.
- force: forces installation of a plugin, even if it is already there.
`

return strings.TrimSpace(helpText)
Expand All @@ -72,10 +73,12 @@ type PluginsInstallArgs struct {
PluginName string
PluginPath string
Version string
Force bool
}

func (pa *PluginsInstallArgs) AddFlagSets(flags *flag.FlagSet) {
flags.StringVar(&pa.PluginPath, "path", "", "install the plugin from a specific path")
flags.BoolVar(&pa.Force, "force", false, "force installation of a plugin, even if already installed")
pa.MetaArgs.AddFlagSets(flags)
}

Expand Down Expand Up @@ -168,6 +171,7 @@ func (c *PluginsInstallCommand) RunContext(buildCtx context.Context, args *Plugi
InFolders: opts.FromFolders,
BinaryInstallationOptions: opts.BinaryInstallationOptions,
Getters: getters,
Force: args.Force,
})

if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion packer/plugin-getter/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ type InstallOptions struct {
// folder of this list.
InFolders []string

// Forces installation of the plugin, even if already installed.
Force bool

BinaryInstallationOptions
}

Expand Down Expand Up @@ -582,7 +585,7 @@ func (pr *Requirement) InstallLatest(opts InstallOptions) (*Installation, error)

log.Printf("[TRACE] found a pre-exising %q checksum file", potentialChecksumer.Type)
// if outputFile is there and matches the checksum: do nothing more.
if err := localChecksum.ChecksumFile(localChecksum.Expected, potentialOutputFilename); err == nil {
if err := localChecksum.ChecksumFile(localChecksum.Expected, potentialOutputFilename); err == nil && !opts.Force {
log.Printf("[INFO] %s v%s plugin is already correctly installed in %q", pr.Identifier, version, potentialOutputFilename)
return nil, nil // success
}
Expand Down
8 changes: 8 additions & 0 deletions packer/plugin-getter/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "0",
OS: "darwin", ARCH: "amd64",
Expand Down Expand Up @@ -385,6 +386,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Expand Down Expand Up @@ -430,6 +432,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "0",
OS: "darwin", ARCH: "amd64",
Expand Down Expand Up @@ -477,6 +480,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Expand Down Expand Up @@ -527,6 +531,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Expand Down Expand Up @@ -577,6 +582,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "linux", ARCH: "amd64",
Expand Down Expand Up @@ -621,6 +627,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
pluginFolderOne,
pluginFolderTwo,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Expand Down Expand Up @@ -662,6 +669,7 @@ func TestRequirement_InstallLatest(t *testing.T) {
[]string{
pluginFolderWrongChecksums,
},
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Expand Down

0 comments on commit acf3ef9

Please sign in to comment.