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 ##############################################################################
13 from api import ApiResource
14 from yardstick.common.utils import result_handler
15 from yardstick.common.utils import source_env
16 from yardstick.common.utils import change_obj_to_dict
17 from yardstick.common.openstack_utils import get_nova_client
18 from yardstick.common import constants as consts
20 LOG = logging.getLogger(__name__)
21 LOG.setLevel(logging.DEBUG)
24 class V2Images(ApiResource):
28 source_env(consts.OPENRC)
30 return result_handler(consts.API_ERROR, 'source openrc error')
32 nova_client = get_nova_client()
34 images_list = nova_client.images.list()
36 return result_handler(consts.API_ERROR, 'get images error')
38 images = [self.get_info(change_obj_to_dict(i)) for i in images_list]
39 status = 1 if all(i['status'] == 'ACTIVE' for i in images) else 0
41 return result_handler(consts.API_SUCCESS, {'status': status, 'images': images})
44 return self._dispatch_post()
46 def get_info(self, data):
48 'name': data.get('name', ''),
49 'size': data.get('OS-EXT-IMG-SIZE:size', ''),
50 'status': data.get('status', ''),
51 'time': data.get('updated', '')
55 def load_image(self, args):
56 thread = threading.Thread(target=self._load_images)
58 return result_handler(consts.API_SUCCESS, {})
60 def _load_images(self):
61 LOG.info('source openrc')
62 source_env(consts.OPENRC)
64 LOG.info('clean images')
65 cmd = [consts.CLEAN_IMAGES_SCRIPT]
66 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
68 _, err = p.communicate()
70 LOG.error('clean image failed: %s', err)
72 LOG.info('load images')
73 cmd = [consts.LOAD_IMAGES_SCRIPT]
74 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
76 _, err = p.communicate()
78 LOG.error('load image failed: %s', err)