Add API(v2) to load images
[yardstick.git] / api / resources / v2 / images.py
1 import logging
2 import subprocess
3 import threading
4
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 import constants as consts
9
10 LOG = logging.getLogger(__name__)
11 LOG.setLevel(logging.DEBUG)
12
13
14 class V2Images(ApiResource):
15
16     def post(self):
17         return self._dispatch_post()
18
19     def load_image(self, args):
20         thread = threading.Thread(target=self._load_images)
21         thread.start()
22         return result_handler(consts.API_SUCCESS, {})
23
24     def _load_images(self):
25         LOG.info('source openrc')
26         source_env(consts.OPENRC)
27
28         LOG.info('clean images')
29         cmd = [consts.CLEAN_IMAGES_SCRIPT]
30         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
31                              cwd=consts.REPOS_DIR)
32         _, err = p.communicate()
33         if p.returncode != 0:
34             LOG.error('clean image failed: %s', err)
35
36         LOG.info('load images')
37         cmd = [consts.LOAD_IMAGES_SCRIPT]
38         p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
39                              cwd=consts.REPOS_DIR)
40         _, err = p.communicate()
41         if p.returncode != 0:
42             LOG.error('load image failed: %s', err)
43
44         LOG.info('Done')