Seed code for ipa-deployer
[ta/ipa-deployer.git] / work / dib-ipa-element / virtmedia-netconf / ironic-bmc-hardware-manager / src / ironic_bmc_hardware_manager / bmc.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 import os
17
18 from ironic_python_agent import hardware
19 from ironic_python_agent import utils
20
21 from oslo_log import log
22 from oslo_concurrency import processutils
23
24
25 LOG = log.getLogger()
26
27
28 class BMCHardwareManager(hardware.GenericHardwareManager):
29     HARDWARE_MANAGER_NAME = 'BMCHardwareManager'
30     HARDWARE_MANAGER_VERSION = '1'
31
32     def evaluate_hardware_support(self):
33         """Declare level of hardware support provided."""
34
35         LOG.info('Running in BMC environment')
36         return hardware.HardwareSupport.SERVICE_PROVIDER
37
38     def list_network_interfaces(self):
39         network_interfaces_list = []
40
41         bmc_mac = self.get_ipmi_info().get('MAC Address', False)
42         if bmc_mac:
43             LOG.info("Adding MAC address net interfaces %s", bmc_mac)
44             bmc_address = self.get_bmc_address()
45             network_interfaces_list.append(hardware.NetworkInterface(
46                 name="BMC_INTERFACE",
47                 mac_addr=bmc_mac,
48                 ipv4_address=bmc_address,
49                 has_carrier=True,
50                 vendor="BMC",
51                 product="Akraino"))
52
53         else:
54             network_interfaces_list = super(BMCHardwareManager, self).list_network_interfaces()
55         return network_interfaces_list
56
57     def get_ipmi_info(self):
58         # These modules are rarely loaded automatically
59         utils.try_execute('modprobe', 'ipmi_msghandler')
60         utils.try_execute('modprobe', 'ipmi_devintf')
61         utils.try_execute('modprobe', 'ipmi_si')
62
63         try:
64             out, _e = utils.execute(
65                 "ipmitool lan print", shell=True, attempts=2)
66         except (processutils.ProcessExecutionError, OSError) as e:
67             # Not error, because it's normal in virtual environment
68             LOG.warning("Cannot get BMC info: %s", e)
69             return {}
70
71         info = {}
72         for line in out.split('\n'):
73             spl = line.find(':')
74             if spl == -1:
75                 continue
76             else:
77                 key = line[0:spl].strip()
78                 if key == '':
79                     continue
80                 info[line[0:spl].strip()] = line[spl+1:].strip()
81         return info
82