Merge "Add API(v2) to create influxdb"
[yardstick.git] / api / resources / v2 / containers.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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
10
11 import logging
12 import threading
13 import time
14 import uuid
15
16 from six.moves import configparser
17 from oslo_serialization import jsonutils
18 from docker import Client
19
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
28
29
30 LOG = logging.getLogger(__name__)
31 LOG.setLevel(logging.DEBUG)
32
33 environment_handler = V2EnvironmentHandler()
34 container_handler = V2ContainerHandler()
35
36
37 class V2Containers(ApiResource):
38
39     def post(self):
40         return self._dispatch_post()
41
42     def create_influxdb(self, args):
43         try:
44             environment_id = args['environment_id']
45         except KeyError:
46             return result_handler(consts.API_ERROR, 'environment_id must be provided')
47
48         try:
49             uuid.UUID(environment_id)
50         except ValueError:
51             return result_handler(consts.API_ERROR, 'invalid environment id')
52
53         try:
54             environment = environment_handler.get_by_uuid(environment_id)
55         except ValueError:
56             return result_handler(consts.API_ERROR, 'no such environment id')
57
58         container_info = environment.container_id
59         container_info = jsonutils.loads(container_info) if container_info else {}
60
61         if container_info.get('influxdb'):
62             return result_handler(consts.API_ERROR, 'influxdb container already exist')
63
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)
68
69         LOG.info('launch influxdb background')
70         args = (name, port, container_id)
71         thread = threading.Thread(target=self._create_influxdb, args=args)
72         thread.start()
73
74         LOG.info('record container in database')
75         container_init_data = {
76             'uuid': container_id,
77             'environment_id': environment_id,
78             'name': name,
79             'port': port,
80             'status': 0
81         }
82         container_handler.insert(container_init_data)
83
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)
88
89         return result_handler(consts.API_SUCCESS, {'uuid': container_id})
90
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'])
94
95     def _create_influxdb(self, name, port, container_id):
96         client = Client(base_url=consts.DOCKER_URL)
97
98         try:
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)
105
106             LOG.info('Createing influxdb container')
107             container = self._create_influxdb_container(client, name, port)
108             LOG.info('Influxdb container is created')
109
110             time.sleep(5)
111
112             container = client.inspect_container(container['Id'])
113             ip = container['NetworkSettings']['Networks']['bridge']['IPAddress']
114             LOG.debug('container ip is: %s', ip)
115
116             LOG.info('Changing output to influxdb')
117             self._change_output_to_influxdb(ip, port)
118
119             LOG.info('Config influxdb')
120             self._config_influxdb(port)
121
122             container_handler.update_attr(container_id, 'status', 1)
123
124             LOG.info('Finished')
125         except Exception:
126             container_handler.update_attr(container_id, 'status', 2)
127             LOG.exception('Creating influxdb failed')
128
129     def _create_influxdb_container(self, client, name, port):
130
131         ports = [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)
136
137         LOG.info('Creating container')
138         container = client.create_container(image='%s:%s' %
139                                             (consts.INFLUXDB_IMAGE,
140                                              consts.INFLUXDB_TAG),
141                                             ports=ports,
142                                             name=name,
143                                             detach=True,
144                                             tty=True,
145                                             host_config=host_config)
146         LOG.info('Starting container')
147         client.start(container)
148         return container
149
150     def _config_influxdb(self, port):
151         try:
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')
158         except Exception:
159             LOG.exception('Config influxdb failed')
160
161     def _change_output_to_influxdb(self, ip, port):
162         utils.makedirs(consts.CONF_DIR)
163
164         parser = configparser.ConfigParser()
165         LOG.info('Reading output sample configuration')
166         parser.read(consts.CONF_SAMPLE_FILE)
167
168         LOG.info('Set dispatcher to influxdb')
169         parser.set('DEFAULT', 'dispatcher', 'influxdb')
170         parser.set('dispatcher_influxdb', 'target',
171                    'http://{}:{}'.format(ip, port))
172
173         LOG.info('Writing to %s', consts.CONF_FILE)
174         with open(consts.CONF_FILE, 'w') as f:
175             parser.write(f)