Merge branch 'feature/fix-vm-after-celery-error' into 'master'
Feature/fix vm after celery error See merge request ungleich-public/dynamicweb!743
This commit is contained in:
commit
abfbc3b69a
2 changed files with 202 additions and 106 deletions
|
@ -0,0 +1,76 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from datacenterlight.tasks import handle_metadata_and_emails
|
||||
from opennebula_api.models import OpenNebulaManager
|
||||
from membership.models import CustomUser
|
||||
import logging
|
||||
import json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '''Updates the DB after manual creation of VM'''
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('vm_id', type=int)
|
||||
parser.add_argument('order_id', type=int)
|
||||
parser.add_argument('user', type=str)
|
||||
parser.add_argument('specs', type=str)
|
||||
parser.add_argument('template', type=str)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
vm_id = options['vm_id']
|
||||
order_id = options['order_id']
|
||||
user_str = options['user']
|
||||
specs_str = options['specs']
|
||||
template_str = options['template']
|
||||
|
||||
json_acceptable_string = user_str.replace("'", "\"")
|
||||
user_dict = json.loads(json_acceptable_string)
|
||||
|
||||
json_acceptable_string = specs_str.replace("'", "\"")
|
||||
specs = json.loads(json_acceptable_string)
|
||||
|
||||
json_acceptable_string = template_str.replace("'", "\"")
|
||||
template = json.loads(json_acceptable_string)
|
||||
if vm_id <= 0:
|
||||
self.stdout.write(self.style.ERROR(
|
||||
'vm_id can\'t be less than or 0. Given: %s' % vm_id))
|
||||
return
|
||||
if vm_id <= 0:
|
||||
self.stdout.write(self.style.ERROR(
|
||||
'order_id can\'t be less than or 0. Given: %s' % vm_id))
|
||||
return
|
||||
if specs_str is None or specs_str == "":
|
||||
self.stdout.write(
|
||||
self.style.ERROR('specs can\'t be empty or None'))
|
||||
return
|
||||
|
||||
user = {
|
||||
'name': user_dict['name'],
|
||||
'email': user_dict['email'],
|
||||
'username': user_dict['username'],
|
||||
'pass': user_dict['pass'],
|
||||
'request_scheme': user_dict['request_scheme'],
|
||||
'request_host': user_dict['request_host'],
|
||||
'language': user_dict['language'],
|
||||
}
|
||||
cu = CustomUser.objects.get(username=user.get('username'))
|
||||
# Create OpenNebulaManager
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
'Connecting using %s' % (cu.username)
|
||||
)
|
||||
)
|
||||
manager = OpenNebulaManager(email=cu.username, password=cu.password)
|
||||
handle_metadata_and_emails(order_id, vm_id, manager, user, specs,
|
||||
template)
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
'Done handling metadata and emails for %s %s %s' % (
|
||||
order_id,
|
||||
vm_id,
|
||||
str(user)
|
||||
)
|
||||
)
|
||||
)
|
|
@ -56,11 +56,6 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id):
|
|||
"Running create_vm_task on {}".format(current_task.request.hostname))
|
||||
vm_id = None
|
||||
try:
|
||||
final_price = (
|
||||
specs.get('total_price') if 'total_price' in specs
|
||||
else specs.get('price')
|
||||
)
|
||||
|
||||
if 'pass' in user:
|
||||
on_user = user.get('username')
|
||||
on_pass = user.get('pass')
|
||||
|
@ -92,6 +87,52 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id):
|
|||
if vm_id is None:
|
||||
raise Exception("Could not create VM")
|
||||
|
||||
handle_metadata_and_emails(order_id, vm_id, manager, user, specs,
|
||||
template)
|
||||
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,
|
||||
'body': ',\n'.join(str(i) for i in self.request.args)
|
||||
}
|
||||
email = EmailMessage(**email_data)
|
||||
email.send()
|
||||
return
|
||||
|
||||
return vm_id
|
||||
|
||||
|
||||
def handle_metadata_and_emails(order_id, vm_id, manager, user, specs,
|
||||
template):
|
||||
"""
|
||||
Handle's setting up of the metadata in Stripe and database and sending of
|
||||
emails to the user after VM creation
|
||||
|
||||
:param order_id: the hosting order id
|
||||
:param vm_id: the id of the vm created
|
||||
:param manager: the OpenNebula Manager instance
|
||||
:param user: the user's dict passed to the celery task
|
||||
:param specs: the specification's dict passed to the celery task
|
||||
:param template: the template dict passed to the celery task
|
||||
|
||||
:return:
|
||||
"""
|
||||
|
||||
custom_user = CustomUser.objects.get(email=user.get('email'))
|
||||
final_price = (
|
||||
specs.get('total_price') if 'total_price' in specs
|
||||
else specs.get('price')
|
||||
)
|
||||
# Update HostingOrder with the created vm_id
|
||||
hosting_order = HostingOrder.objects.filter(id=order_id).first()
|
||||
error_msg = None
|
||||
|
@ -193,24 +234,3 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id):
|
|||
logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id))
|
||||
if vm_id > 0:
|
||||
get_or_create_vm_detail(custom_user, manager, vm_id)
|
||||
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,
|
||||
'body': ',\n'.join(str(i) for i in self.request.args)
|
||||
}
|
||||
email = EmailMessage(**email_data)
|
||||
email.send()
|
||||
return
|
||||
|
||||
return vm_id
|
||||
|
|
Loading…
Reference in a new issue