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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 #pylint: disable=missing-docstring,invalid-name,too-few-public-methods
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
25 JSON_HW_HOST_VAR = """
27 {% for host in hosts %}
29 "vendor": "{{ host.vendor }}",
30 "product_family": "{{ host.product_family }}",
31 "mgmt_mac": "{{ host.mgmt_mac }}"
32 } {% if not loop.last %},{% endif %}
37 def __init__(self, name):
40 self.product_family = None
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()
48 def handle_bootstrapping(self):
51 def handle_provisioning(self):
54 def handle_setup(self):
57 def handle_postconfig(self):
65 def _add_hw_config(self):
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))
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)
84 def _set_hw_types(self):
85 hosts = self._hosts_config_handler.get_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)