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
|
# Copyright 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.
"""Helper classes for PowerOn/PowerOff options (additional Job parameters)."""
import abc
from oslo_log import log as logging
import six
import pypowervm.exceptions as exc
import pypowervm.wrappers.base_partition as bp
from pypowervm.wrappers import job
import pypowervm.wrappers.logical_partition as lpar
LOG = logging.getLogger(__name__)
IPLSrc = lpar.IPLSrc
class BootMode(object):
"""Valid values for the 'bootmode' parameter in power_on.
Not to be confused with pypowervm.wrappers.base_partition.BootMode.
Example usage:
power_on(..., add_parms={BootMode.KEY: BootMode.SMS, ...})
"""
KEY = 'bootmode'
NORM = 'norm'
SMS = 'sms'
DD = 'dd'
DS = 'ds'
OF = 'of'
PBL = 'pbl'
ALL_VALUES = (NORM, SMS, DD, DS, OF, PBL)
class KeylockPos(object):
"""Valid values for the 'keylock' parameter in power_on.
Not to be confused with pypowervm.wrappers.base_partition.KeylockPos.
Example usage:
power_on(..., add_parms={KeylockPos.KEY: KeylockPos.MANUAL, ...})
"""
KEY = 'keylock'
MANUAL = 'manual'
NORMAL = 'norm'
UNKNOWN = 'unknown'
ALL_VALUES = (MANUAL, NORMAL, UNKNOWN)
class RemoveOptical(object):
"""Valid values for the 'remove_optical_*' parameters in power_on.
This is primarily used to remove the config drive after install. KEY_NAME
is required and maps to a VirtualOpticalMedia name to remove. KEY_TIME is
optional and maps to the time, in minutes, to wait before deleting the
media.
Example usage:
power_on(..., add_parms={RemoveOptical.KEY_TIME: <Integer>,
RemoveOptical.KEY_NAME: <String>}, ...)
"""
KEY_NAME = 'remove_optical_name'
KEY_TIME = 'remove_optical_time'
@classmethod
def bld_map(cls, name, time=0):
return {cls.KEY_NAME: name, cls.KEY_TIME: time}
class IBMiOperationType(object):
"""Valid values for the IBMi operation type in power_on."""
KEY = 'OperationType'
ACTIVATE = 'activate'
NETBOOT = 'netboot'
CHANGE_KEYLOCK = 'changeKeylock'
ALL_VALUES = (ACTIVATE, NETBOOT, CHANGE_KEYLOCK)
class PowerOffOperation(object):
"""Valid values for the operation in power_off."""
KEY = 'operation'
VSP = 'shutdown'
OS = 'osshutdown'
DUMPRESTART = 'dumprestart'
ALL_VALUES = (VSP, OS, DUMPRESTART)
class Force(object):
"""Enumeration indicating the strategy for forcing power-off."""
# The force-immediate option is included on the first pass.
TRUE = True
# The force-immediate option is not included on the first pass; but if the
# power-off fails, it is retried with the force-immediate option included.
# This value is False for backward compatibility.
ON_FAILURE = False
# The force-immediate option is not included. If the power-off fails, it
# is not retried.
NO_RETRY = 'no retry'
@six.add_metaclass(abc.ABCMeta)
class _PowerOpts(object):
# Specify a set of string keys that are legal Job parameters for the
# operation. Illegal keys found in legacy_add_parms will be dropped with a
# warning.
# Leaving as None will skip validation and send all legacy_add_parms to the
# Job.
valid_param_keys = None
def __init__(self, legacy_add_parms=None):
"""Initialize a PowerOpts instance.
:param legacy_add_parms: For legacy use only, initialize the internal
parameter map from the specified dictionary of
Job parameter name/value pairs.
"""
self._parm_map = {}
if self.valid_param_keys is None:
self._parm_map.update(legacy_add_parms or {})
else:
for key in legacy_add_parms or {}:
if key in self.valid_param_keys:
self._parm_map[key] = legacy_add_parms[key]
else:
LOG.warning("Ignoring unknown Job parameter %s specified "
"via legacy add_parms.", key)
def __str__(self):
"""String representation of this instance, for log/test purposes."""
parms = ', '.join(["%s=%s" % (key, self._parm_map[key])
for key in sorted(self._parm_map)])
return "%s(%s)" % (self.JOB_SUFFIX, parms)
def _process_enum(self, enum, value):
if value not in enum.ALL_VALUES:
raise exc.InvalidEnumValue(enum=enum.__name__, value=value,
valid_values=str(enum.ALL_VALUES))
self._parm_map[enum.KEY] = value
return self
def _process_bool(self, key, value):
"""Process a boolean option.
All boolean options are false by default. Thus, if value is 'true'/i
or True, the key is added with the value 'true'; otherwise it is
*removed* from the _PowerOpt.
:param key: The JobParameterName.
:param value: A bool (True/False) or string ('true', 'false',
case-insensitive). Default: True.
"""
if key in self._parm_map:
del self._parm_map[key]
if str(value).lower() == 'true':
self._parm_map[key] = 'true'
return self
def is_param_set(self, key):
"""Detect whether a parameter is set.
For some parameters, the absence of the key assumes a default behavior.
For example, is_immediate == False could mean the 'immediate' key is
entirely absent; or that it is present with a value of 'false'. This
method allows the consumer to distinguish between these two scenarios,
typically for the purpose of deciding whether to enact some default
behavior.
:param key: The key of the parameter in question.
:return: True if any value is set for the supplied key; False if that
key is absent from the parameter list.
"""
return key in self._parm_map
def bld_jparms(self):
return [job.Job.create_job_parameter(key, str(val)) for key, val in
six.iteritems(self._parm_map)]
class PowerOnOpts(_PowerOpts):
"""Job parameters for pypowervm.tasks.power.power_on/PowerOp.start."""
JOB_SUFFIX = 'PowerOn'
def bootmode(self, value):
"""Set the boot mode.
:param value: One of the BootMode enum values.
:return self: For chaining.
"""
return self._process_enum(BootMode, value)
def keylock_pos(self, value):
"""Set the Keylock Position.
:param value: One of the KeylockPos enum values.
:return self: For chaining.
"""
return self._process_enum(KeylockPos, value)
def bootstring(self, value):
"""Set the boot string.
:param value: The boot string to use.
:return self: For chaining.
"""
self._parm_map['bootstring'] = value
return self
def force(self, value=True):
"""Add the force option.
:param value: A bool (True/False) or string ('true', 'false',
case-insensitive). Default: True.
:return self: For chaining.
"""
return self._process_bool('force', value)
def remove_optical(self, name, time=0):
"""Add options to remove an optical drive after boot.
:param name: The name of a VirtualOpticalMedia name to remove.
:param time: The time, in minutes, to wait before deleting the media.
:return self: For chaining.
"""
self._parm_map.update(RemoveOptical.bld_map(name, time=time))
return self
def ibmi_ipl_source(self, value):
"""Set the IBMi IPL Source.
:param value: One of the IPLSrc enum values.
:return self: For chaining.
"""
return self._process_enum(IPLSrc, value)
def ibmi_op_type(self, value):
"""Set the IBMi Operation Type.
:param value: One of the IBMiOperationType enum values.
:return self: For chaining.
"""
return self._process_enum(IBMiOperationType, value)
def ibmi_netboot_params(self, ipaddr, serverip, gateway, serverdir,
subnet=None, connspeed=None, duplex=None, mtu=None,
vlanid=None):
"""Set parameters for IBMi netboot.
Use with ibmi_op_type(IBMiOperationType.NETBOOT).
:param ipaddr: IP (v4 or v6) address of the client VM.
:param serverip: IP (v4 or v6) address of the netboot server.
:param gateway: IP (v4 or v6) address of the gateway.
:param serverdir: Location of the netboot image on the server.
:param subnet: Subnet mask. IPv4 only.
:param connspeed: Connection speed.
:param duplex: Duplex mode.
:param mtu: Maximum Transmission Unit.
:param vlanid: VLAN ID.
:return self: For chaining.
"""
self._parm_map['IPAddress'] = ipaddr
self._parm_map['ServerIPAddress'] = serverip
self._parm_map['Gateway'] = gateway
self._parm_map['IBMiImageServerDirectory'] = serverdir
# Optional args
for key, val in (('SubnetMask', subnet),
('ConnectionSpeed', connspeed),
('DuplexMode', duplex),
('VLANID', vlanid),
('MaximumTransmissionUnit', mtu)):
if val is not None:
# connspeed/vlanid/mtu may arrive as ints
self._parm_map[key] = str(val)
return self
class PowerOffOpts(_PowerOpts):
"""Job parameters for pypowervm.tasks.power.power_off/PowerOp.stop.
Use *one* of os_normal, os_immediate, vsp_normal, vsp_hard, or soft_detect.
Optionally specify restart.
"""
JOB_SUFFIX = 'PowerOff'
valid_param_keys = {'operation', 'immediate', 'restart'}
def immediate(self, value=True):
"""Whether to include immediate=true.
This corresponds to "hard" for VSP, "immediate" for OS.
This should only be used with operation(DUMPRESTART). Otherwise, one
of the os_normal, os_immediate, vsp_normal, vsp_hard, or soft_detect
methods should be used.
:param value: A bool (True/False) or string ('true', 'false',
case-insensitive).
:return self: For chaining.
"""
return self._process_bool('immediate', value)
@property
def is_immediate(self):
return self._parm_map.get('immediate') == 'true'
def operation(self, value):
"""The PowerOff operation to perform.
This should only be used for DUMPRESTART. Otherwise, one of the
os_normal, os_immediate, vsp_normal, vsp_hard, or soft_detect methods
should be used.
:param value: One of the PowerOffOperation enum values.
:return self: For chaining.
"""
return self._process_enum(PowerOffOperation, value)
@staticmethod
def can_os_shutdown(part):
"""Can the specified partition perform an OS shutdown?
:param part: LPAR/VIOS wrapper indicating the partition to inspect.
:return: True if the specified partition is capable of OS shutdown;
False otherwise.
"""
# OS shutdown is always available on IBMi partitions.
# OS shutdown is available if RMC is up.
return (part.env == bp.LPARType.OS400) or (part.rmc_state ==
bp.RMCState.ACTIVE)
@property
def is_os(self):
return self._parm_map.get('operation') == PowerOffOperation.OS
def restart(self, value=True):
"""Whether to restart the partition after power-off.
:param value: A bool (True/False) or string ('true', 'false',
case-insensitive). Default: True.
:return self: For chaining.
"""
return self._process_bool('restart', value)
@property
def is_restart(self):
return self._parm_map.get('restart') == 'true'
def os_normal(self):
"""Set up normal OS shutdown.
Sends the 'shutdown' command to the operating system.
:return self: For chaining.
"""
return self.operation(PowerOffOperation.OS).immediate(value=False)
def os_immediate(self):
"""Set up immediate OS shutdown.
Sends the 'shutdown -t now' command to the operating system.
:return self: For chaining.
"""
return self.operation(PowerOffOperation.OS).immediate()
def vsp_normal(self):
"""Set up normal VSP shutdown.
The Virtual Service Processor sends the equivalent of an EPOW event to
the operating system. The result is OS-dependent.
:return self: For chaining.
"""
return self.operation(PowerOffOperation.VSP).immediate(value=False)
def vsp_hard(self):
"""Set up hard VSP shutdown.
Akin to pulling the plug from the partition. Processors are stopped
immediately, and any pending I/O is lost. May result in data
corruption.
:return self: For chaining.
"""
return self.operation(PowerOffOperation.VSP).immediate()
def soft_detect(self, part, immed_if_os=None):
"""Determine the appropriate soft shutdown operation for a partition.
For IBMi partitions, this will always set up an OS shutdown.
For non-IBMi partitions with active RMC, this will set up an OS
shutdown.
For non-IBMi partitions without RMC, this will set up a *normal* VSP
shutdown.
:param part: LPAR or VIOS wrapper indicating the partition being shut
down.
:param immed_if_os: If this method determines that an OS shutdown is to
be performed, this parameter indicates whether that
shutdown should be immediate (True) or not (False).
The default is False for IBMi partitions, and True
for non-IBMi partitions. This parameter is ignored
if a VSP shutdown is detected.
:return self: For chaining.
"""
if self.can_os_shutdown(part):
self.operation(PowerOffOperation.OS)
# Specific 'immediate' behavior requested for OS shutdown?
if immed_if_os is not None:
self.immediate(value=immed_if_os)
else:
# Default is normal for IBMi, immediate for non-IBMi.
self.immediate(value=part.env != bp.LPARType.OS400)
else:
# OS shutdown not available; perform *normal* VSP shutdown.
self.vsp_normal()
return self
|