Ejemplo n.º 1
0
    def _getUuidFromMac(self):
        """
        Use the mac address from the system to hash a uuid.
        """
        # Read mac address of self.deviceName
        if utils.runningInEC2():
            self.deviceName = 'eth0'

        mac = None

        if os.path.exists('/sys/class/net'):
            if not self.deviceName:
                deviceList = sorted( [ x for x in os.listdir('/sys/class/net') 
                                        if x != 'lo' ] )
                if deviceList:
                    self.deviceName = deviceList[0]

            mac = open('/sys/class/net/%s/address' % self.deviceName).read().strip()

        if not mac:
            # Legacy code
            if os.path.exists('/sbin/ifconfig'):
                logger.warn("No sysfs, falling back to ifconfig command.")
                cmd = ['/sbin/ifconfig']
                p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
                sts = p.wait()
                if sts != 0:
                    raise Exception("Unable to run ifconfig to find mac address"
                        " for local uuid generation")
                lines = p.stdout.read().strip()

                # Work around for empty deviceName bug 

                deviceList = None

                if not self.deviceName:
                    deviceList = sorted([ x.split()[0] for x in lines.split('\n')
                                        if 'lo' not in x and 'HWaddr' in x ])
                    if deviceList:
                        self.deviceName = deviceList[0]

                matcher = re.compile('^%s.*HWaddr\W(.*)$' % self.deviceName)

                for line in lines.split('\n'):
                    match = matcher.match(line)
                    if match:
                        mac = match.groups()[0].strip()

        if not mac:
            raise Exception("Unable to find mac address for "
                "local uuid generation")

        mac = mac.lower()

        if len(mac) > 16:
            mac = mac[-16:]
        elif len(mac) < 16:
            mac = mac + '0'*(16-len(mac))
        return self.asString(mac)
Ejemplo n.º 2
0
    def _getUuidFromMac(self):
        """
        Use the mac address from the system to hash a uuid.
        """
        # Read mac address of self.deviceName
        if utils.runningInEC2():
            self.deviceName = 'eth0'

        mac = None

        if os.path.exists('/sys/class/net'):
            if not self.deviceName:
                deviceList = sorted( [ x for x in os.listdir('/sys/class/net') 
                                        if x != 'lo' ] )
                if deviceList:
                    self.deviceName = deviceList[0]

            mac = open('/sys/class/net/%s/address' % self.deviceName).read().strip()

        if not mac:
            # Legacy code
            if os.path.exists('/sbin/ifconfig'):
                logger.warn("No sysfs, falling back to ifconfig command.")
                cmd = ['/sbin/ifconfig']
                p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
                sts = p.wait()
                if sts != 0:
                    raise Exception("Unable to run ifconfig to find mac address"
                        " for local uuid generation")
                lines = p.stdout.read().strip()

                # Work around for empty deviceName bug 

                deviceList = None

                if not self.deviceName:
                    deviceList = sorted([ x.split()[0] for x in lines.split('\n')
                                        if 'lo' not in x and 'HWaddr' in x ])
                    if deviceList:
                        self.deviceName = deviceList[0]

                matcher = re.compile('^%s.*HWaddr\W(.*)$' % self.deviceName)

                for line in lines.split('\n'):
                    match = matcher.match(line)
                    if match:
                        mac = match.groups()[0].strip()

        if not mac:
            raise Exception("Unable to find mac address for "
                "local uuid generation")

        mac = mac.lower()

        if len(mac) > 16:
            mac = mac[-16:]
        elif len(mac) < 16:
            mac = mac + '0'*(16-len(mac))
        return self.asString(mac)
Ejemplo n.º 3
0
 def _getEC2InstanceId(cls):
     """
     Return the EC2 instance ID if the system is running in EC2
     Return None otherwise.
     """
     if not utils.runningInEC2():
         return None
     return cls._readInstanceIdFromEC2()
Ejemplo n.º 4
0
 def _getEC2InstanceId(cls):
     """
     Return the EC2 instance ID if the system is running in EC2
     Return None otherwise.
     """
     if not utils.runningInEC2():
         return None
     return cls._readInstanceIdFromEC2()
Ejemplo n.º 5
0
    def _getLocalIp(cls, destination):
        """Return my IP address visible to the destination host"""

        if utils.runningInEC2():
            return cls._getExternalEC2Ip()

        if "://" not in destination:
            destination = "http://%s" % destination
        hostname, port = util.urlSplit(destination, defaultPort=443)[3:5]
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((hostname, int(port)))
        ret = s.getsockname()[0]
        s.close()
        return ret
Ejemplo n.º 6
0
    def _getLocalIp(cls, destination):
        """Return my IP address visible to the destination host"""

        if utils.runningInEC2():
            return cls._getExternalEC2Ip()

        if "://" not in destination:
            destination = "http://%s" % destination
        hostname, port = util.urlSplit(destination, defaultPort=443)[3:5]
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((hostname, int(port)))
        ret = s.getsockname()[0]
        s.close()
        return ret
Ejemplo n.º 7
0
    def getIpAddresses(self):
        if self.ips:
            return self.ips
        else:
            for iface in self.hardware['Linux_IPProtocolEndpoint'].values():
                ipv4 = iface['IPv4Address']
                if ipv4 is not None and  ipv4 not in ('NULL', '127.0.0.1'):
                    device = iface['Name']
                    device = device.split('_')
                    if len(device) > 1:
                        deviceName = device[1]
                    else:
                        deviceName = device[0]
                    dnsName = self.resolve(ipv4) or ipv4
                    ip = self.IP(ipv4=ipv4, netmask=iface['SubnetMask'],
                        device=deviceName, dns_name=dnsName)
                    self.ips.append(ip)

        if utils.runningInEC2():
            self.ips.append(self._getExternalEC2Network())

        return self.ips
Ejemplo n.º 8
0
    def getIpAddresses(self):
        if self.ips:
            return self.ips
        else:
            for iface in self.hardware['Linux_IPProtocolEndpoint'].values():
                ipv4 = iface['IPv4Address']
                if ipv4 is not None and ipv4 not in ('NULL', '127.0.0.1'):
                    device = iface['Name']
                    device = device.split('_')
                    if len(device) > 1:
                        deviceName = device[1]
                    else:
                        deviceName = device[0]
                    dnsName = self.resolve(ipv4) or ipv4
                    ip = self.IP(ipv4=ipv4,
                                 netmask=iface['SubnetMask'],
                                 device=deviceName,
                                 dns_name=dnsName)
                    self.ips.append(ip)

        if utils.runningInEC2():
            self.ips.append(self._getExternalEC2Network())

        return self.ips