Beispiel #1
0
def mount_fs(mountpoint):
    """
    Mount a filesystem specified in fstab, by mountpoint only.

    Parameters
    ----------
    mountpoint: str
        The mountpoint.

    Returns
    -------
        bool: True on success, False otherwise
    """
    cmd = ['mount', mountpoint]
    pause_msg(cmd)
    _logger.debug('__ Mounting %s' % mountpoint)
    try:
        _, nbcols = terminal_dimension()
        mountwait = ProgressBar(nbcols,
                                0.2,
                                progress_chars=['mounting %s' % mountpoint])
        mountwait.start()
        _logger.debug('Command: %s' % cmd)
        cmdret = run_call_cmd(cmd)
        _logger.debug('%s returned %d' % (cmd, cmdret))
        if cmdret == 0:
            return True
        else:
            raise Exception('%s failed: %d' % (cmd, cmdret))
    except Exception as e:
        _logger.error('  Failed to %s: %s' % (cmd, str(e)))
        return False
    finally:
        if is_thread_running(mountwait):
            mountwait.stop()
Beispiel #2
0
def get_file_from_url(url, dest):
    """
    Download file from the internet specified by url and store in destination.
    Parameters
    ----------
    url: str
        The url.
    dest: str
        The full path of the destination.

    Returns
    -------
        str: the full path of the destination on success, raises an exception
             otherwise.
    """
    _logger.debug('__ Get file from url %s.' % url)
    _, nbcols = terminal_dimension()
    packname = os.path.basename(url)
    try:
        downloadwait = ProgressBar(
            nbcols, 0.2, progress_chars=['pulling oracle-cloud-agent'])
        downloadwait.start()
        _ = urllib.request.urlretrieve(url, dest)
    except Exception as e:
        _logger.warning('Failed to retrieve %s int %s: %s' %
                        (url, dest, str(e)))
        raise ('Failed to retrieve %s int %s: %s' % (url, dest, str(e)))
    finally:
        if system_tools.is_thread_running(downloadwait):
            downloadwait.stop()
Beispiel #3
0
def mount_partition(devname, mountpoint=None):
    """
    Create the mountpoint /mnt/<last part of device specification> and mount a
    partition on on this mountpoint.

    Parameters
    ----------
    devname: str
        The full path of the device.
    mountpoint: str
        The mountpoint, will be generated if not provided.

    Returns
    -------
        str: The mounted partition on Success, None otherwise.
    """
    _logger.debug('__ Mount partition %s.', devname)
    #
    # create mountpoint /mnt/<devname> if not specified.
    if mountpoint is None:
        mntpoint = migrate_data.loopback_root + '/' + devname.rsplit('/')[-1]
        _logger.debug('Loopback mountpoint: %s', mntpoint)
        try:
            if system_tools.exec_mkdir(mntpoint):
                _logger.debug('Mountpoint: %s created.', mntpoint)
        except Exception as e:
            _logger.critical('   Failed to create mountpoint %s: %s', mntpoint, str(e))
            raise OciMigrateException('Failed to create mountpoint %s:' % mntpoint) from e
    else:
        mntpoint = mountpoint
    #
    # actual mount
    cmd = ['mount', devname, mntpoint]
    pause_msg(cmd, pause_flag='_OCI_MOUNT')
    _, nbcols = terminal_dimension()
    try:
        mountpart = ProgressBar(nbcols, 0.2,
                                progress_chars=['mount %s' % devname])
        mountpart.start()
        _logger.debug('command: %s', cmd)
        cmdret = system_tools.run_call_cmd(cmd)
        if cmdret == 0:
            _logger.debug('%s mounted on %s.', devname, mntpoint)
            return mntpoint

        raise Exception('Mount %s failed: %d' % (devname, cmdret))
    except Exception as e:
        #
        # mount failed, need to remove mountpoint.
        _logger.critical('   Failed to mount %s, missing driver, filesystem corruption...: %s', devname, str(e))
        if mountpoint is None:
            if system_tools.exec_rmdir(mntpoint):
                _logger.debug('%s removed', mntpoint)
            else:
                _logger.critical('   Failed to remove mountpoint %s', mntpoint)
    finally:
        if system_tools.is_thread_running(mountpart):
            mountpart.stop()

    return None
Beispiel #4
0
def mount_imgfn(imgname):
    """
    Link vm image with an nbd device.

    Parameters
    ----------
    imgname: str
        Full path of the image file.

    Returns
    -------
        str: Device name on success, raises an exception otherwise.
    """
    #
    # create nbd devices
    _logger.debug('__ Running mount image file %s.' % imgname)
    result_msg(msg='Load nbd')
    if not system_tools.create_nbd():
        raise OciMigrateException('Failed ot load nbd module')
    else:
        _logger.debug('nbd module loaded')
    #
    # find free nbd device
    result_msg(msg='Find free nbd device')
    devpath = system_tools.get_free_nbd()
    _logger.debug('Device %s is free.' % devpath)
    #
    # link img with first free nbd device
    result_msg(msg='Mount image %s' % imgname, result=True)
    _, nbcols = terminal_dimension()
    try:
        mountwait = ProgressBar(nbcols, 0.2, progress_chars=['mounting image'])
        mountwait.start()
        qemucmd = ['-c', devpath, imgname]
        pause_msg(qemucmd)
        qemunbd_ret = system_tools.exec_qemunbd(qemucmd)
        if qemunbd_ret == 0:
            time.sleep(5)
            _logger.debug('qemu-nbd %s succeeded' % qemucmd)
            return devpath
        else:
            _logger.critical('\n   Failed to create nbd devices: %d' %
                             qemunbd_ret)
            raise Exception('Failed to create nbd devices: %d' % qemunbd_ret)
    except Exception as e:
        _logger.critical('\n   Something wrong with creating nbd devices: %s' %
                         str(e))
        raise OciMigrateException('Unable to create nbd devices: %s' % str(e))
    finally:
        if system_tools.is_thread_running(mountwait):
            mountwait.stop()
def main():
    """
    Upload an image from on-premise to an object storage in the Oracle Cloud Infrastructure.

    Returns
    -------
        int: 0 on success, raises exception on failure.
    """
    #
    # set locale
    lc_all_to_set = get_config_data('lc_all')
    os.environ['LC_ALL'] = "%s" % lc_all_to_set
    _logger.debug('Locale set to %s', lc_all_to_set)
    #
    # command line
    cmdline_args = parse_args()
    #
    # input image
    if cmdline_args.input_image:
        image_path = cmdline_args.input_image.name
        console_msg(msg='\n  Uploading %s at %s\n' % (os.path.basename(' '.join(sys.argv)), time.ctime()))
    else:
        raise OciMigrateException('Missing argument: input image path.')
    #
    # object storage
    bucket_name = cmdline_args.bucket_name
    #
    # output image
    if cmdline_args.output_name:
        output_name = cmdline_args.output_name
    else:
        output_name = os.path.splitext(os.path.basename(image_path))[0]
    #
    # yes
    migrate_data.yes_flag = cmdline_args.yes_flag
    #
    # verbose
    migrate_data.verbose_flag = cmdline_args.verbose_flag
    #
    # message
    console_msg(msg='Uploading %s to object storage %s in the Oracle Cloud '
                    'Infrastructure as %s.' % (image_path,
                                               bucket_name,
                                               output_name))
    #
    # collect data
    try:
        #
        # The object storage exist and data.
        object_storage_data = oci_cli_tools.bucket_exists(bucket_name)
        console_msg(msg='Object storage %s present.' % bucket_name)
        #
        # The object is present in object storage.
        if oci_cli_tools.object_exists(object_storage_data, output_name):
            raise OciMigrateException('Object %s already exists object '
                                      'storage %s.' % (output_name, bucket_name))
        _logger.debug('Object %s does not yet exists in object storage %s', output_name, bucket_name)
    except Exception as e:
        exit_with_msg('Unable to upload %s to %s: %s'
                      % (image_path, bucket_name, str(e)))
    #
    # Ask for confirmation to proceed.
    if not read_yn('\n  Agree to proceed uploading %s to %s as %s?'
                   % (image_path, bucket_name, output_name),
                   waitenter=True,
                   suppose_yes=migrate_data.yes_flag):
        exit_with_msg('\n  Exiting.')
    #
    # Uploading the image to the Oracle Cloud Infrastructure.
    _, nb_columns = terminal_dimension()
    try:
        upload_progress = ProgressBar(nb_columns, 0.2,
                                      progress_chars=['uploading %s' % output_name])
        #
        # Verify if object already exists.
        if oci_cli_tools.object_exists(object_storage_data, output_name):
            raise OciMigrateException('Object %s already exists object storage %s.' % (output_name, bucket_name))
        _logger.debug('Object %s does not yet exists in object storage %s', output_name, bucket_name)
        #
        # Upload the image.
        console_msg(msg='\n  Uploading %s, this might take a while....' % image_path)
        upload_progress.start()
        upload_result = oci_cli_tools.upload_image(image_path, bucket_name, output_name)
        _logger.debug('Upload result: %s', upload_result)
        console_msg(msg='  Finished....\n')
        upload_progress.stop()
    except Exception as e:
        _logger.error('  Error while uploading %s to %s: %s.', image_path, bucket_name, str(e))
        exit_with_msg('*** ERROR *** Error while uploading %s to %s: %s.' % (image_path, bucket_name, str(e)))
    finally:
        # if progress thread was started, needs to be terminated.
        if system_tools.is_thread_running(upload_progress):
            upload_progress.stop()
Beispiel #6
0
def mount_lvm2(devname):
    """
    Create the mountpoints /mnt/<last part of lvm partitions> and mount the
    partitions on those mountpoints, if possible.

    Parameters
    ----------
    devname: str
        The full path of the device

    Returns
    -------
        list: The list of mounted partitions.
        ?? need to collect lvm2 list this way??
    """
    _logger.debug('__ Running mount lvm2 %s' % devname)
    try:
        _, nbcols = terminal_dimension()
        mountwait = ProgressBar(int(nbcols),
                                0.2,
                                progress_chars=['mounting lvm'])
        mountwait.start()
        #
        # physical volumes
        if system_tools.exec_pvscan(['--cache'], devname):
            _logger.debug('pvscan %s succeeded' % devname)
        else:
            _logger.critical('   pvscan %s failed' % devname)
        #
        pause_msg('pvscan test')
        #
        # volume groups
        if system_tools.exec_vgscan(['--verbose']):
            _logger.debug('vgscan succeeded')
        else:
            _logger.critical('   vgscan failed')
        #
        pause_msg('vgscan test')
        #
        # logical volumes
        vgs = new_volume_groups()
        if bool(vgs):
            _logger.debug('lvscan succeeded: %s' % vgs)
        else:
            _logger.critical('   lvscan failed')
        #
        pause_msg('lvscan test')
        #
        # make available
        vgchange_args = ['--activate', 'y']
        vgchange_res = system_tools.exec_vgchange(vgchange_args)
        _logger.debug('vgchange:\n%s' % vgchange_res)
        #
        pause_msg('vgchange_res test')
        vgfound = False
        if vgchange_res is not None:
            for resline in vgchange_res.splitlines():
                _logger.debug('vgchange line: %s' % resline)
                for vg in list(vgs.keys()):
                    if vg in resline:
                        _logger.debug('vgfound set to True')
                        vgfound = True
                    else:
                        _logger.debug('vg %s not in l' % vg)
            _logger.debug('vgchange: %s found: %s' % (vgchange_res, vgfound))
            #
            # for the sake of testing
            pause_msg('vgchange_res test')
        else:
            _logger.critical('   vgchange failed')
        return vgs
    except Exception as e:
        _logger.critical('   Mount lvm %s failed: %s' % (devname, str(e)))
        raise OciMigrateException('Mount lvm %s failed: %s' %
                                  (devname, str(e)))
    finally:
        if system_tools.is_thread_running(mountwait):
            mountwait.stop()
Beispiel #7
0
def main():
    """
    Import image from object storage into custom images repository.

    Returns
    -------
        int: 0 on success, raises exception on failure.
    """
    #
    # set locale
    lc_all_to_set = get_config_data('lc_all')
    os.environ['LC_ALL'] = "%s" % lc_all_to_set
    _logger.debug('Locale set to %s', lc_all_to_set)
    #
    # command line
    cmdline_args = parse_args()
    console_msg(msg='Importing %s from %s into %s as %s and setting '
                'launch_mode to %s.' %
                (cmdline_args.image_name, cmdline_args.bucket_name,
                 cmdline_args.bucket_name, cmdline_args.display_name,
                 cmdline_args.launch_mode))
    compartment = cmdline_args.compartment_name
    bucket = cmdline_args.bucket_name
    object_name = cmdline_args.image_name
    display_name = cmdline_args.display_name
    launch_mode = cmdline_args.launch_mode
    migrate_data.verbose_flag = cmdline_args.verbose_flag
    migrate_data.yes_flag = cmdline_args.yes_flag
    #
    # collect data
    try:
        #
        # oci configuration
        oci_config = migrate_tools.get_oci_config()
        _logger.debug('Found oci config file')
        #
        # compartment data for tenancy
        tenancy = oci_config['tenancy']
        _logger.debug('Tenancy: %s', tenancy)
        compartment_dict = oci_cli_tools.get_tenancy_data(tenancy)
        #
        # object storage namespace
        os_namespace = oci_cli_tools.get_os_namespace()
        console_msg(msg='Object storage namespace: %s' % os_namespace)
        #
        # compartment ocid
        compartment_id = oci_cli_tools.find_compartment_id(
            compartment, compartment_dict)
        #
        # object storage exist and data
        object_storage_data = oci_cli_tools.bucket_exists(bucket)
        console_msg(msg='Object storage %s present.' % bucket)
        #
        # object present in object storage
        if oci_cli_tools.object_exists(object_storage_data, object_name):
            _logger.debug('Object %s present in object_storage %s',
                          object_name, bucket)
        else:
            raise OciMigrateException(
                'Object %s does not exist in the  object '
                'storage %s.' % (object_name, bucket))
        #
        # display name present
        if oci_cli_tools.display_name_exists(display_name, compartment_id):
            raise OciMigrateException('Image with name %s already exists.' %
                                      display_name)
        _logger.debug('%s does not exist', display_name)
    except Exception as e:
        exit_with_msg('Error while importing %s data: %s' %
                      (object_name, str(e)))
    #
    # Ask for confirmation to proceed with upload.
    if not read_yn('\n  Import %s to %s as %s' %
                   (object_name, compartment, display_name),
                   waitenter=True,
                   suppose_yes=migrate_data.yes_flag):
        exit_with_msg('Exiting.\n')
    #
    # Start the import.
    try:
        _ = oci_cli_tools.import_image(object_name, display_name,
                                       compartment_id, os_namespace, bucket,
                                       launch_mode)
    except Exception as e:
        exit_with_msg('Failed to import %s: %s' % (object_name, str(e)))
    #
    # Follow up the import.
    finished = False
    _, nb_columns = terminal_dimension()
    import_progress = ProgressBar(
        nb_columns, 0.2, progress_chars=['importing %s' % display_name])
    import_progress.start()
    try:
        while not finished:
            if oci_cli_tools.get_lifecycle_state(display_name, compartment_id) \
                    == 'AVAILABLE':
                finished = True
    except Exception as e:
        _logger.error('Failed to follow up on the import of %s, giving up: %s',
                      display_name, str(e))

    if system_tools.is_thread_running(import_progress):
        import_progress.stop()
    console_msg(msg='Done')
    return 0