Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
import sys
import guestfs
import multiprocessing
import time
import os
import pwd

import logging as LOG
from logging.handlers import SysLogHandler
from logging import config

from multiprocessing import Pool
from distutils.sysconfig import get_python_lib

uid = pwd.getpwnam('nova')[2]
os.setuid(uid)

NUM_RETRY = 3
SLEEP_TIME = 10
APPLIANCE_PATH = get_python_lib() + os.path.sep + 'wlmfs_appliance'

LOG.basicConfig(
     filename='/var/log/workloadmgr/file_search.log',
     level=LOG.DEBUG,
     format= '[%(asctime)s] [%(process)d %(funcName)s:%(lineno)d] [%(msecs)d ms] [%(levelname)s] - [%(message)s]',
     datefmt='%Y-%m-%d %H:%M:%S'
 )


def mount_drive(data):
    try:
        gfs = guestfs.GuestFS(python_return_dict=True)
        drives = data.split(',,')
        LOG.info('Drives: {0}'.format(drives))
        filepath = drives[0]
        drives.pop(0)
        snapshot_id = drives[0]
        drives.pop(0)
        for drive in drives:
            gfs.add_drive_opts(drive, format="qcow2", readonly=1)
        gfs.set_backend("libvirt")
        gfs.set_path(APPLIANCE_PATH)
        gfs.launch()
        dt = {}
        roots = gfs.list_filesystems()
        lt_drives = []
        for root in roots:
            try:
                gfs.mount_ro(root, '/')
            except Exception as ex:
                LOG.exception('Exception in Mounting {0}: {1}'.format(root, ex))
                continue
            val = gfs.glob_expand(filepath)
            disk = {}
            root = root.replace('s', 'v')
            disk[root] = val
            if len(val) > 0:
                for path in val:
                    try:
                        disk[path] = gfs.stat(path)
                        LOG.info('stats for disk path:- {0}'.format(disk.get('path')))
                    except Exception as ex:
                        LOG.exception('Failed to get stats: {0} Retrying one more time.'.format(ex))
                        try:
                            # Sometimes stat doesn't work at first attempt
                            disk[path] = gfs.stat(path)
                        except Exception as ex:
                            LOG.exception('Failed to get stats: {0}'.format(ex))
                            continue
            lt_drives.append(disk)
            LOG.info('Drive stats: {0}'.format(lt_drives))
            LOG.info('started unmounting disks.')
            try:
                gfs.umount_all()
            except Exception as ex:
                LOG.exception('Cannot unmount disks: {0}'.format(ex))
        dt[snapshot_id] = lt_drives
        if len(drives) == 0:
            dt[snapshot_id] = 'Snapshot VM deleted'
        try:
            gfs.close()
        except Exception as ex:
            LOG.exception('cannot close the file handles: {0}'.format(ex))
        return dt
    except Exception as ex:
        if 'gfs' in locals():
            gfs.umount_all()
            gfs.close()
        LOG.exception('Something went wrong: {0}'.format(ex))


def fetch_data(data):
    result = None
    i = 0
    while i <= NUM_RETRY:
        LOG.info('Trying {0} time'.format(i+1))
        try:
             result = mount_drive(data)
        except Exception as ex:
            LOG.exception(ex)
        i += 1
        if result is not None:
            LOG.info('data returned: {0}'.format(result))
            return result
        LOG.info('sleeping for {0}'.format(SLEEP_TIME))
        time.sleep(SLEEP_TIME)

    return {}


def main(argv):
    try:
        data = argv[0].split('|-|')
        LOG.info('spawning processes..')
        with Pool(processes=max(4, multiprocessing.cpu_count())) as pool:
            print(pool.map(fetch_data, data))
    except Exception as ex:
        LOG.exception(ex)


# Driver Code.
if __name__ == '__main__':
    main(sys.argv[1:])

#guestfs = GuestFs()
#it = guestfs.search(['/opt/stack/,snaps_id,ccbf9827-be53-4a38-bae8-82bb2fded6fc'])