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 ##############################################################################
9 from __future__ import absolute_import
20 from six.moves import configparser
21 from oslo_serialization import jsonutils
22 from docker import Client
24 from api.database.handler import AsyncTaskHandler
25 from api.utils import influx
26 from api.utils.common import result_handler
27 from yardstick.common import constants as consts
28 from yardstick.common import utils as yardstick_utils
29 from yardstick.common import openstack_utils
30 from yardstick.common.httpClient import HttpClient
33 logger = logging.getLogger(__name__)
34 logger.setLevel(logging.DEBUG)
37 def createGrafanaContainer(args):
38 task_id = str(uuid.uuid4())
40 thread = threading.Thread(target=_create_grafana, args=(task_id,))
43 return result_handler('success', {'task_id': task_id})
46 def _create_grafana(task_id):
49 client = Client(base_url=consts.DOCKER_URL)
52 image = '{}:{}'.format(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
53 if not _check_image_exist(client, image):
54 client.pull(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
56 _create_grafana_container(client)
64 _update_task_status(task_id)
65 except Exception as e:
66 _update_task_error(task_id, str(e))
67 logger.exception('Error: %s', e)
70 def _create_dashboard():
71 url = 'http://admin:admin@%s:3000/api/dashboards/db' % consts.GRAFANA_IP
72 path = os.path.join(consts.REPOS_DIR, 'dashboard', '*dashboard.json')
74 for i in sorted(glob.iglob(path)):
76 data = jsonutils.load(f)
77 HttpClient().post(url, data)
80 def _create_data_source():
81 url = 'http://admin:admin@%s:3000/api/datasources' % consts.GRAFANA_IP
86 "url": "http://%s:8086" % consts.INFLUXDB_IP,
89 "database": "yardstick",
91 "basicAuthUser": "admin",
92 "basicAuthPassword": "admin",
95 HttpClient().post(url, data)
98 def _create_grafana_container(client):
100 port_bindings = {k: k for k in ports}
101 host_config = client.create_host_config(port_bindings=port_bindings)
103 container = client.create_container(image='%s:%s' % (consts.GRAFANA_IMAGE,
108 host_config=host_config)
109 client.start(container)
112 def _check_image_exist(client, t):
113 return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags'])
116 def createInfluxDBContainer(args):
117 task_id = str(uuid.uuid4())
119 thread = threading.Thread(target=_create_influxdb, args=(task_id,))
122 return result_handler('success', {'task_id': task_id})
125 def _create_influxdb(task_id):
126 _create_task(task_id)
128 client = Client(base_url=consts.DOCKER_URL)
131 _change_output_to_influxdb()
133 if not _check_image_exist(client, '%s:%s' % (consts.INFLUXDB_IMAGE,
134 consts.INFLUXDB_TAG)):
135 client.pull(consts.INFLUXDB_IMAGE, tag=consts.INFLUXDB_TAG)
137 _create_influxdb_container(client)
143 _update_task_status(task_id)
144 except Exception as e:
145 _update_task_error(task_id, str(e))
146 logger.debug('Error: %s', e)
149 def _create_influxdb_container(client):
152 port_bindings = {k: k for k in ports}
153 host_config = client.create_host_config(port_bindings=port_bindings)
155 container = client.create_container(image='%s:%s' % (consts.INFLUXDB_IMAGE,
156 consts.INFLUXDB_TAG),
160 host_config=host_config)
161 client.start(container)
164 def _config_influxdb():
166 client = influx.get_data_db_client()
167 client.create_user(consts.INFLUXDB_USER,
168 consts.INFLUXDB_PASS,
169 consts.INFLUXDB_DB_NAME)
170 client.create_database(consts.INFLUXDB_DB_NAME)
171 logger.info('Success to config influxDB')
172 except Exception as e:
173 logger.debug('Failed to config influxDB: %s', e)
176 def _change_output_to_influxdb():
177 yardstick_utils.makedirs(consts.CONF_DIR)
179 parser = configparser.ConfigParser()
180 parser.read(consts.CONF_SAMPLE_FILE)
182 parser.set('DEFAULT', 'dispatcher', 'influxdb')
183 parser.set('dispatcher_influxdb', 'target',
184 'http://%s:8086' % consts.INFLUXDB_IP)
186 with open(consts.CONF_FILE, 'w') as f:
190 def prepareYardstickEnv(args):
191 task_id = str(uuid.uuid4())
193 thread = threading.Thread(target=_prepare_env_daemon, args=(task_id,))
196 return result_handler('success', {'task_id': task_id})
199 def _already_source_openrc():
200 """Check if openrc is sourced already"""
201 return all(os.environ.get(k) for k in ['OS_AUTH_URL', 'OS_USERNAME',
202 'OS_PASSWORD', 'EXTERNAL_NETWORK'])
205 def _prepare_env_daemon(task_id):
206 _create_task(task_id)
209 _create_directories()
211 rc_file = consts.OPENRC
213 if not _already_source_openrc():
214 if not os.path.exists(rc_file):
215 installer_ip = os.environ.get('INSTALLER_IP', '192.168.200.2')
216 installer_type = os.environ.get('INSTALLER_TYPE', 'compass')
217 _get_remote_rc_file(rc_file, installer_ip, installer_type)
218 _source_file(rc_file)
219 _append_external_network(rc_file)
220 _source_file(rc_file)
226 _update_task_status(task_id)
227 except Exception as e:
228 _update_task_error(task_id, str(e))
229 logger.debug('Error: %s', e)
232 def _create_directories():
233 yardstick_utils.makedirs(consts.CONF_DIR)
236 def _source_file(rc_file):
237 yardstick_utils.source_env(rc_file)
240 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
242 os_fetch_script = os.path.join(consts.RELENG_DIR, consts.FETCH_SCRIPT)
245 cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
247 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
250 if p.returncode != 0:
251 logger.debug('Failed to fetch credentials from installer')
253 if e.errno != errno.EEXIST:
257 def _append_external_network(rc_file):
258 neutron_client = openstack_utils.get_neutron_client()
259 networks = neutron_client.list_networks()['networks']
261 ext_network = next(n['name'] for n in networks if n['router:external'])
262 except StopIteration:
263 logger.warning("Can't find external network")
265 cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
267 with open(rc_file, 'a') as f:
270 if e.errno != errno.EEXIST:
275 cmd = [consts.CLEAN_IMAGES_SCRIPT]
276 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR)
277 output = p.communicate()[0]
278 logger.debug('The result is: %s', output)
282 cmd = [consts.LOAD_IMAGES_SCRIPT]
283 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR)
284 output = p.communicate()[0]
285 logger.debug('The result is: %s', output)
288 def _create_task(task_id):
289 async_handler = AsyncTaskHandler()
294 async_handler.insert(task_dict)
297 def _update_task_status(task_id):
298 async_handler = AsyncTaskHandler()
300 task = async_handler.get_task_by_taskid(task_id)
301 async_handler.update_status(task, 1)
304 def _update_task_error(task_id, error):
305 async_handler = AsyncTaskHandler()
307 task = async_handler.get_task_by_taskid(task_id)
308 async_handler.update_status(task, 2)
309 async_handler.update_error(task, error)