From 376d767f295f8c75cabaf75d5c9a4c6c276d254e Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 18 Jul 2024 13:13:29 +0000 Subject: [PATCH] backport of commit 3053b4e4e1599d526fa01e94e8986272c7b2dfa7 --- command/fmt_test.go | 2 +- datasource/http/data.go | 7 +++--- fix/fixer_comm_config.go | 24 ++++--------------- fix/fixer_galaxy_command.go | 8 ++----- fix/fixer_ssh_timeout.go | 8 ++----- packer/core.go | 5 ++-- packer/plugin-getter/plugins.go | 2 +- post-processor/compress/post-processor.go | 1 - provisioner/shell/provisioner.go | 5 +--- .../windows-restart/provisioner_test.go | 10 ++++---- 10 files changed, 20 insertions(+), 52 deletions(-) diff --git a/command/fmt_test.go b/command/fmt_test.go index 36497bc80dd..73a688ba45a 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -166,7 +166,7 @@ func Test_fmt_pipe(t *testing.T) { p := helperCommand(t, tc.command...) p.Stdin = strings.NewReader(tc.piped) p.Env = append(p.Env, tc.env...) - fmt.Println(fmt.Sprintf("Path: %s", p.Path)) + t.Logf("Path: %s", p.Path) bs, err := p.Output() if err != nil { t.Fatalf("Error occurred running command %v: %s", err, bs) diff --git a/datasource/http/data.go b/datasource/http/data.go index 5359e0c9140..89b8c826ffe 100644 --- a/datasource/http/data.go +++ b/datasource/http/data.go @@ -85,7 +85,7 @@ func isContentTypeText(contentType string) bool { allowedContentTypes := []*regexp.Regexp{ regexp.MustCompile("^text/.+"), regexp.MustCompile("^application/json$"), - regexp.MustCompile("^application/samlmetadata\\+xml"), + regexp.MustCompile(`^application/samlmetadata\+xml`), } for _, r := range allowedContentTypes { @@ -131,9 +131,8 @@ func (d *Datasource) Execute() (cty.Value, error) { contentType := resp.Header.Get("Content-Type") if contentType == "" || isContentTypeText(contentType) == false { - fmt.Println(fmt.Sprintf( - "Content-Type is not recognized as a text type, got %q", - contentType)) + fmt.Printf("Content-Type is not recognized as a text type, got %q\n", + contentType) fmt.Println("If the content is binary data, Packer may not properly handle the contents of the response.") } diff --git a/fix/fixer_comm_config.go b/fix/fixer_comm_config.go index 3d02fcdac33..219d01d9551 100644 --- a/fix/fixer_comm_config.go +++ b/fix/fixer_comm_config.go @@ -15,7 +15,7 @@ type FixerCommConfig struct{} func (FixerCommConfig) DeprecatedOptions() map[string][]string { return map[string][]string{ - "*": []string{"ssh_host_port_min", "ssh_host_port_max", + "*": {"ssh_host_port_min", "ssh_host_port_max", "ssh_skip_nat_mapping"}, } } @@ -46,14 +46,9 @@ func (FixerCommConfig) Fix(input map[string]interface{}) (map[string]interface{} // ssh_host_port_min to host_port_min if _, ok := builders["host_port_min"]; ok { - // drop ssh_host_port_min if it is also included - if _, sshHostPortMinIncluded := builders["ssh_host_port_min"]; sshHostPortMinIncluded { - delete(builders, "ssh_host_port_min") - } - + delete(builders, "ssh_host_port_min") } else if _, ok := builders["ssh_host_port_min"]; ok { - // replace ssh_host_port_min with host_port_min sshHostPortMinRaw := builders["ssh_host_port_min"] delete(builders, "ssh_host_port_min") @@ -62,31 +57,20 @@ func (FixerCommConfig) Fix(input map[string]interface{}) (map[string]interface{} // ssh_host_port_max to host_port_max if _, ok := builders["host_port_max"]; ok { - // drop ssh_host_port_max if it is also included - if _, sshHostPortMaxIncluded := builders["ssh_host_port_max"]; sshHostPortMaxIncluded { - delete(builders, "ssh_host_port_max") - } - + delete(builders, "ssh_host_port_max") } else if _, ok := builders["ssh_host_port_max"]; ok { - // replace ssh_host_port_max with host_port_max sshHostPortMaxRaw := builders["ssh_host_port_max"] delete(builders, "ssh_host_port_max") builders["host_port_max"] = sshHostPortMaxRaw - } // ssh_skip_nat_mapping to skip_nat_mapping if _, ok := builders["skip_nat_mapping"]; ok { - // drop ssh_skip_nat_mapping if it is also included - if _, sshSkipNatMappingIncluded := builders["ssh_skip_nat_mapping"]; sshSkipNatMappingIncluded { - delete(builders, "ssh_skip_nat_mapping") - } - + delete(builders, "ssh_skip_nat_mapping") } else if _, ok := builders["ssh_skip_nat_mapping"]; ok { - // replace ssh_skip_nat_mapping with skip_nat_mapping sshSkipNatMappingRaw := builders["ssh_skip_nat_mapping"] sshSkipNatMappingBool, ok := sshSkipNatMappingRaw.(bool) diff --git a/fix/fixer_galaxy_command.go b/fix/fixer_galaxy_command.go index df36558275b..c39aec43885 100644 --- a/fix/fixer_galaxy_command.go +++ b/fix/fixer_galaxy_command.go @@ -13,7 +13,7 @@ type FixerGalaxyCommand struct{} func (FixerGalaxyCommand) DeprecatedOptions() map[string][]string { return map[string][]string{ - "ansible": []string{"galaxycommand"}, + "ansible": {"galaxycommand"}, } } @@ -40,12 +40,8 @@ func (FixerGalaxyCommand) Fix(input map[string]interface{}) (map[string]interfac } if _, ok := provisioners["galaxy_command"]; ok { - // drop galaxycommand if it is also included - if _, galaxyCommandIncluded := provisioners["galaxycommand"]; galaxyCommandIncluded { - delete(provisioners, "galaxycommand") - } - + delete(provisioners, "galaxycommand") } else { // replace galaxycommand with galaxy_command if it exists diff --git a/fix/fixer_ssh_timeout.go b/fix/fixer_ssh_timeout.go index d73b47d66e8..1279addc356 100644 --- a/fix/fixer_ssh_timeout.go +++ b/fix/fixer_ssh_timeout.go @@ -12,7 +12,7 @@ type FixerSSHTimout struct{} func (FixerSSHTimout) DeprecatedOptions() map[string][]string { return map[string][]string{ - "*": []string{"ssh_wait_timeout"}, + "*": {"ssh_wait_timeout"}, } } @@ -35,12 +35,8 @@ func (FixerSSHTimout) Fix(input map[string]interface{}) (map[string]interface{}, } if _, ok := builders["ssh_timeout"]; ok { - // drop ssh_wait_timeout if it is also included - if _, sshWaitTimeoutIncluded := builders["ssh_wait_timeout"]; sshWaitTimeoutIncluded { - delete(builders, "ssh_wait_timeout") - } - + delete(builders, "ssh_wait_timeout") } else { // replace ssh_wait_timeout with ssh_timeout if it exists diff --git a/packer/core.go b/packer/core.go index cac6469e8ff..f6e3926c4a7 100644 --- a/packer/core.go +++ b/packer/core.go @@ -891,7 +891,7 @@ func (c *Core) renderVarsRecursively() (*interpolate.Context, error) { for _, kv := range sortedMap { // Interpolate the default renderedV, err := interpolate.RenderRegex(kv.Value, ctx, renderFilter) - switch err.(type) { + switch err := err.(type) { case nil: // We only get here if interpolation has succeeded, so something is // different in this loop than in the last one. @@ -910,8 +910,7 @@ func (c *Core) renderVarsRecursively() (*interpolate.Context, error) { shouldRetry = true } case ttmp.ExecError: - castError := err.(ttmp.ExecError) - if strings.Contains(castError.Error(), interpolate.ErrVariableNotSetString) { + if strings.Contains(err.Error(), interpolate.ErrVariableNotSetString) { shouldRetry = true failedInterpolation = fmt.Sprintf(`"%s": "%s"; error: %s`, kv.Key, kv.Value, err) } else { diff --git a/packer/plugin-getter/plugins.go b/packer/plugin-getter/plugins.go index fccf6ef5a95..314eff0225a 100644 --- a/packer/plugin-getter/plugins.go +++ b/packer/plugin-getter/plugins.go @@ -1022,6 +1022,6 @@ func init() { // Should never error if both components are set localAPIVersion, err = NewAPIVersion(fmt.Sprintf("x%s.%s", pluginsdk.APIVersionMajor, pluginsdk.APIVersionMinor)) if err != nil { - panic(fmt.Sprintf("malformed API version in Packer. This is a programming error, please open an error to report it.")) + panic("malformed API version in Packer. This is a programming error, please open an error to report it.") } } diff --git a/post-processor/compress/post-processor.go b/post-processor/compress/post-processor.go index e6d6655f855..edaccbccab2 100644 --- a/post-processor/compress/post-processor.go +++ b/post-processor/compress/post-processor.go @@ -306,7 +306,6 @@ func (config *Config) detectFromFilename() { // We didn't match a known compression format. Default to tar + pgzip config.Algorithm = "pgzip" config.Archive = "tar" - return } func makeBGZFWriter(output io.WriteCloser, compressionLevel int) (io.WriteCloser, error) { diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 7514506b13a..ddf114a7a28 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -372,10 +372,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe if p.config.PauseAfter != 0 { ui.Say(fmt.Sprintf("Pausing %s after this provisioner...", p.config.PauseAfter)) - select { - case <-time.After(p.config.PauseAfter): - return nil - } + time.Sleep(p.config.PauseAfter) } return nil diff --git a/provisioner/windows-restart/provisioner_test.go b/provisioner/windows-restart/provisioner_test.go index c2d934ed539..8a8df1ca75e 100644 --- a/provisioner/windows-restart/provisioner_test.go +++ b/provisioner/windows-restart/provisioner_test.go @@ -210,17 +210,15 @@ func TestProvision_waitForRestartTimeout(t *testing.T) { // Block until cancel comes through waitForCommunicator = func(context.Context, *Provisioner) error { - for { - select { - case <-waitDone: - waitContinue <- true - } + for range waitDone { } + waitContinue <- true + return nil } go func() { err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) - waitDone <- true + close(waitDone) }() <-waitContinue