adding ctx makes the cancel channel redundant

This commit is contained in:
Megan Marsh 2019-09-13 14:23:23 -07:00
parent 4b497006b3
commit 9503802814
1 changed files with 8 additions and 13 deletions

View File

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