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 ##############################################################################
18 from docker import Client
20 from yardstick.common import constants as config
21 from yardstick.common import utils as yardstick_utils
22 from yardstick.common.httpClient import HttpClient
23 from api import conf as api_conf
24 from api.utils import influx
25 from api.utils.common import result_handler
27 logger = logging.getLogger(__name__)
30 def createGrafanaContainer(args):
31 thread = threading.Thread(target=_create_grafana)
33 return result_handler('success', [])
36 def _create_grafana():
37 client = Client(base_url=config.DOCKER_URL)
40 if not _check_image_exist(client, '%s:%s' % (config.GRAFANA_IMAGE,
41 config.GRAFANA_TAGS)):
42 client.pull(config.GRAFANA_IMAGE, config.GRAFANA_TAGS)
44 _create_grafana_container(client)
51 except Exception as e:
52 logger.debug('Error: %s', e)
55 def _create_dashboard():
56 url = 'http://admin:admin@%s:3000/api/dashboards/db' % api_conf.GATEWAY_IP
57 with open('../dashboard/ping_dashboard.json') as dashboard_json:
58 data = json.load(dashboard_json)
59 HttpClient().post(url, data)
62 def _create_data_source():
63 url = 'http://admin:admin@%s:3000/api/datasources' % api_conf.GATEWAY_IP
68 "url": "http://%s:8086" % api_conf.GATEWAY_IP,
71 "database": "yardstick",
73 "basicAuthUser": "admin",
74 "basicAuthPassword": "admin",
77 HttpClient().post(url, data)
80 def _create_grafana_container(client):
82 port_bindings = {k: k for k in ports}
83 host_config = client.create_host_config(port_bindings=port_bindings)
85 container = client.create_container(image='%s:%s' % (config.GRAFANA_IMAGE,
90 host_config=host_config)
91 client.start(container)
94 def _check_image_exist(client, t):
95 return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags'])
98 def createInfluxDBContainer(args):
99 thread = threading.Thread(target=_create_influxdb)
101 return result_handler('success', [])
104 def _create_influxdb():
105 client = Client(base_url=config.DOCKER_URL)
108 _change_output_to_influxdb()
110 if not _check_image_exist(client, '%s:%s' % (config.INFLUXDB_IMAGE,
111 config.INFLUXDB_TAG)):
112 client.pull(config.INFLUXDB_IMAGE, tag=config.INFLUXDB_TAG)
114 _create_influxdb_container(client)
119 except Exception as e:
120 logger.debug('Error: %s', e)
123 def _create_influxdb_container(client):
126 port_bindings = {k: k for k in ports}
127 host_config = client.create_host_config(port_bindings=port_bindings)
129 container = client.create_container(image='%s:%s' % (config.INFLUXDB_IMAGE,
130 config.INFLUXDB_TAG),
134 host_config=host_config)
135 client.start(container)
138 def _config_influxdb():
140 client = influx.get_data_db_client()
141 client.create_user(config.USER, config.PASSWORD, config.DATABASE)
142 client.create_database(config.DATABASE)
143 logger.info('Success to config influxDB')
144 except Exception as e:
145 logger.debug('Failed to config influxDB: %s', e)
148 def _change_output_to_influxdb():
149 yardstick_utils.makedirs(config.YARDSTICK_CONFIG_DIR)
151 parser = ConfigParser.ConfigParser()
152 parser.read(config.YARDSTICK_CONFIG_SAMPLE_FILE)
154 parser.set('DEFAULT', 'dispatcher', 'influxdb')
155 parser.set('dispatcher_influxdb', 'target',
156 'http://%s:8086' % api_conf.GATEWAY_IP)
158 with open(config.YARDSTICK_CONFIG_FILE, 'w') as f:
162 def prepareYardstickEnv(args):
163 thread = threading.Thread(target=_prepare_env_daemon)
165 return result_handler('success', [])
168 def _prepare_env_daemon():
170 installer_ip = os.environ.get('INSTALLER_IP', 'undefined')
171 installer_type = os.environ.get('INSTALLER_TYPE', 'undefined')
173 _check_variables(installer_ip, installer_type)
175 _create_directories()
177 rc_file = config.OPENSTACK_RC_FILE
179 _get_remote_rc_file(rc_file, installer_ip, installer_type)
181 _source_file(rc_file)
183 _append_external_network(rc_file)
185 # update the external_network
186 _source_file(rc_file)
193 def _check_variables(installer_ip, installer_type):
195 if installer_ip == 'undefined':
196 raise SystemExit('Missing INSTALLER_IP')
198 if installer_type == 'undefined':
199 raise SystemExit('Missing INSTALLER_TYPE')
200 elif installer_type not in config.INSTALLERS:
201 raise SystemExit('INSTALLER_TYPE is not correct')
204 def _create_directories():
205 yardstick_utils.makedirs(config.YARDSTICK_CONFIG_DIR)
208 def _source_file(rc_file):
209 yardstick_utils.source_env(rc_file)
212 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
214 os_fetch_script = os.path.join(config.RELENG_DIR, config.OS_FETCH_SCRIPT)
217 cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
219 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
222 if p.returncode != 0:
223 logger.debug('Failed to fetch credentials from installer')
225 if e.errno != errno.EEXIST:
229 def _append_external_network(rc_file):
230 neutron_client = yardstick_utils.get_neutron_client()
231 networks = neutron_client.list_networks()['networks']
233 ext_network = next(n['name'] for n in networks if n['router:external'])
234 except StopIteration:
235 logger.warning("Can't find external network")
237 cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
239 with open(rc_file, 'a') as f:
242 if e.errno != errno.EEXIST:
247 cmd = [config.CLEAN_IMAGES_SCRIPT]
248 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
249 cwd=config.YARDSTICK_REPOS_DIR)
250 output = p.communicate()[0]
251 logger.debug('The result is: %s', output)
255 cmd = [config.LOAD_IMAGES_SCRIPT]
256 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
257 cwd=config.YARDSTICK_REPOS_DIR)
258 output = p.communicate()[0]
259 logger.debug('The result is: %s', output)