X Tutup
Skip to content

Commit 2da0863

Browse files
Refactoring to move closures into methods
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
1 parent 30e0ca0 commit 2da0863

File tree

1 file changed

+61
-62
lines changed

1 file changed

+61
-62
lines changed

drivers/softlayer/driver.go

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -307,78 +307,77 @@ func (d *Driver) PreCreateCheck() error {
307307
return nil
308308
}
309309

310-
func (d *Driver) Create() error {
311-
waitForStart := func() {
312-
log.Infof("Waiting for host to become available")
313-
for {
314-
s, err := d.GetState()
315-
if err != nil {
316-
log.Debugf("Failed to GetState - %+v", err)
317-
continue
318-
}
310+
func (d *Driver) waitForStart() {
311+
log.Infof("Waiting for host to become available")
312+
for {
313+
s, err := d.GetState()
314+
if err != nil {
315+
log.Debugf("Failed to GetState - %+v", err)
316+
continue
317+
}
319318

320-
if s == state.Running {
321-
break
322-
} else {
323-
log.Debugf("Still waiting - state is %s...", s)
324-
}
325-
time.Sleep(2 * time.Second)
319+
if s == state.Running {
320+
break
321+
} else {
322+
log.Debugf("Still waiting - state is %s...", s)
326323
}
324+
time.Sleep(2 * time.Second)
327325
}
326+
}
328327

329-
waitForSetupTransactions := func() {
330-
log.Infof("Waiting for host setup transactions to complete")
331-
// sometimes we'll hit a case where there's no active transaction, but if
332-
// we check again in a few seconds, it moves to the next transaction. We
333-
// don't want to get false-positives, so we check a few times in a row to make sure!
334-
noActiveCount, maxNoActiveCount := 0, 3
335-
for {
336-
t, err := d.GetActiveTransaction()
337-
if err != nil {
338-
noActiveCount = 0
339-
log.Debugf("Failed to GetActiveTransaction - %+v", err)
340-
continue
341-
}
342-
343-
if t == "" {
344-
if noActiveCount == maxNoActiveCount {
345-
break
346-
}
347-
noActiveCount++
348-
} else {
349-
noActiveCount = 0
350-
log.Debugf("Still waiting - active transaction is %s...", t)
351-
}
328+
func (d *Driver) getIp() (string, error) {
329+
log.Infof("Getting Host IP")
330+
for {
331+
var (
332+
ip string
333+
err error
334+
)
335+
if d.deviceConfig.PrivateNet {
336+
ip, err = d.getClient().VirtualGuest().GetPrivateIp(d.Id)
337+
} else {
338+
ip, err = d.getClient().VirtualGuest().GetPublicIp(d.Id)
339+
}
340+
if err != nil {
352341
time.Sleep(2 * time.Second)
342+
continue
343+
}
344+
// not a perfect regex, but should be just fine for our needs
345+
exp := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
346+
if exp.MatchString(ip) {
347+
return ip, nil
353348
}
349+
time.Sleep(2 * time.Second)
354350
}
351+
}
355352

356-
getIp := func() {
357-
log.Infof("Getting Host IP")
358-
for {
359-
var (
360-
ip string
361-
err error
362-
)
363-
if d.deviceConfig.PrivateNet {
364-
ip, err = d.getClient().VirtualGuest().GetPrivateIp(d.Id)
365-
} else {
366-
ip, err = d.getClient().VirtualGuest().GetPublicIp(d.Id)
367-
}
368-
if err != nil {
369-
time.Sleep(2 * time.Second)
370-
continue
371-
}
372-
// not a perfect regex, but should be just fine for our needs
373-
exp := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
374-
if exp.MatchString(ip) {
375-
d.IPAddress = ip
353+
func (d *Driver) waitForSetupTransactions() {
354+
log.Infof("Waiting for host setup transactions to complete")
355+
// sometimes we'll hit a case where there's no active transaction, but if
356+
// we check again in a few seconds, it moves to the next transaction. We
357+
// don't want to get false-positives, so we check a few times in a row to make sure!
358+
noActiveCount, maxNoActiveCount := 0, 3
359+
for {
360+
t, err := d.GetActiveTransaction()
361+
if err != nil {
362+
noActiveCount = 0
363+
log.Debugf("Failed to GetActiveTransaction - %+v", err)
364+
continue
365+
}
366+
367+
if t == "" {
368+
if noActiveCount == maxNoActiveCount {
376369
break
377370
}
378-
time.Sleep(2 * time.Second)
371+
noActiveCount++
372+
} else {
373+
noActiveCount = 0
374+
log.Debugf("Still waiting - active transaction is %s...", t)
379375
}
376+
time.Sleep(2 * time.Second)
380377
}
378+
}
381379

380+
func (d *Driver) Create() error {
382381
log.Infof("Creating SSH key...")
383382
key, err := d.createSSHKey()
384383
if err != nil {
@@ -393,9 +392,9 @@ func (d *Driver) Create() error {
393392
return fmt.Errorf("Error creating host: %q", err)
394393
}
395394
d.Id = id
396-
getIp()
397-
waitForStart()
398-
waitForSetupTransactions()
395+
d.getIp()
396+
d.waitForStart()
397+
d.waitForSetupTransactions()
399398
ssh.WaitForTCP(d.IPAddress + ":22")
400399

401400
cmd, err := drivers.GetSSHCommandFromDriver(d, "sudo apt-get update && DEBIAN_FRONTEND=noninteractive sudo apt-get install -yq curl")

0 commit comments

Comments
 (0)
X Tutup