@@ -592,6 +592,25 @@ func (d *Driver) getMAC() (string, error) {
592
592
return dom .Devices .Interfaces [1 ].Mac .Address , nil
593
593
}
594
594
595
+ func (d * Driver ) getIPByMACFromAPI (mac string ) (string , error ) {
596
+ network , err := d .conn .LookupNetworkByName (d .PrivateNetwork )
597
+ if err != nil {
598
+ log .Errorf ("Failed to lookup network %s" , d .PrivateNetwork )
599
+ return "" , err
600
+ }
601
+ leases , err := network .GetDHCPLeases ()
602
+ if err != nil {
603
+ log .Warnf ("Failed to retrieve DHCP leases from libvirt: %v" , err )
604
+ return "" , err
605
+ }
606
+ for _ , lease := range leases {
607
+ if strings .ToLower (mac ) == strings .ToLower (lease .GetMACAddress ()) {
608
+ return lease .GetIPAddress (), nil
609
+ }
610
+ }
611
+ return "" , errors .New ("failed to match IP for MAC address" )
612
+ }
613
+
595
614
func (d * Driver ) getIPByMACFromLeaseFile (mac string ) (string , error ) {
596
615
leaseFile := fmt .Sprintf (dnsmasqLeases , d .PrivateNetwork )
597
616
data , err := ioutil .ReadFile (leaseFile )
@@ -633,6 +652,11 @@ func (d *Driver) getIPByMacFromSettings(mac string) (string, error) {
633
652
}
634
653
statusFile := fmt .Sprintf (dnsmasqStatus , bridge_name )
635
654
data , err := ioutil .ReadFile (statusFile )
655
+ if err != nil {
656
+ log .Debugf ("Failed to read dnsmasq status from %s" , statusFile )
657
+ return "" , err
658
+ }
659
+
636
660
type Lease struct {
637
661
Ip_address string `json:"ip-address"`
638
662
Mac_address string `json:"mac-address"`
@@ -659,22 +683,27 @@ func (d *Driver) getIPByMacFromSettings(mac string) (string, error) {
659
683
return ipAddr , nil
660
684
}
661
685
686
+ type ipLookupFunc func (mac string ) (string , error )
687
+
662
688
func (d * Driver ) GetIP () (string , error ) {
663
689
log .Debugf ("GetIP called for %s" , d .MachineName )
664
690
mac , err := d .getMAC ()
665
691
if err != nil {
666
692
return "" , err
667
693
}
668
- /*
669
- * TODO - Figure out what version of libvirt changed behavior and
670
- * be smarter about selecting which algorithm to use
671
- */
672
- ip , err := d .getIPByMACFromLeaseFile (mac )
673
- if ip == "" {
674
- ip , err = d .getIPByMacFromSettings (mac )
694
+
695
+ methods := []ipLookupFunc {
696
+ d .getIPByMACFromLeaseFile ,
697
+ d .getIPByMacFromSettings ,
698
+ d .getIPByMACFromAPI ,
699
+ }
700
+ for _ , method := range methods {
701
+ ip , err := method (mac )
702
+ if err == nil {
703
+ return ip , nil
704
+ }
675
705
}
676
- log .Debugf ("Unable to locate IP address for MAC %s" , mac )
677
- return ip , err
706
+ return "" , fmt .Errorf ("unable to locate IP address for MAC %s" )
678
707
}
679
708
680
709
func (d * Driver ) publicSSHKeyPath () string {
0 commit comments