Esempio n. 1
0
    def start_download(self, dict_changes):

        new_file_path, path_selected, type_path_selected, pool_id = self.get_file_path(
            dict_changes)

        table = dict_changes['table']
        id_down = dict_changes['id']
        subdir_url = 'storage'

        # all disk downloads create a desktop

        url_base = self.url_resources
        header_dict = {}

        if len(subdir_url) > 0:
            url_base = url_base + '/' + subdir_url

        # hypervisor to launch download command
        hyp_to_disk_create = get_host_disk_operations_from_path(
            path_selected, pool=pool_id, type_path=type_path_selected)

        if dict_changes.get('url-web', False) is not False:
            url = dict_changes['url-web']

        elif dict_changes.get('url-isard', False) is not False:
            url_isard = dict_changes['url-isard']
            url = url_base + '/' + table + '/' + url_isard
            if len(self.url_code) > 0:
                header_dict['Authorization'] = self.url_code
        else:
            logs.downloads.error((
                'web-url or isard-url must be keys in dictionary for domain {}'
                + ' to download disk file from internet. ').format(id_down))
            exit()

        if new_file_path in self.finalished_threads:
            if new_file_path in self.download_threads.keys():
                self.download_threads.pop(new_file_path)
            self.finalished_threads.remove(new_file_path)

        if table == 'domains':
            d_update_domain = dict_changes['create_dict']
            d_update_domain['hardware']['disks'][0][
                'path_selected'] = path_selected
            update_domain_dict_create_dict(id_down, d_update_domain)

        # launching download threads
        if new_file_path not in self.download_threads:
            self.download_threads[new_file_path] = DownloadThread(
                hyp_to_disk_create, url, new_file_path, table, id_down,
                header_dict, self.finalished_threads)
            self.download_threads[new_file_path].daemon = True
            self.download_threads[new_file_path].start()

        else:
            logs.downloads.info(
                'download thread launched to this path: {}'.format(
                    new_file_path))
Esempio n. 2
0
    def creating_test_disk(self,test_disk_relative_route,size_str='1M',type_path='media',pool_id='default'):

        path_new_disk, path_selected = get_path_to_disk(test_disk_relative_route,
                                                                     pool=pool_id,
                                                                     type_path=type_path)

        hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=pool_id, type_path=type_path)

        cmds = create_cmd_disk_from_scratch(path_new_disk=path_new_disk,
                                            size_str=size_str)

        action = {}
        action['type'] = 'create_disk_from_scratch'
        action['disk_path'] = path_new_disk
        action['index_disk'] = 0
        action['domain'] = False
        action['ssh_commands'] = cmds

        self.manager.q_disk_operations[hyp_to_disk_create].put(action)
Esempio n. 3
0
    def run(self):

        # if self.table == 'domains':
        #     type_path_selected = 'groups'
        # elif self.table in ['isos']:
        #     type_path_selected = 'isos'
        # else:
        #     type_path_selected = 'media'
        #
        # new_file, path_selected = get_path_to_disk(self.path, pool=self.pool, type_path=type_path_selected)
        # logs.downloads.debug("PATHS ___________________________________________________________________")
        # logs.downloads.debug(new_file)
        # logs.downloads.debug(path_selected)
        # logs.downloads.debug(pprint.pformat(self.__dict__))
        #
        # hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=self.pool,
        #                                                                 type_path=type_path_selected)

        # hypervisor to launch download command
        # wait to threads disk_operations are alive
        time_elapsed = 0
        path_selected = self.path_selected
        while True:
            if len(self.manager.t_disk_operations) > 0:

                hyp_to_disk_create = get_host_disk_operations_from_path(
                    path_selected,
                    pool=self.pool_id,
                    type_path=self.type_path_selected)
                logs.downloads.debug(
                    f'Thread download started to in hypervisor: {hyp_to_disk_create}'
                )
                if self.manager.t_disk_operations.get(hyp_to_disk_create,
                                                      False) is not False:
                    if self.manager.t_disk_operations[
                            hyp_to_disk_create].is_alive():
                        d = get_hyp_hostname_user_port_from_id(
                            hyp_to_disk_create)
                        self.hostname = d['hostname']
                        self.user = d['user']
                        self.port = d['port']
                        break
            sleep(0.2)
            time_elapsed += 0.2
            if time_elapsed > TIMEOUT_WAITING_HYPERVISOR_TO_DOWNLOAD:
                logs.downloads.info(
                    f'Timeout ({TIMEOUT_WAITING_HYPERVISOR_TO_DOWNLOAD} sec) waiting hypervisor online to download {url_base}'
                )
                if self.table == 'domains':
                    update_domain_status('DownloadFailed',
                                         self.id,
                                         detail="downloaded disk")
                else:
                    update_status_table(self.table, 'DownloadFailed', self.id)
                self.finalished_threads.append(self.path)
                return False

        header_template = "--header '{header_key}: {header_value}' "
        headers = ''

        if URL_DOWNLOAD_INSECURE_SSL == True:
            insecure_option = '--insecure'
        else:
            insecure_option = ''

        dict_header = {}
        for k, v in self.dict_header.items():
            headers += header_template.format(header_key=k, header_value=v)
            dict_header[k] = v

        # TEST IF url return an stream of data
        ok, error_msg = test_url_for_download(
            self.url,
            url_download_insecure_ssl=URL_DOWNLOAD_INSECURE_SSL,
            timeout_time_limit=TIMEOUT_WAITING_HYPERVISOR_TO_DOWNLOAD,
            dict_header=dict_header)

        if ok is False:
            logs.downloads.error(f'URL check failed for url: {self.url}')
            logs.downloads.error(f'Failed url check reason: {error_msg}')
            update_status_table(self.table,
                                'DownloadFailed',
                                self.id,
                                detail=error_msg)
            return False

        curl_template = "curl {insecure_option} -L -o '{path}' {headers} '{url}'"

        ssh_template = """ssh -oBatchMode=yes -p {port} {user}@{hostname} """ \
                       """ "mkdir -p '{path_dir}'; """ + curl_template + '"'

        logs.downloads.debug(ssh_template)

        ssh_command = ssh_template.format(port=self.port,
                                          user=self.user,
                                          hostname=self.hostname,
                                          path=self.path,
                                          path_dir=dirname(self.path),
                                          headers=headers,
                                          url=self.url,
                                          insecure_option=insecure_option)

        logs.downloads.debug("SSH COMMAND: {}".format(ssh_command))

        p = subprocess.Popen(ssh_command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             preexec_fn=os.setsid)
        rc = p.poll()
        update_status_table(
            self.table, 'Downloading', self.id,
            "downloading in hypervisor: {}".format(self.hostname))
        while rc != 0:
            header = p.stderr.readline().decode('utf8')
            header2 = p.stderr.readline().decode('utf8')
            keys = [
                'total_percent', 'total', 'received_percent', 'received',
                'xferd_percent', 'xferd', 'speed_download_average',
                'speed_upload_average', 'time_total', 'time_spent',
                'time_left', 'speed_current'
            ]

            line = ""

            while True:

                c = p.stderr.read(1).decode('utf8')
                if self.stop is True:

                    curl_cmd = curl_template.format(
                        path=self.path,
                        headers=headers,
                        url=self.url,
                        insecure_option=insecure_option)
                    # for pkill curl order is cleaned
                    curl_cmd = curl_cmd.replace("'", "")
                    curl_cmd = curl_cmd.replace("  ", " ")

                    ssh_cmd_kill_curl = """ssh -p {port} {user}@{hostname} "pkill -f \\"^{curl_cmd}\\" " """.format(
                        port=self.port,
                        user=self.user,
                        hostname=self.hostname,
                        curl_cmd=curl_cmd)

                    logs.downloads.info(
                        'download {} aborted, ready to send ssh kill to curl in hypervisor {}'
                        .format(self.path, self.hostname))

                    # destroy curl in hypervisor
                    p_kill_curl = subprocess.Popen(ssh_cmd_kill_curl,
                                                   shell=True)
                    p_kill_curl.wait(timeout=5)
                    # destroy ssh command
                    try:
                        os.killpg(os.getpgid(p.pid), signal.SIGTERM)
                    except Exception as e:
                        logs.downloads.debug(
                            'ssh process not killed, has finalished')

                    if self.table == 'media':
                        remove_media(self.id)
                    if self.table == 'domains':
                        delete_domain(self.id)
                    # update_status_table(self.table, 'DownloadFailed', self.id, detail="download aborted")
                    return False
                if not c:
                    break
                if c == '\r':
                    if len(line) > 60:
                        values = line.split()
                        logs.downloads.debug(self.url)
                        logs.downloads.debug(line)
                        d_progress = dict(zip(keys, values))
                        try:
                            d_progress['total_percent'] = int(
                                float(d_progress['total_percent']))
                            d_progress['received_percent'] = int(
                                float(d_progress['received_percent']))
                            if d_progress['received_percent'] > 1:
                                pass
                        except:
                            d_progress['total_percent'] = 0
                            d_progress['received_percent'] = 0
                        update_download_percent(d_progress, self.table,
                                                self.id)
                        line = p.stderr.read(60).decode('utf8')

                else:
                    line = line + c

            rc = p.poll()

        if self.stop is True:
            return False
        else:
            logs.downloads.info('File downloaded: {}'.format(self.path))

            assert rc == 0
            if self.table == 'domains':
                # update_table_field(self.table, self.id, 'path_downloaded', self.path)
                d_update_domain = get_domain(self.id)['create_dict']
                # d_update_domain = {'hardware': {'disks': [{}]}}
                d_update_domain['hardware']['disks'][0]['file'] = self.path

                update_domain_dict_create_dict(self.id, d_update_domain)
                self.finalished_threads.append(self.path)
                update_domain_status('Downloaded',
                                     self.id,
                                     detail="downloaded disk")
                update_domain_status('Updating',
                                     self.id,
                                     detail="downloaded disk")
            else:
                self.finalished_threads.append(self.path)
                update_table_field(self.table, self.id, 'path_downloaded',
                                   self.path)
                update_status_table(self.table, 'Downloaded', self.id)
Esempio n. 4
0
    def creating_disks_from_template(self, id_new):
        dict_domain = get_domain(id_new)
        if 'create_dict' in dict_domain.keys():
            dict_to_create = dict_domain['create_dict']

        pool_var = dict_domain['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        # INFO TO DEVELOPER DEBERÍA SER UN FOR PARA CADA DISCO
        # y si el disco no tiene backing_chain, crear un disco vacío
        # del tamño que marcase
        # d['hardware']['disks'][0]['size']
        # el backing_file debería estar asociado a cada disco:
        # d['hardware']['disks'][0]['backing_file']

        for index_disk in range(len(dict_to_create['hardware']['disks'])):
            relative_path = dict_to_create['hardware']['disks'][index_disk][
                'file']
            new_file, path_selected = get_path_to_disk(relative_path,
                                                       pool=pool_id)
            # UPDATE PATH IN DOMAIN
            dict_to_create['hardware']['disks'][index_disk]['file'] = new_file
            dict_to_create['hardware']['disks'][index_disk][
                'path_selected'] = path_selected

        update_table_field('domains', id_new, 'create_dict', dict_to_create)

        #TODO: REVISAR SI RELAMENTE ES NECESARIO o esta acción responde a versiones antiguas de nuestras funciones de creación
        hardware_update = {}
        hardware_update['disks'] = dict_to_create['hardware']['disks']
        update_domain_dict_hardware(id_new, hardware_update)
        ##################

        for index_disk in range(len(dict_to_create['hardware']['disks'])):
            backing_file = dict_to_create['hardware']['disks'][index_disk][
                'parent']
            new_file = dict_to_create['hardware']['disks'][index_disk]['file']
            path_selected = dict_to_create['hardware']['disks'][index_disk][
                'path_selected']
            hyp_to_disk_create = get_host_disk_operations_from_path(
                path_selected, pool=pool_id, type_path='groups')

            cmds = create_cmds_disk_from_base(path_base=backing_file,
                                              path_new=new_file)
            log.debug(
                'commands to disk create to launch in disk_operations: \n{}'.
                format('\n'.join(cmds)))
            action = {}
            action['type'] = 'create_disk'
            action['disk_path'] = new_file
            action['index_disk'] = index_disk
            action['domain'] = id_new
            action['ssh_commands'] = cmds

            try:
                update_domain_status(
                    status='CreatingDisk',
                    id_domain=id_new,
                    hyp_id=False,
                    detail=
                    'Creating disk operation is launched in hypervisor {} ({} operations in queue)'
                    .format(
                        hyp_to_disk_create, self.manager.
                        q_disk_operations[hyp_to_disk_create].qsize()))
                self.manager.q_disk_operations[hyp_to_disk_create].put(action)

            except Exception as e:
                update_domain_status(
                    status='FailedCreatingDomain',
                    id_domain=id_new,
                    hyp_id=False,
                    detail=
                    'Creating disk operation failed when insert action in queue for disk operations'
                )
                log.error(
                    'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'
                    .format(e))
Esempio n. 5
0
    def creating_disk_from_scratch(self, id_new):
        dict_domain = get_domain(id_new)

        pool_var = dict_domain['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        dict_to_create = dict_domain['create_dict']

        if 'disks' in dict_to_create['hardware'].keys():
            if len(dict_to_create['hardware']['disks']) > 0:

                # for index_disk in range(len(dict_to_create['hardware']['disks'])):
                #     relative_path = dict_to_create['hardware']['disks'][index_disk]['file']
                #     path_new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id)
                #     # UPDATE PATH IN DOMAIN
                #     dict_to_create['hardware']['disks'][index_disk]['file'] = new_file
                #     dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected

                relative_path = dict_to_create['hardware']['disks'][0]['file']
                path_new_disk, path_selected = get_path_to_disk(relative_path,
                                                                pool=pool_id)
                # UPDATE PATH IN DOMAIN

                d_update_domain = {'hardware': {'disks': [{}]}}
                d_update_domain['hardware']['disks'][0]['file'] = path_new_disk
                d_update_domain['hardware']['disks'][0][
                    'path_selected'] = path_selected
                update_domain_dict_hardware(id_new, d_update_domain)
                update_domain_dict_create_dict(id_new, d_update_domain)

                size_str = dict_to_create['hardware']['disks'][0]['size']

                hyp_to_disk_create = get_host_disk_operations_from_path(
                    path_selected, pool=pool_id, type_path='groups')

                cmds = create_cmd_disk_from_scratch(
                    path_new_disk=path_new_disk, size_str=size_str)

                action = {}
                action['type'] = 'create_disk_from_scratch'
                action['disk_path'] = path_new_disk
                action['index_disk'] = 0
                action['domain'] = id_new
                action['ssh_commands'] = cmds
                try:
                    update_domain_status(
                        status='CreatingDiskFromScratch',
                        id_domain=id_new,
                        hyp_id=False,
                        detail=
                        'Creating disk commands are launched in hypervisor {} ({} operations in queue)'
                        .format(
                            hyp_to_disk_create, self.manager.
                            q_disk_operations[hyp_to_disk_create].qsize()))
                    self.manager.q_disk_operations[hyp_to_disk_create].put(
                        action)

                except Exception as e:
                    update_domain_status(
                        status='FailedCreatingDomain',
                        id_domain=id_new,
                        hyp_id=False,
                        detail=
                        'Creating disk operation failed when insert action in queue for disk operations'
                    )
                    log.error(
                        'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'
                        .format(e))
Esempio n. 6
0
    def create_template_disks_from_domain(self, id_domain):
        dict_domain = get_domain(id_domain)

        create_dict = dict_domain['create_dict']

        pool_var = create_dict['template_dict']['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        try:
            dict_new_template = create_dict['template_dict']
        except KeyError as e:
            update_domain_status(
                status='Stopped',
                id_domain=id_domain,
                hyp_id=False,
                detail=
                'Action Creating Template from domain failed. No template_json in domain dictionary'
            )
            log.error(
                'No template_dict in keys of domain dictionary, when creating template form domain {}. Exception: {}'
                .format(id_domain, str(e)))
            return False

        disk_index_in_bus = 0
        if 'disks' in dict_domain['hardware']:

            list_disk_template_path_relative = [
                d['file'] for d in create_dict['hardware']['disks']
            ]
            create_disk_template_created_list_in_domain(id_domain)
            for i in range(len(list_disk_template_path_relative)):
                # for disk in dict_domain['hardware']['disks']:
                path_domain_disk = dict_domain['hardware']['disks'][i]['file']

                try:
                    # path_template_disk_relative = dict_new_template['create_dict']['hardware']['disks'][i]['file']
                    path_template_disk_relative = list_disk_template_path_relative[
                        i]
                except KeyError as e:
                    update_domain_status(
                        status='Stopped',
                        id_domain=id_domain,
                        hyp_id=False,
                        detail=
                        'Action Creating Template from domain failed. No disks in template_json in domain dictionary'
                    )
                    log.error(
                        'No disks in template_json in keys of domain dictionary, when creating template form domain {}. Exception: {}'
                        .format(id_domain, str(e)))
                    return False

                if dict_new_template['kind'] == 'base':
                    type_path_selected = 'bases'
                else:
                    type_path_selected = 'templates'

                new_file, path_selected = get_path_to_disk(
                    path_template_disk_relative,
                    pool=pool_id,
                    type_path=type_path_selected)
                path_absolute_template_disk = new_file = new_file.replace(
                    '//', '/')
                dict_new_template['create_dict']['hardware']['disks'][i][
                    'file'] = new_file
                dict_new_template['create_dict']['hardware']['disks'][i][
                    'path_selected'] = path_selected

                update_table_field('domains', id_domain, 'create_dict',
                                   create_dict)

                action = {}
                action['id_domain'] = id_domain
                action['type'] = 'create_template_disk_from_domain'
                action['path_template_disk'] = path_absolute_template_disk
                action['path_domain_disk'] = path_domain_disk
                action['disk_index'] = disk_index_in_bus

                hyp_to_disk_create = get_host_disk_operations_from_path(
                    path_selected, pool=pool_id, type_path=type_path_selected)

                # INFO TO DEVELOPER: falta terminar de ver que hacemos con el pool para crear
                # discos, debería haber un disk operations por pool
                try:

                    update_domain_status(
                        status='CreatingTemplateDisk',
                        id_domain=id_domain,
                        hyp_id=False,
                        detail=
                        'Creating template disk operation is launched in hostname {} ({} operations in queue)'
                        .format(
                            hyp_to_disk_create, self.manager.
                            q_disk_operations[hyp_to_disk_create].qsize()))
                    self.manager.q_disk_operations[hyp_to_disk_create].put(
                        action)
                except Exception as e:
                    update_domain_status(
                        status='Stopped',
                        id_domain=id_domain,
                        hyp_id=False,
                        detail=
                        'Creating template operation failed when insert action in queue for disk operations'
                    )
                    log.error(
                        'Creating disk operation failed when insert action in queue for disk operations in host {}. Exception: {}'
                        .format(hyp_to_disk_create, e))
                    return False

                    disk_index_in_bus = disk_index_in_bus + 1

            return True
Esempio n. 7
0
    def creating_disk_from_scratch(self, id_new):
        dict_domain = get_domain(id_new)

        pool_var = dict_domain['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        dict_to_create = dict_domain['create_dict']

        if 'disks' in dict_to_create['hardware'].keys():
            if len(dict_to_create['hardware']['disks']) > 0:

                # for index_disk in range(len(dict_to_create['hardware']['disks'])):
                #     relative_path = dict_to_create['hardware']['disks'][index_disk]['file']
                #     path_new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id)
                #     # UPDATE PATH IN DOMAIN
                #     dict_to_create['hardware']['disks'][index_disk]['file'] = new_file
                #     dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected

                relative_path = dict_to_create['hardware']['disks'][0]['file']
                path_new_disk, path_selected = get_path_to_disk(relative_path,
                                                                pool=pool_id)
                # UPDATE PATH IN DOMAIN

                d_update_domain = {'hardware': {'disks': [{}]}}
                if len(dict_to_create['hardware']['disks']) > 0:
                    ## supplementary disks
                    for i, dict_other_disk in enumerate(
                            dict_to_create['hardware']['disks'][1:]):
                        path_other_disk, path_other_disk_selected = get_path_to_disk(
                            dict_other_disk['file'],
                            pool=pool_id,
                            type_path=dict_other_disk['type_path'])
                        d_update_domain['hardware']['disks'].append({})
                        d_update_domain['hardware']['disks'][
                            i + 1]['file'] = path_other_disk
                        d_update_domain['hardware']['disks'][
                            i + 1]['path_selected'] = path_other_disk_selected
                        d_update_domain['hardware']['disks'][
                            i + 1]['bus'] = dict_other_disk.get(
                                'bus', 'virtio')
                        if dict_other_disk.get('readonly', True) is True:
                            d_update_domain['hardware']['disks'][
                                i + 1]['readonly'] = True
                        else:
                            pass
                            # TODO
                            # update_media_write_access_by_domain(id_media,id_domain)

                d_update_domain['hardware']['disks'][0]['file'] = path_new_disk
                d_update_domain['hardware']['disks'][0][
                    'path_selected'] = path_selected
                d_update_domain['hardware']['disks'][0][
                    'size'] = dict_to_create['hardware']['disks'][0]['size']
                if 'bus' in dict_to_create['hardware']['disks'][0].keys():
                    if dict_to_create['hardware']['disks'][0][
                            'bus'] in BUS_TYPES:
                        d_update_domain['hardware']['disks'][0][
                            'bus'] = dict_to_create['hardware']['disks'][0][
                                'bus']
                update_domain_dict_hardware(id_new, d_update_domain)
                update_domain_dict_create_dict(id_new, d_update_domain)

                size_str = dict_to_create['hardware']['disks'][0]['size']

                hyp_to_disk_create = get_host_disk_operations_from_path(
                    path_selected, pool=pool_id, type_path='groups')

                cmds = create_cmd_disk_from_scratch(
                    path_new_disk=path_new_disk, size_str=size_str)

                action = {}
                action['type'] = 'create_disk_from_scratch'
                action['disk_path'] = path_new_disk
                action['index_disk'] = 0
                action['domain'] = id_new
                action['ssh_commands'] = cmds
                try:
                    update_domain_status(
                        status='CreatingDiskFromScratch',
                        id_domain=id_new,
                        hyp_id=False,
                        detail=
                        'Creating disk commands are launched in hypervisor {} ({} operations in queue)'
                        .format(
                            hyp_to_disk_create, self.manager.
                            q_disk_operations[hyp_to_disk_create].qsize()))
                    self.manager.q_disk_operations[hyp_to_disk_create].put(
                        action)

                except Exception as e:
                    update_domain_status(
                        status='FailedCreatingDomain',
                        id_domain=id_new,
                        hyp_id=False,
                        detail=
                        'Creating disk operation failed when insert action in queue for disk operations'
                    )
                    log.error(
                        'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'
                        .format(e))

        else:
            update_domain_status(status='CreatingDomain',
                                 id_domain=id_new,
                                 hyp_id=False,
                                 detail='Creating domain withouth disks')
Esempio n. 8
0
    def creating_disks_from_template(self,
                                     id_new):
        dict_domain = get_domain(id_new)
        if 'create_dict' in dict_domain.keys():
            dict_to_create = dict_domain['create_dict']

        pool_var = dict_domain['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        # INFO TO DEVELOPER DEBERÍA SER UN FOR PARA CADA DISCO
        # y si el disco no tiene backing_chain, crear un disco vacío
        # del tamño que marcase
        # d['hardware']['disks'][0]['size']
        # el backing_file debería estar asociado a cada disco:
        # d['hardware']['disks'][0]['backing_file']

        for index_disk in range(len(dict_to_create['hardware']['disks'])):
            relative_path = dict_to_create['hardware']['disks'][index_disk]['file']
            new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id)
            # UPDATE PATH IN DOMAIN
            dict_to_create['hardware']['disks'][index_disk]['file'] = new_file
            dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected

        update_table_field('domains',id_new,'create_dict',dict_to_create)

        #TODO: REVISAR SI RELAMENTE ES NECESARIO o esta acción responde a versiones antiguas de nuestras funciones de creación
        hardware_update = {}
        hardware_update['disks'] = dict_to_create['hardware']['disks']
        update_domain_dict_hardware(id_new, hardware_update)
        ##################

        for index_disk in range(len(dict_to_create['hardware']['disks'])):
            backing_file = dict_to_create['hardware']['disks'][index_disk]['parent']
            new_file = dict_to_create['hardware']['disks'][index_disk]['file']
            path_selected = dict_to_create['hardware']['disks'][index_disk]['path_selected']
            hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=pool_id, type_path='groups')

            cmds = create_cmds_disk_from_base(path_base=backing_file, path_new=new_file)
            log.debug('commands to disk create to launch in disk_operations: \n{}'.format('\n'.join(cmds)))
            action = {}
            action['type'] = 'create_disk'
            action['disk_path'] = new_file
            action['index_disk'] = index_disk
            action['domain'] = id_new
            action['ssh_commands'] = cmds

            try:
                update_domain_status(status='CreatingDisk',
                                     id_domain=id_new,
                                     hyp_id=False,
                                     detail='Creating disk operation is launched in hypervisor {} ({} operations in queue)'.format(
                                         hyp_to_disk_create,
                                         self.manager.q_disk_operations[hyp_to_disk_create].qsize()))
                self.manager.q_disk_operations[hyp_to_disk_create].put(action)

            except Exception as e:
                update_domain_status(status='FailedCreatingDomain',
                                     id_domain=id_new,
                                     hyp_id=False,
                                     detail='Creating disk operation failed when insert action in queue for disk operations')
                log.error(
                    'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'.format(
                        e))
Esempio n. 9
0
    def creating_disk_from_scratch(self,id_new):
        dict_domain = get_domain(id_new)

        pool_var = dict_domain['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        dict_to_create = dict_domain['create_dict']

        if 'disks' in dict_to_create['hardware'].keys():
            if len(dict_to_create['hardware']['disks']) > 0:

                # for index_disk in range(len(dict_to_create['hardware']['disks'])):
                #     relative_path = dict_to_create['hardware']['disks'][index_disk]['file']
                #     path_new_file, path_selected = get_path_to_disk(relative_path, pool=pool_id)
                #     # UPDATE PATH IN DOMAIN
                #     dict_to_create['hardware']['disks'][index_disk]['file'] = new_file
                #     dict_to_create['hardware']['disks'][index_disk]['path_selected'] = path_selected

                relative_path = dict_to_create['hardware']['disks'][0]['file']
                path_new_disk, path_selected = get_path_to_disk(relative_path, pool=pool_id)
                # UPDATE PATH IN DOMAIN

                d_update_domain = {'hardware':{'disks':[{}]}}
                if len(dict_to_create['hardware']['disks']) > 0:
                    ## supplementary disks
                    for i,dict_other_disk in enumerate(dict_to_create['hardware']['disks'][1:]):
                        path_other_disk, path_other_disk_selected = get_path_to_disk(dict_other_disk['file'],
                                                                                     pool=pool_id,
                                                                                     type_path=dict_other_disk['type_path'])
                        d_update_domain['hardware']['disks'].append({})
                        d_update_domain['hardware']['disks'][i+1]['file'] = path_other_disk
                        d_update_domain['hardware']['disks'][i+1]['path_selected'] = path_other_disk_selected
                        d_update_domain['hardware']['disks'][i + 1]['bus'] = dict_other_disk.get('bus','virtio')
                        if dict_other_disk.get('readonly',True) is True:
                            d_update_domain['hardware']['disks'][i + 1]['readonly'] = True
                        else:
                            pass
                            # TODO
                            # update_media_write_access_by_domain(id_media,id_domain)

                d_update_domain['hardware']['disks'][0]['file'] = path_new_disk
                d_update_domain['hardware']['disks'][0]['path_selected'] = path_selected
                d_update_domain['hardware']['disks'][0]['size'] = dict_to_create['hardware']['disks'][0]['size']
                if 'bus' in dict_to_create['hardware']['disks'][0].keys():
                    if dict_to_create['hardware']['disks'][0]['bus'] in BUS_TYPES:
                        d_update_domain['hardware']['disks'][0]['bus'] = dict_to_create['hardware']['disks'][0]['bus']
                update_domain_dict_hardware(id_new, d_update_domain)
                update_domain_dict_create_dict(id_new, d_update_domain)

                size_str = dict_to_create['hardware']['disks'][0]['size']

                hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=pool_id, type_path='groups')

                cmds = create_cmd_disk_from_scratch(path_new_disk=path_new_disk,
                                                         size_str=size_str)

                action = {}
                action['type'] = 'create_disk_from_scratch'
                action['disk_path'] = path_new_disk
                action['index_disk'] = 0
                action['domain'] = id_new
                action['ssh_commands'] = cmds
                try:
                    update_domain_status(status='CreatingDiskFromScratch',
                                         id_domain=id_new,
                                         hyp_id=False,
                                         detail='Creating disk commands are launched in hypervisor {} ({} operations in queue)'.format(
                                             hyp_to_disk_create,
                                             self.manager.q_disk_operations[hyp_to_disk_create].qsize()))
                    self.manager.q_disk_operations[hyp_to_disk_create].put(action)

                except Exception as e:
                    update_domain_status(status='FailedCreatingDomain',
                                         id_domain=id_new,
                                         hyp_id=False,
                                         detail='Creating disk operation failed when insert action in queue for disk operations')
                    log.error(
                        'Creating disk operation failed when insert action in queue for disk operations. Exception: {}'.format(
                            e))

        else:
            update_domain_status(status='CreatingDomain',
                                 id_domain=id_new,
                                 hyp_id=False,
                                 detail='Creating domain withouth disks')
Esempio n. 10
0
    def create_template_disks_from_domain(self, id_domain):
        dict_domain = get_domain(id_domain)

        create_dict = dict_domain['create_dict']

        pool_var = create_dict['template_dict']['hypervisors_pools']
        pool_id = pool_var if type(pool_var) is str else pool_var[0]

        try:
            dict_new_template = create_dict['template_dict']
        except KeyError as e:
            update_domain_status(status='Stopped',
                                 id_domain=id_domain,
                                 hyp_id=False,
                                 detail='Action Creating Template from domain failed. No template_json in domain dictionary')
            log.error(
                'No template_dict in keys of domain dictionary, when creating template form domain {}. Exception: {}'.format(
                    id_domain, str(e)))
            return False

        disk_index_in_bus = 0
        if 'disks' in dict_domain['hardware']:

            list_disk_template_path_relative = [d['file'] for d in create_dict['hardware']['disks']]
            create_disk_template_created_list_in_domain(id_domain)
            for i in range(len(list_disk_template_path_relative)):
                # for disk in dict_domain['hardware']['disks']:
                path_domain_disk = dict_domain['hardware']['disks'][i]['file']

                try:
                    # path_template_disk_relative = dict_new_template['create_dict']['hardware']['disks'][i]['file']
                    path_template_disk_relative = list_disk_template_path_relative[i]
                except KeyError as e:
                    update_domain_status(status='Stopped',
                                         id_domain=id_domain,
                                         hyp_id=False,
                                         detail='Action Creating Template from domain failed. No disks in template_json in domain dictionary')
                    log.error(
                        'No disks in template_json in keys of domain dictionary, when creating template form domain {}. Exception: {}'.format(
                            id_domain, str(e)))
                    return False

                if dict_new_template['kind'] == 'base':
                    type_path_selected = 'bases'
                else:
                    type_path_selected = 'templates'

                new_file, path_selected = get_path_to_disk(path_template_disk_relative, pool=pool_id,
                                                           type_path=type_path_selected)
                path_absolute_template_disk = new_file = new_file.replace('//', '/')
                dict_new_template['create_dict']['hardware']['disks'][i]['file'] = new_file
                dict_new_template['create_dict']['hardware']['disks'][i]['path_selected'] = path_selected

                update_table_field('domains', id_domain, 'create_dict', create_dict)

                action = {}
                action['id_domain'] = id_domain
                action['type'] = 'create_template_disk_from_domain'
                action['path_template_disk'] = path_absolute_template_disk
                action['path_domain_disk'] = path_domain_disk
                action['disk_index'] = disk_index_in_bus

                hyp_to_disk_create = get_host_disk_operations_from_path(path_selected, pool=pool_id,
                                                                        type_path=type_path_selected)

                # INFO TO DEVELOPER: falta terminar de ver que hacemos con el pool para crear
                # discos, debería haber un disk operations por pool
                try:

                    update_domain_status(status='CreatingTemplateDisk',
                                         id_domain=id_domain,
                                         hyp_id=False,
                                         detail='Creating template disk operation is launched in hostname {} ({} operations in queue)'.format(
                                             hyp_to_disk_create,
                                             self.manager.q_disk_operations[hyp_to_disk_create].qsize()))
                    self.manager.q_disk_operations[hyp_to_disk_create].put(action)
                except Exception as e:
                    update_domain_status(status='Stopped',
                                         id_domain=id_domain,
                                         hyp_id=False,
                                         detail='Creating template operation failed when insert action in queue for disk operations')
                    log.error(
                        'Creating disk operation failed when insert action in queue for disk operations in host {}. Exception: {}'.format(
                            hyp_to_disk_create, e))
                    return False

                    disk_index_in_bus = disk_index_in_bus + 1

            return True