Merge "Add host specific VM list to inspector design guide line"
[doctor.git] / doctor_tests / image.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
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 os
10 import urllib.request
11
12 from oslo_config import cfg
13
14 from doctor_tests.identity_auth import get_session
15 from doctor_tests.os_clients import glance_client
16
17 OPTS = [
18     cfg.StrOpt('image_name',
19                default=os.environ.get('IMAGE_NAME', 'cirros'),
20                help='the name of test image',
21                required=True),
22     cfg.StrOpt('image_format',
23                default='qcow2',
24                help='the format of test image',
25                required=True),
26     cfg.StrOpt('image_filename',
27                default='cirros.img',
28                help='the name of image file',
29                required=True),
30     cfg.StrOpt('image_download_url',
31                default='https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img',
32                help='the url where to get the image',
33                required=True),
34 ]
35
36
37 class Image(object):
38
39     def __init__(self, conf, log):
40         self.conf = conf
41         self.log = log
42         self.glance = \
43             glance_client(conf.glance_version, get_session())
44         self.use_existing_image = False
45         self.image = None
46
47     def create(self):
48         self.log.info('image create start......')
49
50         images = {image.name: image for image in self.glance.images.list()}
51         if self.conf.image_name not in images:
52             if not os.path.exists(self.conf.image_filename):
53                 resp = urllib.request.urlopen(self.conf.image_download_url)
54                 with open(self.conf.image_filename, "wb") as file:
55                     file.write(resp.read())
56             self.image = self.glance.images.create(name=self.conf.image_name,
57                                                    disk_format=self.conf.image_format,
58                                                    container_format="bare",
59                                                    visibility="public")
60             self.glance.images.upload(self.image['id'],
61                                       open(self.conf.image_filename, 'rb'))
62         else:
63             self.use_existing_image = True
64             self.image = images[self.conf.image_name]
65
66         self.log.info('image create end......')
67
68     def delete(self):
69         self.log.info('image delete start.......')
70
71         if not self.use_existing_image and self.image:
72             self.glance.images.delete(self.image['id'])
73
74         self.log.info('image delete end.......')