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
43 return result_handler(consts.API_SUCCESS, {'status': status, 'images': images})
46 return self._dispatch_post()
48 def get_info(self, data):
50 'name': data.get('name', ''),
51 'size': data.get('OS-EXT-IMG-SIZE:size', ''),
52 'status': data.get('status', ''),
53 'time': data.get('updated', '')
57 def load_image(self, args):
58 thread = threading.Thread(target=self._load_images)
60 return result_handler(consts.API_SUCCESS, {})
62 def _load_images(self):
63 LOG.info('source openrc')
64 source_env(consts.OPENRC)
66 LOG.info('clean images')
67 cmd = [consts.CLEAN_IMAGES_SCRIPT]
68 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
70 _, err = p.communicate()
72 LOG.error('clean image failed: %s', err)
74 LOG.info('load images')
75 cmd = [consts.LOAD_IMAGES_SCRIPT]
76 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
78 _, err = p.communicate()
80 LOG.error('load image failed: %s', err)