fix behavior when not using IAP, try to use more sophisticated streaming than buffer.String()

This commit is contained in:
Megan Marsh 2020-04-23 15:30:33 -07:00
parent 9353635b43
commit 3e1ddad0c7
3 changed files with 28 additions and 20 deletions

View File

@ -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.

View File

@ -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()
}

View File

@ -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