예제 #1
0
def create_vm_database_info(inst_name, **kwargs):
    """

    :param inst_name: VM name
    :param kwargs: host, user, passwd dict
    :return:
    """
    log.info("Start to create [%s] information to databse.", inst_name)

    host_name = kwargs['host']
    user = kwargs['user'] if kwargs['user'] else "root"
    passwd = str(kwargs['passwd']).replace('\\', '') if kwargs['passwd'] else ""

    virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
    vnet_driver = VirtFactory.get_vnet_driver(host_name, user, passwd)
    db_driver = DbFactory.get_db_driver("VirtHost")

    vm_record = virt_driver.get_vm_record(inst_name=inst_name)
    if not vm_record:
        return False

    hostname = inst_name
    sn = vm_record['uuid']
    cpu_cores = vm_record['VCPUs_live']
    memory_size = vm_record['memory_target']

    disk_info = virt_driver.get_all_disk(inst_name=inst_name)
    disk_num = len(disk_info)
    disk_size = virt_driver.get_disk_size(inst_name, 0)  # only write the system disk size when create

    vm_host_ip = vnet_driver.get_host_manage_interface_infor()['IP']

    ret = db_driver.create(hostname, sn, cpu_cores, memory_size, disk_size, disk_num, vm_host_ip=vm_host_ip)
    if ret:
        log.info("Create record to database successfully.")
    else:
        log.error("Create record to database failed.")

    return ret
예제 #2
0
def update_vm_database_info(inst_name, **kwargs):
    """
    This function is used to sync VM information when config changed, include:cpu_cores, memory_size, disk_num
    :param inst_name:
    :param kwargs:
    :return:
    """
    log.info("Start to update [%s] information to databse.", inst_name)

    host_name = kwargs['host']
    user = kwargs['user'] if kwargs['user'] else "root"
    passwd = str(kwargs['passwd']).replace('\\', '') if kwargs['passwd'] else ""

    virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
    db_driver = DbFactory.get_db_driver("VirtHost")

    vm_record = virt_driver.get_vm_record(inst_name=inst_name)
    if not vm_record:
        return False

    sn = vm_record['uuid']
    if not db_driver.query(sn=sn):
        log.info("No record found with given VM:[%s], don't update database", inst_name)
        return True

    cpu_cores = vm_record['VCPUs_live']
    memory_size = vm_record['memory_target']

    disk_info = virt_driver.get_all_disk(inst_name=inst_name)
    disk_num = len(disk_info)

    # TODO: sync disk size
    # for disk in disk_info:
    #     disk_size += virt_driver.get_disk_size(inst_name, disk)
    sync_data = {"cpu_cores": cpu_cores,
                 "memory_size": memory_size,
                 "disk_num": disk_num
                 }
    try:
        ret = db_driver.update(sn=sn, data=sync_data)
    except Exception as error:
        log.debug("Exception raise when update vm database: %s", error)
        ret = False
    if not ret:
        log.warn("Update database information with ret: [%s], data: %s", ret, sync_data)

    return ret
예제 #3
0
def update_vcpu_to_database(inst_name, **kwargs):
    """
    :param inst_name:
    :return:
    """
    log.info("Update [%s] VCPU information to database.", inst_name)
    host_name = kwargs['host']
    user = kwargs['user'] if kwargs['user'] else "root"
    passwd = str(kwargs['passwd']).replace('\\', '') if kwargs['passwd'] else ""

    virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
    db_driver = DbFactory.get_db_driver("VirtHost")

    vm_record = virt_driver.get_vm_record(inst_name=inst_name)
    if not vm_record:
        return False
    cpu_cores = vm_record['VCPUs_live']
    sn = vm_record['uuid']

    return db_driver.update(sn=sn, data={"cpu_cores": cpu_cores})
예제 #4
0
def update_memory_to_database(inst_name, **kwargs):
    """
    :param inst_name:
    :param kwargs:
    :return:
    """
    log.info("Update [%s] memory information to database.", inst_name)
    host_name = kwargs['host']
    user = kwargs['user'] if kwargs['user'] else "root"
    passwd = str(kwargs['passwd']).replace('\\', '') if kwargs['passwd'] else ""

    virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
    db_driver = DbFactory.get_db_driver("VirtHost")

    vm_record = virt_driver.get_vm_record(inst_name=inst_name)
    if not vm_record:
        return False
    memory_size = vm_record['memory_target']
    sn = vm_record['uuid']

    return db_driver.update(sn=sn, data={"memory_size": memory_size})
예제 #5
0
 def __init__(self, host_name=None, user="******", passwd=""):
     self.virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
     self.vnet_driver = VirtFactory.get_vnet_driver(host_name, user, passwd)
     self.db_driver = DbFactory.get_db_driver("VirtHost")
예제 #6
0
            "Can not connect to virtual driver or DB driver, initial VirtHostDomain failed."
        )
        exit(1)

    virt_driver = virthost.virt_driver

    if options.all:
        log.info("Start power off all VMs in server [%s].", host_name)
        all_vms_names = virt_driver.get_vm_list()
        for vm_name in all_vms_names:
            virthost.power_off_vm(vm_name)
            time.sleep(0.5)
        exit(0)

    elif args:
        virt_driver = VirtFactory.get_virt_driver(host_name, user, passwd)
        res_dict = {}
        for vm_name in args:
            if not virt_driver.is_instance_exists(vm_name):
                log.warn("No VM exists with name [%s].", vm_name)
                # if vmname doesn't exist, set it as power off failed
                res_dict[vm_name] = 1
                continue

            res_dict.setdefault(vm_name, 0)
            ret = virthost.power_off_vm(vm_name)
            if not ret:
                log.error("VM [%s] power on failed.", vm_name)
                res_dict[vm_name] = 1
            time.sleep(0.5)