2014-01-07 15:58:25 -08: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-01-22 11:09:02 -08:00
|
|
|
import abc
|
2014-01-27 16:05:04 -08:00
|
|
|
import os
|
2014-03-05 16:29:58 -08:00
|
|
|
import subprocess
|
2014-01-22 11:09:02 -08:00
|
|
|
|
2014-01-22 11:53:46 -08:00
|
|
|
import stevedore
|
2014-01-21 17:14:40 -08:00
|
|
|
|
2014-03-19 16:19:52 -07:00
|
|
|
from ironic_python_agent import encoding
|
|
|
|
from ironic_python_agent.openstack.common import log
|
|
|
|
from ironic_python_agent import utils
|
2014-03-11 13:31:19 -07:00
|
|
|
|
2014-01-22 11:09:02 -08:00
|
|
|
_global_manager = None
|
|
|
|
|
|
|
|
|
|
|
|
class HardwareSupport(object):
|
|
|
|
"""These are just guidelines to suggest values that might be returned by
|
|
|
|
calls to `evaluate_hardware_support`. No HardwareManager in mainline
|
2014-03-19 16:19:52 -07:00
|
|
|
ironic-python-agent will ever offer a value greater than `MAINLINE`.
|
|
|
|
Service Providers should feel free to return values greater than
|
|
|
|
SERVICE_PROVIDER to distinguish between additional levels of support.
|
2014-01-22 11:09:02 -08:00
|
|
|
"""
|
|
|
|
NONE = 0
|
|
|
|
GENERIC = 1
|
|
|
|
MAINLINE = 2
|
|
|
|
SERVICE_PROVIDER = 3
|
|
|
|
|
|
|
|
|
2014-01-28 11:24:16 -08:00
|
|
|
class HardwareType(object):
|
|
|
|
MAC_ADDRESS = 'mac_address'
|
|
|
|
|
|
|
|
|
|
|
|
class HardwareInfo(encoding.Serializable):
|
|
|
|
def __init__(self, type, id):
|
|
|
|
self.type = type
|
|
|
|
self.id = id
|
|
|
|
|
2014-03-17 10:58:39 -07:00
|
|
|
def serialize(self):
|
2014-03-11 13:31:19 -07:00
|
|
|
return utils.get_ordereddict([
|
2014-01-28 11:24:16 -08:00
|
|
|
('type', self.type),
|
|
|
|
('id', self.id),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2014-01-24 14:57:55 -08:00
|
|
|
class BlockDevice(object):
|
|
|
|
def __init__(self, name, size, start_sector):
|
|
|
|
self.name = name
|
|
|
|
self.size = size
|
|
|
|
self.start_sector = start_sector
|
|
|
|
|
|
|
|
|
2014-01-27 16:05:04 -08:00
|
|
|
class NetworkInterface(object):
|
|
|
|
def __init__(self, name, mac_addr):
|
|
|
|
self.name = name
|
|
|
|
self.mac_address = mac_addr
|
|
|
|
# TODO(russellhaering): Pull these from LLDP
|
|
|
|
self.switch_port_descr = None
|
2014-01-27 16:22:23 -08:00
|
|
|
self.switch_chassis_descr = None
|
2014-01-27 16:05:04 -08:00
|
|
|
|
|
|
|
|
2014-01-22 11:09:02 -08:00
|
|
|
class HardwareManager(object):
|
|
|
|
@abc.abstractmethod
|
|
|
|
def evaluate_hardware_support(cls):
|
|
|
|
pass
|
2014-01-21 17:14:40 -08:00
|
|
|
|
2014-01-22 11:09:02 -08:00
|
|
|
@abc.abstractmethod
|
2014-01-27 16:05:04 -08:00
|
|
|
def list_network_interfaces(self):
|
2014-01-22 11:09:02 -08:00
|
|
|
pass
|
|
|
|
|
2014-01-22 17:29:01 -08:00
|
|
|
@abc.abstractmethod
|
|
|
|
def get_os_install_device(self):
|
|
|
|
pass
|
|
|
|
|
2014-01-28 11:24:16 -08:00
|
|
|
def list_hardware_info(self):
|
|
|
|
hardware_info = []
|
|
|
|
for interface in self.list_network_interfaces():
|
|
|
|
hardware_info.append(HardwareInfo(HardwareType.MAC_ADDRESS,
|
|
|
|
interface.mac_address))
|
|
|
|
return hardware_info
|
|
|
|
|
2014-01-22 11:09:02 -08:00
|
|
|
|
|
|
|
class GenericHardwareManager(HardwareManager):
|
2014-01-27 16:05:04 -08:00
|
|
|
def __init__(self):
|
|
|
|
self.sys_path = '/sys'
|
|
|
|
|
|
|
|
if os.path.isdir('/mnt/sys'):
|
|
|
|
self.sys_path = '/mnt/sys'
|
|
|
|
|
2014-01-22 11:09:02 -08:00
|
|
|
def evaluate_hardware_support(cls):
|
|
|
|
return HardwareSupport.GENERIC
|
2014-01-07 15:58:25 -08:00
|
|
|
|
2014-01-27 16:05:04 -08:00
|
|
|
def _get_interface_info(self, interface_name):
|
2014-03-11 13:31:19 -07:00
|
|
|
addr_path = '{0}/class/net/{1}/address'.format(self.sys_path,
|
2014-01-27 16:05:04 -08:00
|
|
|
interface_name)
|
|
|
|
addr_file = open(addr_path, 'r')
|
|
|
|
mac_addr = addr_file.read().strip()
|
|
|
|
return NetworkInterface(interface_name, mac_addr)
|
|
|
|
|
2014-01-27 18:11:29 -08:00
|
|
|
def _is_device(self, interface_name):
|
2014-03-11 13:31:19 -07:00
|
|
|
device_path = '{0}/class/net/{1}/device'.format(self.sys_path,
|
2014-01-27 18:11:29 -08:00
|
|
|
interface_name)
|
|
|
|
return os.path.exists(device_path)
|
|
|
|
|
2014-01-27 16:05:04 -08:00
|
|
|
def list_network_interfaces(self):
|
2014-03-11 13:31:19 -07:00
|
|
|
iface_names = os.listdir('{0}/class/net'.format(self.sys_path))
|
2014-01-27 18:11:29 -08:00
|
|
|
return [self._get_interface_info(name)
|
|
|
|
for name in iface_names
|
|
|
|
if self._is_device(name)]
|
2014-01-21 17:14:40 -08:00
|
|
|
|
2014-03-05 16:29:58 -08:00
|
|
|
def _cmd(self, command):
|
|
|
|
process = subprocess.Popen(command, stdout=subprocess.PIPE)
|
|
|
|
return process.communicate()
|
2014-01-24 14:57:55 -08:00
|
|
|
|
|
|
|
def _list_block_devices(self):
|
2014-03-05 16:29:58 -08:00
|
|
|
report = self._cmd(['blockdev', '--report'])[0]
|
2014-01-24 14:57:55 -08:00
|
|
|
lines = report.split('\n')
|
2014-03-27 15:35:16 -07:00
|
|
|
lines = [line.split() for line in lines if line != '']
|
2014-01-24 14:57:55 -08:00
|
|
|
startsec_idx = lines[0].index('StartSec')
|
|
|
|
device_idx = lines[0].index('Device')
|
|
|
|
size_idx = lines[0].index('Size')
|
|
|
|
return [BlockDevice(line[device_idx],
|
|
|
|
int(line[size_idx]),
|
|
|
|
int(line[startsec_idx]))
|
|
|
|
for line
|
|
|
|
in lines[1:]]
|
|
|
|
|
2014-01-22 17:29:01 -08:00
|
|
|
def get_os_install_device(self):
|
2014-01-24 14:57:55 -08:00
|
|
|
# Assume anything with a start sector other than 0, is a partition
|
|
|
|
block_devices = [device for device in self._list_block_devices()
|
|
|
|
if device.start_sector == 0]
|
|
|
|
|
|
|
|
# Find the first device larger than 4GB, assume it is the OS disk
|
|
|
|
# TODO(russellhaering): This isn't a valid assumption in all cases,
|
|
|
|
# is there a more reasonable default behavior?
|
|
|
|
block_devices.sort(key=lambda device: device.size)
|
|
|
|
for device in block_devices:
|
|
|
|
if device.size >= (4 * pow(1024, 3)):
|
|
|
|
return device.name
|
2014-01-22 17:29:01 -08:00
|
|
|
|
2014-01-21 17:14:40 -08:00
|
|
|
|
2014-01-22 11:09:02 -08:00
|
|
|
def _compare_extensions(ext1, ext2):
|
|
|
|
mgr1 = ext1.obj
|
|
|
|
mgr2 = ext2.obj
|
|
|
|
return mgr1.evaluate_hardware_support() - mgr2.evaluate_hardware_support()
|
2014-01-21 17:14:40 -08:00
|
|
|
|
|
|
|
|
2014-01-22 17:28:24 -08:00
|
|
|
def get_manager():
|
|
|
|
global _global_manager
|
|
|
|
|
|
|
|
if not _global_manager:
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG = log.getLogger()
|
2014-01-22 17:28:24 -08:00
|
|
|
extension_manager = stevedore.ExtensionManager(
|
2014-03-19 16:19:52 -07:00
|
|
|
namespace='ironic_python_agent.hardware_managers',
|
2014-01-22 17:28:24 -08:00
|
|
|
invoke_on_load=True)
|
|
|
|
|
|
|
|
# There will always be at least one extension available (the
|
|
|
|
# GenericHardwareManager).
|
|
|
|
preferred_extension = sorted(extension_manager, _compare_extensions)[0]
|
|
|
|
preferred_manager = preferred_extension.obj
|
|
|
|
|
|
|
|
if preferred_manager.evaluate_hardware_support() <= 0:
|
|
|
|
raise RuntimeError('No suitable HardwareManager could be found')
|
|
|
|
|
2014-03-17 15:17:27 -07:00
|
|
|
LOG.info('selected hardware manager {0}'.format(
|
|
|
|
preferred_extension.entry_point_target))
|
2014-01-22 17:28:24 -08:00
|
|
|
|
|
|
|
_global_manager = preferred_manager
|
|
|
|
|
|
|
|
return _global_manager
|