1 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
14 """Module to interface with nova and glance."""
19 from glanceclient import exc as glance_exception
21 from glanceclient.openstack.common.apiclient.exceptions import NotFound as GlanceImageNotFound
23 from glanceclient.v1.apiclient.exceptions import NotFound as GlanceImageNotFound
30 class Compute(object):
31 """Class to interface with nova and glance."""
33 def __init__(self, nova_client, glance_client, config):
34 """Create a new compute instance to interact with nova and glance."""
35 self.novaclient = nova_client
36 self.glance_client = glance_client
39 def find_image(self, image_name):
40 """Find an image by name."""
42 return next(self.glance_client.images.list(filters={'name': image_name}), None)
43 except (novaclient.exceptions.NotFound, keystoneauth1.exceptions.http.NotFound,
48 def upload_image_via_url(self, final_image_name, image_file, retry_count=60):
49 """Directly upload image to Nova via URL if image is not present."""
52 # check image is file/url based.
53 with open(image_file) as f_image:
54 img = self.glance_client.images.create(name=str(final_image_name),
56 container_format="bare",
58 self.glance_client.images.upload(img.id, image_data=f_image)
59 # Check for the image in glance
60 while img.status in ['queued', 'saving'] and retry < retry_count:
61 img = self.glance_client.images.get(img.id)
63 LOG.debug("Image not yet active, retrying %s of %s...", retry, retry_count)
64 time.sleep(self.config.generic_poll_sec)
65 if img.status != 'active':
66 LOG.error("Image uploaded but too long to get to active state")
67 raise Exception("Image update active state timeout")
68 except glance_exception.HTTPForbidden:
69 LOG.error("Cannot upload image without admin access. Please make "
70 "sure the image is uploaded and is either public or owned by you.")
73 # catch the exception for file based errors.
74 LOG.error("Failed while uploading the image. Please make sure the "
75 "image at the specified location %s is correct.", image_file)
77 except keystoneauth1.exceptions.http.NotFound as exc:
78 LOG.error("Authentication error while uploading the image: %s", str(exc))
81 LOG.error(traceback.format_exc())
82 LOG.error("Failed to upload image %s.", image_file)
86 def delete_image(self, img_name):
87 """Delete an image by name."""
89 LOG.log("Deleting image %s...", img_name)
90 img = self.find_image(image_name=img_name)
91 self.glance_client.images.delete(img.id)
93 LOG.error("Failed to delete the image %s.", img_name)
98 # Create a server instance with name vmname
99 # and check that it gets into the ACTIVE state
100 def create_server(self, vmname, image, flavor, key_name,
101 nic, sec_group, avail_zone=None, user_data=None,
102 config_drive=None, files=None):
103 """Create a new server."""
105 security_groups = [sec_group['id']]
107 security_groups = None
109 # Also attach the created security group for the test
110 LOG.info('Creating instance %s with AZ: "%s"', vmname, avail_zone)
111 instance = self.novaclient.servers.create(name=vmname,
116 availability_zone=avail_zone,
118 config_drive=config_drive,
120 security_groups=security_groups)
123 def poll_server(self, instance):
124 """Poll a server from its reference."""
125 return self.novaclient.servers.get(instance.id)
127 def get_server_list(self):
128 """Get the list of all servers."""
129 servers_list = self.novaclient.servers.list()
132 def delete_server(self, server):
133 """Delete a server from its object reference."""
134 self.novaclient.servers.delete(server)
136 def find_flavor(self, flavor_type):
137 """Find a flavor by name."""
139 flavor = self.novaclient.flavors.find(name=flavor_type)
144 def create_flavor(self, name, ram, vcpus, disk, ephemeral=0):
145 """Create a flavor."""
146 return self.novaclient.flavors.create(name=name, ram=ram, vcpus=vcpus, disk=disk,
149 def get_hypervisor(self, hyper_name):
150 """Get the hypervisor from its name.
152 Can raise novaclient.exceptions.NotFound
154 # first get the id from name
155 hyper = self.novaclient.hypervisors.search(hyper_name)[0]
156 # get full hypervisor object
157 return self.novaclient.hypervisors.get(hyper.id)