1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
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 ##############################################################################
12 from oslo_config import cfg
14 from identity_auth import get_session
15 from os_clients import glance_client
16 import logger as doctor_log
20 default=os.environ.get('IMAGE_NAME', 'cirros'),
21 help='the name of test image',
25 help='the format of test image',
27 cfg.StrOpt('file_name',
29 help='the name of image file',
32 default='https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img',
33 help='the url where to get the image',
37 LOG = doctor_log.Logger('doctor').getLogger()
42 def __init__(self, conf):
45 glance_client(conf.os_clients.glance_version,
47 self.use_existing_image = False
51 LOG.info('image create start......')
53 images = {image.name: image for image in self.glance.images.list()}
54 if self.conf.image.name not in images:
55 if not os.path.exists(self.conf.image.file_name):
56 resp = urllib2.urlopen(self.conf.image.url)
57 with open(self.conf.image.file_name, "wb") as file:
58 file.write(resp.read())
59 self.image = self.glance.images.create(name=self.conf.image.name,
60 disk_format=self.conf.image.format,
61 container_format="bare",
63 self.glance.images.upload(self.image['id'],
64 open(self.conf.image.file_name, 'rb'))
66 self.use_existing_image = True
67 self.image = images[self.conf.image.name]
69 LOG.info('image create end......')
72 LOG.info('image delete start.......')
74 if not self.use_existing_image and self.image:
75 self.glance.images.delete(self.image['id'])
77 LOG.info('image delete end.......')