File: system.py

package info (click to toggle)
python-sushy 5.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 2,620 kB
  • sloc: python: 14,026; makefile: 24; sh: 2
file content (599 lines) | stat: -rw-r--r-- 24,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

# This is referred from Redfish standard schema.
# https://redfish.dmtf.org/schemas/v1/ComputerSystem.v1_10_0.json

import collections
import logging

from dateutil import parser

from sushy import exceptions
from sushy.resources import base
from sushy.resources.chassis import chassis
from sushy.resources import common
from sushy.resources import constants as res_cons
from sushy.resources.manager import manager
from sushy.resources.manager import virtual_media
from sushy.resources import settings
from sushy.resources.system import bios
from sushy.resources.system import constants as sys_cons
from sushy.resources.system import ethernet_interface
from sushy.resources.system import processor
from sushy.resources.system import secure_boot
from sushy.resources.system import simple_storage as sys_simple_storage
from sushy.resources.system.storage import storage as sys_storage
from sushy import utils


LOG = logging.getLogger(__name__)


class ActionsField(base.CompositeField):
    reset = common.ResetActionField('#ComputerSystem.Reset')


class BootField(base.CompositeField):
    allowed_values = base.Field(
        'BootSourceOverrideTarget@Redfish.AllowableValues',
        adapter=list)

    enabled = base.MappedField('BootSourceOverrideEnabled',
                               sys_cons.BootSourceOverrideEnabled)

    mode = base.MappedField('BootSourceOverrideMode',
                            sys_cons.BootSourceOverrideMode)

    target = base.MappedField('BootSourceOverrideTarget', sys_cons.BootSource)

    http_boot_uri = base.Field('HttpBootUri')


class BootProgressField(base.CompositeField):

    last_boot_seconds_count = base.Field('LastBootTimeSeconds',
                                         adapter=utils.int_or_none)
    """The number of seconds the last boot took to reach OSRunning."""

    last_state = base.MappedField('LastState', sys_cons.BootProgressStates)
    """The last recorded boot progress states."""

    last_state_updated_at = base.Field('LastStateTime',
                                       adapter=parser.parse)
    """The date-time value when the last state field was updated."""

    oem_last_state = base.Field('OemLastState')
    """The OEM last state time to describe OEM specific state information."""


class MemorySummaryField(base.CompositeField):
    health = base.Field(['Status', 'HealthRollup'])
    """The overall health state of memory.

    This signifies health state of memory along with its dependent resources.
    """

    size_gib = base.Field('TotalSystemMemoryGiB', adapter=utils.int_or_none)
    """The size of memory of the system in GiB.

    This signifies the total installed, operating system-accessible memory
    (RAM), measured in GiB.
    """


class System(base.ResourceBase):

    asset_tag = base.Field('AssetTag')
    """The system asset tag"""

    bios_version = base.Field('BiosVersion')
    """The system BIOS version"""

    boot = BootField('Boot', required=True)
    """A dictionary containing the current boot device, frequency and mode"""

    description = base.Field('Description')
    """The system description"""

    hostname = base.Field('HostName')
    """The system hostname"""

    identity = base.Field('Id', required=True)
    """The system identity string"""

    indicator_led = base.MappedField('IndicatorLED', res_cons.IndicatorLED)
    """Whether the indicator LED is lit or off"""

    manufacturer = base.Field('Manufacturer')
    """The system manufacturer"""

    name = base.Field('Name')
    """The system name"""

    part_number = base.Field('PartNumber')
    """The system part number"""

    power_state = base.MappedField('PowerState', res_cons.PowerState)
    """The system power state"""

    serial_number = base.Field('SerialNumber')
    """The system serial number"""

    sku = base.Field('SKU')
    """The system stock-keeping unit"""

    status = common.StatusField('Status')
    """The system status"""

    system_type = base.MappedField('SystemType', sys_cons.SystemType)
    """The system type"""

    uuid = base.Field('UUID')
    """The system UUID"""

    memory_summary = MemorySummaryField('MemorySummary')
    """The summary info of memory of the system in general detail"""

    maintenance_window = settings.MaintenanceWindowField(
        '@Redfish.MaintenanceWindow')
    """Indicates if a given resource has a maintenance window assignment
    for applying settings or operations"""

    _settings = settings.SettingsField()
    """Settings Resource is used to represent the future intended state
    of a Resource
    Ref: http://redfish.dmtf.org/schemas/DSP0266_1.7.0.html#settings-resource
    """

    _actions = ActionsField('Actions', required=True)

    boot_progress = BootProgressField('BootProgress')
    """The last updated boot progress indicator"""

    def __init__(self, connector, identity, redfish_version=None,
                 registries=None, root=None):
        """A class representing a ComputerSystem

        :param connector: A Connector instance
        :param identity: The identity of the System resource
        :param redfish_version: The version of RedFish. Used to construct
            the object according to schema of the given version.
        :param registries: Dict of registries to be used in any resource
            that needs registries to parse messages.
        :param root: Sushy root object. Empty for Sushy root itself.
        """
        super().__init__(
            connector, identity,
            redfish_version=redfish_version,
            registries=registries,
            root=root)

    def _get_reset_action_element(self):
        reset_action = self._actions.reset
        # TODO(dtantsur): make this check also declarative?
        if not reset_action:
            raise exceptions.MissingActionError(action='#ComputerSystem.Reset',
                                                resource=self._path)
        return reset_action

    def get_allowed_reset_system_values(self):
        """Get the allowed values for resetting the system.

        :returns: A set with the allowed values.
        """
        reset_action = self._get_reset_action_element()

        if not reset_action.allowed_values:
            LOG.warning('Could not figure out the allowed values for the '
                        'reset system action for System %s', self.identity)
            return set(res_cons.ResetType)

        return {v for v in res_cons.ResetType
                if v.value in reset_action.allowed_values}

    def reset_system(self, value):
        """Reset the system.

        :param value: The target value.
        :raises: InvalidParameterValueError, if the target value is not
            allowed.
        """
        valid_resets = self.get_allowed_reset_system_values()
        if value not in valid_resets:
            raise exceptions.InvalidParameterValueError(
                parameter='value', value=value, valid_values=valid_resets)

        value = res_cons.ResetType(value).value
        target_uri = self._get_reset_action_element().target_uri

        # TODO(lucasagomes): Check the return code and response body ?
        #                    Probably we should call refresh() as well.
        self._conn.post(target_uri, data={'ResetType': value})

    def get_allowed_system_boot_source_values(self):
        """Get the allowed values for changing the boot source.

        :returns: A set with the allowed values.
        """
        if not self.boot.allowed_values:
            LOG.warning('Could not figure out the allowed values for '
                        'configuring the boot source for System %s',
                        self.identity)
            return set(sys_cons.BootSource)

        return {v for v in sys_cons.BootSource
                if v.value in self.boot.allowed_values}

    def set_system_boot_options(self, target=None, enabled=None, mode=None,
                                http_boot_uri=None):
        """Set boot source and/or boot frequency and/or boot mode.

        Set the boot source and/or boot frequency and/or boot mode to use
        on next reboot of the System.

        :param target: The target boot source,
            a :py:class:`sushy.BootSource` value. Optional.
        :param enabled: How long the override is enabled,
            a :py:class:`sushy.BootSourceOverrideEnabled` value. Optional.
        :param mode: The boot mode,
            a :py:class:`sushy.BootSourceOverrideMode` value. Optional.
        :param http_boot_uri: The requested HTTP Boot URI to transmit to the
            BMC. Only valid when BootSourceOverrideTarget is set to UefiHTTP,
            when utilizing the ``target`` parameter. If no value is supplied,
            and the target is set to UefiHTTP, then an empty value will be
            sent to the BMC to remove any prior setting, allowing the host
            to load configuration from DHCP.
            If not explicitly set, any value will be removed from a BMC when
            UefiHttp boot is not engaged.

        :raises: InvalidParameterValueError, if any information passed is
            invalid.
        """
        data = collections.defaultdict(dict)
        settings_data = collections.defaultdict(dict)

        if self._settings and self._settings.resource_uri:
            settings_resp = self._conn.get(self._settings.resource_uri)
            settings_boot_section = settings_resp.json().get('Boot', {})
        else:
            settings_resp = None
            settings_boot_section = {}

        if target is not None:
            valid_targets = self.get_allowed_system_boot_source_values()
            if target not in valid_targets:
                raise exceptions.InvalidParameterValueError(
                    parameter='target', value=target,
                    valid_values=valid_targets)

            target = sys_cons.BootSource(target)
            # NOTE(janders) on SuperMicro X11 and X12 machines, virtual media
            # is presented as an "USB CD" drive as opposed to a CD drive. Both
            # are present in the list of boot devices, however only selecting
            # UsbCd as the boot source results in a successful boot from
            # vMedia. If "CD" is selected, boot fails even if vMedia is
            # inserted. This code detects a case where a SuperMicro machine is
            # about to attempt boot from CD and overrides the boot device to
            # UsbCd instead which makes boot from vMedia work as expected.
            if (self.manufacturer and self.manufacturer.lower() == 'supermicro'
                    and target == sys_cons.BootSource.CD
                    and sys_cons.BootSource.USB_CD.value
                    in self.boot.allowed_values):
                LOG.debug('Boot from vMedia was requested on a SuperMicro'
                          'machine. Overriding boot device from %s to %s.',
                          target, sys_cons.BootSource.USB_CD)
                target = sys_cons.BootSource.USB_CD
            if (settings_resp and "BootSourceOverrideTarget" in
                    settings_boot_section):
                settings_data['Boot']['BootSourceOverrideTarget'] = \
                    target.value
            else:
                data['Boot']['BootSourceOverrideTarget'] = target.value

        if enabled is not None:
            try:
                fishy_freq = sys_cons.BootSourceOverrideEnabled(enabled).value
            except ValueError:
                raise exceptions.InvalidParameterValueError(
                    parameter='enabled', value=enabled,
                    valid_values=list(sys_cons.BootSourceOverrideEnabled))
            if (settings_resp and "BootSourceOverrideEnabled" in
                    settings_boot_section):
                settings_data['Boot']['BootSourceOverrideEnabled'] = fishy_freq
            else:
                data['Boot']['BootSourceOverrideEnabled'] = fishy_freq

        if mode is not None:
            try:
                fishy_mode = sys_cons.BootSourceOverrideMode(mode).value
            except ValueError:
                raise exceptions.InvalidParameterValueError(
                    parameter='mode', value=mode,
                    valid_values=list(sys_cons.BootSourceOverrideMode))
            if (settings_resp and "BootSourceOverrideMode" in
                    settings_boot_section):
                settings_data['Boot']['BootSourceOverrideMode'] = fishy_mode
            else:
                data['Boot']['BootSourceOverrideMode'] = fishy_mode

        if target == sys_cons.BootSource.UEFI_HTTP:
            # The http_boot_uri value *can* be set independently of the
            # target, but the BMC will just ignore it unless the target
            # is set. So we should only, and explicitly set it when we've
            # been requested to boot from UefiHTTP.
            if not http_boot_uri:
                # This should clear out any old entries, as no URI translates
                # to the intent of "use whatever the dhcp server says".
                http_boot_uri = None

            if (settings_resp and "HttpBootUri" in settings_boot_section):
                settings_data['Boot']['HttpBootUri'] = http_boot_uri
            else:
                data['Boot']['HttpBootUri'] = http_boot_uri
        elif not http_boot_uri:
            # We're not doing boot from URL, we should cleanup any setting
            # which may be from a prior step/call.
            if settings_boot_section.get('HttpBootUri'):
                # If the setting is present, and has any value, unset it.
                data['Boot']['HttpBootUri'] = None

        # TODO(lucasagomes): Check the return code and response body ?
        #                    Probably we should call refresh() as well.
        if settings_data.get('Boot'):
            etag = settings_resp.headers.get('ETag')
            path = self._settings.resource_uri
            self._conn.patch(path, data=settings_data,
                             etag=etag)
        if data.get('Boot'):
            etag = self._get_etag()
            path = self.path
            self._conn.patch(path, data=data, etag=etag)

    # TODO(etingof): we should remove this method, eventually
    def set_system_boot_source(
            self, target, enabled=sys_cons.BootSourceOverrideEnabled.ONCE,
            mode=None):
        """Set boot source and/or boot frequency and/or boot mode.

        Set the boot source and/or boot frequency and/or boot mode to use
        on next reboot of the System.

        This method is obsoleted by `set_system_boot_options`.

        :param target: The target boot source,
            a :py:class:`sushy.BootSource` value.
        :param enabled: The frequency, whether to set it for the next
            a :py:class:`sushy.BootSourceOverrideEnabled` value.
            Default is `ONCE`.
        :param mode: The boot mode,
            a :py:class:`sushy.BootSourceOverrideMode` value.
        :raises: InvalidParameterValueError, if any information passed is
            invalid.
        """
        self.set_system_boot_options(target, enabled, mode)

    def set_indicator_led(self, state):
        """Set IndicatorLED to the given state.

        :param state: Desired LED state, an IndicatorLED value.
        :raises: InvalidParameterValueError, if any information passed is
            invalid.
        """
        try:
            state = res_cons.IndicatorLED(state).value
        except ValueError:
            raise exceptions.InvalidParameterValueError(
                parameter='state', value=state,
                valid_values=' ,'.join(i.value for i in res_cons.IndicatorLED))

        etag = self._get_etag()
        data = {'IndicatorLED': state}

        self._conn.patch(self.path, data=data, etag=etag)
        self.invalidate()

    def _get_processor_collection_path(self):
        """Helper function to find the ProcessorCollection path"""
        return utils.get_sub_resource_path_by(self, 'Processors')

    @property
    @utils.cache_it
    def processors(self):
        """Property to reference `ProcessorCollection` instance

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.
        """
        return processor.ProcessorCollection(
            self._conn, self._get_processor_collection_path(),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)

    @property
    @utils.cache_it
    def ethernet_interfaces(self):
        """Property to reference `EthernetInterfaceCollection` instance

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.
        """
        return ethernet_interface.EthernetInterfaceCollection(
            self._conn,
            utils.get_sub_resource_path_by(self, "EthernetInterfaces"),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)

    @property
    @utils.cache_it
    def bios(self):
        """Property to reference `Bios` instance

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.
        """
        return bios.Bios(
            self._conn,
            utils.get_sub_resource_path_by(self, 'Bios'),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)

    @property
    @utils.cache_it
    def simple_storage(self):
        """A collection of simple storage associated with system.

        This returns a reference to `SimpleStorageCollection` instance.
        SimpleStorage represents the properties of a storage controller and its
        directly-attached devices.

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.

        :raises: MissingAttributeError if 'SimpleStorage/@odata.id' field
            is missing.
        :returns: `SimpleStorageCollection` instance
        """
        return sys_simple_storage.SimpleStorageCollection(
            self._conn, utils.get_sub_resource_path_by(self, "SimpleStorage"),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)

    @property
    @utils.cache_it
    def storage(self):
        """A collection of storage subsystems associated with system.

        This returns a reference to `StorageCollection` instance.
        A storage subsystem represents a set of storage controllers (physical
        or virtual) and the resources such as drives and volumes that can be
        accessed from that subsystem.

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.

        :raises: MissingAttributeError if 'Storage/@odata.id' field
            is missing.
        :returns: `StorageCollection` instance
        """
        return sys_storage.StorageCollection(
            self._conn, utils.get_sub_resource_path_by(self, "Storage"),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)

    @property
    @utils.cache_it
    def managers(self):
        """A list of managers for this system.

        Returns a list of `Manager` objects representing the managers
        that manage this system.

        :raises: MissingAttributeError if '@odata.id' field is missing.
        :returns: A list of `Manager` instances
        """
        try:
            paths = utils.get_sub_resource_path_by(
                self, ["Links", "ManagedBy"], is_collection=True)
        except exceptions.MissingAttributeError as exc_orig:
            LOG.warning('Unable to find ManagedBy attribute for System %s, '
                        'retrying with Managers attribute', self.identity)
            try:
                paths = utils.get_sub_resource_path_by(
                    self, ["Links", "Managers"], is_collection=True)
            except exceptions.MissingAttributeError:
                LOG.error('Both ManagedBy and Managers attributes missing for '
                          'System %s, aborting', self.identity)
                # NOTE(janders) last_error may record only Managers and not
                # ManagedBy MissingAttributeError with this approach
                raise exc_orig
        return [manager.Manager(self._conn, path,
                                redfish_version=self.redfish_version,
                                registries=self.registries, root=self.root)
                for path in paths]

    @property
    @utils.cache_it
    def chassis(self):
        """A list of chassis where this system resides.

        Returns a list of `Chassis` objects representing the chassis
        or cabinets where this system is mounted.

        :raises: MissingAttributeError if '@odata.id' field is missing.
        :returns: A list of `Chassis` instances
        """
        paths = utils.get_sub_resource_path_by(
            self, ["Links", "Chassis"], is_collection=True)

        return [chassis.Chassis(self._conn, path,
                                redfish_version=self.redfish_version,
                                registries=self.registries, root=self.root)
                for path in paths]

    @property
    @utils.cache_it
    def secure_boot(self):
        """Property to reference `SecureBoot` instance

        It is set once when the first time it is queried. On refresh,
        this property is marked as stale (greedy-refresh not done).
        Here the actual refresh of the sub-resource happens, if stale.
        """
        return secure_boot.SecureBoot(
            self._conn,
            utils.get_sub_resource_path_by(self, 'SecureBoot'),
            redfish_version=self.redfish_version,
            registries=self.registries, root=self.root)

    @property
    @utils.cache_it
    def virtual_media(self):
        """Property to reference `VirtualMedia` instance

        :returns: A `VirtualMediaCollection` instance.
        """
        return virtual_media.VirtualMediaCollection(
            self._conn, utils.get_sub_resource_path_by(self, 'VirtualMedia'),
            redfish_version=self.redfish_version, registries=self.registries,
            root=self.root)


class SystemCollection(base.ResourceCollectionBase):

    @property
    def _resource_type(self):
        return System

    def __init__(self, connector, path, redfish_version=None, registries=None,
                 root=None):
        """A class representing a ComputerSystemCollection

        :param connector: A Connector instance
        :param path: The canonical path to the System collection resource
        :param redfish_version: The version of RedFish. Used to construct
            the object according to schema of the given version.
        :param registries: Dict of Redfish Message Registry objects to be
            used in any resource that needs registries to parse messages
        :param root: Sushy root object. Empty for Sushy root itself.
        """
        super().__init__(
            connector, path, redfish_version=redfish_version,
            registries=registries, root=root)