aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/service
diff options
context:
space:
mode:
authorchenjiankun <chenjiankun1@huawei.com>2018-03-14 09:41:01 +0000
committerchenjiankun <chenjiankun1@huawei.com>2018-03-15 06:56:37 +0000
commitc3f91fac56cd938c408c62ff44f5ff039b1fd8e6 (patch)
treebe2c5a64a4908fc5acc0ddf28e798720cbf01cb8 /yardstick/service
parent4b8b674b65830a24230faed71e8d9a1048139c89 (diff)
Create API to get SUT information
JIRA: YARDSTICK-1072 We need to show SUT information in GUI. Change-Id: I40bcd513f3c6a443c82764687637ac5258b99584 Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
Diffstat (limited to 'yardstick/service')
-rw-r--r--yardstick/service/__init__.py12
-rw-r--r--yardstick/service/environment.py101
2 files changed, 113 insertions, 0 deletions
diff --git a/yardstick/service/__init__.py b/yardstick/service/__init__.py
new file mode 100644
index 000000000..1c3953de6
--- /dev/null
+++ b/yardstick/service/__init__.py
@@ -0,0 +1,12 @@
+##############################################################################
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+class Service(object):
+ pass
diff --git a/yardstick/service/environment.py b/yardstick/service/environment.py
new file mode 100644
index 000000000..324589f79
--- /dev/null
+++ b/yardstick/service/environment.py
@@ -0,0 +1,101 @@
+##############################################################################
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import tempfile
+import logging
+import collections
+
+from oslo_serialization import jsonutils
+
+from yardstick.service import Service
+from yardstick.common.exceptions import MissingPodInfoError
+from yardstick.common.exceptions import UnsupportedPodFormatError
+from yardstick.common.ansible_common import AnsibleCommon
+
+LOG = logging.getLogger(__name__)
+
+
+class Environment(Service):
+ def __init__(self, pod=None):
+ super(Environment, self).__init__()
+ # pod can be a dict or a json format string
+ self.pod = pod
+
+ def get_sut_info(self):
+ temdir = tempfile.mkdtemp(prefix='sut')
+
+ nodes = self._load_pod_info()
+ ansible = AnsibleCommon(nodes=nodes)
+ ansible.gen_inventory_ini_dict()
+ sut_info = ansible.get_sut_info(temdir)
+
+ return self._format_sut_info(sut_info)
+
+ def _load_pod_info(self):
+ if self.pod is None:
+ raise MissingPodInfoError
+
+ if isinstance(self.pod, collections.Mapping):
+ try:
+ return self.pod['nodes']
+ except KeyError:
+ raise UnsupportedPodFormatError
+
+ try:
+ return jsonutils.loads(self.pod)['nodes']
+ except (ValueError, KeyError):
+ raise UnsupportedPodFormatError
+
+ def _format_sut_info(self, sut_info):
+ return {k: self._format_node_info(v) for k, v in sut_info.items()}
+
+ def _format_node_info(self, node_info):
+ info = []
+ facts = node_info.get('ansible_facts', {})
+
+ info.append(['hostname', facts.get('ansible_hostname')])
+
+ info.append(['product_name', facts.get('ansible_product_name')])
+ info.append(['product_version', facts.get('ansible_product_version')])
+
+ processors = facts.get('ansible_processor', [])
+ try:
+ processor_type = '{} {}'.format(processors[0], processors[1])
+ except IndexError:
+ LOG.exception('No Processor in SUT data')
+ processor_type = None
+
+ info.append(['processor_type', processor_type])
+ info.append(['architecture', facts.get('ansible_architecture')])
+ info.append(['processor_cores', facts.get('ansible_processor_cores')])
+ info.append(['processor_vcpus', facts.get('ansible_processor_vcpus')])
+
+ memory = facts.get('ansible_memtotal_mb')
+ memory = round(memory * 1.0 / 1024, 2) if memory else None
+ info.append(['memory', '{} GB'.format(memory)])
+
+ devices = facts.get('ansible_devices', {})
+ info.extend([self._get_device_info(k, v) for k, v in devices.items()])
+
+ lsb_description = facts.get('ansible_lsb', {}).get('description')
+ info.append(['OS', lsb_description])
+
+ interfaces = facts.get('ansible_interfaces')
+ info.append(['interfaces', interfaces])
+ if isinstance(interfaces, collections.Sequence):
+ info.extend([self._get_interface_info(facts, i) for i in interfaces])
+ info = [i for i in info if i]
+
+ return info
+
+ def _get_interface_info(self, facts, name):
+ mac = facts.get('ansible_{}'.format(name), {}).get('macaddress')
+ return [name, mac] if mac else []
+
+ def _get_device_info(self, name, info):
+ return ['disk_{}'.format(name), info.get('size')]