FIX: Allow configuration of IPMI privilege level
[ta/cm-plugins.git] / inventoryhandlers / hwinventory / hwinventory.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 #pylint: disable=missing-docstring,invalid-name,too-few-public-methods
16 import os
17 import json
18 import string
19 from jinja2 import Environment
20 from cmframework.apis import cmansibleinventoryconfig
21 from cmframework.apis import cmerror
22 from cmdatahandlers.api import configerror
23 import hw_detector.hw_detect_lib as hw
24
25 JSON_HW_HOST_VAR = """
26 {
27     {% for host in hosts %}
28     "{{ host.name }}": {
29          "vendor": "{{ host.vendor }}",
30          "product_family": "{{ host.product_family }}",
31          "mgmt_mac": "{{ host.mgmt_mac }}"
32     } {% if not loop.last %},{% endif %}
33     {% endfor %}
34 }
35 """
36 class Host(object):
37     def __init__(self, name):
38         self.name = name
39         self.vendor = None
40         self.product_family = None
41
42 class hwinventory(cmansibleinventoryconfig.CMAnsibleInventoryConfigPlugin):
43     def __init__(self, confman, inventory, ownhost):
44         super(hwinventory, self).__init__(confman, inventory, ownhost)
45         self.host_objects = []
46         self._hosts_config_handler = self.confman.get_hosts_config_handler()
47
48     def handle_bootstrapping(self):
49         self.handle()
50
51     def handle_provisioning(self):
52         self.handle()
53
54     def handle_setup(self):
55         pass
56
57     def handle_postconfig(self):
58         self.handle()
59
60     def handle(self):
61         self._set_hw_types()
62         self._add_hw_config()
63
64
65     def _add_hw_config(self):
66         try:
67             text = Environment().from_string(JSON_HW_HOST_VAR).render(
68                 hosts=self.host_objects)
69             inventory = json.loads(text)
70             self.add_global_var("hw_inventory_details", inventory)
71 #            for host in inventory.keys():
72 #                for var, value in inventory[host].iteritems():
73 #                    self.add_host_var(host, var, value)
74         except Exception as exp:
75             raise cmerror.CMError(str(exp))
76
77     def _get_hw_type_of_host(self, name):
78         hwmgmt_addr = self._hosts_config_handler.get_hwmgmt_ip(name)
79         hwmgmt_user = self._hosts_config_handler.get_hwmgmt_user(name)
80         hwmgmt_pass = self._hosts_config_handler.get_hwmgmt_password(name)
81         hwmgmt_priv_level = self._hosts_config_handler.get_hwmgmt_priv_level(name)
82         return hw.get_hw_data(hwmgmt_addr, hwmgmt_user, hwmgmt_pass, hwmgmt_priv_level)
83
84     def _set_hw_types(self):
85         hosts = self._hosts_config_handler.get_hosts()
86         for host in hosts:
87             host_object = Host(host)
88             hw_details = self._get_hw_type_of_host(host)
89             host_object.vendor = hw_details.get("vendor", "Unknown")
90             host_object.product_family = hw_details.get("product_family", "Unknown")
91             host_object.mgmt_mac = hw_details.get('info', {}).get("MAC Address", "00:00:00:00:00:00")
92             self.host_objects.append(host_object)