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 import hw_detector.hw_ipmi_lib as hw_ipmi_lib
16 import hw_detector.hw_types as hw_types
23 for hw in hw_types.types:
24 amount = hw.match(info)
26 matches[hw.hw_type] = amount
29 return sorted(matches, key=matches.get, reverse=True)[0]
33 def get_info(ipmi_addr=None, ipmi_user=None, ipmi_pass=None, detect_virtual=True):
35 if is_virtual(detect_virtual):
36 return {'Board Product' : 'VIRTUAL'}
37 if info.get(ipmi_addr, False):
38 return info[ipmi_addr]
40 info[ipmi_addr] = hw_ipmi_lib.get_ipmi_info(ipmi_addr, ipmi_user, ipmi_pass)
42 return hw_ipmi_lib.get_local_ipmi_info()
43 if ipmi_addr and not info.get(ipmi_addr):
46 return info[ipmi_addr]
48 def get_type(ipmi_addr=None, ipmi_user=None, ipmi_pass=None, detect_virtual=True):
49 info = get_info(ipmi_addr, ipmi_user, ipmi_pass, detect_virtual)
53 matches = get_match(info)
56 def is_virtual(detect_virtual):
57 if not detect_virtual:
59 with open('/proc/cpuinfo') as f:
60 for line in f.readlines():
61 if line.startswith('flags') and 'hypervisor' in line:
65 def _get_hw_with_hw_type(hw_type):
66 for hw in hw_types.types:
67 if hw.hw_type == hw_type:
70 def get_product_family(hw_type):
71 hw = _get_hw_with_hw_type(hw_type)
73 return hw.product_family
77 def get_vendor(hw_type):
78 hw = _get_hw_with_hw_type(hw_type)
83 def hd_with_type(hw_type, hd_type):
84 hw = _get_hw_with_hw_type(hw_type)
86 return hw.get_disk_by_name(hd_type)
87 #Default behaviour hardware that is not detected
91 disk = hd_with_type(hw_type, 'os')
92 return disk if disk else '/dev/sda'