本文整理汇总了Python中service.driver.get_driver函数的典型用法代码示例。如果您正苦于以下问题:Python get_driver函数的具体用法?Python get_driver怎么用?Python get_driver使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_driver函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mount_failed
def mount_failed(
context,
exception_msg,
traceback,
driverCls,
provider,
identity,
volume_id,
unmount=False,
**celery_task_args
):
from service import volume as volume_service
try:
celery_logger.debug("mount_failed task started at %s." % timezone.now())
celery_logger.info("task context=%s" % context)
err_str = "%s\nMount Error Traceback:%s" % (exception_msg, traceback)
celery_logger.error(err_str)
driver = get_driver(driverCls, provider, identity)
volume = driver.get_volume(volume_id)
if unmount:
tmp_status = 'umount_error'
else:
tmp_status = 'mount_error'
return volume_service._update_volume_metadata(
driver, volume, metadata={'tmp_status': tmp_status}
)
except Exception as exc:
celery_logger.warn(exc)
mount_failed.retry(exc=exc)
示例2: _deploy_init_to
def _deploy_init_to(driverCls, provider, identity, instance_id, password=None,
**celery_task_args):
try:
logger.debug("_deploy_init_to task started at %s." % datetime.now())
#Check if instance still exists
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
if not instance:
logger.debug("Instance has been teminated: %s." % instance_id)
return
#NOTE: This is NOT the password passed by argument
#Deploy with no password to use ssh keys
logger.info(instance.extra)
instance._node.extra['password'] = None
kwargs = {}
private_key = "/opt/dev/atmosphere/extras/ssh/id_rsa"
kwargs.update({'ssh_key': private_key})
kwargs.update({'timeout': 120})
msd = init(instance, identity.user.username, password)
kwargs.update({'deploy': msd})
driver.deploy_to(instance, **kwargs)
logger.debug("_deploy_init_to task finished at %s." % datetime.now())
except DeploymentError as exc:
logger.exception(exc)
if isinstance(exc.value, NonZeroDeploymentException):
#The deployment was successful, but the return code on one or more
# steps is bad. Log the exception and do NOT try again!
raise exc.value
#TODO: Check if all exceptions thrown at this time
#fall in this category, and possibly don't retry if
#you hit the Exception block below this.
_deploy_init_to.retry(exc=exc)
except Exception as exc:
logger.exception(exc)
_deploy_init_to.retry(exc=exc)
示例3: mount_volume_task
def mount_volume_task(
driverCls,
provider,
identity,
instance_id,
volume_id,
device_location,
mount_location,
device_type,
mount_prefix=None,
*args,
**kwargs
):
try:
celery_logger.debug("mount task started at %s." % timezone.now())
celery_logger.debug("mount_location: %s" % (mount_location, ))
driver = get_driver(driverCls, provider, identity)
username = identity.get_username()
instance = driver.get_instance(instance_id)
volume = driver.get_volume(volume_id)
try:
attach_data = volume.extra['attachments'][0]
if not device_location:
device_location = attach_data['device']
except (KeyError, IndexError):
celery_logger.warn(
"Volume %s missing attachments in Extra" % (volume, )
)
if not device_location:
raise Exception(
"No device_location found or inferred by volume %s" % volume
)
if not mount_prefix:
mount_prefix = "/vol_"
last_char = device_location[-1] # /dev/sdb --> b
if not mount_location:
mount_location = mount_prefix + last_char
playbook_results = deploy_mount_volume(
instance.ip,
username,
instance.id,
device_location,
mount_location=mount_location,
device_type=device_type
)
celery_logger.info(playbook_results)
if execution_has_failures(
playbook_results
) or execution_has_unreachable(playbook_results):
raise Exception(
"Error encountered while mounting volume: instance_id: {}, volume_id: {}"
.format(instance_id, volume_id)
)
return mount_location
except Exception as exc:
celery_logger.warn(exc)
mount_volume_task.retry(exc=exc)
示例4: umount_task
def umount_task(driverCls, provider, identity, instance_id, volume_id, *args, **kwargs):
try:
celery_logger.debug("umount_task started at %s." % datetime.now())
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
volume = driver.get_volume(volume_id)
attach_data = volume.extra["attachments"][0]
device = attach_data["device"]
# Check mount to find the mount_location for device
private_key = "/opt/dev/atmosphere/extras/ssh/id_rsa"
kwargs.update({"ssh_key": private_key})
kwargs.update({"timeout": 120})
mount_location = None
cm_script = check_mount()
kwargs.update({"deploy": cm_script})
driver.deploy_to(instance, **kwargs)
regex = re.compile("(?P<device>[\w/]+) on (?P<location>.*) type")
for line in cm_script.stdout.split("\n"):
res = regex.search(line)
if not res:
continue
search_dict = res.groupdict()
dev_found = search_dict["device"]
if device == dev_found:
mount_location = search_dict["location"]
break
# Volume not mounted, move along..
if not mount_location:
return
um_script = umount_volume(device)
kwargs.update({"deploy": um_script})
driver.deploy_to(instance, **kwargs)
if "device is busy" in um_script.stdout:
# Show all processes that are making device busy..
lsof_script = lsof_location(mount_location)
kwargs.update({"deploy": lsof_script})
driver.deploy_to(instance, **kwargs)
regex = re.compile("(?P<name>[\w]+)\s*(?P<pid>[\d]+)")
offending_processes = []
for line in lsof_script.stdout.split("\n"):
res = regex.search(line)
if not res:
continue
search_dict = res.groupdict()
offending_processes.append((search_dict["name"], search_dict["pid"]))
raise DeviceBusyException(mount_location, offending_processes)
# Return here if no errors occurred..
celery_logger.debug("umount_task finished at %s." % datetime.now())
except DeviceBusyException:
raise
except Exception as exc:
celery_logger.warn(exc)
umount_task.retry(exc=exc)
示例5: deploy_script
def deploy_script(driverCls, provider, identity, instance_id,
script, **celery_task_args):
try:
logger.debug("deploy_script task started at %s." % datetime.now())
#Check if instance still exists
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
if not instance:
logger.debug("Instance has been teminated: %s." % instance_id)
return
instance._node.extra['password'] = None
kwargs = _generate_ssh_kwargs()
kwargs.update({'deploy': script})
driver.deploy_to(instance, **kwargs)
logger.debug("deploy_script task finished at %s." % datetime.now())
except DeploymentError as exc:
logger.exception(exc)
if isinstance(exc.value, NonZeroDeploymentException):
#The deployment was successful, but the return code on one or more
# steps is bad. Log the exception and do NOT try again!
raise exc.value
#TODO: Check if all exceptions thrown at this time
#fall in this category, and possibly don't retry if
#you hit the Exception block below this.
deploy_script.retry(exc=exc)
except Exception as exc:
logger.exception(exc)
deploy_script.retry(exc=exc)
示例6: update_mount_location
def update_mount_location(new_mount_location,
driverCls, provider, identity,
volume_alias):
"""
"""
from service import volume as volume_service
try:
celery_logger.debug(
"update_mount_location task started at %s." %
datetime.now())
driver = get_driver(driverCls, provider, identity)
volume = driver.get_volume(volume_alias)
if not volume:
return
if not new_mount_location:
return
volume_metadata = volume.extra['metadata']
return volume_service._update_volume_metadata(
driver, volume,
metadata={'mount_location': new_mount_location})
celery_logger.debug(
"update_mount_location task finished at %s." %
datetime.now())
except Exception as exc:
celery_logger.exception(exc)
update_mount_location.retry(exc=exc)
示例7: deploy_init_to
def deploy_init_to(driverCls, provider, identity, instance_id,
username=None, password=None, redeploy=False, *args, **kwargs):
try:
logger.debug("deploy_init_to task started at %s." % datetime.now())
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
if not instance:
logger.debug("Instance has been teminated: %s." % instance_id)
return
image_metadata = driver._connection\
.ex_get_image_metadata(instance.machine)
deploy_chain = get_deploy_chain(driverCls, provider, identity,
instance, username, password, redeploy)
deploy_chain.apply_async()
#Can be really useful when testing.
#if kwargs.get('delay'):
# async.get()
logger.debug("deploy_init_to task finished at %s." % datetime.now())
except SystemExit:
logger.exception("System Exits are BAD! Find this and get rid of it!")
raise Exception("System Exit called")
except NonZeroDeploymentException:
raise
except Exception as exc:
logger.warn(exc)
deploy_init_to.retry(exc=exc)
示例8: _deploy_init_to
def _deploy_init_to(driverCls, provider, identity, instance_id,
username=None, password=None, redeploy=False,
**celery_task_args):
try:
logger.debug("_deploy_init_to task started at %s." % datetime.now())
#Check if instance still exists
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
if not instance:
logger.debug("Instance has been teminated: %s." % instance_id)
return
#NOTE: This is unrelated to the password argument
logger.info(instance.extra)
instance._node.extra['password'] = None
msd = init(instance, identity.user.username, password, redeploy)
kwargs = _generate_ssh_kwargs()
kwargs.update({'deploy': msd})
driver.deploy_to(instance, **kwargs)
_update_status_log(instance, "Deploy Finished")
logger.debug("_deploy_init_to task finished at %s." % datetime.now())
except DeploymentError as exc:
logger.exception(exc)
if isinstance(exc.value, NonZeroDeploymentException):
#The deployment was successful, but the return code on one or more
# steps is bad. Log the exception and do NOT try again!
raise exc.value
#TODO: Check if all exceptions thrown at this time
#fall in this category, and possibly don't retry if
#you hit the Exception block below this.
_deploy_init_to.retry(exc=exc)
except Exception as exc:
logger.exception(exc)
_deploy_init_to.retry(exc=exc)
示例9: _send_instance_email
def _send_instance_email(driverCls, provider, identity, instance_id):
try:
logger.debug("_send_instance_email task started at %s." %
datetime.now())
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
#Breakout if instance has been deleted at this point
if not instance:
logger.debug("Instance has been teminated: %s." % instance_id)
return
username = identity.user.username
profile = UserProfile.objects.get(user__username=username)
if profile.send_emails:
#Only send emails if allowed by profile setting
created = datetime.strptime(instance.extra['created'],
"%Y-%m-%dT%H:%M:%SZ")
send_instance_email(username,
instance.id,
instance.name,
instance.ip,
created,
username)
else:
logger.debug("User %s elected NOT to receive new instance emails"
% username)
logger.debug("_send_instance_email task finished at %s." %
datetime.now())
except Exception as exc:
logger.warn(exc)
_send_instance_email.retry(exc=exc)
示例10: attach_task
def attach_task(driverCls, provider, identity, instance_id, volume_id,
device_choice=None, *args, **kwargs):
try:
celery_logger.debug("attach_task started at %s." % datetime.now())
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
volume = driver.get_volume(volume_id)
# Step 1. Attach the volume
# NOTE: device_choice !== device 100%
driver.attach_volume(instance,
volume,
device_choice)
# When the reslt returns the volume will be 'attaching'
# We can't do anything until the volume is 'available/in-use'
attempts = 0
while True:
volume = driver.get_volume(volume_id)
# Give up if you can't find the volume
if not volume:
return None
if attempts > 6: # After 6 attempts (~1min)
break
# Openstack Check
if isinstance(driver, OSDriver) and\
'attaching' not in volume.extra.get('status', ''):
break
if isinstance(driver, EucaDriver) and\
'attaching' not in volume.extra.get('status', ''):
break
# Exponential backoff..
attempts += 1
sleep_time = 2**attempts
celery_logger.debug("Volume %s is not ready (%s). Sleep for %s"
% (volume.id, volume.extra.get('status', 'no-status'),
sleep_time))
time.sleep(sleep_time)
if 'available' in volume.extra.get('status', ''):
raise Exception("Volume %s failed to attach to instance %s"
% (volume.id, instance.id))
# Device path for euca == openstack
try:
attach_data = volume.extra['attachments'][0]
device = attach_data['device']
except (IndexError, KeyError) as bad_fetch:
celery_logger.warn("Could not find 'device' in "
"volume.extra['attachments']: "
"Volume:%s Extra:%s" % (volume.id, volume.extra))
device = None
celery_logger.debug("attach_task finished at %s." % datetime.now())
return device
except Exception as exc:
celery_logger.warn(exc)
attach_task.retry(exc=exc)
示例11: attach_task
def attach_task(driverCls, provider, identity, instance_id, volume_id,
device_choice=None, *args, **kwargs):
#TODO: chain task attach THEN mount for more robust-ness
try:
logger.debug("attach_task started at %s." % datetime.now())
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
volume = driver.get_volume(volume_id)
#Step 1. Attach the volume
#NOTE: device_choice !== device 100%
driver.attach_volume(instance,
volume,
device_choice)
#When the reslt returns the volume will be 'attaching'
#We can't do anything until the volume is 'available/in-use'
attempts = 0
while True:
volume = driver.get_volume(volume_id)
# Give up if you can't find the volume
if not volume:
return None
if attempts > 6: # After 6 attempts (~1min)
break
#Openstack Check
if isinstance(driver, OSDriver) and\
'attaching' not in volume.extra.get('status',''):
break
attach_set = volume.extra['attachmentSet'][0]
if isinstance(driver, EucaDriver) and\
'attaching' not in attach_set.get('status', ''):
break
# Exponential backoff..
attempts += 1
sleep_time = 2**attempts
logger.debug("Volume %s is not ready (%s). Sleep for %s"
% (volume.id, volume.extra.get('status', 'no-status'),
sleep_time))
time.sleep(sleep_time)
if 'available' in volume.extra.get('status',''):
raise Exception("Volume %s failed to attach to instance %s"
% (volume.id, instance.id))
#Device path for euca == openstack
try:
device = volume.extra['attachmentSet'][0]['device']
except:
device = None
logger.debug("attach_task finished at %s." % datetime.now())
return device
except Exception as exc:
logger.warn(exc)
attach_task.retry(exc=exc)
示例12: clean_empty_ips
def clean_empty_ips(driverCls, provider, identity, *args, **kwargs):
try:
logger.debug("remove_floating_ip task started at %s." % datetime.now())
driver = get_driver(driverCls, provider, identity)
ips_cleaned = driver._clean_floating_ip()
logger.debug("remove_floating_ip task finished at %s." % datetime.now())
return ips_cleaned
except Exception as exc:
logger.warn(exc)
clean_empty_ips.retry(exc=exc)
示例13: deploy_to
def deploy_to(driverCls, provider, identity, instance_id, *args, **kwargs):
try:
logger.debug("deploy_to task started at %s." % datetime.now())
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
driver.deploy_to(instance, *args, **kwargs)
logger.debug("deploy_to task finished at %s." % datetime.now())
except Exception as exc:
logger.warn(exc)
deploy_to.retry(exc=exc)
示例14: add_floating_ip
def add_floating_ip(driverCls, provider, identity,
instance_alias, delete_status=True,
*args, **kwargs):
#For testing ONLY.. Test cases ignore countdown..
if app.conf.CELERY_ALWAYS_EAGER:
logger.debug("Eager task waiting 15 seconds")
time.sleep(15)
try:
logger.debug("add_floating_ip task started at %s." % datetime.now())
#Remove unused floating IPs first, so they can be re-used
driver = get_driver(driverCls, provider, identity)
driver._clean_floating_ip()
#assign if instance doesn't already have an IP addr
instance = driver.get_instance(instance_alias)
if not instance:
logger.debug("Instance has been teminated: %s." % instance_alias)
return None
floating_ips = driver._connection.neutron_list_ips(instance)
if floating_ips:
floating_ip = floating_ips[0]["floating_ip_address"]
else:
floating_ip = driver._connection.neutron_associate_ip(
instance, *args, **kwargs)["floating_ip_address"]
_update_status_log(instance, "Networking Complete")
#TODO: Implement this as its own task, with the result from
#'floating_ip' passed in. Add it to the deploy_chain before deploy_to
hostname = ""
if floating_ip.startswith('128.196'):
regex = re.compile(
"(?P<one>[0-9]+)\.(?P<two>[0-9]+)\."
"(?P<three>[0-9]+)\.(?P<four>[0-9]+)")
r = regex.search(floating_ip)
(one, two, three, four) = r.groups()
hostname = "vm%s-%s.iplantcollaborative.org" % (three, four)
else:
# Find a way to convert new floating IPs to hostnames..
hostname = floating_ip
update_instance_metadata(driver, instance, data={
'public-hostname': hostname,
'public-ip':floating_ip}, replace=False)
logger.info("Assigned IP:%s - Hostname:%s" % (floating_ip, hostname))
#End
logger.debug("add_floating_ip task finished at %s." % datetime.now())
return {"floating_ip":floating_ip, "hostname":hostname}
except Exception as exc:
logger.exception("Error occurred while assigning a floating IP")
#Networking can take a LONG time when an instance first launches,
#it can also be one of those things you 'just miss' by a few seconds..
#So we will retry 30 times using limited exp.backoff
#Max Time: 53min
countdown = min(2**current.request.retries, 128)
add_floating_ip.retry(exc=exc,
countdown=countdown)
示例15: mount_task
def mount_task(driverCls, provider, identity, instance_id, volume_id,
mount_location=None, *args, **kwargs):
try:
logger.debug("mount task started at %s." % datetime.now())
logger.debug("mount_location: %s" % (mount_location, ))
driver = get_driver(driverCls, provider, identity)
instance = driver.get_instance(instance_id)
volume = driver.get_volume(volume_id)
logger.debug(volume)
try:
device = volume.extra['attachmentSet'][0]['device']
except:
device = None
if not device:
#Device was never attached -- Nothing to mount
return
private_key = "/opt/dev/atmosphere/extras/ssh/id_rsa"
kwargs.update({'ssh_key': private_key})
kwargs.update({'timeout': 120})
#Step 2. Check the volume is not already mounted
cm_script = check_mount()
kwargs.update({'deploy': cm_script})
driver.deploy_to(instance, **kwargs)
if device in cm_script.stdout:
#Device has already been mounted. Move along..
return
#Step 3. Find a suitable location to mount the volume
logger.info("Original mount location - %s" % mount_location)
if not mount_location:
inc = 1
while True:
if '/vol%s' % inc in cm_script.stdout:
inc += 1
else:
break
mount_location = '/vol%s' % inc
logger.info("Device location - %s" % device)
logger.info("New mount location - %s" % mount_location)
mv_script = mount_volume(device, mount_location)
kwargs.update({'deploy': mv_script})
driver.deploy_to(instance, **kwargs)
#TODO: Update metadata to include volume mount
# so this data can persist on the UI?
logger.debug("mount task finished at %s." % datetime.now())
return mount_location
except Exception as exc:
logger.warn(exc)
mount_task.retry(exc=exc)