1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
13 from oslo_serialization import jsonutils
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
20 LOG = logging.getLogger(__name__)
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
29 def get_sut_info(self):
30 temdir = tempfile.mkdtemp(prefix='sut')
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)
37 return self._format_sut_info(sut_info)
39 def _load_pod_info(self):
41 raise MissingPodInfoError
43 if isinstance(self.pod, collections.Mapping):
45 return self.pod['nodes']
47 raise UnsupportedPodFormatError
50 return jsonutils.loads(self.pod)['nodes']
51 except (ValueError, KeyError):
52 raise UnsupportedPodFormatError
54 def _format_sut_info(self, sut_info):
55 return {k: self._format_node_info(v) for k, v in sut_info.items()}
57 def _format_node_info(self, node_info):
59 facts = node_info.get('ansible_facts', {})
61 info.append(['hostname', facts.get('ansible_hostname')])
63 info.append(['product_name', facts.get('ansible_product_name')])
64 info.append(['product_version', facts.get('ansible_product_version')])
66 processors = facts.get('ansible_processor', [])
68 processor_type = '{} {}'.format(processors[0], processors[1])
70 LOG.exception('No Processor in SUT data')
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')])
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)])
82 devices = facts.get('ansible_devices', {})
83 info.extend([self._get_device_info(k, v) for k, v in devices.items()])
85 lsb_description = facts.get('ansible_lsb', {}).get('description')
86 info.append(['OS', lsb_description])
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]
96 def _get_interface_info(self, facts, name):
97 mac = facts.get('ansible_{}'.format(name), {}).get('macaddress')
98 return [name, mac] if mac else []
100 def _get_device_info(self, name, info):
101 return ['disk_{}'.format(name), info.get('size')]