dynamicweb/datacenterlight/tasks.py

235 lines
8.8 KiB
Python
Raw Normal View History

from datetime import datetime
2018-07-01 09:05:23 +00:00
from celery import current_task
from celery.exceptions import MaxRetriesExceededError
2017-08-06 16:42:27 +00:00
from celery.utils.log import get_task_logger
from django.conf import settings
from django.core.mail import EmailMessage
from django.core.urlresolvers import reverse
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from dynamicweb.celery import app
from hosting.models import HostingOrder, HostingBill
from membership.models import StripeCustomer, CustomUser
2017-08-06 16:42:27 +00:00
from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import VirtualMachineSerializer
from utils.forms import UserBillingAddressForm
2018-07-01 09:05:23 +00:00
from utils.hosting_utils import get_all_public_keys, get_or_create_vm_detail
from utils.mailer import BaseEmail
2017-08-06 16:42:27 +00:00
from utils.models import BillingAddress
from .models import VMPricing
2017-08-06 16:42:27 +00:00
logger = get_task_logger(__name__)
def retry_task(task, exception=None):
"""Retries the specified task using a "backing off countdown",
meaning that the interval between retries grows exponentially
with every retry.
Arguments:
task:
The task to retry.
exception:
Optionally, the exception that caused the retry.
"""
def backoff(attempts):
return 2 ** attempts
kwargs = {
'countdown': backoff(task.request.retries),
}
if exception:
kwargs['exc'] = exception
raise task.retry(**kwargs)
2017-08-06 16:42:27 +00:00
@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES)
def create_vm_task(self, vm_template_id, user, specs, template,
stripe_customer_id, billing_address_data,
stripe_subscription_id, cc_details):
2017-09-23 07:39:14 +00:00
logger.debug(
"Running create_vm_task on {}".format(current_task.request.hostname))
vm_id = None
2017-08-06 16:42:27 +00:00
try:
final_price = (specs.get('total_price') if 'total_price' in specs
else specs.get('price'))
billing_address = BillingAddress(
cardholder_name=billing_address_data['cardholder_name'],
street_address=billing_address_data['street_address'],
city=billing_address_data['city'],
postal_code=billing_address_data['postal_code'],
country=billing_address_data['country']
)
billing_address.save()
2017-08-06 16:42:27 +00:00
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
2017-09-10 09:56:29 +00:00
if 'pass' in user:
2017-09-10 09:56:29 +00:00
on_user = user.get('email')
on_pass = user.get('pass')
logger.debug("Using user {user} to create VM".format(user=on_user))
vm_name = None
else:
2017-09-10 09:56:29 +00:00
on_user = settings.OPENNEBULA_USERNAME
on_pass = settings.OPENNEBULA_PASSWORD
logger.debug("Using OpenNebula admin user to create VM")
vm_name = "{email}-{template_name}-{date}".format(
email=user.get('email'),
template_name=template.get('name'),
date=int(datetime.now().strftime("%s")))
2017-08-06 16:42:27 +00:00
2017-09-10 09:56:29 +00:00
# Create OpenNebulaManager
manager = OpenNebulaManager(email=on_user, password=on_pass)
2017-08-06 16:42:27 +00:00
vm_id = manager.create_vm(
template_id=vm_template_id,
specs=specs,
ssh_key=settings.ONEADMIN_USER_SSH_PUBLIC_KEY,
vm_name=vm_name
2017-08-06 16:42:27 +00:00
)
if vm_id is None:
raise Exception("Could not create VM")
2018-04-16 03:03:48 +00:00
vm_pricing = VMPricing.get_vm_pricing_by_name(
name=specs['pricing_name']
) if 'pricing_name' in specs else VMPricing.get_default_pricing()
# Create a Hosting Order
order = HostingOrder.create(
price=final_price,
vm_id=vm_id,
customer=customer,
billing_address=billing_address,
vm_pricing=vm_pricing
)
2017-08-06 16:42:27 +00:00
# Create a Hosting Bill
HostingBill.create(
2018-04-16 03:03:48 +00:00
customer=customer, billing_address=billing_address
)
2017-08-06 16:42:27 +00:00
# Create Billing Address for User if he does not have one
if not customer.user.billing_addresses.count():
billing_address_data.update({
'user': customer.user.id
})
billing_address_user_form = UserBillingAddressForm(
billing_address_data)
billing_address_user_form.is_valid()
billing_address_user_form.save()
2017-08-20 20:24:55 +00:00
# Associate an order with a stripe subscription
order.set_subscription_id(stripe_subscription_id, cc_details)
2017-08-06 16:42:27 +00:00
# If the Stripe payment succeeds, set order status approved
order.set_approved()
vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data
context = {
'name': user.get('name'),
'email': user.get('email'),
'cores': specs.get('cpu'),
'memory': specs.get('memory'),
'storage': specs.get('disk_size'),
2018-04-15 23:24:21 +00:00
'price': final_price,
2017-08-06 16:42:27 +00:00
'template': template.get('name'),
2017-09-26 21:58:06 +00:00
'vm_name': vm.get('name'),
'vm_id': vm['vm_id'],
'order_id': order.id
2017-08-06 16:42:27 +00:00
}
if 'pricing_name' in specs:
context['pricing'] = str(VMPricing.get_vm_pricing_by_name(
name=specs['pricing_name']
))
2017-08-06 16:42:27 +00:00
email_data = {
'subject': settings.DCL_TEXT + " Order from %s" % context['email'],
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
'to': ['info@ungleich.ch'],
'body': "\n".join(
["%s=%s" % (k, v) for (k, v) in context.items()]),
2017-08-06 16:42:27 +00:00
'reply_to': [context['email']],
}
email = EmailMessage(**email_data)
email.send()
2017-09-15 07:13:48 +00:00
if 'pass' in user:
lang = 'en-us'
if user.get('language') is not None:
2017-09-23 07:39:14 +00:00
logger.debug(
"Language is set to {}".format(user.get('language')))
lang = user.get('language')
translation.activate(lang)
# Send notification to the user as soon as VM has been booked
context = {
'base_url': "{0}://{1}".format(user.get('request_scheme'),
user.get('request_host')),
'order_url': reverse('hosting:orders',
kwargs={'pk': order.id}),
'page_header': _(
2018-07-01 09:05:23 +00:00
'Your New VM %(vm_name)s at Data Center Light') %
{'vm_name': vm.get('name')},
2017-09-26 22:06:02 +00:00
'vm_name': vm.get('name')
}
email_data = {
'subject': context.get('page_header'),
'to': user.get('email'),
'context': context,
'template_name': 'new_booked_vm',
'template_path': 'hosting/emails/',
'from_address': settings.DCL_SUPPORT_FROM_ADDRESS,
}
email = BaseEmail(**email_data)
email.send()
# try to see if we have the IPv6 of the new vm and that if the ssh
# keys can be configured
vm_ipv6 = manager.get_ipv6(vm_id)
logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id))
if vm_ipv6 is not None:
custom_user = CustomUser.objects.get(email=user.get('email'))
2017-09-24 18:32:36 +00:00
get_or_create_vm_detail(custom_user, manager, vm_id)
if custom_user is not None:
public_keys = get_all_public_keys(custom_user)
keys = [{'value': key, 'state': True} for key in
public_keys]
if len(keys) > 0:
logger.debug(
"Calling configure on {host} for "
"{num_keys} keys".format(
host=vm_ipv6, num_keys=len(keys)
)
)
# Let's delay the task by 75 seconds to be sure
# that we run the cdist configure after the host
# is up
manager.manage_public_key(
keys, hosts=[vm_ipv6], countdown=75
)
2017-08-06 16:42:27 +00:00
except Exception as e:
logger.error(str(e))
try:
retry_task(self)
except MaxRetriesExceededError:
msg_text = 'Finished {} retries for create_vm_task'.format(
self.request.retries)
logger.error(msg_text)
# Try sending email and stop
email_data = {
'subject': '{} CELERY TASK ERROR: {}'.format(settings.DCL_TEXT,
msg_text),
'from_email': current_task.request.hostname,
'to': settings.DCL_ERROR_EMAILS_TO_LIST,
2017-08-06 20:19:27 +00:00
'body': ',\n'.join(str(i) for i in self.request.args)
}
email = EmailMessage(**email_data)
email.send()
return
2017-08-06 16:42:27 +00:00
return vm_id