def vmedia_mount_create(handle, volume_name, remote_share, remote_file,
                        map="www", mount_options="noauto", username="",
                        password="", server_id=1, timeout=60):
    """
    This method will setup the vmedia mapping
    Args:
        handle (ImcHandle)
        volume_name (string): Name of the volume or identity of the image
        map (string): "cifs", "nfs", "www"
        mount_options (string): Options to be passed while mounting the image
        remote_share (string): URI of the image
        remote_file (string): name of the image
        username (string): username
        password (string): password
        server_id (int): Server Id to be specified for C3260 platforms
    Returns:
        CommVMediaMap object
    Examples:
        vmedia_mount_add(
            handle,
            volume_name="c",
            map="www",
            mount_options="noauto", "nolock" etc.
            remote_share="http://1.1.1.1/files",
            remote_file="ubuntu-14.04.2-server-amd64.iso",
            username="******",
            password="******")
    """
    image_type = remote_file.split('.')[-1]
    vmedia_mount_remove_image(handle, image_type)

    mo = CommVMediaMap(parent_mo_or_dn=_get_vmedia_mo_dn(handle, server_id),
                       volume_name=volume_name)
    mo.map = map
    if mount_options:
        mo.mount_options = mount_options
    mo.remote_share = remote_share
    mo.remote_file = remote_file
    mo.username = username
    mo.password = password

    handle.add_mo(mo, modify_present="True")

    wait_time = 0
    interval = 10
    while wait_time < timeout:
        time.sleep(interval)
        mo = handle.query_dn(mo.dn)
        existing_mapping_status = mo.mapping_status
        if existing_mapping_status == "OK":
            return mo
        elif re.match(r"ERROR", existing_mapping_status):
            raise ImcOperationError("vmedia_mount_create",
                                    mo.mapping_status)
        wait_time += interval

    raise ImcOperationError("vmedia_mount_create",
                            "ERROR - Mapped image status stuck at %s" %
                            existing_mapping_status)
Beispiel #2
0
def vmedia_mount_delete(handle, volume_name, server_id=1):
    """
    This method will remove the vmedia mapping referred to by the volume name

    Args:
        handle (ImcHandle)
        volume_name (string): Name of the volume or identity of the image
        server_id (int): Server Id to be specified for C3260 platforms

    Returns:
        None

    Raises:
        Exception when the mapping is not found

    Examples:
        vmedia_mount_remove(handle, volume_name="c")
    """

    vmediamap_mo = CommVMediaMap(
        parent_mo_or_dn=_get_vmedia_mo_dn(handle, server_id),
        volume_name=volume_name)
    vmediamap_mo = handle.query_dn(dn=vmediamap_mo.dn)
    if vmediamap_mo is None:
        raise ValueError("Volume '%s' does not exist" % volume_name)

    handle.remove_mo(vmediamap_mo)
def UnmountVmedia(handle, volume_name):
    log = logging.getLogger()
    vmediamap_mo = CommVMediaMap(
        parent_mo_or_dn=_get_vmedia_mo_dn(handle, server_id=1),
        volume_name=volume_name)
    vmediamap_mo = handle.query_dn(dn=vmediamap_mo.dn)
    if vmediamap_mo is None:
        log.error("Volume '%s' does not exist on %s", volume_name, handle.ip)
        raise ValueError("Volume '%s' does not exist on %s" % (volume_name, handle.ip))

    handle.remove_mo(vmediamap_mo)
Beispiel #4
0
def vmedia_mount_add(handle,
                     volume_name,
                     mount_protocol,
                     mount_options=None,
                     remote_share=None,
                     remote_file=None,
                     user_id="",
                     password="",
                     server_id=1):
    """
    This method will setup the vmedia mapping
    Args:
        handle (ImcHandle)
        volume_name (string): Name of the volume or identity of the image
        mount_protocol (string): "cifs", "nfs", "www"
        mount_options (string): Options to be passed while mounting the image
        remote_share (string): URI of the image
        remote_file (string): name of the image
        user_id (string): username
        password (string): password
        server_id (int): Server Id to be specified for C3260 platforms

    Returns:
        CommVMediaMap object

    Examples:
        vmedia_mount_add(
            handle,
            volume_name="c",
            mount_protocol="www",
            mount_options="noauto", "nolock" etc.
            remote_share="http://1.1.1.1/files",
            remote_file="ubuntu-14.04.2-server-amd64.iso",
            user_id="abcd",
            password="******")
    """

    vmediamap_mo = CommVMediaMap(parent_mo_or_dn=_get_vmedia_mo_dn(
        handle, server_id),
                                 volume_name=volume_name)
    vmediamap_mo.map = mount_protocol
    if mount_options:
        vmediamap_mo.mount_options = mount_options
    vmediamap_mo.remote_share = remote_share
    vmediamap_mo.remote_file = remote_file
    vmediamap_mo.username = user_id
    vmediamap_mo.password = password

    handle.add_mo(vmediamap_mo, modify_present="True")
    return vmediamap_mo
Beispiel #5
0
def vmedia_mount_add(handle, volume_name, mount_protocol,
                     mount_options=None, remote_share=None,
                     remote_file=None, user_id="", password="", server_id=1):
    """
    This method will setup the vmedia mapping
    Args:
        handle (ImcHandle)
        volume_name (string): Name of the volume or identity of the image
        mount_protocol (string): "cifs", "nfs", "www"
        mount_options (string): Options to be passed while mounting the image
        remote_share (string): URI of the image
        remote_file (string): name of the image
        user_id (string): username
        password (string): password
        server_id (int): Server Id to be specified for C3260 platforms

    Returns:
        CommVMediaMap object

    Examples:
        vmedia_mount_add(
            handle,
            volume_name="c",
            mount_protocol="www",
            mount_options="noauto", "nolock" etc.
            remote_share="http://1.1.1.1/files",
            remote_file="ubuntu-14.04.2-server-amd64.iso",
            user_id="abcd",
            password="******")
    """

    vmediamap_mo = CommVMediaMap(
        parent_mo_or_dn=_get_vmedia_mo_dn(handle, server_id),
        volume_name=volume_name)
    vmediamap_mo.map = mount_protocol
    if mount_options:
        vmediamap_mo.mount_options = mount_options
    vmediamap_mo.remote_share = remote_share
    vmediamap_mo.remote_file = remote_file
    vmediamap_mo.username = user_id
    vmediamap_mo.password = password

    handle.add_mo(vmediamap_mo, modify_present="True")
    return vmediamap_mo
Beispiel #6
0
def test_invalid_remove_vmedia_all(login_mock, query_mock, remove_mock):
    # Patch ImcHandle.login to create a Faux ImcHandle object w/o real CIMC
    # Patch ImcHandle.query_children to simulate CIMC interaction w/o real CIMC
    # Patch ImcHandle.remove_mo to simulate CIMC interaction w/o real CIMC
    login_mock.return_value = True
    test_cimc = ImcHandle(ip='169.254.1.1',
                          username='******',
                          password='******')
    test_cimc._set_platform(platform=IMC_PLATFORM.TYPE_CLASSIC)

    # Scenario: Three pre-exising mounts, only two unsuccessfully
    vmedia1 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="One.iso")
    vmedia1.remote_share = "http://169.254.1.2/"
    vmedia1.remote_file = "One.iso"
    vmedia2 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="Two")
    vmedia2.remote_share = "http://169.254.1.2/"
    vmedia2.remote_file = "Two.iso"
    vmedia3 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="Three")
    vmedia3.remote_share = "http://169.254.1.2/"
    vmedia3.remote_file = "Three.iso"
    query_mock.side_effect = [[vmedia1, vmedia2, vmedia3], [vmedia1]]
    assert_raises(ImcOperationError, vmedia_mount_remove_all, test_cimc)
Beispiel #7
0
def test_vmedia_get_existing_uri(login_mock, query_mock):
    # Patch ImcHandle.login to create a Faux ImcHandle w/o real CIMC
    # Patch ImcHandle.query_children to simulate CIMC interaction
    login_mock.return_value = True
    test_cimc = ImcHandle(ip='169.254.1.1',
                          username='******',
                          password='******')
    test_cimc._set_platform(platform=IMC_PLATFORM.TYPE_CLASSIC)

    # Scenario: No pre-existing mappings
    query_mock.return_value = []
    assert vmedia_get_existing_uri(test_cimc) == []
    #  Assert query_children called with correct in_dn
    assert query_mock.mock_calls[0] == \
        call(in_dn='sys/svc-ext/vmedia-svc')

    # Scenario: Three pre-existing mappings
    vmedia1 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="One.iso")
    vmedia1.remote_share = "http://169.254.1.2/"
    vmedia1.remote_file = "One.iso"
    vmedia2 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="Two")
    vmedia2.remote_share = "http://169.254.1.2/"
    vmedia2.remote_file = "Two.iso"
    vmedia3 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="Three")
    vmedia3.remote_share = "http://169.254.1.2/"
    vmedia3.remote_file = "Three.iso"
    query_mock.return_value = [vmedia1, vmedia2, vmedia3]
    assert vmedia_get_existing_uri(test_cimc) == \
        ["http://169.254.1.2/One.iso",
         "http://169.254.1.2/Two.iso",
         "http://169.254.1.2/Three.iso"]
Beispiel #8
0
def test_valid_remove_vmedia_all(login_mock, query_mock, remove_mock):
    # Patch ImcHandle.login to create a Faux ImcHandle object w/o real CIMC
    # Patch ImcHandle.query_children to simulate CIMC interaction w/o real CIMC
    # Patch ImcHandle.remove_mo to simulate CIMC interaction w/o real CIMC
    login_mock.return_value = True
    test_cimc = ImcHandle(ip='169.254.1.1',
                          username='******',
                          password='******')
    test_cimc._set_platform(platform=IMC_PLATFORM.TYPE_CLASSIC)

    # Scenario: server has no vmedia mounts
    query_mock.return_value = []
    assert vmedia_mount_remove_all(test_cimc) is True
    assert remove_mock.mock_calls == []

    # Scenario: Three pre-exising mounts, removed successfully
    vmedia1 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="One.iso")
    vmedia1.remote_share = "http://169.254.1.2/"
    vmedia1.remote_file = "One.iso"
    vmedia2 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="Two")
    vmedia2.remote_share = "http://169.254.1.2/"
    vmedia2.remote_file = "Two.iso"
    vmedia3 = CommVMediaMap(parent_mo_or_dn="sys/svc-ext/vmedia-svc",
                            volume_name="Three")
    vmedia3.remote_share = "http://169.254.1.2/"
    vmedia3.remote_file = "Three.iso"
    # query_mocked call first time in remove_existing_virtual_media
    # query_mock called a second time in get_existing_virtual_media_uri
    query_mock.side_effect = [[vmedia1, vmedia2, vmedia3], []]
    assert vmedia_mount_remove_all(test_cimc) is True
    assert remove_mock.mock_calls == [call(vmedia1),
                                      call(vmedia2),
                                      call(vmedia3)]
Beispiel #9
0
def vmedia_mount_create(handle, volume_name, remote_share, remote_file,
                        map="www", mount_options="noauto", username="",
                        password="", server_id=1, timeout=60):
    """
    This method will setup the vmedia mapping
    Args:
        handle (ImcHandle)
        volume_name (string): Name of the volume or identity of the image
        map (string): "cifs", "nfs", "www"
        mount_options (string): Options to be passed while mounting the image
        remote_share (string): URI of the image
        remote_file (string): name of the image
        username (string): username
        password (string): password
        server_id (int): Server Id to be specified for C3260 platforms

    Returns:
        CommVMediaMap object

    Examples:
        vmedia_mount_add(
            handle,
            volume_name="c",
            map="www",
            mount_options="noauto", "nolock" etc.
            remote_share="http://1.1.1.1/files",
            remote_file="ubuntu-14.04.2-server-amd64.iso",
            username="******",
            password="******")
    """
    image_type = remote_file.split('.')[-1]
    vmedia_mount_remove_image(handle, image_type)

    mo = CommVMediaMap(parent_mo_or_dn=_get_vmedia_mo_dn(handle, server_id),
                       volume_name=volume_name)
    mo.map = map
    if mount_options:
        mo.mount_options = mount_options
    mo.remote_share = remote_share
    mo.remote_file = remote_file
    mo.username = username
    mo.password = password

    handle.add_mo(mo, modify_present="True")

    wait_time = 0
    interval = 10
    while wait_time < timeout:
        time.sleep(interval)
        mo = handle.query_dn(mo.dn)
        existing_mapping_status = mo.mapping_status
        if existing_mapping_status == "OK":
            return mo
        elif re.match(r"ERROR", existing_mapping_status):
            raise ImcOperationError("vmedia_mount_create",
                                    mo.mapping_status)
        wait_time += interval

    raise ImcOperationError("vmedia_mount_create",
                            "ERROR - Mapped ISO status stuck at %s" %
                            existing_mapping_status)
Beispiel #10
0
def vmedia_mount_create_all(handle, mappings = None, server_id=1, timeout=60):
    """
        This method will make one request to create all the vmedia mappings
        Args:
            handle (ImcHandle)
            mappings (list): list of mappings dict
              keys:
                volume_name (string): Name of the volume or identity of the image
                map (string): "cifs", "nfs", "www"
                mount_options (string): Options to be passed while mounting the image
                remote_share (string): URI of the image
                remote_file (string): name of the image
                username (string): username
                password (string): password
            server_id (int): Server Id to be specified for C3260 platforms

        Returns:
            List of CommVMediaMap object

        Examples:
            vmedia_mount_create_all(
                handle,
                mappings=[{volume_name: "A",
                map: "www"
                mount_options: "ro"
                remote_share: "http://10.10.10.20/test/"
                remote_file: "a.iso"
                username: ""
                password: ""}]
            )
        """

    api = 'vmedia_mount_create_all'
    mos = []
    dn_to_vmedia_dict = {}

    for mapping in mappings:

        volume_name  = mapping.get('volume_name')
        map          = mapping.get('map')
        remote_share = mapping.get('remote_share')
        remote_file  = mapping.get('remote_file')
        _validate_api_prop('volume_name', volume_name, api)
        _validate_api_prop('map',map, api)
        _validate_api_prop('remote_share', remote_share, api)
        _validate_api_prop('remote_file', remote_file,api)
        params = {
            'map':         map,
            'remote_file':  remote_file,
            'remote_share': remote_share
        }
        if mapping.get('mount_options'):
            params['mount_options'] = mapping.get('mount_options')
        if map != CommVMediaMapConsts.MAP_NFS:
            mount_options = mapping.get('mount_options')

            #In CIMC, security context authentication protocol for CIFS Share, is always set to "ntlm" by default.
            #If authentication protocol is set to none, CIMC rejects the mapping and the deployment fails.
            #Hence, removing the security context from parameters mount_options if the authentication protocol is set to none.

            mount_options_array = mount_options.split(",")
            for option in mount_options_array:
                if "sec" in option and len(option.split("=")) >= 2 and option.split("=")[1] == "none":
                    mount_options_array.remove(option)
            if len(mount_options_array) == 0:
                del params['mount_options']
            else:
                new_mount_options = ','.join([str(element) for element in mount_options_array])
                params['mount_options'] = new_mount_options
            if mapping.get('username'):
                params['username'] = mapping.get('username')
            if mapping.get('password'):
                params['password'] = mapping.get('password')

        mo = CommVMediaMap(parent_mo_or_dn=_get_vmedia_mo_dn(handle, server_id),
                           volume_name=volume_name)
        mo.set_prop_multiple(**params)
        mos.append(mo)
        dn_to_vmedia_dict[mo.dn] = mo.volume_name

    response = handle.set_mos(mos)
    if response:
        ret = process_conf_mos_response(response, api, False, 'Create Virtual Media mapping failed',vmedia_mounts_callback,
                                               dn_to_vmedia_dict)
        if len(ret) != 0:
            error_msg = 'Create Virtual Media mapping failed:\n'
            for item in ret:
                obj = item["Object"]
                error = item["Error"]
                error = sanitize_message(error)
                error_msg += "[Virtual Media mapping " + obj + "] " + error + "\n"

            raise ImcOperationErrorDetail(api, error_msg, ret)

    mapping_error_msg = ''
    timeout_error_msg = ''
    for mo in mos:
        wait_time = 0
        interval = 10
        while wait_time < timeout:
            mapping_mo = handle.query_dn(mo.dn)
            if mapping_mo:
                existing_mapping_status = mapping_mo.mapping_status
                if existing_mapping_status.lower() == "ok":
                    break
                elif re.match(r"error", existing_mapping_status.lower()):
                    mapping_error_msg += "[Virtual Media mapping "+ mo.volume_name + "] " +existing_mapping_status + "\n"
                    break

            time.sleep(interval)
            wait_time += interval

        if wait_time >= timeout:
            timeout_error_msg += "[Virtual Media mapping "+ mo.volume_name +"] \n"

    if len(mapping_error_msg) != 0:
        raise ImcOperationErrorDetail(api,"Create Virtual Media mapping failed: "+ mapping_error_msg,[])

    if len(timeout_error_msg) != 0:
        raise ImcOperationErrorDetail(api,"Create Virtual Media mapping timed out: "+ timeout_error_msg,[])

    results = {}
    results["changed"] = True
    results["msg"] = ""
    results["msg_params"] = ret

    return results