Beispiel #1
0
def create_cmd_disk_from_scratch(path_new_disk,
                                 size_str,
                                 cluster_size = '4k',
                                 disk_type = 'qcow2',
                                 incremental = True,
                                 user_owner='qemu',
                                 group_owner='qemu'
                                 ):
    cmds1 = list()
    path_dir = extract_dir_path(path_new_disk)
    touch_test_path = path_dir + '/.touch_test'
    cmd_qemu_img = "qemu-img create -f {disk_type} -o cluster_size={cluster_size} {file_path} {size_str}".\
                               format(disk_type = disk_type,
                                       size_str = size_str,
                                   cluster_size = cluster_size,
                                      file_path = path_new_disk)

    cmds1.append({'title': 'mkdir dir', 'cmd': 'mkdir -p {}'.format(path_dir)})
    cmds1.append({'title': 'pre-delete touch', 'cmd': 'rm -f {}'.format(touch_test_path)})
    cmds1.append({'title': 'touch', 'cmd': 'touch {}'.format(touch_test_path)})
    cmds1.append({'title': 'readonly_touch', 'cmd': 'chmod a-wx,g+r,u+r {}'.format(touch_test_path)})
    cmds1.append({'title': 'chgrp_touch', 'cmd': 'chgrp {} {}'.format(group_owner, touch_test_path)})
    cmds1.append({'title': 'chown', 'cmd': 'chown {} {}'.format(user_owner, touch_test_path)})
    cmds1.append({'title': 'verify_touch',
                  'cmd': 'stat -c \'mountpoint:%m group:%G user:%U rights:%A\' {}'.format(touch_test_path)})
    cmds1.append({'title': 'df_mountpoint', 'cmd': 'df $(stat -c \'%m\' {})'.format(touch_test_path)})
    cmds1.append({'title': 'rm_touch', 'cmd': 'rm -f {}'.format(touch_test_path)})

    cmds1.append({'title': 'launch qemu-img', 'cmd': cmd_qemu_img})
    cmds1.append({'title': 'test_if_qcow_exists', 'cmd': 'stat -c \'%d\' {}'.format(path_new_disk)})

    return cmds1
Beispiel #2
0
def create_cmd_disk_from_scratch(path_new_disk,
                                 size_str,
                                 cluster_size='4k',
                                 disk_type='qcow2',
                                 incremental=True,
                                 user_owner='qemu',
                                 group_owner='qemu'):
    cmds1 = list()
    path_dir = extract_dir_path(path_new_disk)
    touch_test_path = path_dir + '/.touch_test'
    cmd_qemu_img = "qemu-img create -f {disk_type} -o cluster_size={cluster_size} {file_path} {size_str}".\
                               format(disk_type = disk_type,
                                       size_str = size_str,
                                   cluster_size = cluster_size,
                                      file_path = path_new_disk)

    cmds1.append({'title': 'mkdir dir', 'cmd': 'mkdir -p {}'.format(path_dir)})
    cmds1.append({
        'title': 'pre-delete touch',
        'cmd': 'rm -f {}'.format(touch_test_path)
    })
    cmds1.append({'title': 'touch', 'cmd': 'touch {}'.format(touch_test_path)})
    cmds1.append({
        'title': 'readonly_touch',
        'cmd': 'chmod a-wx,g+r,u+r {}'.format(touch_test_path)
    })
    cmds1.append({
        'title': 'chgrp_touch',
        'cmd': 'chgrp {} {}'.format(group_owner, touch_test_path)
    })
    cmds1.append({
        'title': 'chown',
        'cmd': 'chown {} {}'.format(user_owner, touch_test_path)
    })
    cmds1.append({
        'title':
        'verify_touch',
        'cmd':
        'stat -c \'mountpoint:%m group:%G user:%U rights:%A\' {}'.format(
            touch_test_path)
    })
    cmds1.append({
        'title': 'df_mountpoint',
        'cmd': 'df $(stat -c \'%m\' {})'.format(touch_test_path)
    })
    cmds1.append({
        'title': 'rm_touch',
        'cmd': 'rm -f {}'.format(touch_test_path)
    })

    cmds1.append({'title': 'launch qemu-img', 'cmd': cmd_qemu_img})
    cmds1.append({
        'title': 'test_if_qcow_exists',
        'cmd': 'stat -c \'%d\' {}'.format(path_new_disk)
    })

    return cmds1
Beispiel #3
0
def create_cmds_disk_template_from_domain(path_template_disk, path_domain_disk, user_owner='qemu', group_owner='qemu',
                                          clustersize='4k'):
    # INFO TO DEVELOPER, OJO SI NOS PASAN UN PATH CON ESPACIOS,HABRÍA QUE PONER COMILLAS EN TODOS LOS COMANDOS
    cmds1 = list()
    path_dir_template = extract_dir_path(path_template_disk)
    touch_test_path = path_dir_template + '/.touch_test'

    cmds1.append({'title': 'mkdir_template_dir', 'cmd': 'mkdir -p {}'.format(path_dir_template)})
    cmds1.append({'title': 'pre-delete touch', 'cmd': 'rm -f {}'.format(touch_test_path)})
    cmds1.append({'title': 'touch', 'cmd': 'touch {}'.format(touch_test_path)})
    cmds1.append({'title': 'readonly_touch', 'cmd': 'chmod a-wx,g+r,u+r {}'.format(touch_test_path)})
    cmds1.append({'title': 'chgrp_touch', 'cmd': 'chgrp {} {}'.format(group_owner, touch_test_path)})
    cmds1.append({'title': 'chown', 'cmd': 'chown {} {}'.format(user_owner, touch_test_path)})
    cmds1.append({'title': 'verify_touch',
                  'cmd': 'stat -c \'mountpoint:%m group:%G user:%U rights:%A\' {}'.format(touch_test_path)})
    # with busybox stat -c %m is not an option and fail
    #cmds1.append({'title': 'df_template_mountpoint', 'cmd': 'df $(stat -c \'%m\' {})'.format(touch_test_path)})
    cmds1.append({'title': 'df_template_mountpoint', 'cmd': 'df {}'.format(touch_test_path)})
    cmds1.append({'title': 'size_template_disk', 'cmd': 'stat -c \'%s\' {}'.format(path_domain_disk)})
    cmds1.append({'title': 'rm_touch', 'cmd': 'rm -f {}'.format(touch_test_path)})
    # path_template_disk must be error
    cmds1.append({'title': 'test_if_template_exists', 'cmd': 'stat -c \'%d\' {}'.format(path_template_disk)})
    # compare file system to do mv or rsync with progress
    cmds1.append({'title': 'filesystem_template', 'cmd': 'stat -c \'%d\' {}'.format(path_dir_template)})
    cmds1.append({'title': 'filesystem_domain', 'cmd': 'stat -c \'%d\' {}'.format(path_domain_disk)})

    # #path_template_disk must be error
    # cmds1.append('stat -c \'%d\' {})'.format(path_template_disk))
    # #compare file system to do mv or rsync with progress
    # cmds1.append('fs_template=$(stat -c \'%d\' {})'.format(path_dir_template))
    # cmds1.append('fs_domain=$(stat -c \'%d\' {})'.format(path_domain_disk))
    # cmds1.append('if [ "$fs_template" == "$fs_domain" ]; then echo "equal filesystem"; else echo "disctint filesystem"; fi')


    # then move or rsync and recreate disk_domain with cmds2

    # INFO TO DEVELOPER, HABRÍA QUE VERIFICAR CON UN MD5 QUE NO LA LIA AL HACER LA COPIA Y QUE SON IDÉNTICAS A LA QUE SE HA MOVIDO??

    cmds2 = list()
    # path_domain_disk must be error
    cmds2.append({'title': 'test_if_disk_template_exists', 'cmd': 'stat -c \'%s\' {}'.format(path_template_disk)})
    cmds2.append({'title': 'test_if_disk_domain_exists', 'cmd': 'stat -c \'%s\' {}'.format(path_domain_disk)})

    cmds3 = list()
    cmds3.append({'title': 'create_disk_domain_from_new_template',
                  'cmd': create_disk_from_base_cmd(path_domain_disk, path_template_disk)})
    cmds3.append({'title': 'test_if_disk_domain_exists', 'cmd': 'stat -c \'%s\' {}'.format(path_domain_disk)})
    cmds3.append({'title': 'backing_chain_disk_domain', 'cmd': backing_chain_cmd(path_domain_disk)})
    cmds3.append({'title': 'backing_chain_disk_template', 'cmd': backing_chain_cmd(path_template_disk)})

    return cmds1, cmds2, cmds3
Beispiel #4
0
def create_cmd_disk_from_virtbuilder(path_new_qcow,
                                     os_version,
                                     id_os_virt_install,
                                     name_domain_in_xml,
                                     size_str,
                                     memory_in_mb,
                                     options_cmd='',
                                     user_owner='qemu',
                                     group_owner='qemu'):
    cmds1 = list()
    path_dir = extract_dir_path(path_new_qcow)
    path_big_disk = path_new_qcow + '.big'
    path_dir_tmp_sparsify = path_dir + '/tmp'
    touch_test_path = path_dir + '/.touch_test'

    cmd_virt_builder = 'virt-builder {os}  --machine-readable --output {path} --size {size} --format qcow2 {options}' \
        .format(os=os_version, path=path_new_qcow, size=size_str, options=options_cmd)
    cmd_virt_sparsify = 'virt-sparsify --in-place {path_new_qcow}' \
        .format(path_new_qcow=path_new_qcow)
    cmd_virt_install = 'virt-install --import --dry-run --print-xml --disk {} --memory {} --os-variant {} --name {}' \
        .format(path_new_qcow, memory_in_mb, id_os_virt_install, name_domain_in_xml)

    cmds1.append({'title': 'mkdir dir', 'cmd': 'mkdir -p {}'.format(path_dir)})
    cmds1.append({'title': 'mkdir dir tmp sparsify', 'cmd': 'mkdir -p {}'.format(path_dir_tmp_sparsify)})
    cmds1.append({'title': 'pre-delete touch', 'cmd': 'rm -f {}'.format(touch_test_path)})
    cmds1.append({'title': 'touch', 'cmd': 'touch {}'.format(touch_test_path)})
    cmds1.append({'title': 'readonly_touch', 'cmd': 'chmod a-wx,g+r,u+r {}'.format(touch_test_path)})
    cmds1.append({'title': 'chgrp_touch', 'cmd': 'chgrp {} {}'.format(group_owner, touch_test_path)})
    cmds1.append({'title': 'chown', 'cmd': 'chown {} {}'.format(user_owner, touch_test_path)})
    cmds1.append({'title': 'verify_touch',
                  'cmd': 'stat -c \'mountpoint:%m group:%G user:%U rights:%A\' {}'.format(touch_test_path)})
    cmds1.append({'title': 'df_mountpoint', 'cmd': 'df $(stat -c \'%m\' {})'.format(touch_test_path)})
    cmds1.append({'title': 'rm_touch', 'cmd': 'rm -f {}'.format(touch_test_path)})

    # INFO TO DEVELOPER - todo: launch virt-builder and virt-sparify in other thread to capture output to detail
    cmds1.append({'title': 'launch virt-builder', 'cmd': cmd_virt_builder})
    cmds1.append({'title': 'launch virt-sparsify', 'cmd': cmd_virt_sparsify})

    cmds1.append({'title': 'rmdir tmp sparsify', 'cmd': 'rmdir {}'.format(path_dir_tmp_sparsify)})
    cmds1.append({'title': 'rm big qcow', 'cmd': 'rm -f {}'.format(path_big_disk)})
    cmds1.append({'title': 'test_if_qcow_exists', 'cmd': 'stat -c \'%d\' {}'.format(path_new_qcow)})
    cmds1.append({'title': 'xml from virt-install', 'cmd': cmd_virt_install})

    s = "\n".join([c['cmd'] for c in cmds1])
    print(s)
    return cmds1
Beispiel #5
0
def create_cmds_disk_from_base(path_base, path_new, clustersize='4k'):
    # INFO TO DEVELOPER todo: hay que verificar primero si el disco no existe, ya que si no lo machaca creo
    # no se bien cuaqndo hacerlo y si vale la pena, habríamos de manejarlo como una excepción
    # o hacer un stat una vez creado y verificar que devuelve algo

    cmds = list()
    path_dir = extract_dir_path(path_new)
    cmd = 'mkdir -p {}'.format(path_dir)
    cmds.append(cmd)

    cmd = create_disk_from_base_cmd(path_new, path_base, clustersize)
    log.debug('creating disk cmd: {}'.format(cmd))
    cmds.append(cmd)

    # INFO TO DEVELOPER todo: hay que verificar primero si el disco no existe, ya que si no lo machaca creo
    # no se bien cuaqndo hacerlo y si vale la pena

    cmd = backing_chain_cmd(path_new)
    cmds.append(cmd)

    return cmds
Beispiel #6
0
def create_cmds_disk_from_base(path_base, path_new, clustersize='4k'):
    # INFO TO DEVELOPER todo: hay que verificar primero si el disco no existe, ya que si no lo machaca creo
    # no se bien cuaqndo hacerlo y si vale la pena, habríamos de manejarlo como una excepción
    # o hacer un stat una vez creado y verificar que devuelve algo

    cmds = list()
    path_dir = extract_dir_path(path_new)
    cmd = 'mkdir -p {}'.format(path_dir)
    cmds.append(cmd)

    cmd = create_disk_from_base_cmd(path_new, path_base, clustersize)
    log.debug('creating disk cmd: {}'.format(cmd))
    cmds.append(cmd)

    # INFO TO DEVELOPER todo: hay que verificar primero si el disco no existe, ya que si no lo machaca creo
    # no se bien cuaqndo hacerlo y si vale la pena

    cmd = backing_chain_cmd(path_new)
    cmds.append(cmd)

    return cmds
Beispiel #7
0
def create_cmd_disk_from_virtbuilder(path_new_qcow,
                                     os_version,
                                     id_os_virt_install,
                                     name_domain_in_xml,
                                     size_str,
                                     memory_in_mb,
                                     options_cmd='',
                                     user_owner='qemu',
                                     group_owner='qemu'):
    cmds1 = list()
    path_dir = extract_dir_path(path_new_qcow)
    path_big_disk = path_new_qcow + '.big'
    path_dir_tmp_sparsify = path_dir + '/tmp'
    touch_test_path = path_dir + '/.touch_test'

    cmd_virt_builder = 'virt-builder {os}  --machine-readable --output {path} --size {size} --format qcow2 {options}' \
        .format(os=os_version, path=path_new_qcow, size=size_str, options=options_cmd)
    cmd_virt_sparsify = 'virt-sparsify --in-place {path_new_qcow}' \
        .format(path_new_qcow=path_new_qcow)
    cmd_virt_install = 'virt-install --import --dry-run --print-xml --disk {} --memory {} --os-variant {} --name {}' \
        .format(path_new_qcow, memory_in_mb, id_os_virt_install, name_domain_in_xml)

    cmds1.append({'title': 'mkdir dir', 'cmd': 'mkdir -p {}'.format(path_dir)})
    cmds1.append({
        'title': 'mkdir dir tmp sparsify',
        'cmd': 'mkdir -p {}'.format(path_dir_tmp_sparsify)
    })
    cmds1.append({
        'title': 'pre-delete touch',
        'cmd': 'rm -f {}'.format(touch_test_path)
    })
    cmds1.append({'title': 'touch', 'cmd': 'touch {}'.format(touch_test_path)})
    cmds1.append({
        'title': 'readonly_touch',
        'cmd': 'chmod a-wx,g+r,u+r {}'.format(touch_test_path)
    })
    cmds1.append({
        'title': 'chgrp_touch',
        'cmd': 'chgrp {} {}'.format(group_owner, touch_test_path)
    })
    cmds1.append({
        'title': 'chown',
        'cmd': 'chown {} {}'.format(user_owner, touch_test_path)
    })
    cmds1.append({
        'title':
        'verify_touch',
        'cmd':
        'stat -c \'mountpoint:%m group:%G user:%U rights:%A\' {}'.format(
            touch_test_path)
    })
    cmds1.append({
        'title': 'df_mountpoint',
        'cmd': 'df $(stat -c \'%m\' {})'.format(touch_test_path)
    })
    cmds1.append({
        'title': 'rm_touch',
        'cmd': 'rm -f {}'.format(touch_test_path)
    })

    # INFO TO DEVELOPER - todo: launch virt-builder and virt-sparify in other thread to capture output to detail
    cmds1.append({'title': 'launch virt-builder', 'cmd': cmd_virt_builder})
    cmds1.append({'title': 'launch virt-sparsify', 'cmd': cmd_virt_sparsify})

    cmds1.append({
        'title': 'rmdir tmp sparsify',
        'cmd': 'rmdir {}'.format(path_dir_tmp_sparsify)
    })
    cmds1.append({
        'title': 'rm big qcow',
        'cmd': 'rm -f {}'.format(path_big_disk)
    })
    cmds1.append({
        'title': 'test_if_qcow_exists',
        'cmd': 'stat -c \'%d\' {}'.format(path_new_qcow)
    })
    cmds1.append({'title': 'xml from virt-install', 'cmd': cmd_virt_install})

    s = "\n".join([c['cmd'] for c in cmds1])
    print(s)
    return cmds1
Beispiel #8
0
def verify_output_cmds1_template_from_domain(cmds_done, path_domain_disk,
                                             path_template_disk, id_domain):
    move_tool = None
    cmd_to_move = None
    path_dir_template = extract_dir_path(path_template_disk)
    error_severity = None

    d = [a for a in cmds_done if a['title'] == 'filesystem_domain'][0]
    if len(d['err']) > 0:
        log.error(
            'domain disk {} doesn\'t exist or permissions access error. Domain:{}. Error: {}'
            .format(path_domain_disk, id_domain, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'],
                                                     d['err']))
        error_severity = 'Hard'
    else:
        fs_domain_code = int(d['out'])

    d = [a for a in cmds_done if a['title'] == 'filesystem_template'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error(
            'directory for template disk {} can not be created or permissions access error. Domain:{}. Error: {}'
            .format(path_template_disk, id_domain, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'],
                                                     d['err']))
        error_severity = 'Hard'
    elif error_severity != 'Hard':
        fs_template_code = int(d['out'])
        if fs_template_code == fs_domain_code:
            move_tool = 'mv'
        else:
            move_tool = 'rsync'

    d = [a for a in cmds_done if a['title'] == 'touch'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error(
            'When try to write in directory {} fail with error: {} '.format(
                path_dir_template, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'],
                                                     d['err']))
        error_severity = 'Hard'

    d = [a for a in cmds_done if a['title'] == 'df_template_mountpoint'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error(
            'When try to know disk free space previous to create template in template directory, command fail'
        )
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'],
                                                     d['err']))
        error_severity = 'Hard'
    elif error_severity != 'Hard':
        df_bytes = int(d['out'].splitlines()[-1].split()[3]) * 1024
        log.debug('disk free for create template from domain {}: {}'.format(
            id_domain, size_format(df_bytes)))

    d = [a for a in cmds_done if a['title'] == 'size_template_disk'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error(
            'When try to access domain file disk {} fail with error: {} '.
            format(path_domain_disk, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'],
                                                     d['err']))
        error_severity = 'Hard'
    elif error_severity != 'Hard':
        disk_size_bytes = int(d['out'])
        log.debug('disk {} size: {} , template filesystem df: {}'.format(
            path_domain_disk, size_format(disk_size_bytes),
            size_format(df_bytes)))
        if disk_size_bytes >= df_bytes:
            log.error(
                'Not enough free space to create template from {}'.format(
                    id_domain))
            log.debug('cmd: {}, out: {}, err: {}'.format(
                d['cmd'], d['out'], d['err']))
            error_severity = 'Hard'

    d = [a for a in cmds_done if a['title'] == 'test_if_template_exists'][0]
    if len(d['err']) == 0:
        log.error(
            'File template that must be created and it exists!! File path: {}'.
            format(path_template_disk))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'],
                                                     d['err']))
        error_severity = 'Hard'

    d = [a for a in cmds_done if a['title'] == 'verify_touch'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        if d['out'].find('rights:-r--r--r--') > 0:
            log.debug(
                'change rights readonly test passed for create template from domain {}'
                .format(id_domain))
            log.debug('cmd: {}, out: {}, err: {}'.format(
                d['cmd'], d['out'], d['err']))
        else:
            log.error(
                'PROBLEM change rights readonly test passed for create template from domain {}'
                .format(id_domain))
            log.error('cmd: {}, out: {}, err: {}'.format(
                d['cmd'], d['out'], d['err']))
            error_severity = 'Soft'
            # INFO TO DEVELOPER, DE MOMENTO NO MIRAMOS SI EL PROPIETARIO Y GRUPO ES qemu, si se cree importante aquí habría que implementarlo

    if error_severity == None and move_tool != None:
        if move_tool == 'mv':
            cmd_to_move = 'mv {} {}'.format(path_domain_disk,
                                            path_template_disk)
        if move_tool == 'rsync':
            # cmd_to_move = 'rsync -aP --remove-source-files "{}" "{}"'.format(path_domain_disk,path_template_disk)
            cmd_to_move = 'rsync -aP --inplace --bwlimit=30000 --remove-source-files "{}" "{}"'.format(
                path_domain_disk, path_template_disk)

    return error_severity, move_tool, cmd_to_move
Beispiel #9
0
def create_cmds_disk_template_from_domain(path_template_disk,
                                          path_domain_disk,
                                          user_owner='qemu',
                                          group_owner='qemu',
                                          clustersize='4k'):
    # INFO TO DEVELOPER, OJO SI NOS PASAN UN PATH CON ESPACIOS,HABRÍA QUE PONER COMILLAS EN TODOS LOS COMANDOS
    cmds1 = list()
    path_dir_template = extract_dir_path(path_template_disk)
    touch_test_path = path_dir_template + '/.touch_test'

    cmds1.append({
        'title': 'mkdir_template_dir',
        'cmd': 'mkdir -p {}'.format(path_dir_template)
    })
    cmds1.append({
        'title': 'pre-delete touch',
        'cmd': 'rm -f {}'.format(touch_test_path)
    })
    cmds1.append({'title': 'touch', 'cmd': 'touch {}'.format(touch_test_path)})
    cmds1.append({
        'title': 'readonly_touch',
        'cmd': 'chmod a-wx,g+r,u+r {}'.format(touch_test_path)
    })
    cmds1.append({
        'title': 'chgrp_touch',
        'cmd': 'chgrp {} {}'.format(group_owner, touch_test_path)
    })
    cmds1.append({
        'title': 'chown',
        'cmd': 'chown {} {}'.format(user_owner, touch_test_path)
    })
    cmds1.append({
        'title':
        'verify_touch',
        'cmd':
        'stat -c \'mountpoint:%m group:%G user:%U rights:%A\' {}'.format(
            touch_test_path)
    })
    # with busybox stat -c %m is not an option and fail
    #cmds1.append({'title': 'df_template_mountpoint', 'cmd': 'df $(stat -c \'%m\' {})'.format(touch_test_path)})
    cmds1.append({
        'title': 'df_template_mountpoint',
        'cmd': 'df {}'.format(touch_test_path)
    })
    cmds1.append({
        'title': 'size_template_disk',
        'cmd': 'stat -c \'%s\' {}'.format(path_domain_disk)
    })
    cmds1.append({
        'title': 'rm_touch',
        'cmd': 'rm -f {}'.format(touch_test_path)
    })
    # path_template_disk must be error
    cmds1.append({
        'title': 'test_if_template_exists',
        'cmd': 'stat -c \'%d\' {}'.format(path_template_disk)
    })
    # compare file system to do mv or rsync with progress
    cmds1.append({
        'title': 'filesystem_template',
        'cmd': 'stat -c \'%d\' {}'.format(path_dir_template)
    })
    cmds1.append({
        'title': 'filesystem_domain',
        'cmd': 'stat -c \'%d\' {}'.format(path_domain_disk)
    })

    # #path_template_disk must be error
    # cmds1.append('stat -c \'%d\' {})'.format(path_template_disk))
    # #compare file system to do mv or rsync with progress
    # cmds1.append('fs_template=$(stat -c \'%d\' {})'.format(path_dir_template))
    # cmds1.append('fs_domain=$(stat -c \'%d\' {})'.format(path_domain_disk))
    # cmds1.append('if [ "$fs_template" == "$fs_domain" ]; then echo "equal filesystem"; else echo "disctint filesystem"; fi')

    # then move or rsync and recreate disk_domain with cmds2

    # INFO TO DEVELOPER, HABRÍA QUE VERIFICAR CON UN MD5 QUE NO LA LIA AL HACER LA COPIA Y QUE SON IDÉNTICAS A LA QUE SE HA MOVIDO??

    cmds2 = list()
    # path_domain_disk must be error
    cmds2.append({
        'title': 'test_if_disk_template_exists',
        'cmd': 'stat -c \'%s\' {}'.format(path_template_disk)
    })
    cmds2.append({
        'title': 'test_if_disk_domain_exists',
        'cmd': 'stat -c \'%s\' {}'.format(path_domain_disk)
    })

    cmds3 = list()
    cmds3.append({
        'title':
        'create_disk_domain_from_new_template',
        'cmd':
        create_disk_from_base_cmd(path_domain_disk, path_template_disk)
    })
    cmds3.append({
        'title': 'test_if_disk_domain_exists',
        'cmd': 'stat -c \'%s\' {}'.format(path_domain_disk)
    })
    cmds3.append({
        'title': 'backing_chain_disk_domain',
        'cmd': backing_chain_cmd(path_domain_disk)
    })
    cmds3.append({
        'title': 'backing_chain_disk_template',
        'cmd': backing_chain_cmd(path_template_disk)
    })

    return cmds1, cmds2, cmds3
Beispiel #10
0
def verify_output_cmds1_template_from_domain(cmds_done, path_domain_disk, path_template_disk, id_domain):
    move_tool = None
    cmd_to_move = None
    path_dir_template = extract_dir_path(path_template_disk)
    error_severity = None

    d = [a for a in cmds_done if a['title'] == 'filesystem_domain'][0]
    if len(d['err']) > 0:
        log.error(
            'domain disk {} doesn\'t exist or permissions access error. Domain:{}. Error: {}'.format(path_domain_disk,
                                                                                                     id_domain,
                                                                                                     d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        error_severity = 'Hard'
    else:
        fs_domain_code = int(d['out'])

    d = [a for a in cmds_done if a['title'] == 'filesystem_template'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error(
            'directory for template disk {} can not be created or permissions access error. Domain:{}. Error: {}'.format(
                path_template_disk, id_domain, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        error_severity = 'Hard'
    elif error_severity != 'Hard':
        fs_template_code = int(d['out'])
        if fs_template_code == fs_domain_code:
            move_tool = 'mv'
        else:
            move_tool = 'rsync'

    d = [a for a in cmds_done if a['title'] == 'touch'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error('When try to write in directory {} fail with error: {} '.format(path_dir_template, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        error_severity = 'Hard'

    d = [a for a in cmds_done if a['title'] == 'df_template_mountpoint'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error('When try to know disk free space previous to create template in template directory, command fail')
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        error_severity = 'Hard'
    elif error_severity != 'Hard':
        try:
            df_bytes = int(d['out'].splitlines()[-1].split()[3]) * 1024
        except:
            #if mount point is too large df split output in two lines
            try:
                df_bytes = int(d['out'].splitlines()[-1].split()[2]) * 1024
            except:
                log.info('When try to know disk free space previous to create template output is not standard')
                log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
                df_bytes = 999999999
        log.debug('disk free for create template from domain {}: {}'.format(id_domain, size_format(df_bytes)))

    d = [a for a in cmds_done if a['title'] == 'size_template_disk'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        log.error('When try to access domain file disk {} fail with error: {} '.format(path_domain_disk, d['err']))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        error_severity = 'Hard'
    elif error_severity != 'Hard':
        disk_size_bytes = int(d['out'])
        log.debug('disk {} size: {} , template filesystem df: {}'.format(path_domain_disk, size_format(disk_size_bytes),
                                                                         size_format(df_bytes)))
        if disk_size_bytes >= df_bytes:
            log.error('Not enough free space to create template from {}'.format(id_domain))
            log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
            error_severity = 'Hard'

    d = [a for a in cmds_done if a['title'] == 'test_if_template_exists'][0]
    if len(d['err']) == 0:
        log.error('File template that must be created and it exists!! File path: {}'.format(path_template_disk))
        log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        error_severity = 'Hard'

    d = [a for a in cmds_done if a['title'] == 'verify_touch'][0]
    if len(d['err']) > 0 and error_severity != 'Hard':
        if d['out'].find('rights:-r--r--r--') > 0:
            log.debug('change rights readonly test passed for create template from domain {}'.format(id_domain))
            log.debug('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
        else:
            log.error('PROBLEM change rights readonly test passed for create template from domain {}'.format(id_domain))
            log.error('cmd: {}, out: {}, err: {}'.format(d['cmd'], d['out'], d['err']))
            error_severity = 'Soft'
            # INFO TO DEVELOPER, DE MOMENTO NO MIRAMOS SI EL PROPIETARIO Y GRUPO ES qemu, si se cree importante aquí habría que implementarlo

    if error_severity == None and move_tool != None:
        if move_tool == 'mv':
            cmd_to_move = 'mv {} {}'.format(path_domain_disk, path_template_disk)
        if move_tool == 'rsync':
            # cmd_to_move = 'rsync -aP --remove-source-files "{}" "{}"'.format(path_domain_disk,path_template_disk)
            cmd_to_move = 'rsync -aP --inplace --bwlimit=30000 --remove-source-files "{}" "{}"'.format(path_domain_disk,
                                                                                                       path_template_disk)

    return error_severity, move_tool, cmd_to_move
Beispiel #11
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
Beispiel #12
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