2
# -*- coding: utf-8 -*-
4
# Copyright 2014-2015 Canonical Limited.
6
# This file is part of charm-helpers.
8
# charm-helpers is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License version 3 as
10
# published by the Free Software Foundation.
12
# charm-helpers is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
21
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
23
from charmhelpers.fetch import (
28
from charmhelpers.core.hookenv import (
34
from netifaces import interfaces as network_interfaces
36
apt_install('python-netifaces')
37
from netifaces import interfaces as network_interfaces
43
from charmhelpers.core.kernel import modprobe
77
ABI_VERSION_FILE = "/sys/class/infiniband_mad/abi_version"
80
class DeviceInfo(object):
84
def install_packages():
86
apt_install(REQUIRED_PACKAGES, fatal=True)
90
for module in REQUIRED_MODULES:
91
modprobe(module, persist=True)
95
"""Check if infiniband is loaded on the system"""
96
return os.path.exists(ABI_VERSION_FILE)
100
"""Return full output of ibstat"""
101
return subprocess.check_output(["ibstat"])
105
"""Returns a list of IB enabled devices"""
106
return subprocess.check_output(['ibstat', '-l']).splitlines()
109
def device_info(device):
110
"""Returns a DeviceInfo object with the current device settings"""
112
status = subprocess.check_output([
113
'ibstat', device, '-s']).splitlines()
116
"CA type: (.*)": "device_type",
117
"Number of ports: (.*)": "num_ports",
118
"Firmware version: (.*)": "fw_ver",
119
"Hardware version: (.*)": "hw_ver",
120
"Node GUID: (.*)": "node_guid",
121
"System image GUID: (.*)": "sys_guid",
124
device = DeviceInfo()
127
for expression, key in regexes.items():
128
matches = re.search(expression, line)
130
setattr(device, key, matches.group(1))
135
def ipoib_interfaces():
136
"""Return a list of IPOIB capable ethernet interfaces"""
139
for interface in network_interfaces():
141
driver = re.search('^driver: (.+)$', subprocess.check_output([
143
interface]), re.M).group(1)
145
if driver in IPOIB_DRIVERS:
146
interfaces.append(interface)
148
log("Skipping interface %s" % interface, level=INFO)