From 3e1ddad0c7073dc68493585fb8b63e64b99cb257 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 23 Apr 2020 15:30:33 -0700 Subject: [PATCH] fix behavior when not using IAP, try to use more sophisticated streaming than buffer.String() --- builder/googlecompute/config.go | 14 +++++----- builder/googlecompute/step_start_tunnel.go | 4 +++ builder/googlecompute/tunnel_driver.go | 30 ++++++++++++---------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index e1cea263c..5b5c7c564 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -336,12 +336,14 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { } // Configure IAP: Update SSH config to use localhost proxy instead - if c.Comm.Type == "ssh" { - c.Comm.SSHHost = "localhost" - } else { - err := fmt.Errorf("Error: IAP tunnel currently only implemnted for" + - " SSH communicator") - errs = packer.MultiErrorAppend(errs, err) + if c.IAPConfig.IAP { + if c.Comm.Type == "ssh" { + c.Comm.SSHHost = "localhost" + } else { + err := fmt.Errorf("Error: IAP tunnel currently only implemnted for" + + " SSH communicator") + errs = packer.MultiErrorAppend(errs, err) + } } // Process required parameters. diff --git a/builder/googlecompute/step_start_tunnel.go b/builder/googlecompute/step_start_tunnel.go index 202cf1819..e7935ab59 100644 --- a/builder/googlecompute/step_start_tunnel.go +++ b/builder/googlecompute/step_start_tunnel.go @@ -205,5 +205,9 @@ func (s *StepStartTunnel) Run(ctx context.Context, state multistep.StateBag) mul // Cleanup stops the IAP tunnel and cleans up processes. func (s *StepStartTunnel) Cleanup(state multistep.StateBag) { + if !s.IAPConf.IAP { + log.Printf("Skipping cleanup of IAP tunnel; \"iap\" is false.") + return + } s.tunnelDriver.StopTunnel() } diff --git a/builder/googlecompute/tunnel_driver.go b/builder/googlecompute/tunnel_driver.go index 2c657ffcb..e98d0fe40 100644 --- a/builder/googlecompute/tunnel_driver.go +++ b/builder/googlecompute/tunnel_driver.go @@ -3,6 +3,7 @@ package googlecompute import ( + "bufio" "bytes" "context" "fmt" @@ -38,23 +39,24 @@ func (t *TunnelDriverLinux) StartTunnel(cancelCtx context.Context, tempScriptFil err) return err } - // Wait for tunnel to launch and gather response. TODO: do this without - // a sleep. - time.Sleep(30 * time.Second) - - // Track stdout. - sout := stdout.String() - if sout != "" { - log.Printf("[start-iap-tunnel] stdout is:") - } + time.Sleep(10 * time.Second) + // read stdout + scanner := bufio.NewScanner(&stderr) log.Printf("[start-iap-tunnel] stderr is:") - serr := stderr.String() - log.Println(serr) - if strings.Contains(serr, "ERROR") { - errIdx := strings.Index(serr, "ERROR:") - return fmt.Errorf("ERROR: %s", serr[errIdx+7:]) + for scanner.Scan() { + line := scanner.Text() + log.Println(line) + + if strings.Contains(line, "ERROR") { + errIdx := strings.Index(line, "ERROR:") + return fmt.Errorf("ERROR: %s", line[errIdx+7:]) + } } + if err := scanner.Err(); err != nil { + log.Printf("Error scanning tunnel stderr: %s", err) + } + // Store successful command on step so we can access it to cancel it // later. t.cmd = cmd