def add_environment(self, args): task_id = args['task_id'] try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') environment_handler = V2EnvironmentHandler() try: environment_handler.get_by_uuid(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'no such environment id') LOG.info('update environment_id in task') task_handler = V2TaskHandler() task_handler.update_attr(task_id, {'environment_id': environment_id}) return result_handler(consts.API_SUCCESS, {'uuid': task_id})
def delete(self, image_id): try: uuid.UUID(image_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid image id') image_handler = V2ImageHandler() try: image = image_handler.get_by_uuid(image_id) except ValueError: return result_handler(consts.API_ERROR, 'no such image id') LOG.info('delete image in openstack') glance_client = get_glance_client() try: image_o = next((i for i in glance_client.images.list() if i.name == image.name)) except StopIteration: return result_handler(consts.API_ERROR, 'can not find image') glance_client.images.delete(image_o.id) LOG.info('delete image in environment') environment_id = image.environment_id environment_handler = V2EnvironmentHandler() environment = environment_handler.get_by_uuid(environment_id) image_list = environment.image_id.split(',') image_list.remove(image_id) environment_handler.update_attr(environment_id, {'image_id': ','.join(image_list)}) LOG.info('delete image in DB') image_handler.delete_by_uuid(image_id) return result_handler(consts.API_SUCCESS, {'image': image_id})
def get(self, environment_id): try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') environment_handler = V2EnvironmentHandler() try: environment = environment_handler.get_by_uuid(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'no such environment id') if not environment.pod_id: return result_handler(consts.API_SUCCESS, {'sut': {}}) pod_handler = V2PodHandler() try: pod = pod_handler.get_by_uuid(environment.pod_id) except ValueError: return result_handler(consts.API_ERROR, 'no such pod id') else: pod_content = pod.content env = Environment(pod=pod_content) sut_info = env.get_sut_info() return result_handler(consts.API_SUCCESS, {'sut': sut_info})
def upload_image(self, args): try: image_file = args['file'] except KeyError: return result_handler(consts.API_ERROR, 'file must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') environment_handler = V2EnvironmentHandler() try: environment = environment_handler.get_by_uuid(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'no such environment') file_path = os.path.join(consts.IMAGE_DIR, image_file.filename) LOG.info('saving file') image_file.save(file_path) LOG.info('loading image') self._load_image(image_file.filename, file_path) LOG.info('creating image in DB') image_handler = V2ImageHandler() image_id = str(uuid.uuid4()) image_init_data = { 'uuid': image_id, 'name': image_file.filename, 'environment_id': environment_id } image_handler.insert(image_init_data) LOG.info('update image in environment') if environment.image_id: image_list = environment.image_id.split(',') image_list.append(image_id) new_image_id = ','.join(image_list) else: new_image_id = image_id environment_handler.update_attr(environment_id, {'image_id': new_image_id}) return result_handler(consts.API_SUCCESS, {'uuid': image_id})
def upload_image_by_url(self, args): try: url = args['url'] except KeyError: return result_handler(consts.API_ERROR, 'url must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') environment_handler = V2EnvironmentHandler() try: environment = environment_handler.get_by_uuid(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'no such environment') thread = threading.Thread(target=self._do_upload_image_by_url, args=(url, )) thread.start() file_name = url.split('/')[-1] LOG.info('creating image in DB') image_handler = V2ImageHandler() image_id = str(uuid.uuid4()) image_init_data = { 'uuid': image_id, 'name': file_name, 'environment_id': environment_id } image_handler.insert(image_init_data) LOG.info('update image in environment') if environment.image_id: image_list = environment.image_id.split(',') image_list.append(image_id) new_image_id = ','.join(image_list) else: new_image_id = image_id environment_handler.update_attr(environment_id, {'image_id': new_image_id}) return result_handler(consts.API_SUCCESS, {'uuid': image_id})
def _write_into_database(self, environment_id, openrc_id, openrc_data): LOG.info('writing openrc to database') openrc_handler = V2OpenrcHandler() openrc_init_data = { 'uuid': openrc_id, 'environment_id': environment_id, 'content': jsonutils.dumps(openrc_data) } openrc_handler.insert(openrc_init_data) LOG.info('binding openrc to environment: %s', environment_id) environment_handler = V2EnvironmentHandler() environment_handler.update_attr(environment_id, {'openrc_id': openrc_id})
def create_environment(self, args): try: name = args['name'] except KeyError: return result_handler(consts.API_ERROR, 'name must be provided') env_id = str(uuid.uuid4()) environment_handler = V2EnvironmentHandler() env_init_data = {'name': name, 'uuid': env_id} environment_handler.insert(env_init_data) return result_handler(consts.API_SUCCESS, {'uuid': env_id})
def get(self): environment_handler = V2EnvironmentHandler() environments = [ change_obj_to_dict(e) for e in environment_handler.list_all() ] for e in environments: container_info = e['container_id'] e['container_id'] = jsonutils.loads( container_info) if container_info else {} data = {'environments': environments} return result_handler(consts.API_SUCCESS, data)
def get(self, environment_id): try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') environment_handler = V2EnvironmentHandler() try: environment = environment_handler.get_by_uuid(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'no such environment id') environment = change_obj_to_dict(environment) container_id = environment['container_id'] environment['container_id'] = jsonutils.loads( container_id) if container_id else {} return result_handler(consts.API_SUCCESS, {'environment': environment})
def delete(self, environment_id): try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') environment_handler = V2EnvironmentHandler() try: environment = environment_handler.get_by_uuid(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'no such environment id') if environment.openrc_id: LOG.info('delete openrc: %s', environment.openrc_id) openrc_handler = V2OpenrcHandler() openrc_handler.delete_by_uuid(environment.openrc_id) if environment.pod_id: LOG.info('delete pod: %s', environment.pod_id) pod_handler = V2PodHandler() pod_handler.delete_by_uuid(environment.pod_id) if environment.container_id: LOG.info('delete containers') container_info = jsonutils.loads(environment.container_id) container_handler = V2ContainerHandler() client = Client(base_url=consts.DOCKER_URL) for k, v in container_info.items(): LOG.info('start delete: %s', k) container = container_handler.get_by_uuid(v) LOG.debug('container name: %s', container.name) try: client.remove_container(container.name, force=True) except Exception: LOG.exception('remove container failed') container_handler.delete_by_uuid(v) environment_handler.delete_by_uuid(environment_id) return result_handler(consts.API_SUCCESS, {'environment': environment_id})
def upload_pod_file(self, args): try: upload_file = args['file'] except KeyError: return result_handler(consts.API_ERROR, 'file must be provided') try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: uuid.UUID(environment_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid environment id') LOG.info('writing pod file: %s', consts.POD_FILE) upload_file.save(consts.POD_FILE) with open(consts.POD_FILE) as f: data = yaml_load(TaskTemplate.render(f.read())) LOG.debug('pod content is: %s', data) LOG.info('create pod in database') pod_id = str(uuid.uuid4()) pod_handler = V2PodHandler() pod_init_data = { 'uuid': pod_id, 'environment_id': environment_id, 'content': jsonutils.dumps(data) } pod_handler.insert(pod_init_data) LOG.info('update pod in environment') environment_handler = V2EnvironmentHandler() environment_handler.update_attr(environment_id, {'pod_id': pod_id}) return result_handler(consts.API_SUCCESS, { 'uuid': pod_id, 'pod': data })
def delete(self, pod_id): try: uuid.UUID(pod_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid pod id') pod_handler = V2PodHandler() try: pod = pod_handler.get_by_uuid(pod_id) except ValueError: return result_handler(consts.API_ERROR, 'no such pod') LOG.info('update pod in environment') environment_handler = V2EnvironmentHandler() environment_handler.update_attr(pod.environment_id, {'pod_id': None}) LOG.info('delete pod in database') pod_handler.delete_by_uuid(pod_id) return result_handler(consts.API_SUCCESS, {'pod': pod_id})
def delete(self, openrc_id): try: uuid.UUID(openrc_id) except ValueError: return result_handler(consts.API_ERROR, 'invalid openrc id') LOG.info('Geting openrc: %s', openrc_id) openrc_handler = V2OpenrcHandler() try: openrc = openrc_handler.get_by_uuid(openrc_id) except ValueError: return result_handler(consts.API_ERROR, 'no such openrc id') LOG.info('update openrc in environment') environment_handler = V2EnvironmentHandler() environment_handler.update_attr(openrc.environment_id, {'openrc_id': None}) openrc_handler.delete_by_uuid(openrc_id) return result_handler(consts.API_SUCCESS, {'openrc': openrc_id})
from api import ApiResource from api.utils import influx from api.database.v2.handlers import V2ContainerHandler from api.database.v2.handlers import V2EnvironmentHandler from yardstick.common import constants as consts from yardstick.common import utils from yardstick.common.utils import result_handler from yardstick.common.utils import get_free_port from yardstick.common.httpClient import HttpClient LOG = logging.getLogger(__name__) LOG.setLevel(logging.DEBUG) environment_handler = V2EnvironmentHandler() container_handler = V2ContainerHandler() class V2Containers(ApiResource): def post(self): return self._dispatch_post() def create_influxdb(self, args): try: environment_id = args['environment_id'] except KeyError: return result_handler(consts.API_ERROR, 'environment_id must be provided') try: