Merge "ansible: disable Extra cloud image kernel stub"
[yardstick.git] / yardstick / service / environment.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import tempfile
10 import logging
11 import collections
12
13 from oslo_serialization import jsonutils
14
15 from yardstick.service import Service
16 from yardstick.common.exceptions import MissingPodInfoError
17 from yardstick.common.exceptions import UnsupportedPodFormatError
18 from yardstick.common.ansible_common import AnsibleCommon
19
20 LOG = logging.getLogger(__name__)
21
22
23 class Environment(Service):
24     def __init__(self, pod=None):
25         super(Environment, self).__init__()
26         # pod can be a dict or a json format string
27         self.pod = pod
28
29     def get_sut_info(self):
30         temdir = tempfile.mkdtemp(prefix='sut')
31
32         nodes = self._load_pod_info()
33         ansible = AnsibleCommon(nodes=nodes)
34         ansible.gen_inventory_ini_dict()
35         sut_info = ansible.get_sut_info(temdir)
36
37         return self._format_sut_info(sut_info)
38
39     def _load_pod_info(self):
40         if self.pod is None:
41             raise MissingPodInfoError
42
43         if isinstance(self.pod, collections.Mapping):
44             try:
45                 return self.pod['nodes']
46             except KeyError:
47                 raise UnsupportedPodFormatError
48
49         try:
50             return jsonutils.loads(self.pod)['nodes']
51         except (ValueError, KeyError):
52             raise UnsupportedPodFormatError
53
54     def _format_sut_info(self, sut_info):
55         return {k: self._format_node_info(v) for k, v in sut_info.items()}
56
57     def _format_node_info(self, node_info):
58         info = []
59         facts = node_info.get('ansible_facts', {})
60
61         info.append(['hostname', facts.get('ansible_hostname')])
62
63         info.append(['product_name', facts.get('ansible_product_name')])
64         info.append(['product_version', facts.get('ansible_product_version')])
65
66         processors = facts.get('ansible_processor', [])
67         try:
68             processor_type = '{} {}'.format(processors[0], processors[1])
69         except IndexError:
70             LOG.exception('No Processor in SUT data')
71             processor_type = None
72
73         info.append(['processor_type', processor_type])
74         info.append(['architecture', facts.get('ansible_architecture')])
75         info.append(['processor_cores', facts.get('ansible_processor_cores')])
76         info.append(['processor_vcpus', facts.get('ansible_processor_vcpus')])
77
78         memory = facts.get('ansible_memtotal_mb')
79         memory = round(memory * 1.0 / 1024, 2) if memory else None
80         info.append(['memory', '{} GB'.format(memory)])
81
82         devices = facts.get('ansible_devices', {})
83         info.extend([self._get_device_info(k, v) for k, v in devices.items()])
84
85         lsb_description = facts.get('ansible_lsb', {}).get('description')
86         info.append(['OS', lsb_description])
87
88         interfaces = facts.get('ansible_interfaces')
89         info.append(['interfaces', interfaces])
90         if isinstance(interfaces, collections.Sequence):
91             info.extend([self._get_interface_info(facts, i) for i in interfaces])
92         info = [i for i in info if i]
93
94         return info
95
96     def _get_interface_info(self, facts, name):
97         mac = facts.get('ansible_{}'.format(name), {}).get('macaddress')
98         return [name, mac] if mac else []
99
100     def _get_device_info(self, name, info):
101         return ['disk_{}'.format(name), info.get('size')]