def get_guest_ip(self, str_mac):
        DHCP_SERVER = self.data["DHCP_SERVER"]
        DHCP_USERNAME = self.data["DHCP_USERNAME"]
        DHCP_PASSWORD = self.data["DHCP_PASSWORD"]

        self.log('INFO', 'Query IP for MAC {} from DHCP server'.format(str_mac))

        time_start = time.time()
        guest_ip = ''
        while time.time() - time_start < 300:
            try:
                guest_ip = dhcp_query_ip(server=DHCP_SERVER,
                                         username=DHCP_USERNAME,
                                         password=DHCP_PASSWORD,
                                         mac=str_mac)
                rsp = os.system('ping -c 1 {}'.format(guest_ip))
                if rsp != 0:
                    self.log('INFO', 'Find an IP {} lease for MAC {}, but this IP is not online'.
                             format(guest_ip, str_mac))
                    time.sleep(30)
                    continue
                else:
                    self.log('INFO', 'Find an IP {} lease for MAC {}, this IP works'.
                             format(guest_ip, str_mac))
                    break
            except:
                self.log('WARNING', 'Fail to query IP for MAC {}'.format(str_mac))

        if not guest_ip:
            raise Exception('Fail to get IP for MAC {} in 300s'.format(str_mac))
        else:
            return guest_ip
Beispiel #2
0
    def get_guest_ip(self, str_mac):
        DHCP_SERVER = self.data["DHCP_SERVER"]
        DHCP_USERNAME = self.data["DHCP_USERNAME"]
        DHCP_PASSWORD = self.data["DHCP_PASSWORD"]

        self.log('INFO', 'Query IP for MAC {} from DHCP server'.format(str_mac))

        time_start = time.time()
        guest_ip = ''
        while time.time() - time_start < 300:
            try:
                guest_ip = dhcp_query_ip(server=DHCP_SERVER,
                                         username=DHCP_USERNAME,
                                         password=DHCP_PASSWORD,
                                         mac=str_mac)
                rsp = os.system('ping -c 1 {}'.format(guest_ip))
                if rsp != 0:
                    self.log('INFO', 'Find an IP {} lease for MAC {}, but this IP is not online'.
                             format(guest_ip, str_mac))
                    time.sleep(30)
                    continue
                else:
                    self.log('INFO', 'Find an IP {} lease for MAC {}, this IP works'.
                             format(guest_ip, str_mac))
                    break
            except:
                self.log('WARNING', 'Fail to query IP for MAC {}'.format(str_mac))

        if not guest_ip:
            raise Exception('Fail to get IP for MAC {} in 300s'.format(str_mac))
        else:
            return guest_ip
    def get_guest_ip(self, macs):
        DHCP_SERVER = self.data["DHCP_SERVER"]
        DHCP_USERNAME = self.data["DHCP_USERNAME"]
        DHCP_PASSWORD = self.data["DHCP_PASSWORD"]

        self.log('INFO', 'Query IP for MAC {} from DHCP server'.format(macs))

        time_start = time.time()
        elapse_time = 1200
        guest_ip = None
        while time.time() - time_start < elapse_time:
            for str_mac in macs:
                try:
                    guest_ip = dhcp_query_ip(server=DHCP_SERVER,
                                             username=DHCP_USERNAME,
                                             password=DHCP_PASSWORD,
                                             mac=str_mac)
                    rsp = os.system('ping -c 1 {}'.format(guest_ip))
                    if rsp != 0:
                        self.log('INFO', 'Find an IP {} lease for MAC {}, but this IP is not online'.
                                 format(guest_ip, str_mac))
                        guest_ip = None
                    else:
                        self.log('INFO', 'Find an IP {} lease for MAC {}, this IP works'.
                                 format(guest_ip, str_mac))
                        return guest_ip
                except:
                    self.log('WARNING', 'Fail to query IP for MAC {}'.format(str_mac))
            time.sleep(30)

        if not guest_ip:
            self.log('WARNING', 'Fail to get IP for MAC {} in {}s'.format(str_mac, elapse_time))
            return
Beispiel #4
0
    def get_host_ssh(self, obj_node, username, password, **kwargs):
        '''
        Build a SSH connection to host in virtual node
        '''
        if 'qemu_ip' in kwargs:
            self.log('INFO', 'Build host SSH to {}@{}'.format(username, kwargs['qemu_ip']))
            conn = CSSH(ip=kwargs['qemu_ip'], username=username, password=password, port=kwargs.get('port', 22))
            conn.connect()
            return conn

        # To get host's IP address:
        #     - Get MAC
        #     - Get IP via DHCP lease on DHCP server
        #     - Check if IP is available
        #     - SSH to host
        if 'dhcp_server' in kwargs and 'dhcp_user' in kwargs and 'dhcp_pass' in kwargs:
            self.log('INFO', 'Query host IP from DHCP server {} ...'.format(kwargs['dhcp_server']))

            # Get IP
            rsp_qemu = obj_node.get_bmc().ssh.remote_shell('ps aux | grep qemu')
            if rsp_qemu['exitcode'] != 0:
                raise Exception('Fail to get node {} qemu MAC address'.format(obj_node.get_name()))
            qemu_mac = rsp_qemu['stdout'].split('mac=')[1].split(' ')[0].lower()
            self.log('INFO', 'Node {} host MAC address is: {}'.format(obj_node.get_name(), qemu_mac))

            self.log('INFO', 'Wait node {} host IP address in up to 300s ...'.format(obj_node.get_name()))
            time_start = time.time()
            qemu_ip = ''
            while time.time() - time_start < 300:
                try:
                    qemu_ip = dhcp_query_ip(server=kwargs['dhcp_server'],
                                            username=kwargs['dhcp_user'],
                                            password=kwargs['dhcp_pass'],
                                            mac=qemu_mac)
                    if qemu_ip == obj_node.get_bmc().get_ip():
                        self.log('WARNING', 'IP lease for mac {} is {} to node {}\'s BMC, '
                                            'waiting for host IP lease, retry after 30s ...'.
                                 format(qemu_mac, qemu_ip, obj_node.get_name()))
                        time.sleep(30)
                        continue
                    rsp = os.system('ping -c 1 {}'.format(qemu_ip))
                    if rsp != 0:
                        self.log('WARNING', 'Find an IP {} lease for mac {} on node {}, '
                                            'but this IP address is not available, retry after 30s ...'.
                                 format(qemu_ip, qemu_mac, obj_node.get_name()))
                        time.sleep(30)
                        continue
                    else:
                        self.log('INFO', 'Node {} host get IP {}'.format(obj_node.get_name(), qemu_ip))
                        break
                except Exception, e:
                    self.log('WARNING', 'Fail to get node {}\'s host IP: {}'.format(obj_node.get_name(), e))
                    time.sleep(30)

            if not qemu_ip:
                raise Exception('Fail to get node {}\'s host IP in 300s, '
                                'check if vSwith\'s promiscuous mode is "Accept"'.
                                format(obj_node.get_name()))

            conn = CSSH(ip=qemu_ip,
                        username=kwargs.get('host_username', 'june'),
                        password=kwargs.get('host_password', '111111'),
                        port=kwargs.get('port', 22))
            conn.connect()
            return conn