示例#1
0
    def get_power_state(self, task):
        """Gets the current power state.

        :param task: a TaskManager instance.
        :returns: one of :mod:`ironic.common.states` POWER_OFF,
                  POWER_ON or ERROR.
        :raises: OneViewError if fails to retrieve power state of OneView
                 resource
        """
        oneview_client = common.get_hponeview_client()
        server_hardware = task.node.driver_info.get('server_hardware_uri')
        try:
            server_hardware = oneview_client.server_hardware.get(
                server_hardware)
        except client_exception.HPOneViewException as exc:
            LOG.error(
                "Error getting power state for node %(node)s. Error:"
                "%(error)s", {
                    'node': task.node.uuid,
                    'error': exc
                })
            raise exception.OneViewError(error=exc)
        else:
            power_state = server_hardware.get('powerState')
            return GET_POWER_STATE_MAP.get(power_state)
示例#2
0
def set_onetime_boot(task):
    """Set onetime boot to server hardware.

    Change the onetime boot option of an OneView server hardware.

    :param task: a task from TaskManager.
    """
    driver_internal_info = task.node.driver_internal_info
    next_boot_device = driver_internal_info.get('next_boot_device')

    if next_boot_device:
        boot_device = next_boot_device.get('boot_device')
        persistent = next_boot_device.get('persistent')

        if not persistent:
            client = common.get_hponeview_client()
            server_hardware = task.node.driver_info.get('server_hardware_uri')
            ilo_client = common.get_ilorest_client(client, server_hardware)
            boot_device = BOOT_DEVICE_MAP_ILO.get(boot_device)
            body = {
                "Boot": {
                    "BootSourceOverrideTarget": boot_device,
                    "BootSourceOverrideEnabled": "Once"
                }
            }
            ilo_client.patch(path=ILO_SYSTEM_PATH, body=body,
                             headers=ILO_REQUEST_HEADERS)
示例#3
0
def set_boot_device(task):
    """Sets the boot device for a node.

    Sets the boot device to use on next reboot of the node.

    :param task: a task from TaskManager.
    :raises: InvalidParameterValue if an invalid boot device is
             specified.
    :raises: OneViewError if the communication with OneView fails
    """
    oneview_client = common.get_hponeview_client()
    common.ensure_server_profile(task)
    driver_internal_info = task.node.driver_internal_info
    next_boot_device = driver_internal_info.get('next_boot_device')

    if next_boot_device:
        boot_device = next_boot_device.get('boot_device')
        persistent = next_boot_device.get('persistent')

        if boot_device not in sorted(BOOT_DEVICE_MAP_ONEVIEW):
            raise exception.InvalidParameterValue(
                _("Invalid boot device %s specified.") % boot_device)

        LOG.debug(
            "Setting boot device to %(boot_device)s and "
            "persistent to %(persistent)s for node %(node)s", {
                "boot_device": boot_device,
                "persistent": persistent,
                "node": task.node.uuid
            })

        profile = task.node.driver_info.get('applied_server_profile_uri')
        boot_device = BOOT_DEVICE_MAP_ONEVIEW.get(boot_device)

        try:
            server_profile = oneview_client.server_profiles.get(profile)
            boot = server_profile.get('boot', {})
            order = boot.get('order', [])
            if boot_device in order:
                order.remove(boot_device)
            order.insert(0, boot_device)
            boot['order'] = order
            server_profile['boot'] = boot
            oneview_client.server_profiles.update(server_profile, profile)
            set_onetime_boot(task)
            driver_internal_info.pop('next_boot_device', None)
            task.node.driver_internal_info = driver_internal_info
            task.node.save()
        except client_exception.HPOneViewException as oneview_exc:
            msg = (_("Error setting boot device on OneView. Error: %s") %
                   oneview_exc)
            raise exception.OneViewError(error=msg)

    else:
        LOG.debug(
            "Not going to set boot device because there is no "
            "'next_boot_device' on driver_internal_info "
            "for the %(node)s", {"node": task.node.uuid})
示例#4
0
    def get_boot_device(self, task):
        """Get the current boot device from the node.

        Gets the boot device from the node 'next_boot_device on
        driver_internal_info namespace if exists. Gets through
        a request to OneView otherwise.

        :param task: a task from TaskManager.
        :returns: a dictionary containing:
            :boot_device: the boot device, one of
                :mod:`ironic.common.boot_devices` [PXE, DISK, CDROM]
            :persistent: Whether the boot device will persist to all
                future boots or not, None if it is unknown.
        :raises: InvalidParameterValue if the boot device is unknown
        :raises: OneViewError if the communication with OneView fails
        """
        oneview_client = common.get_hponeview_client()
        driver_internal_info = task.node.driver_internal_info
        next_boot_device = driver_internal_info.get('next_boot_device')

        if next_boot_device:
            return next_boot_device

        driver_info = task.node.driver_info
        server_profile = driver_info.get('applied_server_profile_uri')

        try:
            profile = oneview_client.server_profiles.get(server_profile)
            primary_device = None
            boot = profile.get('boot', {})
            boot_order = boot.get('order', [])
            if boot_order:
                primary_device = boot_order[0]
        except client_exception.HPOneViewException as exc:
            msg = _("Error on node: %(node)s while getting Server Profile: "
                    "%(profile)s of the from OneView. Error: %(error)s.") % {
                        'profile': server_profile,
                        'node': task.node.uuid,
                        'error': exc
                    }
            raise exception.OneViewError(msg)

        if primary_device not in BOOT_DEVICE_MAP_ONEVIEW_REV:
            raise exception.InvalidParameterValue(
                _("Unsupported boot device %(device)s for node: %(node)s") % {
                    "device": primary_device,
                    "node": task.node.uuid
                })

        boot_device = {
            'boot_device': BOOT_DEVICE_MAP_ONEVIEW_REV.get(primary_device),
            'persistent': not _is_onetime_boot(task)
        }

        return boot_device
示例#5
0
    def set_power_state(self, task, power_state, timeout=None):
        """Set the power state of the task's node.

        :param task: a TaskManager instance.
        :param power_state: The desired power state POWER_ON, POWER_OFF or
                            REBOOT from :mod:`ironic.common.states`.
        :param timeout: timeout (in seconds) positive integer (> 0) for any
                        power state. ``None`` indicates to use default timeout.
        :raises: InvalidParameterValue if an invalid power state was specified.
        :raises: PowerStateFailure if the power couldn't be set to power_state.
        :raises: OneViewError if OneView fails setting the power state.
        """
        client = common.get_hponeview_client()
        if deploy_utils.is_node_in_use_by_oneview(task.node):
            raise exception.PowerStateFailure(_(
                "Cannot set power state '%(power_state)s' to node %(node)s. "
                "The node is in use by OneView.") %
                {'power_state': power_state,
                 'node': task.node.uuid})

        if power_state not in SET_POWER_STATE_MAP:
            raise exception.InvalidParameterValue(
                _("set_power_state called with invalid power state %s.")
                % power_state)

        LOG.debug('Setting power state of node %(node_uuid)s to '
                  '%(power_state)s',
                  {'node_uuid': task.node.uuid, 'power_state': power_state})

        server_hardware = task.node.driver_info.get('server_hardware_uri')
        timeout = (-1 if timeout is None else timeout)

        try:
            if power_state == states.POWER_ON:
                management.set_boot_device(task)
                client.server_hardware.update_power_state(
                    SET_POWER_STATE_MAP.get(power_state),
                    server_hardware, timeout=timeout)
            elif power_state == states.REBOOT:
                client.server_hardware.update_power_state(
                    SET_POWER_STATE_MAP.get(states.POWER_OFF), server_hardware,
                    timeout=timeout)
                management.set_boot_device(task)
                client.server_hardware.update_power_state(
                    SET_POWER_STATE_MAP.get(states.POWER_ON), server_hardware,
                    timeout=timeout)
            else:
                client.server_hardware.update_power_state(
                    SET_POWER_STATE_MAP.get(power_state), server_hardware,
                    timeout=timeout)
        except client_exception.HPOneViewException as exc:
            raise exception.OneViewError(
                _("Error setting power state: %s") % exc)
示例#6
0
def _is_onetime_boot(task):
    """Check onetime boot from server hardware.

    Check if the onetime boot option of an OneView server hardware
    is set to 'Once' in iLO.

    :param task: a task from TaskManager.
    :returns: Boolean value. True if onetime boot is 'Once'
              False otherwise.

    """
    client = common.get_hponeview_client()
    server_hardware = task.node.driver_info.get('server_hardware_uri')
    ilo_client = common.get_ilorest_client(client, server_hardware)
    response = ilo_client.get(path=ILO_SYSTEM_PATH,
                              headers=ILO_REQUEST_HEADERS)
    boot = response.dict.get('Boot')
    onetime_boot = boot.get('BootSourceOverrideEnabled')
    return onetime_boot == 'Once'
示例#7
0
def deallocate_server_hardware_from_ironic(task):
    """Deallocate Server Hardware from ironic.

    :param task: a TaskManager object
    :raises OneViewError: if an error occurs while deallocating the Server
            Hardware to ironic

    """
    node = task.node
    oneview_client = common.get_hponeview_client()

    if is_node_in_use_by_ironic(node):
        oneview_info = common.get_oneview_info(node)
        server_profile_uri = oneview_info.get('applied_server_profile_uri')

        try:
            task.driver.power.set_power_state(task, states.POWER_OFF)
            oneview_client.server_profiles.delete(server_profile_uri)
            _del_applied_server_profile_uri_field(node)
            LOG.info(
                "Server Profile %(server_profile_uri)s was deleted "
                "from node %(node_uuid)s in OneView.", {
                    'server_profile_uri': server_profile_uri,
                    'node_uuid': node.uuid
                })
        except client_exception.HPOneViewException as e:
            msg = (_("Error while deleting applied Server Profile from node "
                     "%(node_uuid)s. Error: %(error)s") % {
                         'node_uuid': node.uuid,
                         'error': e
                     })
            raise exception.OneViewError(error=msg)

    else:
        LOG.warning(
            "Cannot deallocate node %(node_uuid)s "
            "in OneView because it is not in use by "
            "ironic.", {'node_uuid': node.uuid})
示例#8
0
def is_node_in_use_by_ironic(node):
    """Check if node is in use by ironic in OneView.

    :param node: an ironic node object.
    :returns: Boolean value. True if node is in use by ironic,
              False otherwise.
    :raises OneViewError: if not possible to get OneView's information
            for the given node, if not possible to retrieve Server Hardware
            from OneView.
    """
    oneview_client = common.get_hponeview_client()
    positive = _("Node '%s' is in use by Ironic.") % node.uuid
    negative = _("Node '%s' is not in use by Ironic.") % node.uuid

    def predicate(server_hardware, applied_sp_uri):
        # Check if Profile exists in Oneview and it is equals of the one
        # applied by ironic
        return _is_node_in_use(server_hardware,
                               applied_sp_uri,
                               by_oneview=False)

    return _check_applied_server_profile(oneview_client, node, predicate,
                                         positive, negative)
示例#9
0
 def __init__(self):
     super(OneViewAgentDeploy, self).__init__()
     self.client = common.get_hponeview_client()
     self.oneview_client = common.get_oneview_client()
示例#10
0
 def test_get_hponeview_client(self, mock_hponeview_client):
     common.get_hponeview_client()
     mock_hponeview_client.assert_called_once_with(self.config)
示例#11
0
 def __init__(self):
     super(OneViewPower, self).__init__()
     self.client = common.get_hponeview_client()
     self.oneview_client = common.get_oneview_client()
示例#12
0
def allocate_server_hardware_to_ironic(node, server_profile_name):
    """Allocate Server Hardware to ironic.

    :param node: an ironic node object.
    :param server_profile_name: a formatted string with the Server Profile
           name.
    :raises OneViewError: if an error occurs while allocating the Server
            Hardware to ironic or the node is already in use by OneView.
    """
    oneview_client = common.get_hponeview_client()
    node_in_use_by_oneview = is_node_in_use_by_oneview(node)

    if not node_in_use_by_oneview:

        oneview_info = common.get_oneview_info(node)

        applied_sp_uri = node.driver_info.get('applied_server_profile_uri')
        sh_uri = oneview_info.get("server_hardware_uri")
        spt_uri = oneview_info.get("server_profile_template_uri")
        server_hardware = oneview_client.server_hardware.get(sh_uri)

        if not server_hardware:
            msg = _("An error occurred during allocating server hardware to "
                    "ironic. Server hardware: %s not found.") % sh_uri
            raise exception.OneViewError(error=msg)

        # Don't have Server Profile on OneView but has
        # `applied_server_profile_uri` on driver_info
        if not server_hardware.get('serverProfileUri') and applied_sp_uri:

            _del_applied_server_profile_uri_field(node)
            LOG.info(
                "Inconsistent 'applied_server_profile_uri' parameter "
                "value in driver_info. There is no Server Profile "
                "applied to node %(node_uuid)s. Value deleted.",
                {"node_uuid": node.uuid})

        # applied_server_profile_uri exists and is equal to Server profile
        # applied on Hardware. Do not apply again.
        if (applied_sp_uri
                and server_hardware.get('serverProfileUri') == applied_sp_uri):
            LOG.info(
                "The Server Profile %(applied_sp_uri)s was already applied "
                "by ironic on node %(node_uuid)s. Reusing.", {
                    "node_uuid": node.uuid,
                    "applied_sp_uri": applied_sp_uri
                })
            return

        try:
            applied_profile = _create_profile_from_template(
                oneview_client, server_profile_name, sh_uri, spt_uri)
            _add_applied_server_profile_uri_field(node, applied_profile)

            LOG.info(
                "Server Profile %(server_profile_uuid)s was successfully"
                " applied to node %(node_uuid)s.", {
                    "node_uuid": node.uuid,
                    "server_profile_uuid": applied_profile.get('uri')
                })

        except client_exception.HPOneViewInvalidResource as e:
            LOG.error(
                "An error occurred during allocating server "
                "hardware to ironic during prepare: %s", e)
            raise exception.OneViewError(error=e)
    else:
        msg = _("Node %s is already in use by OneView.") % node.uuid
        raise exception.OneViewError(error=msg)