4 # Copyright 2016 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 from . import session
as sess_drv
22 from . import keystone
as ks_drv
23 from . import nova
as nv_drv
24 from . import neutron
as nt_drv
25 from . import glance
as gl_drv
26 from . import ceilometer
as ce_drv
27 from . import cinder
as ci_drv
28 from . import utils
as drv_utils
31 import keystoneclient
.exceptions
as KeystoneExceptions
34 class ValidationError(Exception):
38 class DriverUtilities(object):
40 Class with utility method
42 def __init__(self
, driver
):
44 Constructor of DriverUtilities class
46 driver: Object of OpenstackDriver
48 self
.flavor_utils
= drv_utils
.FlavorUtils(driver
)
49 self
.network_utils
= drv_utils
.NetworkUtils(driver
)
50 self
.image_utils
= drv_utils
.ImageUtils(driver
)
51 self
.compute_utils
= drv_utils
.ComputeUtils(driver
)
55 return self
.flavor_utils
59 return self
.compute_utils
63 return self
.network_utils
67 return self
.image_utils
70 class OpenstackDriver(object):
72 Driver for openstack nova, neutron, glance, keystone, swift, cinder services
74 def __init__(self
, logger
= None, **kwargs
):
76 OpenstackDriver Driver constructor
78 logger: (instance of logging.Logger)
79 kwargs: A dictionary of
81 username (string) : Username for project/tenant.
82 password (string) : Password
83 auth_url (string) : Keystone Authentication URL.
84 project (string) : Openstack project name
85 mgmt_network(string, optional) : Management network name. Each VM created with this cloud-account will
86 have a default interface into management network.
87 cert_validate (boolean, optional) : In case of SSL/TLS connection if certificate validation is required or not.
88 user_domain : Domain name for user
89 project_domain : Domain name for project
95 self
.log
= logging
.getLogger('rwcal.openstack.driver')
96 self
.log
.setLevel(logging
.DEBUG
)
100 args
= dict(auth_url
= kwargs
['auth_url'],
101 username
= kwargs
['username'],
102 password
= kwargs
['password'],
103 project_name
= kwargs
['project'],
104 project_domain_name
= kwargs
['project_domain'] if 'project_domain' in kwargs
else None,
105 user_domain_name
= kwargs
['user_domain'] if 'user_domain' in kwargs
else None,)
107 cert_validate
= kwargs
['cert_validate'] if 'cert_validate' in kwargs
else False
108 region
= kwargs
['region_name'] if 'region_name' in kwargs
else False
109 mgmt_network
= kwargs
['mgmt_network'] if 'mgmt_network' in kwargs
else None
111 discover
= ks_drv
.KeystoneVersionDiscover(kwargs
['auth_url'], logger
= self
.log
)
112 (major
, minor
) = discover
.get_version()
114 self
.sess_drv
= sess_drv
.SessionDriver(auth_method
= 'password',
115 version
= str(major
),
116 cert_validate
= cert_validate
,
120 self
.ks_drv
= ks_drv
.KeystoneDriver(str(major
),
124 self
.nova_drv
= nv_drv
.NovaDriver(self
.sess_drv
,
125 region_name
= region
,
128 self
.neutron_drv
= nt_drv
.NeutronDriver(self
.sess_drv
,
129 region_name
= region
,
132 self
.glance_drv
= gl_drv
.GlanceDriver(self
.sess_drv
,
133 region_name
= region
,
136 self
.cinder_drv
= ci_drv
.CinderDriver(self
.sess_drv
,
137 region_name
= region
,
140 self
.ceilo_drv
= ce_drv
.CeilometerDriver(self
.sess_drv
,
141 region_name
= region
,
144 self
.utils
= DriverUtilities(self
)
146 self
._mgmt
_network
= mgmt_network
148 self
._cache
= dict(neutron
= dict(),
152 self
.build_resource_cache()
155 def nova_cache(self
):
156 return self
._cache
['nova']
159 def neutron_cache(self
):
160 return self
._cache
['neutron']
163 def glance_cache(self
):
164 return self
._cache
['glance']
167 def cinder_cache(self
):
168 return self
._cache
['cinder']
170 def build_resource_cache(self
):
171 self
.build_network_resource_cache()
172 self
.build_nova_resource_cache()
173 self
.build_cinder_resource_cache()
174 self
.build_glance_resource_cache()
176 def _cache_populate(self
, method
, datatype
, *args
, **kwargs
):
178 rsp
= method(*args
, **kwargs
)
179 except Exception as e
:
180 self
.log
.exception("Exception %s occured during execution of %s",
186 def _build_nova_security_group_list(self
):
187 self
.log
.info("Building Nova security group cache")
188 self
.nova_cache
['security_groups'] = self
._cache
_populate
(self
.nova_drv
.security_group_list
,
190 return self
.nova_cache
['security_groups']
192 def _build_nova_affinity_group_list(self
):
193 self
.log
.info("Building Nova affinity/anti-affinity group cache")
194 self
.nova_cache
['affinity_groups'] = self
._cache
_populate
(self
.nova_server_group_list
,
196 return self
.nova_cache
['affinity_groups']
198 def _build_neutron_security_group_list(self
):
199 self
.log
.info("Discovering neutron security group")
200 self
.neutron_cache
['security_groups'] = self
._cache
_populate
(self
.neutron_security_group_list
,
202 return self
.neutron_cache
['security_groups']
204 def _build_neutron_subnet_prefix_list(self
):
205 self
.log
.info("Discovering subnet prefix pools")
206 self
.neutron_cache
['subnet_pool'] = self
._cache
_populate
(self
.neutron_subnetpool_list
,
208 return self
.neutron_cache
['subnet_pool']
210 def _get_neutron_mgmt_network(self
):
211 if self
._mgmt
_network
:
212 self
.log
.info("Discovering management network %s", self
._mgmt
_network
)
213 network_list
= self
._cache
_populate
(self
.neutron_drv
.network_get
,
215 **{'network_name':self
._mgmt
_network
})
217 self
.neutron_cache
['mgmt_net'] = network_list
['id']
219 raise KeyError("Error")
222 def _build_glance_image_list(self
):
223 self
.log
.info("Discovering images")
224 self
.glance_cache
['images'] = self
._cache
_populate
(self
.glance_image_list
,
226 return self
.glance_cache
['images']
229 def build_nova_resource_cache(self
):
230 self
.log
.info("Building nova resource cache")
231 self
._build
_nova
_security
_group
_list
()
232 self
._build
_nova
_affinity
_group
_list
()
235 def build_network_resource_cache(self
):
236 self
.log
.info("Building network resource cache")
237 self
._get
_neutron
_mgmt
_network
()
238 self
._build
_neutron
_security
_group
_list
()
239 self
._build
_neutron
_subnet
_prefix
_list
()
241 def build_cinder_resource_cache(self
):
245 def build_glance_resource_cache(self
):
246 self
.log
.info("Building glance resource cache")
247 self
._build
_glance
_image
_list
()
251 def _nova_affinity_group(self
):
252 if 'affinity_groups' in self
.nova_cache
:
253 return self
.nova_cache
['affinity_groups']
255 return self
._build
_nova
_affinity
_group
_list
()
258 def _nova_security_groups(self
):
259 if 'security_groups' in self
.nova_cache
:
260 return self
.nova_cache
['security_groups']
262 return self
._build
_nova
_security
_group
_list
()
265 def mgmt_network(self
):
266 return self
._mgmt
_network
269 def _mgmt_network_id(self
):
270 if 'mgmt_net' in self
.neutron_cache
:
271 return self
.neutron_cache
['mgmt_net']
276 def _neutron_security_groups(self
):
277 if 'security_groups' in self
.neutron_cache
:
278 return self
.neutron_cache
['security_groups']
280 return self
._build
_neutron
_security
_group
_list
()
283 def _neutron_subnet_prefix_pool(self
):
284 if 'subnet_pool' in self
.neutron_cache
:
285 return self
.neutron_cache
['subnet_pool']
287 return self
._build
_neutron
_subnet
_prefix
_list
()
290 def _glance_image_list(self
):
291 if 'images' in self
.glance_cache
:
292 return self
.glance_cache
['images']
294 return self
._build
_glance
_image
_list
()
296 def validate_account_creds(self
):
298 self
.sess_drv
.invalidate_auth_token()
299 self
.sess_drv
.auth_token
300 self
.build_resource_cache()
301 except KeystoneExceptions
.AuthorizationFailure
as e
:
302 self
.log
.error("Unable to authenticate or validate the existing credentials. Exception: %s", str(e
))
303 raise ValidationError("Invalid Credentials: "+ str(e
))
304 except Exception as e
:
305 self
.log
.error("Could not connect to Openstack. Exception: %s", str(e
))
306 raise ValidationError("Connection Error: "+ str(e
))
309 def glance_image_create(self
, **kwargs
):
310 if not 'disk_format' in kwargs
:
311 kwargs
['disk_format'] = 'qcow2'
312 if not 'container_format' in kwargs
:
313 kwargs
['container_format'] = 'bare'
314 if not 'min_disk' in kwargs
:
315 kwargs
['min_disk'] = 0
316 if not 'min_ram' in kwargs
:
317 kwargs
['min_ram'] = 0
318 return self
.glance_drv
.image_create(**kwargs
)
320 def glance_image_upload(self
, image_id
, fd
):
321 self
.glance_drv
.image_upload(image_id
, fd
)
323 def glance_image_add_location(self
, image_id
, location
):
324 self
.glance_drv
.image_add_location(image_id
, location
)
326 def glance_image_update(self
, image_id
, remove_props
= None, **kwargs
):
327 self
.glance_drv
.image_update(image_id
, remove_props
=remove_props
, **kwargs
)
329 def glance_image_delete(self
, image_id
):
330 self
.glance_drv
.image_delete(image_id
)
332 def glance_image_list(self
):
333 return self
.glance_drv
.image_list()
335 def glance_image_get(self
, image_id
):
336 return self
.glance_drv
.image_get(image_id
)
338 def nova_flavor_list(self
):
339 return self
.nova_drv
.flavor_list()
341 def nova_flavor_find(self
, **kwargs
):
342 return self
.nova_drv
.flavor_find(**kwargs
)
344 def nova_flavor_create(self
, name
, ram
, vcpus
, disk
, epa_specs
= dict()):
345 return self
.nova_drv
.flavor_create(name
,
349 extra_specs
= epa_specs
)
351 def nova_flavor_delete(self
, flavor_id
):
352 self
.nova_drv
.flavor_delete(flavor_id
)
354 def nova_flavor_get(self
, flavor_id
):
355 return self
.nova_drv
.flavor_get(flavor_id
)
357 def nova_server_create(self
, **kwargs
):
358 if 'security_groups' not in kwargs
:
359 kwargs
['security_groups'] = [ s
['name'] for s
in self
._nova
_security
_groups
]
360 return self
.nova_drv
.server_create(**kwargs
)
362 def nova_server_add_port(self
, server_id
, port_id
):
363 self
.nova_drv
.server_add_port(server_id
, port_id
)
365 def nova_server_delete_port(self
, server_id
, port_id
):
366 self
.nova_drv
.server_delete_port(server_id
, port_id
)
368 def nova_server_start(self
, server_id
):
369 self
.nova_drv
.server_start(server_id
)
371 def nova_server_stop(self
, server_id
):
372 self
.nova_drv
.server_stop(server_id
)
374 def nova_server_delete(self
, server_id
):
375 self
.nova_drv
.server_delete(server_id
)
377 def nova_server_reboot(self
, server_id
):
378 self
.nova_drv
.server_reboot(server_id
, reboot_type
='HARD')
380 def nova_server_rebuild(self
, server_id
, image_id
):
381 self
.nova_drv
.server_rebuild(server_id
, image_id
)
383 def nova_floating_ip_list(self
):
384 return self
.nova_drv
.floating_ip_list()
386 def nova_floating_ip_create(self
, pool
= None):
387 return self
.nova_drv
.floating_ip_create(pool
)
389 def nova_floating_ip_delete(self
, floating_ip
):
390 self
.nova_drv
.floating_ip_delete(floating_ip
)
392 def nova_floating_ip_assign(self
, server_id
, floating_ip
, fixed_ip
):
393 self
.nova_drv
.floating_ip_assign(server_id
, floating_ip
, fixed_ip
)
395 def nova_floating_ip_release(self
, server_id
, floating_ip
):
396 self
.nova_drv
.floating_ip_release(server_id
, floating_ip
)
398 def nova_server_list(self
):
399 return self
.nova_drv
.server_list()
401 def nova_server_get(self
, server_id
):
402 return self
.nova_drv
.server_get(server_id
)
404 def nova_server_console(self
, server_id
):
405 return self
.nova_drv
.server_console(server_id
)
407 def nova_server_group_list(self
):
408 return self
.nova_drv
.group_list()
410 def nova_volume_list(self
, server_id
):
411 return self
.nova_drv
.volume_list(server_id
)
413 def neutron_network_list(self
):
414 return self
.neutron_drv
.network_list()
416 def neutron_network_get(self
, network_id
):
417 return self
.neutron_drv
.network_get(network_id
=network_id
)
419 def neutron_network_create(self
, **kwargs
):
420 return self
.neutron_drv
.network_create(**kwargs
)
422 def neutron_network_delete(self
, network_id
):
423 self
.neutron_drv
.network_delete(network_id
)
425 def neutron_subnet_list(self
):
426 return self
.neutron_drv
.subnet_list(**{})
428 def neutron_subnet_get(self
, subnet_id
):
429 return self
.neutron_drv
.subnet_get(subnet_id
)
431 def neutron_subnet_create(self
, **kwargs
):
432 return self
.neutron_drv
.subnet_create(**kwargs
)
434 def netruon_subnet_delete(self
, subnet_id
):
435 self
.neutron_drv
.subnet_delete(subnet_id
)
437 def neutron_subnetpool_list(self
):
438 return self
.neutron_drv
.subnetpool_list()
440 def netruon_subnetpool_by_name(self
, pool_name
):
441 pool_list
= self
.neutron_drv
.subnetpool_list(**{'name': pool_name
})
447 def neutron_port_list(self
, **kwargs
):
448 return self
.neutron_drv
.port_list(**kwargs
)
450 def neutron_port_get(self
, port_id
):
451 return self
.neutron_drv
.port_get(port_id
)
453 def neutron_port_create(self
, **kwargs
):
454 port_id
= self
.neutron_drv
.port_create([kwargs
])[0]
455 if 'vm_id' in kwargs
:
456 self
.nova_server_add_port(kwargs
['vm_id'], port_id
)
459 def neutron_multi_port_create(self
, ports
):
460 return self
.neutron_drv
.port_create(ports
)
462 def neutron_security_group_list(self
):
463 return self
.neutron_drv
.security_group_list(**{})
465 def neutron_security_group_by_name(self
, group_name
):
466 group_list
= self
.neutron_drv
.security_group_list(**{'name': group_name
})
472 def neutron_port_delete(self
, port_id
):
473 self
.neutron_drv
.port_delete(port_id
)
475 def ceilo_meter_endpoint(self
):
476 return self
.ceilo_drv
.endpoint
478 def ceilo_meter_list(self
):
479 return self
.ceilo_drv
.meters
481 def ceilo_nfvi_metrics(self
, vim_id
):
482 """Returns a dict of NFVI metrics for a given VM
485 vim_id - the VIM ID of the VM to retrieve the metrics for
488 A dict of NFVI metrics
491 return self
.ceilo_drv
.nfvi_metrics(vim_id
)
493 def ceilo_alarm_list(self
):
494 """Returns a list of ceilometer alarms"""
495 return self
.ceilo_drv
.client
.alarms
.list()
497 def ceilo_alarm_create(self
,
510 """Create a new Alarm
513 name - the name of the alarm
514 meter - the name of the meter to measure
515 statistic - the type of statistic used to trigger the alarm
516 ('avg', 'min', 'max', 'count', 'sum')
517 operation - the relational operator that, combined with the
518 threshold value, determines when the alarm is
519 triggered ('lt', 'le', 'eq', 'ge', 'gt')
520 threshold - the value of the statistic that will trigger the
522 period - the duration (seconds) over which to evaluate the
524 evaluations - the number of samples of the meter statistic to
525 collect when evaluating the threshold
526 severity - a measure of the urgency or importance of the alarm
527 ('low', 'moderate', 'critical')
528 repeat - a flag that indicates whether the alarm should be
529 triggered once (False) or repeatedly while the alarm
530 condition is true (True)
531 enabled - a flag that indicates whether the alarm is enabled
532 (True) or disabled (False)
533 actions - a dict specifying the URLs for webhooks. The dict can
534 have up to 3 keys: 'insufficient_data', 'alarm',
535 'ok'. Each key is associated with a list of URLs to
536 webhooks that will be invoked when one of the 3
538 kwargs - an arbitrary dict of keyword arguments that are
539 passed to the ceilometer client
542 ok_actions
= actions
.get('ok') if actions
is not None else None
543 alarm_actions
= actions
.get('alarm') if actions
is not None else None
544 insufficient_data_actions
= actions
.get('insufficient_data') if actions
is not None else None
546 return self
.ceilo_drv
.client
.alarms
.create(name
=name
,
549 comparison_operator
=operation
,
552 evaluation_periods
=evaluations
,
554 repeat_actions
=repeat
,
556 ok_actions
=ok_actions
,
557 alarm_actions
=alarm_actions
,
558 insufficient_data_actions
=insufficient_data_actions
,
561 def ceilo_alarm_update(self
, alarm_id
, **kwargs
):
562 """Updates an existing alarm
565 alarm_id - the identifier of the alarm to update
566 kwargs - a dict of the alarm attributes to update
569 return self
.ceilo_drv
.client
.alarms
.update(alarm_id
, **kwargs
)
571 def ceilo_alarm_delete(self
, alarm_id
):
572 self
.ceilo_drv
.client
.alarms
.delete(alarm_id
)
574 def cinder_volume_list(self
):
575 return self
.cinder_drv
.volume_list()
577 def cinder_volume_get(self
,vol_id
):
578 return self
.cinder_drv
.volume_get(vol_id
)
580 def cinder_volume_set_metadata(self
, volumeid
, metadata
):
581 return self
.cinder_drv
.volume_set_metadata(volumeid
, metadata
)
583 def cinder_volume_delete_metadata(self
, volumeid
, metadata
):
584 return self
.cinder_drv
.volume_delete_metadata(volumeid
, metadata
)