From 2da08635e80cc01afe70f27cc1bec11cd44c095c Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 16 Mar 2015 20:46:41 -0400 Subject: [PATCH] Refactoring to move closures into methods Signed-off-by: Dave Henderson --- drivers/softlayer/driver.go | 147 ++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 74 deletions(-) diff --git a/drivers/softlayer/driver.go b/drivers/softlayer/driver.go index f3dfccc250..5f5bc3ce65 100644 --- a/drivers/softlayer/driver.go +++ b/drivers/softlayer/driver.go @@ -307,78 +307,77 @@ func (d *Driver) PreCreateCheck() error { return nil } +func (d *Driver) waitForStart() { + log.Infof("Waiting for host to become available") + for { + s, err := d.GetState() + if err != nil { + log.Debugf("Failed to GetState - %+v", err) + continue + } + + if s == state.Running { + break + } else { + log.Debugf("Still waiting - state is %s...", s) + } + time.Sleep(2 * time.Second) + } +} + +func (d *Driver) getIp() (string, error) { + log.Infof("Getting Host IP") + for { + var ( + ip string + err error + ) + if d.deviceConfig.PrivateNet { + ip, err = d.getClient().VirtualGuest().GetPrivateIp(d.Id) + } else { + ip, err = d.getClient().VirtualGuest().GetPublicIp(d.Id) + } + if err != nil { + time.Sleep(2 * time.Second) + continue + } + // not a perfect regex, but should be just fine for our needs + exp := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`) + if exp.MatchString(ip) { + return ip, nil + } + time.Sleep(2 * time.Second) + } +} + +func (d *Driver) waitForSetupTransactions() { + log.Infof("Waiting for host setup transactions to complete") + // sometimes we'll hit a case where there's no active transaction, but if + // we check again in a few seconds, it moves to the next transaction. We + // don't want to get false-positives, so we check a few times in a row to make sure! + noActiveCount, maxNoActiveCount := 0, 3 + for { + t, err := d.GetActiveTransaction() + if err != nil { + noActiveCount = 0 + log.Debugf("Failed to GetActiveTransaction - %+v", err) + continue + } + + if t == "" { + if noActiveCount == maxNoActiveCount { + break + } + noActiveCount++ + } else { + noActiveCount = 0 + log.Debugf("Still waiting - active transaction is %s...", t) + } + time.Sleep(2 * time.Second) + } +} + func (d *Driver) Create() error { - waitForStart := func() { - log.Infof("Waiting for host to become available") - for { - s, err := d.GetState() - if err != nil { - log.Debugf("Failed to GetState - %+v", err) - continue - } - - if s == state.Running { - break - } else { - log.Debugf("Still waiting - state is %s...", s) - } - time.Sleep(2 * time.Second) - } - } - - waitForSetupTransactions := func() { - log.Infof("Waiting for host setup transactions to complete") - // sometimes we'll hit a case where there's no active transaction, but if - // we check again in a few seconds, it moves to the next transaction. We - // don't want to get false-positives, so we check a few times in a row to make sure! - noActiveCount, maxNoActiveCount := 0, 3 - for { - t, err := d.GetActiveTransaction() - if err != nil { - noActiveCount = 0 - log.Debugf("Failed to GetActiveTransaction - %+v", err) - continue - } - - if t == "" { - if noActiveCount == maxNoActiveCount { - break - } - noActiveCount++ - } else { - noActiveCount = 0 - log.Debugf("Still waiting - active transaction is %s...", t) - } - time.Sleep(2 * time.Second) - } - } - - getIp := func() { - log.Infof("Getting Host IP") - for { - var ( - ip string - err error - ) - if d.deviceConfig.PrivateNet { - ip, err = d.getClient().VirtualGuest().GetPrivateIp(d.Id) - } else { - ip, err = d.getClient().VirtualGuest().GetPublicIp(d.Id) - } - if err != nil { - time.Sleep(2 * time.Second) - continue - } - // not a perfect regex, but should be just fine for our needs - exp := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`) - if exp.MatchString(ip) { - d.IPAddress = ip - break - } - time.Sleep(2 * time.Second) - } - } - log.Infof("Creating SSH key...") key, err := d.createSSHKey() if err != nil { @@ -393,9 +392,9 @@ func (d *Driver) Create() error { return fmt.Errorf("Error creating host: %q", err) } d.Id = id - getIp() - waitForStart() - waitForSetupTransactions() + d.getIp() + d.waitForStart() + d.waitForSetupTransactions() ssh.WaitForTCP(d.IPAddress + ":22") cmd, err := drivers.GetSSHCommandFromDriver(d, "sudo apt-get update && DEBIAN_FRONTEND=noninteractive sudo apt-get install -yq curl")