Merge "yardstick env influxdb/grafana cmd support centos"
[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
41         return result_handler(consts.API_SUCCESS, {'status': status, 'images': images})
42
43     def post(self):
44         return self._dispatch_post()
45
46     def get_info(self, data):
47         result = {
48             'name': data.get('name', ''),
49             'size': data.get('OS-EXT-IMG-SIZE:size', ''),
50             'status': data.get('status', ''),
51             'time': data.get('updated', '')
52         }
53         return result
54
55     def load_image(self, args):
56         thread = threading.Thread(target=self._load_images)
57         thread.start()
58         return result_handler(consts.API_SUCCESS, {})
59
60     def _load_images(self):
61         LOG.info('source openrc')
62         source_env(consts.OPENRC)
63
64         LOG.info('clean images')
65         cmd = [consts.CLEAN_IMAGES_SCRIPT]
66         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
67                              cwd=consts.REPOS_DIR)
68         _, err = p.communicate()
69         if p.returncode != 0:
70             LOG.error('clean image failed: %s', err)
71
72         LOG.info('load images')
73         cmd = [consts.LOAD_IMAGES_SCRIPT]
74         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
75                              cwd=consts.REPOS_DIR)
76         _, err = p.communicate()
77         if p.returncode != 0:
78             LOG.error('load image failed: %s', err)
79
80         LOG.info('Done')