File: power.py

package info (click to toggle)
python-pypowervm 1.1.24%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,400 kB
  • sloc: python: 29,780; xml: 174; makefile: 14
file content (493 lines) | stat: -rw-r--r-- 23,048 bytes parent folder | download | duplicates (2)
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
# Copyright 2014, 2017 IBM Corp.
#
# 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.

"""Tasks to start, stop, and reboot partitions."""

from oslo_config import cfg
from oslo_log import log as logging
import six

import pypowervm.const as c
import pypowervm.exceptions as pexc
from pypowervm.i18n import _
import pypowervm.log as lgc
import pypowervm.tasks.power_opts as popts
import pypowervm.wrappers.base_partition as bp
from pypowervm.wrappers import job

LOG = logging.getLogger(__name__)

CONF = cfg.CONF

# Error codes indicate osshutdown is not supported
_OSSHUTDOWN_RMC_ERRS = ['HSCL0DB4', 'PVME01050905', 'PVME01050402']
# Error codes indicate partition is already powered off
_ALREADY_POWERED_OFF_ERRS = ['HSCL1558', 'PVME04000005', 'PVME01050901']
# Error codes indicate partition is already powered on
_ALREADY_POWERED_ON_ERRS = ['HSCL3681', 'PVME01042026']

BootMode = popts.BootMode
KeylockPos = popts.KeylockPos
RemoveOptical = popts.RemoveOptical
Force = popts.Force


class PowerOp(object):
    """Provides granular control over a partition PowerOn/Off Job.

    Use the start or stop @classmethod to invoke the appropriate Job.  Jobs
    invoked through these methods are never retried.  If they fail or time out,
    they raise relevant exceptions - see the methods' docstrings for details.
    """
    @classmethod
    def start(cls, part, opts=None, timeout=CONF.pypowervm_job_request_timeout,
              synchronous=True):
        """Power on a partition.

        :param part: Partition (LPAR or VIOS) wrapper indicating the partition
                     to power on.
        :param opts: An instance of power_opts.PowerOnOpts indicating
                     additional options to specify to the PowerOn operation.
                     By default, no additional options are used.
        :param timeout: value in seconds for specifying how long to wait for
                        the Job to complete.
        :param synchronous: If True, this method will not return until the Job
                            completes (whether success or failure) or times
                            out.  If False, this method will return as soon as
                            the Job has started on the server (that is,
                            achieved any state beyond NOT_ACTIVE).  Note that
                            timeout is still possible in this case.
        :raise VMPowerOnTimeout: If the Job timed out.
        :raise VMPowerOnFailure: If the Job failed for some reason other than
                                 that the partition was already powered on.
        """
        try:
            cls._run(part, opts or popts.PowerOnOpts(), timeout,
                     synchronous=synchronous)
        except pexc.JobRequestTimedOut as error:
            LOG.exception(error)
            raise pexc.VMPowerOnTimeout(lpar_nm=part.name, timeout=timeout)
        except pexc.JobRequestFailed as error:
            emsg = six.text_type(error)
            # If already powered on, don't send exception
            if (any(err_prefix in emsg
                    for err_prefix in _ALREADY_POWERED_ON_ERRS)):
                LOG.warning(_("Partition %s already powered on."), part.name)
                return
            LOG.exception(error)
            raise pexc.VMPowerOnFailure(lpar_nm=part.name, reason=emsg)

    @classmethod
    def stop(cls, part, opts=None, timeout=CONF.pypowervm_job_request_timeout,
             synchronous=True):
        """Power off a partition.

        :param part: LPAR/VIOS wrapper indicating the partition to power off.
        :param opts: An instance of power_opts.PowerOffOpts indicating the type
                     of shutdown to perform, and any additional options.  If
                     not specified, PowerOffOpts.soft_detect is used, with no
                     restart.
        :param timeout: value in seconds for specifying how long to wait for
                        the Job to complete.
        :param synchronous: If True, this method will not return until the Job
                            completes (whether success or failure) or times
                            out.  If False, this method will return as soon as
                            the Job has started on the server (that is,
                            achieved any state beyond NOT_ACTIVE).  Note that
                            timeout is still possible in this case.
        :raise VMPowerOffTimeout: If the Job timed out.
        :raise VMPowerOffFailure: If the Job failed for some reason other than
                                  that the partition was already powered off,
                                  and restart was not requested.
        :return: A PowerOp instance which can be invoked via the run method.
        :raise OSShutdownNoRMC: OP_PWROFF_OS was requested on a non-IBMi
                                partition with no RMC connection.
        """
        if opts is None:
            opts = popts.PowerOffOpts().soft_detect(part)

        if opts.is_os and not opts.can_os_shutdown(part):
            raise pexc.OSShutdownNoRMC(lpar_nm=part.name)

        try:
            cls._run(part, opts, timeout, synchronous=synchronous)
        except pexc.JobRequestTimedOut as error:
            LOG.exception(error)
            raise pexc.VMPowerOffTimeout(lpar_nm=part.name, timeout=timeout)
        except pexc.JobRequestFailed as error:
            emsg = six.text_type(error)
            # If already powered off and not a reboot, don't send exception
            if (any(err_prefix in emsg
                    for err_prefix in _ALREADY_POWERED_OFF_ERRS) and
                    not opts.is_restart):
                LOG.warning(_("Partition %s already powered off."), part.name)
                return
            LOG.exception(error)
            raise pexc.VMPowerOffFailure(lpar_nm=part.name, reason=emsg)

    @classmethod
    def _run(cls, part, opts, timeout, synchronous=True):
        """Fetch, fill out, and run a Power* Job for this PowerOp.

        Do not invoke this method directly; it is used by the start and stop
        class methods.

        :param part: LPAR/VIOS wrapper of the partition to power on/off.
        :param opts: Instance of power_opts.PowerOnOpts or PowerOffOpts
                     indicating the type of operation to perform, and any
                     additional options.
        :param timeout: value in seconds for specifying how long to wait for
                        the Job to complete.
        :param synchronous: If True, this method will not return until the Job
                            completes (whether success or failure) or times
                            out.  If False, this method will return as soon as
                            the Job has started on the server (that is,
                            achieved any state beyond NOT_ACTIVE).  Note that
                            timeout is still possible in this case.
        :raise VMPowerOffTimeout: If the Job timed out.
        :raise VMPowerOffFailure: If the Job failed.
        """
        # Fetch the Job template wrapper
        jwrap = job.Job.wrap(part.adapter.read(
            part.schema_type, part.uuid, suffix_type=c.SUFFIX_TYPE_DO,
            suffix_parm=opts.JOB_SUFFIX))

        LOG.debug("Executing power operation for partition %(lpar_nm)s with "
                  "timeout=%(timeout)d and synchronous=%(synchronous)s: "
                  "%(opts)s",
                  dict(lpar_nm=part.name, timeout=timeout,
                       synchronous=synchronous, opts=str(opts)))
        # Run the Job, letting exceptions raise up.
        jwrap.run_job(part.uuid, job_parms=opts.bld_jparms(), timeout=timeout,
                      synchronous=synchronous)


def _legacy_power_opts(klass, add_parms):
    """Detect (and warn) if add_parms is a legacy dict vs. a Power*Opts.

    Usage:
        opts, legacy = _legacy_power_opts(PowerOnOpts, add_parms)
        if legacy:
            # Do other stuff based on legacy behavior
        else:
            # Do other stuff based on new behavior

    :param klass: The class we expect, either PowerOnOpts or PowerOffOpts.
    :param add_parms: The add_parms argument to check.
    :return: An instance of klass, which is either add_parms, constructed by
             passing it to the klass __init__'s legacy_add_parms.
    :return: True if add_parms was a legacy dict; False otherwise.
    """
    if isinstance(add_parms, klass):
        return add_parms, False
    else:
        if add_parms is not None:
            import warnings
            warnings.warn(_("Specifying add_parms as a dict is deprecated. "
                            "Please specify a %s instance instead.") %
                          klass.__name__, DeprecationWarning)
        return klass(legacy_add_parms=add_parms), True


@lgc.logcall_args
def power_on(part, host_uuid, add_parms=None, synchronous=True):
    """Will Power On a Logical Partition or Virtual I/O Server.

    :param part: The LPAR/VIOS wrapper of the partition to power on.
    :param host_uuid: Not used.  Retained for backward compatibility.
    :param add_parms: A power_opts.PowerOnOpts instance; or (deprecated) a dict
                      of parameters to pass directly to the job template.  If
                      unspecified, a default PowerOnOpts instance is used, with
                      no additional parameters.
    :param synchronous: If True (the default), this method will not return
                        until the PowerOn Job completes (whether success or
                        failure) or times out.  If False, this method will
                        return as soon as the Job has started on the server
                        (that is, achieved any state beyond NOT_ACTIVE).  Note
                        that timeout is still possible in this case.
    :raise VMPowerOnFailure: If the operation failed.
    :raise VMPowerOnTimeout: If the operation timed out.
    """
    PowerOp.start(
        part, opts=_legacy_power_opts(popts.PowerOnOpts, add_parms)[0],
        synchronous=synchronous)


def _pwroff_soft_ibmi_flow(part, restart, immediate, timeout):
    """Normal (non-hard) power-off retry flow for IBMi partitions.

          ==================
          opts.is_immediate? <--START
          ==================
           |             |
           NO           YES
           V             V
    =========          ============          ==========          ============
    OS normal -FAIL*-> OS immediate -FAIL*-> VSP normal -FAIL*-> return False
    =========          ============          ==========          ============
        |_________________ | ___________________|
                          |||
                        SUCCESS
                           V         *VMPowerOffTimeout OR
                      ===========     VMPowerOffFailure
                      return True
                      ===========

    :param part restart timeout: See power_off.
    :param immediate: Boolean.  Indicates whether to try os-normal first
                      (False, the default) before progressing to os-immediate.
                      If True, skip trying os-normal shutdown.
    :return: True if the power-off succeeded; False otherwise.
    :raise VMPowerOffTimeout: If the last power-off attempt timed out.
    :raise VMPowerOffFailure: If the last power-off attempt failed.
    """
    opts = popts.PowerOffOpts().restart(value=restart)
    # If immediate was already specified, skip OS-normal.
    if not immediate:
        # ==> OS normal
        try:
            PowerOp.stop(part, opts=opts.os_normal(), timeout=timeout)
            return True
        except pexc.VMPowerOffFailure:
            LOG.warning(_("IBMi OS normal shutdown failed.  Trying OS "
                          "immediate shutdown.  Partition: %s"), part.name)
            # Fall through to OS immediate, with default timeout
            timeout = CONF.pypowervm_job_request_timeout

    # ==> OS immediate
    try:
        PowerOp.stop(part, opts=opts.os_immediate(), timeout=timeout)
        return True
    except pexc.VMPowerOffFailure:
        LOG.warning(_("IBMi OS immediate shutdown failed.  Trying VSP normal "
                      "shutdown.  Partition: %s"), part.name)
        # Fall through to VSP normal

    # ==> VSP normal
    try:
        PowerOp.stop(part, opts=opts.vsp_normal(), timeout=timeout)
        return True
    except pexc.VMPowerOffFailure:
        LOG.warning("IBMi VSP normal shutdown failed.  Partition: %s",
                    part.name)

    return False


def _pwroff_soft_standard_flow(part, restart, timeout):
    """Normal (non-hard) power-off retry flow for non-IBMi partitions.

    START
    |     +---VMPowerOffTimeout-------------------------------------+
    V     |                                                         V
    ========  VMPowerOffFailure  ==========  VMPowerOffFailure  ============
    OS immed ----   or      ---> VSP normal ----   or      ---> return False
    ========   OSShutdownNoRMC   ==========  VMPowerOffTimeout  ============
          | _________________________/
          |/
       SUCCESS
          V
     ===========
     return True
     ===========

    :param part restart timeout: See power_off.
    :return: True if the power-off succeeded; False otherwise.
    :raise VMPowerOffTimeout: If the last power-off attempt timed out.
    :raise VMPowerOffFailure: If the last power-off attempt failed.
    """
    # For backward compatibility, OS shutdown is always immediate.  We don't
    # let PowerOn decide whether to use OS or VSP; instead we trap
    # OSShutdownNoRMC (which is very quick) so we can keep this progression
    # linear.

    opts = popts.PowerOffOpts().restart(value=restart)
    # ==> OS immediate
    try:
        PowerOp.stop(part, opts=opts.os_immediate(), timeout=timeout)
        return True
    except pexc.VMPowerOffTimeout:
        LOG.warning(_("Non-IBMi OS immediate shutdown timed out.  Trying VSP "
                      "hard shutdown.  Partition: %s"), part.name)
        return False
    except pexc.VMPowerOffFailure:
        LOG.warning(_("Non-IBMi OS immediate shutdown failed.  Trying VSP "
                      "normal shutdown.  Partition: %s"), part.name)
        # Fall through to VSP normal, but with default timeout
        timeout = CONF.pypowervm_job_request_timeout
    except pexc.OSShutdownNoRMC as error:
        LOG.warning(error.args[0])
        # Fall through to VSP normal

    # ==> VSP normal
    try:
        PowerOp.stop(part, opts.vsp_normal(), timeout=timeout)
        return True
    except pexc.VMPowerOffFailure:
        LOG.warning("Non-IBMi VSP normal shutdown failed.  Partition: %s",
                    part.name)

    return False


def _power_off_progressive(part, timeout, restart, ibmi_immed=False):

    # Do the progressive-retry sequence appropriate to the partition type.
    if part.env == bp.LPARType.OS400:
        # The IBMi progression.
        if _pwroff_soft_ibmi_flow(part, restart, ibmi_immed, timeout):
            return
            # Fall through to VSP hard
    else:
        # The non-IBMi progression.
        if _pwroff_soft_standard_flow(part, restart, timeout):
            return
            # Fall through to VSP hard

    # If we got here, force_immediate == ON_FAILURE, so fall back to VSP hard.
    # Let this one finish or raise.
    # ==> VSP hard
    LOG.warning(_("VSP hard shutdown with default timeout.  Partition: %s"),
                part.name)
    PowerOp.stop(part, popts.PowerOffOpts().vsp_hard().restart(value=restart))


def _power_off_single(part, opts, force_immediate, timeout):
    """No-retry single power-off operation.

    :param part force_immediate timeout: See power_off.
                                    force_immediate is either TRUE or NO_RETRY.
    :param opts: A PowerOffOpts instance.  The operation and immediate params
                 are overwritten by this method.  Any other options (such as
                 restart) remain unaffected.
    :raise VMPowerOffFailure: If the operation failed.
    :raise VMPowerOffTimeout: If the operation timed out.
    """
    # If force_immediate=TRUE, always VSP hard shutdown.
    if force_immediate == Force.TRUE:
        PowerOp.stop(part, opts=opts.vsp_hard(), timeout=timeout)
    # If no retries, just do the single "soft" power-off requested
    elif force_immediate == Force.NO_RETRY:
        # opts is already set up for soft_detect
        PowerOp.stop(part, opts=opts, timeout=timeout)

    return


@lgc.logcall_args
def power_off(part, host_uuid, force_immediate=Force.ON_FAILURE, restart=False,
              timeout=CONF.pypowervm_job_request_timeout, add_parms=None):
    """Will Power Off a Logical Partition or Virtual I/O Server.

    DEPRECATED.  Use PowerOp.stop() for single power-off.
                 Use power_off_progressive for soft-retry flows.

    Depending on the force_immediate flag and the partition's type and RMC
    state, this method may attempt increasingly aggressive mechanisms for
    shutting down the OS if initial attempts fail or time out.

    :param part: The LPAR/VIOS wrapper of the instance to power off.
    :param host_uuid: Not used.  Retained for backward compatibility.
    :param force_immediate: DEPRECATED.
        - If you want Force.NO_RETRY behavior, use PowerOp.stop() with the
          specific operation/immediate settings desired.
        - If you want Force.TRUE behavior, use
          PowerOp.stop(..., opts=PowerOffOpts().vsp_hard())
        - If add_parms is a PowerOffOpts with an operation set, force_immediate
          (and restart) is ignored - the method call is equivalent to:
          PowerOp.stop(part, opts=add_parms, timeout=timeout)
        - This flag retains its legacy behavior only if add_parms is either a
          legacy dict or a PowerOffOpts with no operation set:
            - Force.TRUE: The force-immediate option is included on the first
                          pass.
            - Force.NO_RETRY: The force-immediate option is not included.  If
                              the power-off fails or times out,
                              VMPowerOffFailure is raised immediately.
            - Force.ON_FAILURE: The force-immediate option is not included on
                                the first pass; but if the power-off fails
                                (including timeout), it is retried with the
                                force-immediate option added.
    :param restart: DEPRECATED: Use a PowerOffOpts instance for add_parms, with
                    restart specified therein.
                    Boolean.  Perform a restart after the power off.  If
                    add_parms is a PowerOffOpts instance, this parameter is
                    ignored.
    :param timeout: Time in seconds to wait for the instance to stop.
    :param add_parms: A power_opts.PowerOffOpts instance; or (deprecated) a
                      dict of parameters to pass directly to the job template.
                      If unspecified, a default PowerOffOpts instance is used,
                      with operation/immediate/restart depending on the
                      force_immediate and restart parameters, and no additional
                      options.
    :raise VMPowerOffFailure: If the operation failed (possibly after retrying)
    :raise VMPowerOffTimeout: If the operation timed out (possibly after
                              retrying).
    """
    import warnings
    warnings.warn("The power_off method is deprecated.  Please use either "
                  "PowerOp.stop or power_off_progressive.", DeprecationWarning)

    opts, legacy = _legacy_power_opts(popts.PowerOffOpts, add_parms)
    if legacy:
        # Decide whether to insist on 'immediate' for OS shutdown.  Do that
        # only if add_parms explicitly included immediate=true.  Otherwise, let
        # soft_detect decide.
        opts.soft_detect(part, immed_if_os=opts.is_immediate or None)
        # Add the restart option if necessary.
        opts.restart(value=restart)
    elif opts.is_param_set(popts.PowerOffOperation.KEY):
        # If a PowerOffOpt was provided with no operation, it's just being used
        # to specify e.g. restart, and we should fall through to the soft
        # flows.  But if an operation was specified, we just want to do that
        # single operation.  Setting NO_RETRY results in using whatever hard/
        # immediate setting is in the PowerOffOpt.
        force_immediate = Force.NO_RETRY

    if force_immediate != Force.ON_FAILURE:
        return _power_off_single(part, opts, force_immediate, timeout)

    # Do the progressive-retry sequence appropriate to the partition type and
    # the force_immediate flag.
    _power_off_progressive(part, timeout, restart,
                           ibmi_immed=opts.is_immediate)


def power_off_progressive(part, restart=False, ibmi_immed=False,
                          timeout=CONF.pypowervm_job_request_timeout):
    """Attempt soft power-off, retrying with increasing aggression on failure.

    IBMi partitions always start with OS shutdown.  If ibmi_immed == False,
    os-normal shutdown is tried first; then os-immediate; then vsp-normal; then
    vsp-hard.  If ibmi_immed == True, os-normal is skipped, but the rest of the
    progression is the same.

    For non-IBMi partitions:
    If RMC is up, os-immediate is tried first.  If this times out, vsp hard is
    performed next; otherwise, vsp-normal is attempted before vsp-hard.
    If RMC is down, vsp-normal is tried first, then vsp-hard.

    :param part: The LPAR/VIOS wrapper of the instance to power off.
    :param restart: Boolean.  Perform a restart after the power off.
    :param ibmi_immed: Boolean.  Indicates whether to try os-normal first
                        (False, the default) before progressing to
                        os-immediate.  If True, skip trying os-normal shutdown.
                        Only applies to IBMi partitions.
    :param timeout: Time in seconds to wait for the instance to stop.  This is
                    only applied to the first attempt in the progression.
    :raise VMPowerOffFailure: If the last attempt in the progression failed.
    :raise VMPowerOffTimeout: If the last attempt in the progression timed out.
    """
    _power_off_progressive(part, timeout, restart, ibmi_immed=ibmi_immed)