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
22 from six.moves import configparser
23 from oslo_serialization import jsonutils
24 from docker import Client
26 from api.database.handler import AsyncTaskHandler
27 from api.utils import influx
28 from api.utils.common import result_handler
29 from yardstick.common import constants as consts
30 from yardstick.common import utils as common_utils
31 from yardstick.common import openstack_utils
32 from yardstick.common.httpClient import HttpClient
35 logger = logging.getLogger(__name__)
36 logger.setLevel(logging.DEBUG)
39 def createGrafanaContainer(args):
40 task_id = str(uuid.uuid4())
42 thread = threading.Thread(target=_create_grafana, args=(task_id,))
45 return result_handler('success', {'task_id': task_id})
48 def _create_grafana(task_id):
51 client = Client(base_url=consts.DOCKER_URL)
54 image = '{}:{}'.format(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
55 if not _check_image_exist(client, image):
56 client.pull(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
58 _create_grafana_container(client)
66 _update_task_status(task_id)
67 except Exception as e:
68 _update_task_error(task_id, str(e))
69 logger.exception('Error: %s', e)
72 def _create_dashboard():
73 url = 'http://admin:admin@%s:3000/api/dashboards/db' % consts.GRAFANA_IP
74 path = os.path.join(consts.REPOS_DIR, 'dashboard', '*dashboard.json')
76 for i in sorted(glob.iglob(path)):
78 data = jsonutils.load(f)
79 HttpClient().post(url, data)
82 def _create_data_source():
83 url = 'http://admin:admin@%s:3000/api/datasources' % consts.GRAFANA_IP
88 "url": "http://%s:8086" % consts.INFLUXDB_IP,
91 "database": "yardstick",
93 "basicAuthUser": "admin",
94 "basicAuthPassword": "admin",
97 HttpClient().post(url, data)
100 def _create_grafana_container(client):
102 port_bindings = {k: k for k in ports}
103 restart_policy = {"MaximumRetryCount": 0, "Name": "always"}
104 host_config = client.create_host_config(port_bindings=port_bindings,
105 restart_policy=restart_policy)
107 container = client.create_container(image='%s:%s' % (consts.GRAFANA_IMAGE,
112 host_config=host_config)
113 client.start(container)
116 def _check_image_exist(client, t):
117 return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags'])
120 def createInfluxDBContainer(args):
121 task_id = str(uuid.uuid4())
123 thread = threading.Thread(target=_create_influxdb, args=(task_id,))
126 return result_handler('success', {'task_id': task_id})
129 def _create_influxdb(task_id):
130 _create_task(task_id)
132 client = Client(base_url=consts.DOCKER_URL)
135 _change_output_to_influxdb()
137 if not _check_image_exist(client, '%s:%s' % (consts.INFLUXDB_IMAGE,
138 consts.INFLUXDB_TAG)):
139 client.pull(consts.INFLUXDB_IMAGE, tag=consts.INFLUXDB_TAG)
141 _create_influxdb_container(client)
147 _update_task_status(task_id)
148 except Exception as e:
149 _update_task_error(task_id, str(e))
150 logger.debug('Error: %s', e)
153 def _create_influxdb_container(client):
156 port_bindings = {k: k for k in ports}
157 restart_policy = {"MaximumRetryCount": 0, "Name": "always"}
158 host_config = client.create_host_config(port_bindings=port_bindings,
159 restart_policy=restart_policy)
161 container = client.create_container(image='%s:%s' % (consts.INFLUXDB_IMAGE,
162 consts.INFLUXDB_TAG),
166 host_config=host_config)
167 client.start(container)
170 def _config_influxdb():
172 client = influx.get_data_db_client()
173 client.create_user(consts.INFLUXDB_USER,
174 consts.INFLUXDB_PASS,
175 consts.INFLUXDB_DB_NAME)
176 client.create_database(consts.INFLUXDB_DB_NAME)
177 logger.info('Success to config influxDB')
178 except Exception as e:
179 logger.debug('Failed to config influxDB: %s', e)
182 def _change_output_to_influxdb():
183 common_utils.makedirs(consts.CONF_DIR)
185 parser = configparser.ConfigParser()
186 parser.read(consts.CONF_SAMPLE_FILE)
188 parser.set('DEFAULT', 'dispatcher', 'influxdb')
189 parser.set('dispatcher_influxdb', 'target',
190 'http://%s:8086' % consts.INFLUXDB_IP)
192 with open(consts.CONF_FILE, 'w') as f:
196 def prepareYardstickEnv(args):
197 task_id = str(uuid.uuid4())
199 thread = threading.Thread(target=_prepare_env_daemon, args=(task_id,))
202 return result_handler('success', {'task_id': task_id})
205 def _already_source_openrc():
206 """Check if openrc is sourced already"""
207 return all(os.environ.get(k) for k in ['OS_AUTH_URL', 'OS_USERNAME',
208 'OS_PASSWORD', 'EXTERNAL_NETWORK'])
211 def _prepare_env_daemon(task_id):
212 _create_task(task_id)
215 _create_directories()
217 rc_file = consts.OPENRC
219 if not _already_source_openrc():
220 if not os.path.exists(rc_file):
221 installer_ip = os.environ.get('INSTALLER_IP', '192.168.200.2')
222 installer_type = os.environ.get('INSTALLER_TYPE', 'compass')
223 _get_remote_rc_file(rc_file, installer_ip, installer_type)
224 _source_file(rc_file)
225 _append_external_network(rc_file)
226 _source_file(rc_file)
232 _update_task_status(task_id)
233 except Exception as e:
234 _update_task_error(task_id, str(e))
235 logger.debug('Error: %s', e)
238 def _create_directories():
239 common_utils.makedirs(consts.CONF_DIR)
242 def _source_file(rc_file):
243 common_utils.source_env(rc_file)
246 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
248 os_fetch_script = os.path.join(consts.RELENG_DIR, consts.FETCH_SCRIPT)
251 cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
253 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
256 if p.returncode != 0:
257 logger.debug('Failed to fetch credentials from installer')
259 if e.errno != errno.EEXIST:
263 def _append_external_network(rc_file):
264 neutron_client = openstack_utils.get_neutron_client()
265 networks = neutron_client.list_networks()['networks']
267 ext_network = next(n['name'] for n in networks if n['router:external'])
268 except StopIteration:
269 logger.warning("Can't find external network")
271 cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
273 with open(rc_file, 'a') as f:
276 if e.errno != errno.EEXIST:
281 cmd = [consts.CLEAN_IMAGES_SCRIPT]
282 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR)
283 output = p.communicate()[0]
284 logger.debug('The result is: %s', output)
288 cmd = [consts.LOAD_IMAGES_SCRIPT]
289 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=consts.REPOS_DIR)
290 output = p.communicate()[0]
291 logger.debug('The result is: %s', output)
294 def _create_task(task_id):
295 async_handler = AsyncTaskHandler()
300 async_handler.insert(task_dict)
303 def _update_task_status(task_id):
304 async_handler = AsyncTaskHandler()
306 task = async_handler.get_task_by_taskid(task_id)
307 async_handler.update_status(task, 1)
310 def _update_task_error(task_id, error):
311 async_handler = AsyncTaskHandler()
313 task = async_handler.get_task_by_taskid(task_id)
314 async_handler.update_status(task, 2)
315 async_handler.update_error(task, error)
318 def update_openrc(args):
320 openrc_vars = args['openrc']
322 return result_handler(consts.API_ERROR, 'openrc must be provided')
324 if not isinstance(openrc_vars, collections.Mapping):
325 return result_handler(consts.API_ERROR, 'args should be a dict')
327 lines = ['export {}={}\n'.format(k, v) for k, v in openrc_vars.items()]
328 logger.debug('Writing: %s', ''.join(lines))
330 logger.info('Writing openrc: Writing')
331 common_utils.makedirs(consts.CONF_DIR)
333 with open(consts.OPENRC, 'w') as f:
335 logger.info('Writing openrc: Done')
337 logger.info('Source openrc: Sourcing')
339 _source_file(consts.OPENRC)
340 except Exception as e:
341 logger.exception('Failed to source openrc')
342 return result_handler(consts.API_ERROR, str(e))
343 logger.info('Source openrc: Done')
345 return result_handler(consts.API_SUCCESS, {'openrc': openrc_vars})
348 def upload_pod_file(args):
350 pod_file = args['file']
352 return result_handler(consts.API_ERROR, 'file must be provided')
354 logger.info('Checking file')
355 data = yaml.load(pod_file.read())
356 if not isinstance(data, collections.Mapping):
357 return result_handler(consts.API_ERROR, 'invalid yaml file')
359 logger.info('Writing file')
360 with open(consts.POD_FILE, 'w') as f:
361 yaml.dump(data, f, default_flow_style=False)
362 logger.info('Writing finished')
364 return result_handler(consts.API_SUCCESS, {'pod_info': data})
367 def update_hosts(hosts_ip):
368 if not isinstance(hosts_ip, dict):
369 return result_handler(consts.API_ERROR, 'Error, args should be a dict')
370 logger.info('Writing hosts: Writing')
371 hosts_list = ['\n{} {}'.format(ip, host_name)
372 for host_name, ip in hosts_ip.items()]
373 logger.debug('Writing: %s', hosts_list)
374 with open(consts.ETC_HOSTS, 'a') as f:
375 f.writelines(hosts_list)
376 logger.info('Writing hosts: Done')
377 return result_handler(consts.API_SUCCESS, 'success')