Add hardware type for Ampere Hawk server
[ta/hw-detector.git] / src / hw_detector / hw_utils.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 import hw_detector.hw_ipmi_lib as hw_ipmi_lib
16 import hw_detector.hw_types as hw_types
17
18 info = {}
19
20 def get_match(info):
21     matches = {}
22
23     for hw in hw_types.types:
24         amount = hw.match(info)
25         if amount != 0:
26             matches[hw.hw_type] = amount
27
28     if matches:
29         return sorted(matches, key=matches.get, reverse=True)[0]
30
31     return 'Unknown'
32
33 def get_info(ipmi_addr=None, ipmi_user=None, ipmi_pass=None, ipmi_priv_level='ADMINISTRATOR', detect_virtual=True):
34     global info
35     if is_virtual(detect_virtual):
36         return {'Board Product' : 'VIRTUAL'}
37     if info.get(ipmi_addr, False):
38         return info[ipmi_addr]
39     if ipmi_addr:
40         info[ipmi_addr] = hw_ipmi_lib.get_ipmi_info(ipmi_addr, ipmi_user, ipmi_pass, ipmi_priv_level)
41     else:
42         return hw_ipmi_lib.get_local_ipmi_info()
43     if ipmi_addr and not info.get(ipmi_addr):
44         info[ipmi_addr] = {}
45
46     return info[ipmi_addr]
47
48 def get_type(ipmi_addr=None, ipmi_user=None, ipmi_pass=None, ipmi_priv_level='ADMINISTRATOR', detect_virtual=True):
49     info = get_info(ipmi_addr, ipmi_user, ipmi_pass, ipmi_priv_level, detect_virtual)
50     if not info:
51         return 'Unknown'
52
53     matches = get_match(info)
54     return matches
55
56 def is_virtual(detect_virtual):
57     if not detect_virtual:
58         return False
59     with open('/proc/cpuinfo') as f:
60         for line in f.readlines():
61             if line.startswith('flags') and 'hypervisor' in  line:
62                 return True
63     return False
64
65 def _get_hw_with_hw_type(hw_type):
66     for hw in hw_types.types:
67         if hw.hw_type == hw_type:
68             return hw
69
70 def get_product_family(hw_type):
71     hw = _get_hw_with_hw_type(hw_type)
72     if hw:
73         return hw.product_family
74     return None
75
76
77 def get_vendor(hw_type):
78     hw = _get_hw_with_hw_type(hw_type)
79     if hw:
80         return hw.vendor
81     return None
82
83 def hd_with_type(hw_type, hd_type):
84     hw = _get_hw_with_hw_type(hw_type)
85     if hw:
86         return hw.get_disk_by_name(hd_type)
87     #Default behaviour hardware that is not detected
88     return None
89
90 def os_hd(hw_type):
91     disk = hd_with_type(hw_type, 'os')
92     return disk if disk else '/dev/sda'