Debian Codegraph @

config_tempest/flavors.py

raw
1 # Copyright 2016 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from operator import itemgetter
17
18 from config_tempest import constants as C
19
20
21 class Flavors(object):
22 def __init__(self, client, allow_creation, conf, min_memory, min_disk,
23 no_rng=False):
24 """Init.
25
26 :type client: FlavorsClient object from tempest lib
27 :type allow_creation: boolean
28 :type conf: TempestConf object
29 """
30 self.client = client
31 self.allow_creation = allow_creation
32 self._conf = conf
33 self.flavor_list = self.client.list_flavors()['flavors']
34 min_memory_alt = C.DEFAULT_FLAVOR_RAM_ALT
35 name = 'm1.nano'
36 name_alt = 'm1.micro'
37 if min_memory != C.DEFAULT_FLAVOR_RAM:
38 min_memory_alt = min_memory + 1
39 name = 'custom'
40 name_alt = 'custom_alt'
41 self._conf.set('volume', 'volume_size', str(min_disk))
42 self.prefs = [
43 {'key': 'flavor_ref', 'name': name, 'ram': min_memory,
44 'disk': min_disk, 'no_rng': no_rng},
45 {'key': 'flavor_ref_alt', 'name': name_alt,
46 'ram': min_memory_alt, 'disk': min_disk, 'no_rng': no_rng}
47 ]
48
49 def create_tempest_flavors(self):
50 """Find or create flavors and set them in conf.
51
52 If 'flavor_ref' and 'flavor_ref_alt' are specified in conf, it will
53 try to find them, if not found, it raises an Exception.
54 Otherwise it will try finding or creating the required base flavors
55 (m1.nano and m1.micro by default) and set their ids in conf.
56 """
57 for pref in self.prefs:
58 flavor_id = None
59 if self._conf.has_option('compute', pref['key']):
60 flavor_id = self._conf.get('compute', pref['key'])
61 flavor_id = self.find_flavor_by_id(flavor_id)
62 if flavor_id is None:
63 raise Exception("%s id '%s' specified by user doesn't"
64 " exist", pref['key'], flavor_id)
65 else:
66 flavor_id = self.create_flavor(pref['name'], pref['ram'],
67 C.DEFAULT_FLAVOR_VCPUS,
68 pref['disk'],
69 no_rng=pref['no_rng'])
70 self._conf.set('compute', pref['key'], flavor_id)
71
72 def create_flavor(self, flavor_name, ram, vcpus, disk, no_rng=False):
73 """Create flavors or try to discover two smallest ones available.
74
75 :param flavor_name: flavor name to be created (usually m1.nano or
76 m1.micro)
77 :param ram: memory of created flavor in MB
78 :param vcpus: number of VCPUs for the flavor
79 :param disk: size of disk for flavor in GB
80 :param no_rng: boolean, if True, flavor will be created with RNG device
81 """
82 flavor_id = self.find_flavor_by_name(flavor_name)
83 if flavor_id is not None:
84 C.LOG.info("(no change) Found flavor '%s'", flavor_name)
85 return flavor_id
86 elif self.allow_creation:
87 C.LOG.info("Creating flavor '%s'", flavor_name)
88 resp = self.client.create_flavor(name=flavor_name,
89 ram=ram, vcpus=vcpus,
90 disk=disk, id=None)
91 args = {'flavor_id': resp['flavor']['id'],
92 'hw_rng:allowed': 'True'}
93 if no_rng:
94 args.pop('hw_rng:allowed')
95 self.client.set_flavor_extra_spec(**args)
96 return resp['flavor']['id']
97 else:
98 if len(self.flavor_list) < 2:
99 raise Exception("Creation of flavors is not allowed and not "
100 "enough available flavors found. Either use --"
101 "create argument or create flavors manually.")
102 # return id of the discovered flavor
103 return self.discover_smallest_flavor(flavor_name)
104
105 def find_flavor_by_id(self, flavor_id):
106 """Look for a flavor by its id.
107
108 :type flavor_id: string
109 :return: flavor id or None if not found
110 :rtype: string or None
111 """
112 found = [f for f in self.flavor_list if f['id'] == flavor_id]
113 if found:
114 C.LOG.info("Found flavor '%s' by it's id '%s'",
115 found[0]['name'], flavor_id)
116 # return flavor's id
117 return found[0]['id']
118 return None
119
120 def find_flavor_by_name(self, flavor_name):
121 """Look for a flavor by its name.
122
123 :type flavor_name: string
124 :return: flavor id or None if not found
125 :rtype: string or None
126 """
127 found = [f for f in self.flavor_list if f['name'] == flavor_name]
128 if found:
129 # return flavor's id
130 return found[0]['id']
131 return None
132
133 def discover_smallest_flavor(self, flavor_name=""):
134 """Discover the two smallest available flavors in the system.
135
136 If flavor_name contains "micro", the method returns the second
137 smallest flavor found.
138 :param flavor_name: [m1.nano, m1.micro]
139 """
140 C.LOG.warning("Flavor '%s' not found and creation is not allowed. "
141 "Trying to autodetect the smallest flavor available.",
142 flavor_name)
143 flavors = []
144 for flavor in self.flavor_list:
145 f = self.client.show_flavor(flavor['id'])['flavor']
146 flavors.append((f['name'], f['id'], f['ram'],
147 f['disk'], f['vcpus']))
148
149 # order by ram, disk size and vcpus number and take first two of them
150 flavors = sorted(flavors, key=itemgetter(2, 3, 4))[:2]
151
152 f = None
153 if "micro" in flavor_name:
154 # take the second smaller one
155 f = flavors[1]
156 else:
157 f = flavors[0]
158 C.LOG.warning("Found '%s' flavor (id: '%s', ram: '%s', disk: '%s', "
159 "vcpus: '%s') ", f[0], f[1], f[2], f[3], f[4])
160 # return flavor's id
161 return f[1]