Example #1
0
def configure_host_by_mac(mac_addr):
    
    avl_private_ip = None
    ip_info = db.private_ip_pool(mac_addr=mac_addr)
    if ip_info:
        avl_private_ip = ip_info.private_ip
    else:
        avl_ip = db((~db.private_ip_pool.id.belongs(db()._select(db.host.host_ip)))
                    & (db.private_ip_pool.vlan == HOST_VLAN_ID)).select(db.private_ip_pool.private_ip)
        if avl_ip.first():
            ip_info = avl_ip.first()
            avl_private_ip = ip_info['private_ip']

    if avl_private_ip:
        logger.debug('Available IP for mac address %s is %s'%(mac_addr, avl_private_ip))
        host_name = 'host'+str(avl_private_ip.split('.')[3])
        create_dhcp_entry(host_name, mac_addr, avl_private_ip)
        db.host[0] = dict(host_ip=ip_info['id'], 
                          host_name=host_name, 
                          mac_addr=mac_addr, 
                          status=HOST_STATUS_DOWN)
        return 'Host configured. Proceed for PXE boot.'
    else:
        logger.error('Available Private IPs for host are exhausted.')
        return 'Available Private IPs for host are exhausted.'
Example #2
0
def add_orphan_vm(vm_name, host_id):

    host_details = db.host[host_id]
    host_ip = host_details.host_ip.private_ip
    connection_object = libvirt.openReadOnly("qemu+ssh://root@" + host_ip +
                                             "/system")
    domain = connection_object.lookupByName(vm_name)
    vm_state = domain.info()[0]
    vm_status = vm_state_map[vm_state]
    # Parse domain XML to get information about VM
    root = etree.fromstring(domain.XMLDesc(0))

    ram_elem = root.xpath('memory')[0]
    ram_in_kb = int(ram_elem.text)
    ram_in_mb = int(round(int(ram_in_kb) / (1024), 0))

    cpu_elem = root.xpath('vcpu')[0]
    cpu = int(cpu_elem.text)

    vnc_elem = root.xpath("devices/graphics[@type='vnc']")[0]
    vnc_port = vnc_elem.attrib['port']

    mac_elem = root.xpath("devices/interface[@type='network']/mac")[0]
    mac_address = mac_elem.attrib['address']

    ip_addr = db.private_ip_pool(mac_addr=mac_address)
    ip_address = None
    if ip_addr:
        ip_address = ip_addr['id']

    template_elem = root.xpath("devices/disk[@type='file']/source")[0]
    template_file = template_elem.attrib['file']

    command = "qemu-img info " + template_file + " | grep 'virtual size'"
    ret = execute_remote_cmd(
        host_ip, 'root',
        command)  # Returns e.g. virtual size: 40G (42949672960 bytes)
    hdd = int(ret[ret.index(':') + 1:ret.index('G ')].strip())

    security_domain_row = db.security_domain(vlan=ip_addr['vlan'])

    db.vm_data.insert(
        vm_name=vm_name,
        vm_identity=(vm_name),
        RAM=ram_in_mb,
        HDD=hdd,
        extra_HDD=0,
        vCPU=cpu,
        host_id=host_id,
        template_id=1,  #TBD
        datastore_id=1,  #TBD
        owner_id=SYSTEM_USER,
        requester_id=SYSTEM_USER,
        private_ip=ip_address,
        vnc_port=vnc_port,
        purpose='Added by System',
        security_domain=security_domain_row['id'],
        status=vm_status)

    return
Example #3
0
def configure_host_by_mac(mac_addr):
    
    avl_private_ip = None
    ip_info = db.private_ip_pool(mac_addr=mac_addr)
    if ip_info:
        avl_private_ip = ip_info.private_ip
    else:
        avl_ip = db((~db.private_ip_pool.id.belongs(db()._select(db.host.host_ip)))
                    & (db.private_ip_pool.vlan == HOST_VLAN_ID)).select(db.private_ip_pool.private_ip)
        if avl_ip.first():
            ip_info = avl_ip.first()
            avl_private_ip = ip_info['private_ip']

    if avl_private_ip:
        logger.debug('Available IP for mac address %s is %s'%(mac_addr, avl_private_ip))
        host_name = 'host'+str(avl_private_ip.split('.')[3])
        create_dhcp_entry(host_name, mac_addr, avl_private_ip)
        db.host[0] = dict(host_ip=ip_info['id'], 
                          host_name=host_name, 
                          mac_addr=mac_addr, 
                          status=HOST_STATUS_DOWN)
        return 'Host configured. Proceed for PXE boot.'
    else:
        logger.error('Available Private IPs for host are exhausted.')
        return 'Available Private IPs for host are exhausted.'
Example #4
0
def add_orphan_vm(vm_name, host_id):
    """
    Add Orphan VM information to database. VM information is retrieved from the VM definition XML.
    'System User' is added as owner of the VM.
    """
    host_details = db.host[host_id]
    host_ip = host_details.host_ip.private_ip
    connection_object = libvirt.openReadOnly("qemu+ssh://root@" + host_ip + "/system")
    domain = connection_object.lookupByName(vm_name)
    vm_state = domain.info()[0]
    vm_status = vm_state_map[vm_state]    
    # Parse domain XML to get information about VM
    root = etree.fromstring(domain.XMLDesc(0))

    ram_elem = root.xpath('memory')[0]
    ram_in_kb = int(ram_elem.text)
    ram_in_mb = int(round(int(ram_in_kb)/(1024),0))

    cpu_elem = root.xpath('vcpu')[0]
    cpu = int(cpu_elem.text)

    vnc_elem = root.xpath("devices/graphics[@type='vnc']")[0]
    vnc_port = vnc_elem.attrib['port']
    
    mac_elem = root.xpath("devices/interface[@type='network']/mac")[0]
    mac_address = mac_elem.attrib['address']

    ip_addr = db.private_ip_pool(mac_addr = mac_address)
    ip_address = None
    if ip_addr:
        ip_address = ip_addr['id']
    
    template_elem = root.xpath("devices/disk[@type='file']/source")[0]
    template_file = template_elem.attrib['file']
    
    command = "qemu-img info " + template_file + " | grep 'virtual size'"
    ret = execute_remote_cmd(host_ip, 'root', command) # Returns e.g. virtual size: 40G (42949672960 bytes)
    hdd = int(ret[ret.index(':')+1:ret.index('G ')].strip())

    security_domain_row = db.security_domain(vlan=ip_addr['vlan'])
    
    db.vm_data.insert(
        vm_name = vm_name, 
        vm_identity = (vm_name), 
        RAM = ram_in_mb,
        HDD = hdd,
        extra_HDD = 0,
        vCPU = cpu,
        host_id = host_id,
        template_id = 1, #TBD
        datastore_id = 1, #TBD
        owner_id = SYSTEM_USER,
        requester_id = SYSTEM_USER,
        private_ip = ip_address,
        vnc_port = vnc_port,
        purpose = 'Added by System',
        security_domain = security_domain_row['id'],
        status = vm_status)
        
    return
Example #5
0
def exec_launch_vm_image(vm_id, vm_users, extra_disk_list):

    #Get VM details
    vm_details = db.vm_data[vm_id]
    #Make entry into user_vm_map
    add_vm_users(vm_id, vm_details.requester_id, vm_details.owner_id, vm_users)

    if vm_details.private_ip != None:
        private_ip_info = db.private_ip_pool(private_ip=vm_details.private_ip)
        if not private_ip_info:
            vlan_id = db.security_domain[vm_details.security_domain].vlan.id
            ip_pool_id = db.private_ip_pool.insert(
                private_ip=vm_details.private_ip, vlan=vlan_id)
            #Add DHCP entry for private IP
            add_private_ip(ip_pool_id)

    for extra_disk in extra_disk_list:

        db.attached_disks.insert(vm_id=vm_details.id,
                                 datastore_id=vm_details.datastore_id,
                                 capacity=0,
                                 attached_disk_name=extra_disk)


#   Call Launch VM Image
    launch_existing_vm_image(vm_details)
Example #6
0
def delete_host_from_db(host_id):
    
    host_data = db.host[host_id]
    host_ip = host_data.host_ip.private_ip
    private_ip_data = db.private_ip_pool(private_ip = host_ip)    
    if private_ip_data:
        remove_dhcp_entry(host_data.host_name, private_ip_data['private_ip'])
    db(db.scheduler_task.uuid == (UUID_VM_UTIL_RRD + "=" + str(host_ip))).delete()
    del db.host[host_id]
Example #7
0
def delete_host_from_db(host_id):
    
    host_data = db.host[host_id]
    host_ip = host_data.host_ip.private_ip
    private_ip_data = db.private_ip_pool(private_ip = host_ip)    
    if private_ip_data:
        remove_dhcp_entry(host_data.host_name, private_ip_data['private_ip'])
    db(db.scheduler_task.uuid == (UUID_VM_UTIL_RRD + "=" + str(host_ip))).delete()
    del db.host[host_id]
Example #8
0
def add_private_ip_range(rangeFrom, rangeTo, vlan):

    failed = 0
    dhcp_info_list = []
    for ip_addr in get_ips_in_range(rangeFrom, rangeTo):
        mac_address = None
        if vlan != HOST_VLAN_ID:
            while True:
                mac_address = generate_random_mac()
                if not (db.private_ip_pool(mac_addr=mac_address)):break
        
        if(db.private_ip_pool(private_ip=ip_addr)):
            failed += 1
        else:
            db.private_ip_pool.insert(private_ip=ip_addr, mac_addr=mac_address, vlan=vlan)
            if vlan != HOST_VLAN_ID:
                dhcp_info_list.append((None, mac_address, ip_addr))
    
    create_dhcp_bulk_entry(dhcp_info_list)
    
    return failed
Example #9
0
def add_private_ip_range(rangeFrom, rangeTo, vlan):

    failed = 0
    dhcp_info_list = []
    for ip_addr in get_ips_in_range(rangeFrom, rangeTo):
        mac_address = None
        if vlan != HOST_VLAN_ID:
            while True:
                mac_address = generate_random_mac()
                if not (db.private_ip_pool(mac_addr=mac_address)):break
        
        if(db.private_ip_pool(private_ip=ip_addr)):
            failed += 1
        else:
            db.private_ip_pool.insert(private_ip=ip_addr, mac_addr=mac_address, vlan=vlan)
            if vlan != HOST_VLAN_ID:
                dhcp_info_list.append((None, mac_address, ip_addr))
    
    create_dhcp_bulk_entry(dhcp_info_list)
    
    return failed
Example #10
0
def add_private_ip(ip_pool_id):

    private_ip_pool = db.private_ip_pool[ip_pool_id]
    if private_ip_pool.vlan != HOST_VLAN_ID:
        mac_address = private_ip_pool.mac_addr
        if mac_address == None:
            while True:
                mac_address = generate_random_mac()
                if not (db.private_ip_pool(mac_addr=mac_address)):break
            #Update generated mac address in DB
            private_ip_pool.update_record(mac_addr=mac_address)

        create_dhcp_entry(None, mac_address, private_ip_pool.private_ip)
Example #11
0
def add_private_ip(ip_pool_id):

    private_ip_pool = db.private_ip_pool[ip_pool_id]
    if private_ip_pool.vlan != HOST_VLAN_ID:
        mac_address = private_ip_pool.mac_addr
        if mac_address == None:
            while True:
                mac_address = generate_random_mac()
                if not (db.private_ip_pool(mac_addr=mac_address)):break
            #Update generated mac address in DB
            private_ip_pool.update_record(mac_addr=mac_address)

        create_dhcp_entry(None, mac_address, private_ip_pool.private_ip)
Example #12
0
def get_private_ip_ref_link(row):
    """Returns link to VM settings page if IP is assigned to a VM
    or to host details page if IP is assigned to a Host"""
    vm_data = db.private_ip_pool(private_ip=row.id)
    host_data = db.host(host_ip=row.id)
    if vm_data:
        return A(vm_data.vm_name,
                 _href=URL(r=request, c='user', f='settings', args=vm_data.id))
    elif host_data:
        return A(host_data.host_name,
                 _href=URL(r=request,
                           c='admin',
                           f='host_config',
                           args=host_data.id))
    else:
        return 'Unassigned'
Example #13
0
def get_host_form(host_ip):
    
    form = get_add_host_form()
    private_ip_data = db.private_ip_pool(private_ip = host_ip)
    if private_ip_data:
        form.vars.host_ip = private_ip_data.id
        form.vars.host_name = 'host'+str(host_ip.split('.')[3])
        if is_host_available(host_ip):
            form.vars.CPUs = get_host_cpu(host_ip)
            form.vars.RAM  = get_host_ram(host_ip)
            form.vars.HDD = get_host_hdd(host_ip)
            form.vars.host_type = get_host_type(host_ip)
            form.vars.status = HOST_STATUS_UP
        else:
            form.vars.status = HOST_STATUS_DOWN

    return form
Example #14
0
def get_host_form(host_ip):
    
    form = get_add_host_form()
    private_ip_data = db.private_ip_pool(private_ip = host_ip)
    if private_ip_data:
        form.vars.host_ip = private_ip_data.id
        form.vars.host_name = 'host'+str(host_ip.split('.')[3])
        if is_host_available(host_ip):
            form.vars.CPUs = get_host_cpu(host_ip)
            form.vars.RAM  = get_host_ram(host_ip)
            form.vars.HDD = get_host_hdd(host_ip)
            form.vars.host_type = get_host_type(host_ip)
            form.vars.status = HOST_STATUS_UP
        else:
            form.vars.status = HOST_STATUS_DOWN

    return form
Example #15
0
def launch_vm_image_validation(form):

    requester_name = request.post_vars.requester_user
    requester_info = get_user_info(requester_name)

    if requester_info != None:
        form.vars.requester_id = requester_info[0]
    else:
        form.errors.requester_user = '******'

    owner_name = request.post_vars.owner_user
    owner_info = get_user_info(owner_name)

    if owner_info != None:
        form.vars.owner_id = owner_info[0]
    else:
        form.errors.owner_user = '******'

    #Verify if qcow2 image is present
    (vm_image_name,
     image_present) = get_vm_image_location(form.vars.datastore_id,
                                            form.vars.vm_identity)
    if not image_present:
        form.errors.vm_identity = vm_image_name + ' not found'

    if form.vars.private_ip.strip() != '':
        #Verify if public IP is available
        if is_valid_ipv4(form.vars.private_ip):
            # Verify if IP valid in given security domain
            security_domain = db.security_domain[form.vars.security_domain]
            sd_ip_range = security_domain.vlan.vlan_addr
            vlan_ip_prefix = sd_ip_range[:sd_ip_range.rindex('.') + 1]

            if not form.vars.private_ip.startswith(vlan_ip_prefix):
                form.errors.private_ip = 'Private IP is not valid for given security domain'
            else:
                # Check if IP is already assigned
                private_ip_info = db.private_ip_pool(
                    private_ip=form.vars.private_ip)
                if private_ip_info:
                    if private_ip_info.vm_id != None:
                        form.errors.private_ip = 'Private IP is not available'

        else:
            form.errors.private_ip = 'Private IP is not valid'
    else:
        form.vars.private_ip = None

    if form.vars.public_ip != PUBLIC_IP_NOT_ASSIGNED:
        #Check if Valid IP
        if is_valid_ipv4(form.vars.public_ip):
            public_ip_info = db.public_ip_pool(public_ip=form.vars.public_ip)
            if public_ip_info:
                if public_ip_info.vm_id != None:
                    form.errors.public_ip = 'Public IP is not available'
            else:
                form.errors.public_ip = 'Public IP is not configured'
        elif form.vars.public_ip == '':
            form.vars.public_ip = None
        else:
            form.errors.public_ip = 'Public IP is not valid'

    if not form.errors:
        vm_users = request.post_vars.vm_users
        user_list = []
        if vm_users and len(vm_users) > 1:
            for vm_user in vm_users[1:-1].split('|'):
                user_list.append(
                    db(db.user.username == vm_user).select(
                        db.user.id).first()['id'])

        form.vars.collaborators = user_list

        template_info = db.template[form.vars.template_id]
        form.vars.HDD = template_info.hdd

        extra_disks = request.post_vars.extra_disks
        disk_list = []
        if extra_disks and len(extra_disks) > 1:
            for extra_disk in extra_disks[1:-1].split('|'):
                disk_list.append(extra_disk)

        form.vars.extra_disk_list = disk_list