koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 1 | from typing import Any, Dict |
| 2 | from urllib.parse import urlparse |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame^] | 3 | |
| 4 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 5 | from .keystone import KeystoneAuth |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 6 | |
| 7 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 8 | def total_lab_info(data: Dict[str, Any]) -> Dict[str, Any]: |
gstepanov | 1fbb71d | 2015-04-14 19:56:23 +0300 | [diff] [blame] | 9 | lab_data = {} |
| 10 | lab_data['nodes_count'] = len(data['nodes']) |
| 11 | lab_data['total_memory'] = 0 |
| 12 | lab_data['total_disk'] = 0 |
| 13 | lab_data['processor_count'] = 0 |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 14 | |
| 15 | for node in data['nodes']: |
gstepanov | 1fbb71d | 2015-04-14 19:56:23 +0300 | [diff] [blame] | 16 | lab_data['total_memory'] += node['memory']['total'] |
| 17 | lab_data['processor_count'] += len(node['processors']) |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 18 | |
| 19 | for disk in node['disks']: |
gstepanov | 1fbb71d | 2015-04-14 19:56:23 +0300 | [diff] [blame] | 20 | lab_data['total_disk'] += disk['size'] |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 21 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 22 | def to_gb(x): |
| 23 | return x / (1024 ** 3) |
| 24 | |
koder aka kdanilov | a047e1b | 2015-04-21 23:16:59 +0300 | [diff] [blame] | 25 | lab_data['total_memory'] = to_gb(lab_data['total_memory']) |
| 26 | lab_data['total_disk'] = to_gb(lab_data['total_disk']) |
gstepanov | 1fbb71d | 2015-04-14 19:56:23 +0300 | [diff] [blame] | 27 | return lab_data |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 28 | |
| 29 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 30 | def collect_lab_data(url: str, cred: Dict[str, str]) -> Dict[str, Any]: |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 31 | u = urlparse(url) |
| 32 | keystone = KeystoneAuth(root_url=url, creds=cred, admin_node_ip=u.hostname) |
| 33 | lab_info = keystone.do(method='get', path="/api/nodes") |
| 34 | fuel_version = keystone.do(method='get', path="/api/version/") |
| 35 | |
| 36 | nodes = [] |
| 37 | result = {} |
| 38 | |
| 39 | for node in lab_info: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame^] | 40 | # TODO(koder): give p,i,d,... vars meaningful names |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 41 | d = {} |
| 42 | d['name'] = node['name'] |
| 43 | p = [] |
| 44 | i = [] |
| 45 | disks = [] |
| 46 | devices = [] |
| 47 | |
| 48 | for processor in node['meta']['cpu']['spec']: |
gstepanov | dae5712 | 2015-03-02 20:50:41 +0200 | [diff] [blame] | 49 | p.append(processor) |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 50 | |
| 51 | for iface in node['meta']['interfaces']: |
| 52 | i.append(iface) |
| 53 | |
| 54 | m = node['meta']['memory'].copy() |
| 55 | |
| 56 | for disk in node['meta']['disks']: |
| 57 | disks.append(disk) |
| 58 | |
| 59 | d['memory'] = m |
| 60 | d['disks'] = disks |
| 61 | d['devices'] = devices |
| 62 | d['interfaces'] = i |
| 63 | d['processors'] = p |
| 64 | |
| 65 | nodes.append(d) |
| 66 | |
| 67 | result['nodes'] = nodes |
gstepanov | ff25b5c | 2015-03-02 20:48:17 +0200 | [diff] [blame] | 68 | result['fuel_version'] = fuel_version['release'] |
| 69 | |
gstepanov | dae5712 | 2015-03-02 20:50:41 +0200 | [diff] [blame] | 70 | return result |