1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 ##############################################################################
12 from oslo_serialization import jsonutils
13 from docker import Client
15 from api import ApiResource
16 from api.database.v2.handlers import V2EnvironmentHandler
17 from api.database.v2.handlers import V2OpenrcHandler
18 from api.database.v2.handlers import V2PodHandler
19 from api.database.v2.handlers import V2ContainerHandler
20 from yardstick.common.utils import result_handler
21 from yardstick.common.utils import change_obj_to_dict
22 from yardstick.common import constants as consts
24 LOG = logging.getLogger(__name__)
25 LOG.setLevel(logging.DEBUG)
28 class V2Environments(ApiResource):
31 environment_handler = V2EnvironmentHandler()
32 environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]
34 for e in environments:
35 container_info = e['container_id']
36 e['container_id'] = jsonutils.loads(container_info) if container_info else {}
38 image_id = e['image_id']
39 e['image_id'] = image_id.split(',') if image_id else []
42 'environments': environments
45 return result_handler(consts.API_SUCCESS, data)
48 return self._dispatch_post()
50 def create_environment(self, args):
54 return result_handler(consts.API_ERROR, 'name must be provided')
56 env_id = str(uuid.uuid4())
58 environment_handler = V2EnvironmentHandler()
64 environment_handler.insert(env_init_data)
66 return result_handler(consts.API_SUCCESS, {'uuid': env_id})
69 class V2Environment(ApiResource):
71 def get(self, environment_id):
73 uuid.UUID(environment_id)
75 return result_handler(consts.API_ERROR, 'invalid environment id')
77 environment_handler = V2EnvironmentHandler()
79 environment = environment_handler.get_by_uuid(environment_id)
81 return result_handler(consts.API_ERROR, 'no such environment id')
83 environment = change_obj_to_dict(environment)
85 container_id = environment['container_id']
86 environment['container_id'] = jsonutils.loads(container_id) if container_id else {}
88 image_id = environment['image_id']
89 environment['image_id'] = image_id.split(',') if image_id else []
91 return result_handler(consts.API_SUCCESS, {'environment': environment})
93 def delete(self, environment_id):
95 uuid.UUID(environment_id)
97 return result_handler(consts.API_ERROR, 'invalid environment id')
99 environment_handler = V2EnvironmentHandler()
101 environment = environment_handler.get_by_uuid(environment_id)
103 return result_handler(consts.API_ERROR, 'no such environment id')
105 if environment.openrc_id:
106 LOG.info('delete openrc: %s', environment.openrc_id)
107 openrc_handler = V2OpenrcHandler()
108 openrc_handler.delete_by_uuid(environment.openrc_id)
110 if environment.pod_id:
111 LOG.info('delete pod: %s', environment.pod_id)
112 pod_handler = V2PodHandler()
113 pod_handler.delete_by_uuid(environment.pod_id)
115 if environment.container_id:
116 LOG.info('delete containers')
117 container_info = jsonutils.loads(environment.container_id)
119 container_handler = V2ContainerHandler()
120 client = Client(base_url=consts.DOCKER_URL)
121 for k, v in container_info.items():
122 LOG.info('start delete: %s', k)
123 container = container_handler.get_by_uuid(v)
124 LOG.debug('container name: %s', container.name)
126 client.remove_container(container.name, force=True)
128 LOG.exception('remove container failed')
129 container_handler.delete_by_uuid(v)
131 environment_handler.delete_by_uuid(environment_id)
133 return result_handler(consts.API_SUCCESS, {'environment': environment_id})