# OLD Releng macros
- parameter:
name: project-parameter
parameters:
- string:
name: PROJECT
default: '{project}'
description: "JJB configured##############################################################################
# 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
##############################################################################
from __future__ import absolute_import
import errno
import logging
import ipaddress
import os
import subprocess
import threading
import time
import uuid
import glob
import six
import yaml
import collections
from six.moves import configparser
from oslo_serialization import jsonutils
from docker import Client
from docker.errors import APIError
from requests.exceptions import HTTPError
from api.database.v1.handlers import AsyncTaskHandler
from api.utils import influx
from api import ApiResource
from yardstick.common import constants as consts
from yardstick.common import utils
from yardstick.common.utils import result_handler
from yardstick.common import openstack_utils
from yardstick.common.httpClient import HttpClient
from yardstick.common.yaml_loader import yaml_load
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
async_handler = AsyncTaskHandler()
class V1Env(ApiResource):
def post(self):
return self._dispatch_post()
def create_grafana(self, *args):
task_id = str(uuid.uuid4())
thread = threading.Thread(target=self._create_grafana, args=(task_id,))
thread.start()
return result_handler(consts.API_SUCCESS, {'task_id': task_id})
def _create_grafana(self, task_id):
self._create_task(task_id)
client = Client(base_url=consts.DOCKER_URL)
try:
LOG.info('Checking if grafana image exist')
image = '{}:{}'.format(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
if not self._check_image_exist(client, image):
LOG.info('Grafana image not exist, start pulling')
client.pull(consts.GRAFANA_IMAGE, consts.GRAFANA_TAG)
LOG.info('Createing grafana container')
container = self._create_grafana_container(client)
LOG.info('Grafana container is created')
time.sleep(5)
container = client.inspect_container(container['Id'])
ip = container['NetworkSettings']['Networks']['bridge']['IPAddress']
LOG.debug('container ip is: %s', ip)
LOG.info('Creating data source for grafana')
self._create_data_source(ip)
LOG.info('Creating dashboard for grafana')
self._create_dashboard(ip)
self._update_task_status(task_id)
LOG.info('Finished')
except (APIError, HTTPError) as e:
self._update_task_error(task_id, str(e))
LOG.exception('Create grafana failed')
def _create_dashboard(self, ip):
url = 'http://admin:admin@{}:{}/api/dashboards/db'.format(ip, consts.GRAFANA_PORT)
path = os.path.join(consts.REPOS_DIR, 'dashboard', 'opnfv_yardstick_tc*.json')
for i in sorted(glob.iglob(path)):
with open(i) as f:
data = jsonutils.load(f)
try:
HttpClient().post(url, {"dashboard": data})
except Exception:
LOG.exception('Create dashboard %s failed', i)
raise
def _create_data_source(self, ip):
url = 'http://admin:admin@{}:{}/api/datasources'.format(ip, consts.GRAFANA_PORT)
influx_conf = utils.parse_ini_file(consts.CONF_FILE).get('dispatcher_influxdb', {})
data = {
"name": "yardstick",
"type": "influxdb",
"access": "proxy",
"url": influx_conf.get('target', ''),
"password": influx_conf.get('password', ''),
"user": influx_conf.get('username', ''),
"database": "yardstick",
"basicAuth": True,
"basicAuthUser": "admin",
"basicAuthPassword": "admin",
"isDefault": True,
}
try:
HttpClient().post(url, data, timeout=60)
except Exception:
LOG.exception('Create datasources failed')
raise
def _create_grafana_container(self, client):
ports = [consts.GRAFANA_PORT]
port_bindings = {consts.GRAFANA_PORT: consts.GRAFANA_MAPPING_PORT}
restart_policy = {"MaximumRetryCount": 0, "Name": "always"}
host_config = client.create_host_config(port_bindings=port_bindings,
restart_policy=restart_policy)
LOG.info('Creating container')
container = client.create_container(image='%s:%s' %
(consts.GRAFANA_IMAGE,
consts.GRAFANA_TAG),
ports=ports,
detach=True,
tty=True,
host_config=host_config)
LOG.info('Starting container')
client.start(container)
return container
def _check_image_exist(self, client, t):
return any(t in a['RepoTags'][0]
for a in client.images() if a['RepoTags'])
def create_influxdb(self, *args):
task_id = str(uuid.uuid4())
thread = threading.Thread(target=self._create_influxdb, args=(task_id,))
thread.start()
return result_handler(consts.API_SUCCESS, {'task_id': task_id})
def _create_influxdb(self, task_id):
self._create_task(task_id)
client = Client(base_url=consts.DOCKER_URL)
try:
LOG.info('Checking if influxdb image exist')
if not self._check_image_exist(client, '%s:%s' %
(consts.INFLUXDB_IMAGE,
consts.INFLUXDB_TAG)):
LOG.info('Influxdb image not exist, start pulling')
client.pull(cons