Merge "Enable vnf/tg instantiate as blocking call."
[yardstick.git] / api / resources / v2 / images.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
3 #
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 ##############################################################################
9 import logging
10 import subprocess
11 import threading
12
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
19
20 LOG = logging.getLogger(__name__)
21 LOG.setLevel(logging.DEBUG)
22
23
24 class V2Images(ApiResource):
25
26     def get(self):
27         try:
28             source_env(consts.OPENRC)
29         except:
30             return result_handler(consts.API_ERROR, 'source openrc error')
31
32         nova_client = get_nova_client()
33         try:
34             images_list = nova_client.images.list()
35         except:
36             return result_handler(consts.API_ERROR, 'get images error')
37         else:
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
40             if not images:
41                 status = 0
42
43         return result_handler(consts.API_SUCCESS, {'status': status, 'images': images})
44
45     def post(self):
46         return self._dispatch_post()
47
48     def get_info(self, data):
49         result = {
50             'name': data.get('name', ''),
51             'size': data.get('OS-EXT-IMG-SIZE:size', ''),
52             'status': data.get('status', ''),
53             'time': data.get('updated', '')
54         }
55         return result
56
57     def load_image(self, args):
58         thread = threading.Thread(target=self._load_images)
59         thread.start()
60         return result_handler(consts.API_SUCCESS, {})
61
62     def _load_images(self):
63         LOG.info('source openrc')
64         source_env(consts.OPENRC)
65
66         LOG.info('clean images')
67         cmd = [consts.CLEAN_IMAGES_SCRIPT]
68         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
69                              cwd=consts.REPOS_DIR)
70         _, err = p.communicate()
71         if p.returncode != 0:
72             LOG.error('clean image failed: %s', err)
73
74         LOG.info('load images')
75         cmd = [consts.LOAD_IMAGES_SCRIPT]
76         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
77                              cwd=consts.REPOS_DIR)
78         _, err = p.communicate()
79         if p.returncode != 0:
80             LOG.error('load image failed: %s', err)
81
82         LOG.info('Done')