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
23 from docker.utils import create_host_config
25 from api.database.handler import AsyncTaskHandler
26 from api.utils import influx
27 from api.utils.common import result_handler
28 from yardstick.common import constants as consts
29 from yardstick.common import utils as yardstick_utils
30 from yardstick.common import openstack_utils
31 from yardstick.common.httpClient import HttpClient
34 logger = logging.getLogger(__name__)
35 logger.setLevel(logging.DEBUG)
38 def createGrafanaContainer(args):
39 task_id = str(uuid.uuid4())
41 thread = threading.Thread(target=_create_grafana, args=(task_id,))
44 return result_handler('success', {'task_id': task_id})
47 def _create_grafana(task_id):
50 client = Client(base_url=consts.DOCKER_URL)
53 image = '{}:{}'.format(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
54 if not _check_image_exist(client, image):
55 client.pull(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
57 _create_grafana_container(client)
65 _update_task_status(task_id)
66 except Exception as e:
67 _update_task_error(task_id, str(e))
68 logger.exception('Error: %s', e)
71 def _create_dashboard():
72 url = 'http://admin:admin@%s:3000/api/dashboards/db' % consts.GRAFANA_IP
73 path = os.path.join(consts.REPOS_DIR, 'dashboard', '*dashboard.json')
75 for i in sorted(glob.iglob(path)):
77 data = jsonutils.load(f)
78 HttpClient().post(url, data)
81 def _create_data_source():
82 url = 'http://admin:admin@%s:3000/api/datasources' % consts.GRAFANA_IP
87 "url": "http://%s:8086" % consts.INFLUXDB_IP,
90 "database": "yardstick",
92 "basicAuthUser": "admin",
93 "basicAuthPassword": "admin",
96 HttpClient().post(url, data)
99 def _create_grafana_container(client):
101 port_bindings = {k: k for k in ports}
102 restart_policy = {"MaximumRetryCount": 0, "Name": "always"}
103 host_config = client.create_host_config(port_bindings=port_bindings,
104 restart_policy=restart_policy)
106 container = client.create_container(image='%s:%s' % (consts.GRAFANA_IMAGE,
111 host_config=host_config)
112 client.start(container)
115 def _check_image_exist(client, t):
116 return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags'])
119 def createInfluxDBContainer(args):
120 task_id = str(uuid.uuid4())
122 thread = threading.Thread(target=_create_influxdb, args=(task_id,))
125 return result_handler('success', {'task_id': task_id})
128 def _create_influxdb(task_id):
129 _create_task(task_id)
131 client = Client(base_url=consts.DOCKER_URL)
134 _change_output_to_influxdb()
136 if not _check_image_exist(client, '%s:%s' % (consts.INFLUXDB_IMAGE,
137 consts.INFLUXDB_TAG)):
138 client.pull(consts.INFLUXDB_IMAGE, tag=consts.INFLUXDB_TAG)
140 _create_influxdb_container(client)
146 _update_task_status(task_id)
147 except Exception as e:
148 _update_task_error(task_id, str(e))
149 logger.debug('Error: %s', e)
152 def _create_influxdb_container(client):
155 port_bindings = {k: k for k in ports}
156 restart_policy = {"MaximumRetryCount": 0, "Name": "always"}
157 host_config = client.create_host_config(port_bindings=port_bindings,
158 restart_policy=restart_policy)
160 container = client.create_container(image='%s:%s' % (consts.INFLUXDB_IMAGE,
161 consts.INFLUXDB_TAG),
165 host_config=host_config)
166 client.start(container)
169 def _config_influxdb():
171 client = influx.get_data_db_client()
172 client.create_user(consts.INFLUXDB_USER,
173 consts.INFLUXDB_PASS,
174 consts.INFLUXDB_DB_NAME)
175 client.create_database(consts.INFLUXDB_DB_NAME)
176 logger.info('Success to config influxDB')
177 except Exception as e:
178 logger.debug('Failed to config influxDB: %s', e)
181 def _change_output_to_influxdb():
182 yardstick_utils.makedirs(consts.CONF_DIR)
184 parser = configparser.ConfigParser()
185 parser.read(consts.CONF_SAMPLE_FILE)
187 parser.set('DEFAULT', 'dispatcher', 'influxdb')
188 parser.set('dispatcher_influxdb', 'target',
189 'http://%s:8086' % consts.INFLUXDB_IP)
191 with open(consts.CONF_FILE, 'w') as f:
195 def prepareYardstickEnv(args):
196 task_id = str(uuid.uuid4())
198 thread = threading.Thread(target=_prepare_env_daemon, args=(task_id,))
201 return result_handler('success', {'task_id': task_id})
204 def _already_source_openrc():
205 """Check if openrc is sourced already"""
206 return all(os.environ.get(k) for k in ['OS_AUTH_URL', 'OS_USERNAME',
207 'OS_PASSWORD', 'EXTERNAL_NETWORK'])
210 def _prepare_env_daemon(task_id):
211 _create_task(task_id)
214 _create_directories()
216 rc_file = consts.OPENRC
218 if not _already_source_openrc():
219 if not os.path.exists(rc_file):
220 installer_ip = os.environ.get('INSTALLER_IP', '192.168.200.2')
221 installer_type = os.environ.get('INSTALLER_TYPE', 'compass')
222 _get_remote_rc_file(rc_file, installer_ip, installer_type)
223 _source_file(rc_file)
224 _append_external_network(rc_file)
225 _source_file(rc_file)
231 _update_task_status(task_id)
232 except Exception as e:
233 _update_task_error(task_id, str(e))
234 logger.debug('Error: %s', e)
237 def _create_directories():
238 yardstick_utils.makedirs(consts.CONF_DIR)
241 def _source_file(rc_file):
242 yardstick_utils.source_env(rc_file)
245 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
247 os_fetch_script = os.path.join(consts.RELENG_DIR, consts.FETCH_SCRIPT)
250 cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
252 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
255 if p.returncode != 0:
256 logger.debug('Failed to fetch credentials from installer')
258 if e.errno != errno.EEXIST:
262 def _append_external_network(rc_file):
263 neutron_client = openstack_utils.get_neutron_client()
264 networks = neutron_client.list_networks()['networks']
266 ext_network = next(n['name'] for n in networks if n['router:external'])
267 except StopIteration:
268 logger.warning("Can't find external network")
270 cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
272 with open(rc_file, 'a') as f:
275 if e.errno != errno.EEXIST:
280 cmd = [consts.CLEAN_IMAGES_SCRIPT]
281 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR)
282 output = p.communicate()[0]
283 logger.debug('The result is: %s', output)
287 cmd = [consts.LOAD_IMAGES_SCRIPT]
288 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR)
289 output = p.communicate()[0]
290 logger.debug('The result is: %s', output)
293 def _create_task(task_id):
294 async_handler = AsyncTaskHandler()
299 async_handler.insert(task_dict)
302 def _update_task_status(task_id):
303 async_handler = AsyncTaskHandler()
305 task = async_handler.get_task_by_taskid(task_id)
306 async_handler.update_status(task, 1)
309 def _update_task_error(task_id, error):
310 async_handler = AsyncTaskHandler()
312 task = async_handler.get_task_by_taskid(task_id)
313 async_handler.update_status(task, 2)
314 async_handler.update_error(task, error)