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
16 from six.moves import configparser
17 from oslo_serialization import jsonutils
18 from docker import Client
20 from api import ApiResource
21 from api.utils import influx
22 from api.database.v2.handlers import V2ContainerHandler
23 from api.database.v2.handlers import V2EnvironmentHandler
24 from yardstick.common import constants as consts
25 from yardstick.common import utils
26 from yardstick.common.utils import result_handler
27 from yardstick.common.utils import get_free_port
30 LOG = logging.getLogger(__name__)
31 LOG.setLevel(logging.DEBUG)
33 environment_handler = V2EnvironmentHandler()
34 container_handler = V2ContainerHandler()
37 class V2Containers(ApiResource):
40 return self._dispatch_post()
42 def create_influxdb(self, args):
44 environment_id = args['environment_id']
46 return result_handler(consts.API_ERROR, 'environment_id must be provided')
49 uuid.UUID(environment_id)
51 return result_handler(consts.API_ERROR, 'invalid environment id')
54 environment = environment_handler.get_by_uuid(environment_id)
56 return result_handler(consts.API_ERROR, 'no such environment id')
58 container_info = environment.container_id
59 container_info = jsonutils.loads(container_info) if container_info else {}
61 if container_info.get('influxdb'):
62 return result_handler(consts.API_ERROR, 'influxdb container already exist')
64 name = 'influxdb-{}'.format(environment_id[:8])
65 port = get_free_port(consts.SERVER_IP)
66 container_id = str(uuid.uuid4())
67 LOG.info('%s will launch on : %s', name, port)
69 LOG.info('launch influxdb background')
70 args = (name, port, container_id)
71 thread = threading.Thread(target=self._create_influxdb, args=args)
74 LOG.info('record container in database')
75 container_init_data = {
77 'environment_id': environment_id,
82 container_handler.insert(container_init_data)
84 LOG.info('update container in environment')
85 container_info['influxdb'] = container_id
86 environment_info = {'container_id': jsonutils.dumps(container_info)}
87 environment_handler.update_attr(environment_id, environment_info)
89 return result_handler(consts.API_SUCCESS, {'uuid': container_id})
91 def _check_image_exist(self, client, t):
92 return any(t in a['RepoTags'][0]
93 for a in client.images() if a['RepoTags'])
95 def _create_influxdb(self, name, port, container_id):
96 client = Client(base_url=consts.DOCKER_URL)
99 LOG.info('Checking if influxdb image exist')
100 if not self._check_image_exist(client, '%s:%s' %
101 (consts.INFLUXDB_IMAGE,
102 consts.INFLUXDB_TAG)):
103 LOG.info('Influxdb image not exist, start pulling')
104 client.pull(consts.INFLUXDB_IMAGE, tag=consts.INFLUXDB_TAG)
106 LOG.info('Createing influxdb container')
107 container = self._create_influxdb_container(client, name, port)
108 LOG.info('Influxdb container is created')
112 container = client.inspect_container(container['Id'])
113 ip = container['NetworkSettings']['Networks']['bridge']['IPAddress']
114 LOG.debug('container ip is: %s', ip)
116 LOG.info('Changing output to influxdb')
117 self._change_output_to_influxdb(ip, port)
119 LOG.info('Config influxdb')
120 self._config_influxdb(port)
122 container_handler.update_attr(container_id, 'status', 1)
126 container_handler.update_attr(container_id, 'status', 2)
127 LOG.exception('Creating influxdb failed')
129 def _create_influxdb_container(self, client, name, port):
132 port_bindings = {8086: port}
133 restart_policy = {"MaximumRetryCount": 0, "Name": "always"}
134 host_config = client.create_host_config(port_bindings=port_bindings,
135 restart_policy=restart_policy)
137 LOG.info('Creating container')
138 container = client.create_container(image='%s:%s' %
139 (consts.INFLUXDB_IMAGE,
140 consts.INFLUXDB_TAG),
145 host_config=host_config)
146 LOG.info('Starting container')
147 client.start(container)
150 def _config_influxdb(self, port):
152 client = influx.get_data_db_client()
153 client.create_user(consts.INFLUXDB_USER,
154 consts.INFLUXDB_PASS,
155 consts.INFLUXDB_DB_NAME)
156 client.create_database(consts.INFLUXDB_DB_NAME)
157 LOG.info('Success to config influxDB')
159 LOG.exception('Config influxdb failed')
161 def _change_output_to_influxdb(self, ip, port):
162 utils.makedirs(consts.CONF_DIR)
164 parser = configparser.ConfigParser()
165 LOG.info('Reading output sample configuration')
166 parser.read(consts.CONF_SAMPLE_FILE)
168 LOG.info('Set dispatcher to influxdb')
169 parser.set('DEFAULT', 'dispatcher', 'influxdb')
170 parser.set('dispatcher_influxdb', 'target',
171 'http://{}:{}'.format(ip, port))
173 LOG.info('Writing to %s', consts.CONF_FILE)
174 with open(consts.CONF_FILE, 'w') as f: