5 from api import ApiResource
6 from yardstick.common.utils import result_handler
7 from yardstick.common.utils import source_env
8 from yardstick.common.utils import change_obj_to_dict
9 from yardstick.common.openstack_utils import get_nova_client
10 from yardstick.common import constants as consts
12 LOG = logging.getLogger(__name__)
13 LOG.setLevel(logging.DEBUG)
16 class V2Images(ApiResource):
20 source_env(consts.OPENRC)
22 return result_handler(consts.API_ERROR, 'source openrc error')
24 nova_client = get_nova_client()
26 images_list = nova_client.images.list()
28 return result_handler(consts.API_ERROR, 'get images error')
30 images = [self.get_info(change_obj_to_dict(i)) for i in images_list]
31 status = 1 if all(i['status'] == 'ACTIVE' for i in images) else 0
35 return result_handler(consts.API_SUCCESS, {'status': status, 'images': images})
38 return self._dispatch_post()
40 def get_info(self, data):
42 'name': data.get('name', ''),
43 'size': data.get('OS-EXT-IMG-SIZE:size', ''),
44 'status': data.get('status', ''),
45 'time': data.get('updated', '')
49 def load_image(self, args):
50 thread = threading.Thread(target=self._load_images)
52 return result_handler(consts.API_SUCCESS, {})
54 def _load_images(self):
55 LOG.info('source openrc')
56 source_env(consts.OPENRC)
58 LOG.info('clean images')
59 cmd = [consts.CLEAN_IMAGES_SCRIPT]
60 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
62 _, err = p.communicate()
64 LOG.error('clean image failed: %s', err)
66 LOG.info('load images')
67 cmd = [consts.LOAD_IMAGES_SCRIPT]
68 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
70 _, err = p.communicate()
72 LOG.error('load image failed: %s', err)