From 9503802814bdba423939089f6b0610f855520017 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 13 Sep 2019 14:23:23 -0700 Subject: [PATCH] adding ctx makes the cancel channel redundant --- builder/amazon/common/step_get_password.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/builder/amazon/common/step_get_password.go b/builder/amazon/common/step_get_password.go index 562085da9..3b751cc98 100644 --- a/builder/amazon/common/step_get_password.go +++ b/builder/amazon/common/step_get_password.go @@ -46,14 +46,13 @@ func (s *StepGetPassword) Run(ctx context.Context, state multistep.StateBag) mul // Get the password var password string var err error - cancel := make(chan struct{}) waitDone := make(chan bool, 1) go func() { ui.Say("Waiting for auto-generated password for instance...") ui.Message( "It is normal for this process to take up to 15 minutes,\n" + "but it usually takes around 5. Please wait.") - password, err = s.waitForPassword(state, cancel, ctx) + password, err = s.waitForPassword(ctx, state) waitDone <- true }() @@ -77,16 +76,12 @@ WaitLoop: err := fmt.Errorf("Timeout waiting for password.") state.Put("error", err) ui.Error(err.Error()) - close(cancel) return multistep.ActionHalt - case <-time.After(1 * time.Second): - if _, ok := state.GetOk(multistep.StateCancelled); ok { - // The step sequence was cancelled, so cancel waiting for password - // and just start the halting process. - close(cancel) - log.Println("[WARN] Interrupt detected, quitting waiting for password.") - return multistep.ActionHalt - } + case <-ctx.Done(): + // The step sequence was cancelled, so cancel waiting for password + // and just start the halting process. + log.Println("[WARN] Interrupt detected, quitting waiting for password.") + return multistep.ActionHalt } } @@ -107,14 +102,14 @@ func (s *StepGetPassword) Cleanup(multistep.StateBag) { commonhelper.RemoveSharedStateFile("winrm_password", s.BuildName) } -func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}, ctx context.Context) (string, error) { +func (s *StepGetPassword) waitForPassword(ctx context.Context, state multistep.StateBag) (string, error) { ec2conn := state.Get("ec2").(*ec2.EC2) instance := state.Get("instance").(*ec2.Instance) privateKey := s.Comm.SSHPrivateKey for { select { - case <-cancel: + case <-ctx.Done(): log.Println("[INFO] Retrieve password wait cancelled. Exiting loop.") return "", errors.New("Retrieve password wait cancelled") case <-time.After(5 * time.Second):