예제 #1
0
def display_name_exists(display_name, compartment_id):
    """
    Verify if the image with display_name exist in compartment.

    Parameters
    ----------
    display_name: str
        The display name.
    compartment_id: str
        The ocid of the compartment.

    Returns
    -------
        bool: True on success, False otherwise.
    """
    _logger.debug('__ Verify if %s exists in compartment %s', display_name, compartment_id)
    cmd = ['oci', 'compute', 'image', 'list',
           '--compartment-id', '%s' % compartment_id,
           '--display-name', '%s' % display_name]
    object_status = system_tools.run_popen_cmd(cmd)['output'].decode('utf-8')
    if bool(object_status):
        _logger.debug('Object %s is present in %s', display_name, compartment_id)
        return True
    _logger.debug('Object %s is not present in %s', display_name, compartment_id)
    return False
예제 #2
0
def get_lifecycle_state(display_name, compartment_id):
    """
    Collect the lifecycle state of on object in a compartment.

    Parameters
    ----------
    display_name: str
        The object name.
    compartment_id: str
        The compartment ocid.

    Returns
    -------
        str: the lifecycle state.
    """
    _logger.debug('__ Retrieving the lifecycle state of %s', display_name)
    cmd = ['oci', 'compute', 'image', 'list',
           '--compartment-id', '%s' % compartment_id,
           '--display-name', '%s' % display_name]
    try:
        object_list = json.loads(system_tools.run_popen_cmd(cmd)['output'].decode('utf-8'))
        for object_x in object_list['data']:
            if object_x['display-name'] == display_name:
                return object_x['lifecycle-state']

            _logger.debug('object %s not found.', display_name)
            return None
    except Exception as e:
        raise OciMigrateException('Failed to collect the compute image list:') from e
예제 #3
0
 def b10_is_cloud_init_enabled():
     """
     Verify if cloud-init package is enabled after install.
     Returns
     -------
        bool: True on success, False otherwise.
     """
     _logger.debug('__ Is cloud-init enabled.')
     cmd = ['systemctl', 'list-unit-files']
     enabled = False
     try:
         _logger.debug('command: %s', cmd)
         output = system_tools.run_popen_cmd(cmd)['output'].decode(
             'utf-8').splitlines()
         for service in output:
             svc = service.split() if len(service) > 1 else ['a', 'b']
             if 'cloud-init' in svc[0]:
                 _logger.debug('Found service cloud-init: %s', svc)
                 if svc[-1] == 'enabled':
                     _logger.debug('Service cloud-init is enabled.')
                     enabled = True
                     break
         return enabled
     except Exception as e:
         _logger.warning('   Failed to execute systemctl: %s', str(e))
         raise OciMigrateException('\nFailed to execute systemctl: ') from e
예제 #4
0
def upload_image(imgname, bucket_name, ociname):
    """
    Upload the validated and updated image imgname to the OCI object storage
    bucket_name as ociname.

    Parameters
    ----------
    imgname: str
        The on-premise custom image.
    bucket_name: str
        The OCI object storage name.
    ociname:
        The OCI image name.

    Returns
    -------
        bool: True on success, raises an exception otherwise.
    """
    cmd = ['oci', 'os', 'object', 'put',
           '--bucket-name', bucket_name,
           '--file', imgname,
           '--name', ociname,
           '--part-size', '100',
           '--parallel-upload-count', '6']
    _logger.debug('__ Running %s', cmd)
    pause_msg(cmd)
    try:
        upload_result = system_tools.run_popen_cmd(cmd)['output'].decode('utf-8')
        _logger.debug('Successfully uploaded %s to %s as %s: %s.', imgname, bucket_name, ociname, upload_result)
        return True
    except Exception as e:
        _logger.critical('   Failed to upload %s to object storage %s as %s: %s.',
                         imgname, bucket_name, ociname, str(e))
        raise OciMigrateException('Failed to upload %s to object storage %s as %s:'
                                  % (imgname, bucket_name, ociname)) from e
예제 #5
0
def bucket_exists(bucket_name):
    """
    Verify if bucket_name exits.

    Parameters
    ----------
    bucket_name: str
        The bucket_name.

    Returns
    -------
        dict: The bucket on success, raise an exception otherwise
    """
    cmd = [
        'oci', 'os', 'object', 'list', '--all', '--bucket-name', bucket_name
    ]
    _logger.debug('__ Running %s.', cmd)
    pause_msg(cmd)
    try:
        bucket_result = json.loads(
            system_tools.run_popen_cmd(cmd)['output'].decode('utf-8'))
        _logger.debug('Result: \n%s', bucket_result)
        return bucket_result
    except Exception as e:
        _logger.debug(
            'Bucket %s does not exists or the authorisation is missing: %s.',
            bucket_name, str(e))
        raise OciMigrateException(
            'Bucket %s does not exists or the authorisation is missing:' %
            bucket_name) from e
예제 #6
0
def qemu_img_version():
    """
    Retrieve the version of qemu-img.

    Returns:
    -------
        version_data: str
        version_nb: int
    """
    _logger.debug('__ Retrieve qemu-img release data.')
    cmd = ['qemu-img', '--version']
    cmd_dict = system_tools.run_popen_cmd(cmd)
    if bool(cmd_dict):
        version_string = cmd_dict['output'].decode('utf-8').splitlines()
    else:
        return -1
    ptrn = re.compile(r'[. -]')
    _logger.debug('qemu-img version: %s', version_string)
    for lin in version_string:
        if 'version' in lin:
            ver_elts = ptrn.split(lin)
            for elt in ver_elts:
                if elt.isnumeric():
                    return lin, int(elt)
    return 0
예제 #7
0
def get_os_namespace():
    """
    Collect the object storage namespace name.

    Returns
    -------
       str: object storage namespace name
       raises an exception on failure.
    """
    _logger.debug('__ Collect the object storage namespace name.')
    cmd = ['oci', 'os', 'ns', 'get']
    try:
        ns_dict = json.loads(system_tools.run_popen_cmd(cmd)['output'].decode('utf-8'))
        return ns_dict['data']
    except Exception as e:
        raise OciMigrateException('Failed to collect object storage namespace name:') from e
예제 #8
0
def import_image(image_name, display_name, compartment_id, os_namespace,
                 os_name, launch_mode):
    """
    Import an os image from object storage in the custom images repository
    from the OCI.

    Parameters
    ----------
    image_name: str
        The name of the image in the object storage.
    display_name: str
        The name the image will be stored in the custom images repository.
    compartment_id: str
        The ocid of the compartment the image will be stored.
    os_namespace: str
        The name of the object storage namespace used.
    os_name: str
        The object storage name.
    launch_mode: str
        The mode the instance created from the custom image will be started.

    Returns
    -------
        bool: True on success, False otherwise.
    """
    # _logger.debug('__ Importing image %s to %s as %s'
    #              % (image_name, compartment_id, display_name))
    cmd = [
        'oci', 'compute', 'image', 'import', 'from-object', '--bucket-name',
        '%s' % os_name, '--compartment-id',
        '%s' % compartment_id, '--name',
        '%s' % image_name, '--namespace',
        '%s' % os_namespace, '--launch-mode', launch_mode, '--display-name',
        '%s' % display_name
    ]
    _logger.debug('__ Running %s' % cmd)
    try:
        _ = system_tools.run_popen_cmd(cmd)
        _logger.debug('Successfully started import of %s' % image_name)
        return True
    except Exception as e:
        raise OciMigrateException('Failed to start import of %s: %s' %
                                  (image_name, str(e)))
예제 #9
0
def get_tenancy_data(tenancy):
    """
    Collect the compartment data for a tenancy.

    Parameters
    ----------
    tenancy: str
        The tenancy ocid

    Returns
    -------
        dict: a dictionary with the data of all compartments in this tenancy.
        raises an exception on failure.
    """
    _logger.debug('__ Collecting compartment data for tenancy %s', tenancy)
    cmd = ['oci', 'iam', 'compartment', 'list', '-c', '%s' % tenancy, '--all']
    _logger.debug('Running %s', cmd)
    try:
        return json.loads(system_tools.run_popen_cmd(cmd)['output'].decode('utf-8'))
    except Exception as e:
        raise OciMigrateException('Failed to collect compartment data for tenancy %s:' % tenancy) from e
def qemu_img_version():
    """
    Retrieve the version of qemu-img.

    Returns:
    -------
        versiondata: str
        versionnb: int
    """
    _logger.debug('__ Retrieve qemu-img release data.')
    cmd = ['qemu-img', '--version']
    versionstring = system_tools.run_popen_cmd(cmd).decode('utf-8').splitlines()
    ptrn = re.compile(r'[. -]')
    _logger.debug('qemu-img version: %s' % versionstring)
    for lin in versionstring:
        if 'version' in lin:
            ver_elts = ptrn.split(lin)
            for elt in ver_elts:
                if elt.isnumeric():
                    return lin, int(elt)
    return 0
예제 #11
0
    def exec_yum(cmd):
        """
        Execute a yum command.

        Parameters
        ----------
        cmd: list
            The yum command parameters as s list.

        Returns
        -------
            str: yum output on success, raises an exception otherwise.
        """
        cmd = ['yum'] + cmd
        _logger.debug('Running yum command: %s' % cmd)
        try:
            _logger.debug('command: %s' % cmd)
            output = system_tools.run_popen_cmd(cmd).decode('utf-8')
            _logger.debug('yum command output: %s' % str(output))
            return output
        except Exception as e:
            _logger.critical('   Failed to execute yum: %s' % str(e))
            raise OciMigrateException('\nFailed to execute yum: %s' % str(e))
예제 #12
0
    def _exec_dnf(cmd):
        """
        Execute a dnf command.

        Parameters
        ----------
        cmd: list
            The dnf command parameters as s list.

        Returns
        -------
            str: dnf output on success, raises an exception otherwise.
        """
        cmd = ['dnf'] + cmd
        _logger.debug('Running dnf command: %s', cmd)
        try:
            _logger.debug('command: %s', cmd)
            output = system_tools.run_popen_cmd(cmd)['output'].decode('utf-8')
            _logger.debug('dnf command output: %s', str(output))
            return output
        except Exception as e:
            _logger.critical('   Failed to execute dnf: %s', str(e))
            raise OciMigrateException('\nFailed to execute dnf.') from e
예제 #13
0
    def _exec_apt(cmd):
        """
        Execute an apt command.

        Parameters
        ----------
        cmd: list
            The apt command as a list.

        Returns
        -------
            str: apt output on success, raises an exception otherwise.
        """
        cmd = ['/usr/bin/apt'] + cmd
        _logger.debug('apt command: %s', cmd)
        try:
            _logger.debug('command: %s', cmd)
            output = system_tools.run_popen_cmd(cmd)['output'].decode('utf-8')
            _logger.debug('apt command output: %s', str(output))
            return output
        except Exception as e:
            _logger.warning('   Failed to execute apt: %s', str(e))
            raise OciMigrateException('\nFailed to execute apt:') from e