blob: 19fdad98533135993fb2c37f5a40bf590f8bbbaf [file] [log] [blame]
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03001from typing import Any, Dict
2from urllib.parse import urlparse
3from .keystone import KeystoneAuth
gstepanovff25b5c2015-03-02 20:48:17 +02004
5
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03006def total_lab_info(data: Dict[str, Any]) -> Dict[str, Any]:
gstepanov1fbb71d2015-04-14 19:56:23 +03007 lab_data = {}
8 lab_data['nodes_count'] = len(data['nodes'])
9 lab_data['total_memory'] = 0
10 lab_data['total_disk'] = 0
11 lab_data['processor_count'] = 0
gstepanovff25b5c2015-03-02 20:48:17 +020012
13 for node in data['nodes']:
gstepanov1fbb71d2015-04-14 19:56:23 +030014 lab_data['total_memory'] += node['memory']['total']
15 lab_data['processor_count'] += len(node['processors'])
gstepanovff25b5c2015-03-02 20:48:17 +020016
17 for disk in node['disks']:
gstepanov1fbb71d2015-04-14 19:56:23 +030018 lab_data['total_disk'] += disk['size']
gstepanovff25b5c2015-03-02 20:48:17 +020019
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030020 def to_gb(x):
21 return x / (1024 ** 3)
22
koder aka kdanilova047e1b2015-04-21 23:16:59 +030023 lab_data['total_memory'] = to_gb(lab_data['total_memory'])
24 lab_data['total_disk'] = to_gb(lab_data['total_disk'])
gstepanov1fbb71d2015-04-14 19:56:23 +030025 return lab_data
gstepanovff25b5c2015-03-02 20:48:17 +020026
27
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030028def collect_lab_data(url: str, cred: Dict[str, str]) -> Dict[str, Any]:
gstepanovff25b5c2015-03-02 20:48:17 +020029 u = urlparse(url)
30 keystone = KeystoneAuth(root_url=url, creds=cred, admin_node_ip=u.hostname)
31 lab_info = keystone.do(method='get', path="/api/nodes")
32 fuel_version = keystone.do(method='get', path="/api/version/")
33
34 nodes = []
35 result = {}
36
37 for node in lab_info:
38 # <koder>: give p,i,d,... vars meaningful names
39 d = {}
40 d['name'] = node['name']
41 p = []
42 i = []
43 disks = []
44 devices = []
45
46 for processor in node['meta']['cpu']['spec']:
gstepanovdae57122015-03-02 20:50:41 +020047 p.append(processor)
gstepanovff25b5c2015-03-02 20:48:17 +020048
49 for iface in node['meta']['interfaces']:
50 i.append(iface)
51
52 m = node['meta']['memory'].copy()
53
54 for disk in node['meta']['disks']:
55 disks.append(disk)
56
57 d['memory'] = m
58 d['disks'] = disks
59 d['devices'] = devices
60 d['interfaces'] = i
61 d['processors'] = p
62
63 nodes.append(d)
64
65 result['nodes'] = nodes
gstepanovff25b5c2015-03-02 20:48:17 +020066 result['fuel_version'] = fuel_version['release']
67
gstepanovdae57122015-03-02 20:50:41 +020068 return result