From e8d3a55b5f0b70271ce33b6bd84c6619d7e7c37c Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Wed, 19 Jun 2024 16:06:15 -0400 Subject: [PATCH] packer: address errcheck lint errors In a couple places in the codebase we didn't check the errors from functions we execute. In some cases this is harmless (or at least ignorable), but others may need to log what went wrong, so for all the reported occurrences we either ignore explicitly or handle the error with a log. --- main.go | 6 +++--- packer/plugin-getter/plugins.go | 2 +- packer/plugin_client.go | 13 ++++++++----- packer/progressbar.go | 2 +- post-processor/checksum/post-processor.go | 2 +- post-processor/compress/post-processor.go | 2 +- provisioner/shell/provisioner.go | 2 +- provisioner/windows-restart/provisioner.go | 11 +++++++++-- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 6becbe491..c0f1f740f 100644 --- a/main.go +++ b/main.go @@ -416,15 +416,15 @@ func copyOutput(r io.Reader, doneCh chan<- struct{}) { wg.Add(3) go func() { defer wg.Done() - io.Copy(os.Stderr, stderrR) + _, _ = io.Copy(os.Stderr, stderrR) }() go func() { defer wg.Done() - io.Copy(os.Stdout, stdoutR) + _, _ = io.Copy(os.Stdout, stdoutR) }() go func() { defer wg.Done() - io.Copy(os.Stdout, defaultR) + _, _ = io.Copy(os.Stdout, defaultR) }() wg.Wait() diff --git a/packer/plugin-getter/plugins.go b/packer/plugin-getter/plugins.go index 1554960d6..fccf6ef5a 100644 --- a/packer/plugin-getter/plugins.go +++ b/packer/plugin-getter/plugins.go @@ -869,7 +869,7 @@ func (pr *Requirement) InstallLatest(opts InstallOptions) (*Installation, error) } copyFrom, err = f.Open() if err != nil { - multierror.Append(errs, fmt.Errorf("failed to open temp file: %w", err)) + errs = multierror.Append(errs, fmt.Errorf("failed to open temp file: %w", err)) return nil, errs } break diff --git a/packer/plugin_client.go b/packer/plugin_client.go index 01e899e28..e230edddb 100644 --- a/packer/plugin_client.go +++ b/packer/plugin_client.go @@ -198,7 +198,7 @@ func (c *PluginClient) Kill() { return } - cmd.Process.Kill() + _ = cmd.Process.Kill() // Wait for the client to finish logging so we have a complete log <-c.doneLogging @@ -247,7 +247,7 @@ func (c *PluginClient) Start() (net.Addr, error) { r := recover() if err != nil || r != nil { - cmd.Process.Kill() + _ = cmd.Process.Kill() } if r != nil { @@ -264,7 +264,7 @@ func (c *PluginClient) Start() (net.Addr, error) { defer stdout_w.Close() // Wait for the command to end. - cmd.Wait() + _ = cmd.Wait() // Log and make sure to flush the logs write away log.Printf("%s: plugin process exited\n", cmd.Path) @@ -377,7 +377,7 @@ func (c *PluginClient) logStderr(r io.Reader) { for { line, err := bufR.ReadString('\n') if line != "" { - c.config.Stderr.Write([]byte(line)) + _, _ = c.config.Stderr.Write([]byte(line)) line = strings.TrimRightFunc(line, unicode.IsSpace) @@ -406,7 +406,10 @@ func (c *PluginClient) Client() (*packerrpc.Client, error) { if tcpConn, ok := conn.(*net.TCPConn); ok { // Make sure to set keep alive so that the connection doesn't die - tcpConn.SetKeepAlive(true) + err = tcpConn.SetKeepAlive(true) + if err != nil { + log.Printf("[ERROR] failed to set keepalive for TCP connection to plugin: %s. Some instability may occur.", err) + } } client, err := packerrpc.NewClient(conn) diff --git a/packer/progressbar.go b/packer/progressbar.go index 0a9225047..57bf11ea4 100644 --- a/packer/progressbar.go +++ b/packer/progressbar.go @@ -62,7 +62,7 @@ func (p *UiProgressBar) TrackProgress(src string, currentSize, totalSize int64, newPb.Finish() p.pbs-- if p.pbs <= 0 { - p.pool.Stop() + _ = p.pool.Stop() p.pool = nil } return nil diff --git a/post-processor/checksum/post-processor.go b/post-processor/checksum/post-processor.go index efd51b6a1..0a80059e9 100644 --- a/post-processor/checksum/post-processor.go +++ b/post-processor/checksum/post-processor.go @@ -151,7 +151,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa return nil, false, true, fmt.Errorf("unable to compute %s hash for %s", ct, art) } fr.Close() - fw.WriteString(fmt.Sprintf("%x\t%s\n", h.Sum(nil), filepath.Base(art))) + _, _ = fw.WriteString(fmt.Sprintf("%x\t%s\n", h.Sum(nil), filepath.Base(art))) fw.Close() h.Reset() } diff --git a/post-processor/compress/post-processor.go b/post-processor/compress/post-processor.go index 07a74e813..e6d6655f8 100644 --- a/post-processor/compress/post-processor.go +++ b/post-processor/compress/post-processor.go @@ -371,7 +371,7 @@ func makePgzipWriter(output io.WriteCloser, compressionLevel int) (io.WriteClose if err != nil { return nil, ErrInvalidCompressionLevel } - gzipWriter.SetConcurrency(500000, runtime.GOMAXPROCS(-1)) + _ = gzipWriter.SetConcurrency(500000, runtime.GOMAXPROCS(-1)) return gzipWriter, nil } diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 593f898ca..7514506b1 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -203,7 +203,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe // Write our contents to it writer := bufio.NewWriter(tf) - writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang)) + _, _ = writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang)) for _, command := range p.config.Inline { p.config.ctx.Data = generatedData command, err := interpolate.Render(command, &p.config.ctx) diff --git a/provisioner/windows-restart/provisioner.go b/provisioner/windows-restart/provisioner.go index 4c1113a0d..83acda009 100644 --- a/provisioner/windows-restart/provisioner.go +++ b/provisioner/windows-restart/provisioner.go @@ -165,7 +165,10 @@ var waitForRestart = func(ctx context.Context, p *Provisioner, comm packersdk.Co if cmd.ExitStatus() == 0 { // Cancel reboot we created to test if machine was already rebooting cmd = &packersdk.RemoteCmd{Command: abortcommand} - cmd.RunWithUi(ctx, comm, ui) + err = cmd.RunWithUi(ctx, comm, ui) + if err != nil { + log.Printf("[ERROR] failed to run remote shutdown command: %s, build will likely hang.", err) + } break } } @@ -249,7 +252,11 @@ var waitForCommunicator = func(ctx context.Context, p *Provisioner) error { cmdModuleLoad.Stdout = &buf cmdModuleLoad.Stdout = io.MultiWriter(cmdModuleLoad.Stdout, &buf2) - cmdModuleLoad.RunWithUi(ctx, p.comm, p.ui) + err := cmdModuleLoad.RunWithUi(ctx, p.comm, p.ui) + if err != nil { + log.Printf("[ERROR] failed to run restart command on guest: %s. Build may hang.", err) + } + stdoutToRead := buf2.String() if !strings.Contains(stdoutToRead, "restarted.") {