2014-04-09 14:31:31 -07:00
|
|
|
# Copyright 2013 Rackspace, Inc.
|
|
|
|
#
|
|
|
|
# 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.
|
2014-04-04 17:31:23 +04:00
|
|
|
|
2015-03-16 01:20:02 -07:00
|
|
|
import copy
|
2015-03-09 12:23:12 +00:00
|
|
|
import glob
|
|
|
|
import os
|
2015-05-04 10:11:57 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2014-03-11 13:31:19 -07:00
|
|
|
|
2014-12-01 18:17:35 +02:00
|
|
|
from oslo_concurrency import processutils
|
2015-03-09 23:48:47 +01:00
|
|
|
from oslo_log import log as logging
|
2015-03-10 11:20:40 +00:00
|
|
|
from six.moves.urllib import parse
|
2014-12-01 18:17:35 +02:00
|
|
|
|
2015-03-09 12:23:12 +00:00
|
|
|
from ironic_python_agent import errors
|
2014-04-04 17:31:23 +04:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2014-03-11 13:31:19 -07:00
|
|
|
|
2015-03-10 11:20:40 +00:00
|
|
|
SUPPORTED_ROOT_DEVICE_HINTS = set(('size', 'model', 'wwn', 'serial', 'vendor'))
|
|
|
|
|
2015-03-16 01:20:02 -07:00
|
|
|
# Agent parameters can be pased by kernel command-line arguments and/or
|
|
|
|
# by virtual media. Virtual media parameters passed would be available
|
|
|
|
# when the agent is started, but might not be available for re-reading
|
|
|
|
# later on because:
|
|
|
|
# * Virtual media might be exposed from Swift and swift temp url might
|
|
|
|
# expire.
|
|
|
|
# * Ironic might have removed the floppy image from Swift after starting
|
|
|
|
# the deploy.
|
|
|
|
#
|
|
|
|
# Even if it's available, there is no need to re-read from the device and
|
|
|
|
# /proc/cmdline again, because it is never going to change. So we cache the
|
|
|
|
# agent parameters that was passed (by proc/cmdline and/or virtual media)
|
|
|
|
# when we read it for the first time, and then use this cache.
|
|
|
|
AGENT_PARAMS_CACHED = dict()
|
|
|
|
|
2015-03-10 11:20:40 +00:00
|
|
|
|
2014-04-04 17:31:23 +04:00
|
|
|
def execute(*cmd, **kwargs):
|
|
|
|
"""Convenience wrapper around oslo's execute() method."""
|
|
|
|
result = processutils.execute(*cmd, **kwargs)
|
2015-08-01 11:50:58 +03:00
|
|
|
LOG.debug('Execution completed, command line is "%s"', ' '.join(cmd))
|
|
|
|
LOG.debug('Command stdout is: "%s"', result[0])
|
|
|
|
LOG.debug('Command stderr is: "%s"', result[1])
|
2014-04-04 17:31:23 +04:00
|
|
|
return result
|
2015-03-09 12:23:12 +00:00
|
|
|
|
|
|
|
|
2015-08-06 13:03:27 +02:00
|
|
|
def try_execute(*cmd, **kwargs):
|
|
|
|
"""The same as execute but returns None on error."""
|
|
|
|
try:
|
|
|
|
return execute(*cmd, **kwargs)
|
|
|
|
except (processutils.ProcessExecutionError, OSError) as e:
|
|
|
|
LOG.debug('Command failed: %s', e)
|
|
|
|
|
|
|
|
|
2015-03-09 12:23:12 +00:00
|
|
|
def _read_params_from_file(filepath):
|
|
|
|
"""Extract key=value pairs from a file.
|
|
|
|
|
|
|
|
:param filepath: path to a file containing key=value pairs separated by
|
|
|
|
whitespace or newlines.
|
|
|
|
:returns: a dictionary representing the content of the file
|
|
|
|
"""
|
|
|
|
with open(filepath) as f:
|
|
|
|
cmdline = f.read()
|
|
|
|
|
|
|
|
options = cmdline.split()
|
|
|
|
params = {}
|
|
|
|
for option in options:
|
|
|
|
if '=' not in option:
|
|
|
|
continue
|
|
|
|
k, v = option.split('=', 1)
|
|
|
|
params[k] = v
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def _get_vmedia_device():
|
|
|
|
"""Finds the device filename of the virtual media device using sysfs.
|
|
|
|
|
|
|
|
:returns: a string containing the filename of the virtual media device
|
|
|
|
"""
|
|
|
|
sysfs_device_models = glob.glob("/sys/class/block/*/device/model")
|
|
|
|
vmedia_device_model = "virtual media"
|
|
|
|
for model_file in sysfs_device_models:
|
|
|
|
try:
|
|
|
|
with open(model_file) as model_file_fobj:
|
|
|
|
if vmedia_device_model in model_file_fobj.read().lower():
|
|
|
|
vmedia_device = model_file.split('/')[4]
|
|
|
|
return vmedia_device
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def _get_vmedia_params():
|
2015-06-03 16:56:50 -07:00
|
|
|
"""This method returns the parameters passed through virtual media floppy.
|
2015-03-09 12:23:12 +00:00
|
|
|
|
|
|
|
:returns: a partial dict of potential agent configuration parameters
|
|
|
|
:raises: VirtualMediaBootError when it cannot find the virtual media device
|
|
|
|
"""
|
|
|
|
parameters_file = "parameters.txt"
|
|
|
|
|
2015-03-12 04:14:11 +00:00
|
|
|
vmedia_device_file = "/dev/disk/by-label/ir-vfd-dev"
|
|
|
|
if not os.path.exists(vmedia_device_file):
|
|
|
|
|
|
|
|
# TODO(rameshg87): This block of code is there only for compatibility
|
|
|
|
# reasons (so that newer agent can work with older Ironic). Remove
|
|
|
|
# this after Liberty release.
|
|
|
|
vmedia_device = _get_vmedia_device()
|
|
|
|
if not vmedia_device:
|
|
|
|
msg = "Unable to find virtual media device"
|
|
|
|
raise errors.VirtualMediaBootError(msg)
|
|
|
|
|
|
|
|
vmedia_device_file = os.path.join("/dev", vmedia_device)
|
2015-03-09 12:23:12 +00:00
|
|
|
|
2015-05-04 10:11:57 +00:00
|
|
|
vmedia_mount_point = tempfile.mkdtemp()
|
2015-03-09 12:23:12 +00:00
|
|
|
try:
|
2015-05-04 10:11:57 +00:00
|
|
|
try:
|
|
|
|
stdout, stderr = execute("mount", vmedia_device_file,
|
|
|
|
vmedia_mount_point)
|
|
|
|
except processutils.ProcessExecutionError as e:
|
|
|
|
msg = ("Unable to mount virtual media device %(device)s: "
|
|
|
|
"%(error)s" % {'device': vmedia_device_file, 'error': e})
|
|
|
|
raise errors.VirtualMediaBootError(msg)
|
2015-03-09 12:23:12 +00:00
|
|
|
|
2015-05-04 10:11:57 +00:00
|
|
|
parameters_file_path = os.path.join(vmedia_mount_point,
|
|
|
|
parameters_file)
|
|
|
|
params = _read_params_from_file(parameters_file_path)
|
2015-03-09 12:23:12 +00:00
|
|
|
|
2015-05-04 10:11:57 +00:00
|
|
|
try:
|
|
|
|
stdout, stderr = execute("umount", vmedia_mount_point)
|
|
|
|
except processutils.ProcessExecutionError as e:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
shutil.rmtree(vmedia_mount_point)
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2015-03-09 12:23:12 +00:00
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
2015-03-16 01:20:02 -07:00
|
|
|
def _get_cached_params():
|
|
|
|
"""Helper method to get cached params to ease unit testing."""
|
|
|
|
return AGENT_PARAMS_CACHED
|
|
|
|
|
|
|
|
|
|
|
|
def _set_cached_params(params):
|
|
|
|
"""Helper method to set cached params to ease unit testing."""
|
|
|
|
global AGENT_PARAMS_CACHED
|
|
|
|
AGENT_PARAMS_CACHED = params
|
|
|
|
|
|
|
|
|
2015-03-09 12:23:12 +00:00
|
|
|
def get_agent_params():
|
|
|
|
"""Gets parameters passed to the agent via kernel cmdline or vmedia.
|
|
|
|
|
|
|
|
Parameters can be passed using either the kernel commandline or through
|
|
|
|
virtual media. If boot_method is vmedia, merge params provided via vmedia
|
|
|
|
with those read from the kernel command line.
|
|
|
|
|
|
|
|
Although it should never happen, if a variable is both set by vmedia and
|
|
|
|
kernel command line, the setting in vmedia will take precedence.
|
|
|
|
|
|
|
|
:returns: a dict of potential configuration parameters for the agent
|
|
|
|
"""
|
|
|
|
|
2015-03-16 01:20:02 -07:00
|
|
|
# Check if we have the parameters cached
|
|
|
|
params = _get_cached_params()
|
|
|
|
if not params:
|
|
|
|
params = _read_params_from_file('/proc/cmdline')
|
2015-03-09 12:23:12 +00:00
|
|
|
|
2015-03-16 01:20:02 -07:00
|
|
|
# If the node booted over virtual media, the parameters are passed
|
|
|
|
# in a text file within the virtual media floppy.
|
|
|
|
if params.get('boot_method') == 'vmedia':
|
|
|
|
vmedia_params = _get_vmedia_params()
|
|
|
|
params.update(vmedia_params)
|
|
|
|
|
|
|
|
# Cache the parameters so that it can be used later on.
|
|
|
|
_set_cached_params(params)
|
|
|
|
|
|
|
|
return copy.deepcopy(params)
|
2015-03-10 11:20:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def normalize(string):
|
|
|
|
"""Return a normalized string."""
|
|
|
|
# Since we can't use space on the kernel cmdline, Ironic will
|
|
|
|
# urlencode the values.
|
|
|
|
return parse.unquote(string).lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
def parse_root_device_hints():
|
|
|
|
"""Parse the root device hints.
|
|
|
|
|
|
|
|
Parse the root device hints given by Ironic via kernel cmdline
|
|
|
|
or vmedia.
|
|
|
|
|
|
|
|
:returns: A dict with the hints or an empty dict if no hints are
|
|
|
|
passed.
|
|
|
|
:raises: DeviceNotFound if there are unsupported hints.
|
|
|
|
|
|
|
|
"""
|
|
|
|
root_device = get_agent_params().get('root_device')
|
|
|
|
if not root_device:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
hints = dict((item.split('=') for item in root_device.split(',')))
|
|
|
|
|
|
|
|
# Find invalid hints for logging
|
|
|
|
not_supported = set(hints) - SUPPORTED_ROOT_DEVICE_HINTS
|
|
|
|
if not_supported:
|
|
|
|
error_msg = ('No device can be found because the following hints: '
|
|
|
|
'"%(not_supported)s" are not supported by this version '
|
|
|
|
'of IPA. Supported hints are: "%(supported)s"',
|
|
|
|
{'not_supported': ', '.join(not_supported),
|
|
|
|
'supported': ', '.join(SUPPORTED_ROOT_DEVICE_HINTS)})
|
|
|
|
raise errors.DeviceNotFound(error_msg)
|
|
|
|
|
|
|
|
# Normalise the values
|
2015-07-15 16:08:10 +01:00
|
|
|
hints = {k: normalize(v) for k, v in hints.items()}
|
2015-03-10 11:20:40 +00:00
|
|
|
|
|
|
|
if 'size' in hints:
|
|
|
|
# NOTE(lucasagomes): Ironic should validate before passing to
|
|
|
|
# the deploy ramdisk
|
|
|
|
hints['size'] = int(hints['size'])
|
|
|
|
|
|
|
|
return hints
|