Exemplo n.º 1
0
 def _create_eval_domain(self, id_t, i):
     d = DICT_CREATE.copy()
     t = get_domain(id_t)
     id_domain = "_eval_{}_{}".format(id_t, i)
     disk_path = "{}/{}/{}/{}.qcow2".format(self.user['category'], self.user['group'], self.user['id'], id_domain)
     d['create_dict']['hardware']['disks'][0]['file'] = disk_path
     d['create_dict']['hardware']['disks'][0]['parent'] = t['disks_info'][0]['filename']
     d['create_dict']['hardware']['memory'] = t['create_dict']['hardware']['memory']
     d['create_dict']['hardware']['currentMemory'] = t['create_dict']['hardware']['memory']  # / 0,7?  =>  +30 %
     d['create_dict']['origin'] = t['id']
     d['id'] = id_domain
     d['name'] = id_domain[1:]  # remove first char
     d['icon'] = t['icon']
     d['os'] = t['os']
     insert_domain(d)
Exemplo n.º 2
0
    def create_template_in_db(self, id_domain):
        domain_dict = get_domain(id_domain)
        template_dict = domain_dict['create_dict']['template_dict']
        template_dict['status'] = 'CreatingNewTemplateInDB'
        template_id = template_dict['id']
        if insert_domain(template_dict)['inserted'] == 1:
            hw_dict = domain_dict['hardware'].copy()
            for i in range(len(hw_dict['disks'])):
                hw_dict['disks'][i]['file'] = template_dict['create_dict'][
                    'hardware']['disks'][i]['file']
            update_table_field('domains',
                               template_id,
                               'hardware',
                               hw_dict,
                               merge_dict=False)
            xml_parsed = update_xml_from_dict_domain(id_domain=template_id,
                                                     xml=domain_dict['xml'])
            if xml_parsed is False:
                update_domain_status(
                    status='Failed',
                    id_domain=template_id,
                    hyp_id=False,
                    detail='XML Parser Error, xml is not valid')
                return False
            remove_disk_template_created_list_in_domain(id_domain)
            remove_dict_new_template_from_domain(id_domain)
            if 'parents' in domain_dict.keys():
                domain_parents_chain_update = domain_dict['parents'].copy()
            else:
                domain_parents_chain_update = []

            domain_parents_chain_update.append(template_id)
            update_table_field('domains', id_domain, 'parents',
                               domain_parents_chain_update)
            update_origin_and_parents_to_new_template(id_domain, template_id)
            # update_table_field('domains', template_id, 'xml', xml_parsed, merge_dict=False)
            update_domain_status(
                status='Stopped',
                id_domain=template_id,
                hyp_id=False,
                detail=
                'Template created, ready to create domains from this template')
            update_domain_status(
                status='Stopped',
                id_domain=id_domain,
                hyp_id=False,
                detail=
                'Template created from this domain, now domain is ready to start again'
            )

        else:
            log.error(
                'template {} can not be inserted in rethink, domain_id duplicated??'
                .format(template_id))
            return False
Exemplo n.º 3
0
    def ferrary_from_domain(self,
                            id_domain,
                            num_domains,
                            start_index=0,
                            dir_to_ferrary_disks=None,
                            prefix=None):

        if dir_to_ferrary_disks is None:
            dir_to_ferrary_disks = CONFIG_DICT['FERRARY'][
                'DIR_TO_FERRARY_DISKS'.lower()]
        if prefix is None:
            prefix = CONFIG_DICT['FERRARY']['PREFIX'.lower()]
        ferrary = []
        for i in range(start_index, num_domains + start_index):
            d = dict()
            d['index'] = str(i).zfill(3)
            d['id'] = prefix + id_domain + d['index']
            d['dict_domain'], d['cmd'] = self.domain_from_template(
                id_template=id_domain,
                id_new=d['id'],
                only_cmds=True,
                path_to_disk_dir=dir_to_ferrary_disks)
            ferrary.append(d)

        cmds = []
        cmds.append(ferrary[0]['cmd'][0])
        cmds = cmds + list(itertools.chain([d['cmd'][1] for d in ferrary]))

        before = time.time()

        cmds_result = exec_remote_list_of_cmds(VDESKTOP_DISK_OPERATINOS, cmds)
        after = time.time()
        duration = after - before
        log.debug(
            'FERRARY: {} disks created in {} with name in {} seconds'.format(
                num_domains, dir_to_ferrary_disks, prefix + id_domain + 'XXX',
                duration))

        for dict_domain_new in [d['dict_domain'] for d in ferrary]:
            insert_domain(dict_domain_new)

        return ferrary
Exemplo n.º 4
0
 def _create_eval_domain(self, id_t, i):
     d = DICT_CREATE.copy()
     t = get_domain(id_t)
     id_domain = "_eval_{}_{}".format(id_t, i)
     disk_path = "{}/{}/{}/{}.qcow2".format(self.user['category'],
                                            self.user['group'],
                                            self.user['id'], id_domain)
     d['create_dict']['hardware']['disks'][0]['file'] = disk_path
     d['create_dict']['hardware']['disks'][0]['parent'] = t['disks_info'][
         0]['filename']
     d['create_dict']['hardware']['memory'] = t['create_dict']['hardware'][
         'memory']
     d['create_dict']['hardware']['currentMemory'] = t['create_dict'][
         'hardware']['memory']  # / 0,7?  =>  +30 %
     d['create_dict']['origin'] = t['id']
     d['id'] = id_domain
     d['name'] = id_domain[1:]  # remove first char
     d['icon'] = t['icon']
     d['os'] = t['os']
     insert_domain(d)
Exemplo n.º 5
0
    def ferrary_from_domain(self,
                            id_domain,
                            num_domains,
                            start_index=0,
                            dir_to_ferrary_disks=None,
                            prefix=None):

        if dir_to_ferrary_disks is None:
            dir_to_ferrary_disks = CONFIG_DICT['FERRARY']['DIR_TO_FERRARY_DISKS'.lower()]
        if prefix is None:
            prefix = CONFIG_DICT['FERRARY']['PREFIX'.lower()]
        ferrary = []
        for i in range(start_index, num_domains + start_index):
            d = dict()
            d['index'] = str(i).zfill(3)
            d['id'] = prefix + id_domain + d['index']
            d['dict_domain'], d['cmd'] = self.domain_from_template(id_template=id_domain,
                                                                   id_new=d['id'],
                                                                   only_cmds=True,
                                                                   path_to_disk_dir=dir_to_ferrary_disks)
            ferrary.append(d)

        cmds = []
        cmds.append(ferrary[0]['cmd'][0])
        cmds = cmds + list(itertools.chain([d['cmd'][1] for d in ferrary]))

        before = time.time()

        cmds_result = exec_remote_list_of_cmds(VDESKTOP_DISK_OPERATINOS, cmds)
        after = time.time()
        duration = after - before
        log.debug('FERRARY: {} disks created in {} with name in {} seconds'.format(num_domains, dir_to_ferrary_disks,
                                                                                   prefix + id_domain + 'XXX',
                                                                                   duration))

        for dict_domain_new in [d['dict_domain'] for d in ferrary]:
            insert_domain(dict_domain_new)

        return ferrary
Exemplo n.º 6
0
def new_domain_from_template(name, description, template_origin, user, group,
                             category):
    id = '_' + user + '_' + name
    dir_disk = category + '/' + group + '/' + name
    disk_filename = name + '.qcow2'
    relative_path = dir_disk + '/' + disk_filename
    template = get_domain(template_origin)
    # new_backing_chain = [relative_dir]
    # new_backing_chain.append(template['hardware']['disks'][0]['file'])
    base_disk_path = template['hardware']['disks'][0]['file']

    new_hardware_dict = template['hardware'].copy()
    new_hardware_dict['disks'][0]['file'] = relative_path
    new_hardware_dict['disks'][0]['parent'] = base_disk_path
    new_hardware_dict['name'] = id
    new_hardware_dict['uuid'] = None

    domain = {
        'id': id,
        'kind': 'desktop',
        'user': user,
        'status': 'Creating',
        'detail': None,
        'category': category,
        'group': group,
        'server': False,
        'hardware': new_hardware_dict,
        'hypervisors_pools': template['hypervisors_pools'],
        'xml': template['xml'],
        'name': name,
        'description': description,
        'icon': template['icon'],
        'os': template['icon']
    }

    result = insert_domain(domain)
    if result['errors'] > 0:
        log.error(
            'error when inserting new domain {} in db, duplicate id??'.format(
                id))
        return False
    else:
        return id
Exemplo n.º 7
0
    def create_template_in_db(self, id_domain):
        domain_dict = get_domain(id_domain)
        template_dict = domain_dict['create_dict']['template_dict']
        template_dict['status'] = 'CreatingNewTemplateInDB'
        template_id = template_dict['id']
        if insert_domain(template_dict)['inserted'] == 1:
            hw_dict = domain_dict['hardware'].copy()
            for i in range(len(hw_dict['disks'])):
                hw_dict['disks'][i]['file'] = template_dict['create_dict']['hardware']['disks'][i]['file']
            update_table_field('domains', template_id, 'hardware', hw_dict, merge_dict=False)
            xml_parsed = update_xml_from_dict_domain(id_domain=template_id, xml=domain_dict['xml'])
            if xml_parsed is False:
                update_domain_status(status='Failed',
                                     id_domain=template_id,
                                     hyp_id=False,
                                     detail='XML Parser Error, xml is not valid')
                return False
            remove_disk_template_created_list_in_domain(id_domain)
            remove_dict_new_template_from_domain(id_domain)
            if 'parents' in domain_dict.keys():
                domain_parents_chain_update = domain_dict['parents'].copy()
            else:
                domain_parents_chain_update = []

            domain_parents_chain_update.append(template_id)
            update_table_field('domains', id_domain, 'parents', domain_parents_chain_update)
            update_origin_and_parents_to_new_template(id_domain,template_id)
            # update_table_field('domains', template_id, 'xml', xml_parsed, merge_dict=False)
            update_domain_status(status='Stopped',
                                 id_domain=template_id,
                                 hyp_id=False,
                                 detail='Template created, ready to create domains from this template')
            update_domain_status(status='Stopped',
                                 id_domain=id_domain,
                                 hyp_id=False,
                                 detail='Template created from this domain, now domain is ready to start again')


        else:
            log.error('template {} can not be inserted in rethink, domain_id duplicated??'.format(template_id))
            return False
Exemplo n.º 8
0
def new_domain_from_template(name, description, template_origin, user, group, category):
    id = '_' + user + '_' + name
    dir_disk = category + '/' + group + '/' + name
    disk_filename = name + '.qcow2'
    relative_path = dir_disk + '/' + disk_filename
    template = get_domain(template_origin)
    # new_backing_chain = [relative_dir]
    # new_backing_chain.append(template['hardware']['disks'][0]['file'])
    base_disk_path = template['hardware']['disks'][0]['file']

    new_hardware_dict = template['hardware'].copy()
    new_hardware_dict['disks'][0]['file'] = relative_path
    new_hardware_dict['disks'][0]['parent'] = base_disk_path
    new_hardware_dict['name'] = id
    new_hardware_dict['uuid'] = None

    domain = {'id': id,
              'kind': 'desktop',
              'user': user,
              'status': 'Creating',
              'detail': None,
              'category': category,
              'group': group,
              'server': False,
              'hardware': new_hardware_dict,
              'hypervisors_pools': template['hypervisors_pools'],
              'xml': template['xml'],
              'name': name,
              'description': description,
              'icon': template['icon'],
              'os': template['icon']}

    result = insert_domain(domain)
    if result['errors'] > 0:
        log.error('error when inserting new domain {} in db, duplicate id??'.format(id))
        return False
    else:
        return id
Exemplo n.º 9
0
    'detail': None,
    'group': 'test_users',
    'hypervisors_pools': ['default'],
    'icon': 'windows',
    'id': '_test1_prova_win7',
    'kind': 'desktop',
    'name': 'prova win7',
    'os': 'windows',
    'server': False,
    'status': 'Creating',
    'user': '******',
    'xml': None
}

DICT_CREATE = DICT_CREATE_WIN7.copy()
PREFIX = 'maquina_prova'
NUM_BULK = int(sys.argv[1])
disk_path = DICT_CREATE['create_dict']['hardware']['disks'][0]['file']
name = DICT_CREATE['id']
for i in range(1, 1 + NUM_BULK):
    id_domain = '{}_{}'.format(PREFIX, str(i).zfill(2))
    d = DICT_CREATE.copy()

    d['create_dict']['hardware']['disks'][0]['file'] = disk_path.replace(
        'prova_win7.qcow', id_domain + '.qcow')
    d['id'] = name.replace('prova_win7', id_domain)
    d['name'] = id_domain

    pprint.pprint(d)
    insert_domain(d)
Exemplo n.º 10
0
    def domain_from_template(self,
                             id_template,
                             id_new,
                             user,
                             category,
                             group,
                             name,
                             description,
                             cpu,
                             ram,
                             current_ram=-1,
                             id_net=None,
                             force_server=None,
                             only_cmds=False,
                             path_to_disk_dir=None,
                             disk_filename=None,
                             create_domain_in_db=True):

        # INFO TO DEVELOPER: falta verificar que el id no existe y si existe salir enseguida, ya que si no haríamos updates y
        # creaciónes de disco peligrosas
        dict_domain_template = get_domain(id_template)
        dict_domain_new = dict_domain_template.copy()
        dict_domain_new['id'] = id_new
        dict_domain_new['user'] = user
        dict_domain_new['category'] = category
        dict_domain_new['group'] = group
        dict_domain_new['kind'] = 'desktop'
        dict_domain_new['name'] = name
        dict_domain_new['description'] = description
        dict_domain_new['status'] = 'CreatingDisk'
        dict_domain_new['detail'] = 'Defining new domain'

        if force_server == True:
            dict_domain_new['server'] = True
        elif force_server == False:
            dict_domain_new['server'] = False
        else:
            dict_domain_new['server'] = dict_domain_template['server']

        x = DomainXML(dict_domain_template['xml'])
        x.set_name(id_new)
        x.set_title(name)
        x.set_description(description)

        old_path_disk = dict_domain_template['hardware']['disks'][0]['file']
        old_path_dir = extract_dir_path(old_path_disk)

        DEFAULT_GROUP_DIR = CONFIG_DICT['REMOTEOPERATIONS'][
            'default_group_dir']

        if path_to_disk_dir is None:
            path_to_disk_dir = DEFAULT_GROUP_DIR + '/' + \
                               dict_domain_template['category'] + '/' + \
                               dict_domain_template['group'] + '/' + \
                               dict_domain_template['user']

        if len(old_path_disk[len(old_path_dir) + 1:-1].split('.')) > 1:
            extension = old_path_disk[len(old_path_dir) + 1:-1].split('.')[1]
        else:
            extension = 'qcow'

        if disk_filename is None:
            disk_filename = id_new + '.' + extension

        new_path_disk = path_to_disk_dir + '/' + disk_filename

        x.set_vcpu(cpu)
        x.set_memory(ram, current=current_ram)
        x.set_vdisk(new_path_disk)
        x.randomize_vm()

        dict_domain_new['hardware'] = x.vm_dict
        dict_domain_new['xml'] = x.return_xml()

        cmds = create_cmds_disk_from_base(
            old_path_disk,
            new_path_disk,
        )

        if only_cmds is True:
            dict_domain_new['status'] = 'Crashed'
            dict_domain_new[
                'detail'] = 'Disk not created, only for testing ui purpose, create command is not launched'
            return dict_domain_new, cmds

        else:
            action = {}
            action['type'] = 'create_disk'
            action['disk_path'] = new_path_disk
            action['domain'] = id_new
            action['ssh_commands'] = cmds
            if hasattr(self.pool, 'queue_disk_operation'):
                self.pool.queue_disk_operation.put(action)
                # err,out = create_disk_from_base(old_path_disk,new_path_disk)
                dict_domain_new['status'] = 'CreatingDisk'
                dict_domain_new[
                    'detail'] = 'Creating disk operation is launched ({} operations in queue)'.format(
                        self.pool.queue_disk_operation.qsize())
                # list_backing_chain = backing_chain(new_path_disk)

                # dict_domain_new['backing_chain'] = list_backing_chain
            else:
                log.error('queue disk operation is not created')
                dict_domain_new['status'] = 'Crashed'
                dict_domain_new[
                    'detail'] = 'Disk not created, queue for disk creation does not exist'

            if create_domain_in_db is True:
                insert_domain(dict_domain_new)

            return dict_domain_new
Exemplo n.º 11
0
 'icon': 'windows',
 'id': '_test1_prova_win7',
 'kind': 'desktop',
 'name': 'prova win7',
 'os': 'windows',
 'server': False,
 'status': 'Creating',
 'user': '******',
 'xml': None}


DICT_CREATE = DICT_CREATE_WIN7.copy()
PREFIX = 'maquina_prova'
NUM_BULK = int(sys.argv[1])
disk_path=DICT_CREATE['create_dict']['hardware']['disks'][0]['file']
name = DICT_CREATE['id']
for i in range(1,1+NUM_BULK):
    id_domain = '{}_{}'.format(PREFIX,str(i).zfill(2))
    d = DICT_CREATE.copy()

    d['create_dict']['hardware']['disks'][0]['file']=disk_path.replace('prova_win7.qcow',id_domain + '.qcow')
    d['id'] = name.replace('prova_win7',id_domain)
    d['name'] = id_domain

    pprint.pprint(d)
    insert_domain(d)


        

Exemplo n.º 12
0
def create_domain_from_template(name_normalized,
                                    id_template,
                                    id_user,
                                    description = '',
                                    only_dict = False):

    regex = re.compile("^[a-zA-Z0-9-_.]*$")
    if regex.match(name_normalized) is None:
        logs.bulk.error('name {} for domain is not normalized, only lower chars, letters and _ are allowed'.format(name_normalized))
        return False

    id_new_domain = '_' + id_user + '_' + name_normalized

    dict_template = t = get_domain(id_template)
    if dict_template is None:
        logs.bulk.error('template {} does not exist in database'.format(id_template))
        return False
    if dict_template['kind'] == 'desktop':
        logs.bulk.error('domain {} is a desktop, can not derivate from a desktop only from template or base'.format(id_template))
        return False

    if get_domain(id_new_domain) is not None:
        logs.bulk.error('domain {} can not be created because it already exists in database'.format(id_new_domain))
        return False

    dict_user = u = get_user(id_user)
    if dict_user is None:
        logs.bulk.error('user {} does not exist in database, can not create domains '.format(id_user))
        return False

    d_new = {}
    d_new["allowed"] = {"categories": False,
                        "groups": False,
                        "roles": False,
                        "users": False
                        }
    d_new['category'] = u['category']
    d_new['create_dict'] = t['create_dict']
    d_new['description'] = description
    d_new['detail'] = ''
    d_new['group'] = u['group']
    d_new['hypervisors_pools'] = t['hypervisors_pools']
    d_new['icon'] = t['icon']
    d_new['id'] = id_new_domain
    d_new['kind'] = 'desktop'
    d_new['name'] = name_normalized
    d_new['os'] = t['os']
    d_new['server'] = False
    d_new['status'] = 'Creating'
    d_new['user'] = id_user
    d_new['xml'] = ''


    # modify create_dict

    path_relative_disk = '{}/{}/{}/{}.qcow2'.format(u['category'], u['group'], id_user, name_normalized)

    d_new['create_dict']['origin'] = id_template

    try:
        d_new['create_dict'].pop('hypervisors_pools')
    except KeyError:
        pass

    d_new['create_dict']['hardware']['disks'][0]['parent'] = t['hardware']['disks'][0]['file']
    d_new['create_dict']['hardware']['disks'][0]['file'] = path_relative_disk

    if only_dict is True:
        return d_new
    else:
        result = insert_domain(d_new)
        if result['inserted'] == 1:
            logs.bulk.info('domain {} created from bulk operation'.format(id_new_domain))
            return True
        else:
            logs.bulk.error('error inserting domain {} in database'.format(id_new_domain))
            return False
Exemplo n.º 13
0
    def domain_from_template(self,
                             id_template,
                             id_new,
                             user,
                             category,
                             group,
                             name,
                             description,
                             cpu,
                             ram,
                             current_ram=-1,
                             id_net=None,
                             force_server=None,
                             only_cmds=False,
                             path_to_disk_dir=None,
                             disk_filename=None,
                             create_domain_in_db=True):

        # INFO TO DEVELOPER: falta verificar que el id no existe y si existe salir enseguida, ya que si no haríamos updates y
        # creaciónes de disco peligrosas
        dict_domain_template = get_domain(id_template)
        dict_domain_new = dict_domain_template.copy()
        dict_domain_new['id'] = id_new
        dict_domain_new['user'] = user
        dict_domain_new['category'] = category
        dict_domain_new['group'] = group
        dict_domain_new['kind'] = 'desktop'
        dict_domain_new['name'] = name
        dict_domain_new['description'] = description
        dict_domain_new['status'] = 'CreatingDisk'
        dict_domain_new['detail'] = 'Defining new domain'

        if force_server == True:
            dict_domain_new['server'] = True
        elif force_server == False:
            dict_domain_new['server'] = False
        else:
            dict_domain_new['server'] = dict_domain_template['server']

        x = DomainXML(dict_domain_template['xml'])
        if x.parser is False:
            log.error('error when parsing xml')
            dict_domain_new['status'] = 'FailedCreatingDomain'
            dict_domain_new['detail'] = 'XML Parser have failed, xml with errors'
            return False

        x.set_name(id_new)
        x.set_title(name)
        x.set_description(description)

        old_path_disk = dict_domain_template['hardware']['disks'][0]['file']
        old_path_dir = extract_dir_path(old_path_disk)

        #DEFAULT_GROUP_DIR = CONFIG_DICT['REMOTEOPERATIONS']['default_group_dir']

        if path_to_disk_dir is None:
            path_to_disk_dir = DEFAULT_GROUP_DIR + '/' + \
                               dict_domain_template['category'] + '/' + \
                               dict_domain_template['group'] + '/' + \
                               dict_domain_template['user']

        if len(old_path_disk[len(old_path_dir) + 1:-1].split('.')) > 1:
            extension = old_path_disk[len(old_path_dir) + 1:-1].split('.')[1]
        else:
            extension = 'qcow'

        if disk_filename is None:
            disk_filename = id_new + '.' + extension

        new_path_disk = path_to_disk_dir + '/' + disk_filename

        x.set_vcpu(cpu)
        x.set_memory(ram,current=current_ram)
        x.set_vdisk(new_path_disk)
        x.randomize_vm()

        dict_domain_new['hardware'] = x.vm_dict
        dict_domain_new['xml'] = x.return_xml()

        cmds = create_cmds_disk_from_base(old_path_disk, new_path_disk, )

        if only_cmds is True:
            dict_domain_new['status'] = 'Crashed'
            dict_domain_new['detail'] = 'Disk not created, only for testing ui purpose, create command is not launched'
            return dict_domain_new, cmds


        else:
            action = {}
            action['type'] = 'create_disk'
            action['disk_path'] = new_path_disk
            action['domain'] = id_new
            action['ssh_commands'] = cmds
            if hasattr(self.pool, 'queue_disk_operation'):
                self.pool.queue_disk_operation.put(action)
                # err,out = create_disk_from_base(old_path_disk,new_path_disk)
                dict_domain_new['status'] = 'CreatingDisk'
                dict_domain_new['detail'] = 'Creating disk operation is launched ({} operations in queue)'.format(
                    self.pool.queue_disk_operation.qsize())
                # list_backing_chain = backing_chain(new_path_disk)

                # dict_domain_new['backing_chain'] = list_backing_chain
            else:
                log.error('queue disk operation is not created')
                dict_domain_new['status'] = 'Crashed'
                dict_domain_new['detail'] = 'Disk not created, queue for disk creation does not exist'

            if create_domain_in_db is True:
                insert_domain(dict_domain_new)

            return dict_domain_new