dynamicweb/datacenterlight/management/commands/fix_vm_after_celery_error.py

77 lines
2.7 KiB
Python
Raw Normal View History

2020-12-07 04:23:14 +00:00
from django.core.management.base import BaseCommand
from datacenterlight.tasks import handle_metadata_and_emails
from opennebula_api.models import OpenNebulaManager
2020-12-07 05:18:48 +00:00
from membership.models import CustomUser
2020-12-07 04:23:14 +00:00
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)
2020-12-07 05:01:45 +00:00
parser.add_argument('user', type=str)
2020-12-07 04:23:14 +00:00
parser.add_argument('specs', type=str)
2020-12-07 05:01:45 +00:00
parser.add_argument('template', type=str)
2020-12-07 04:23:14 +00:00
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("'", "\"")
2020-12-07 05:10:55 +00:00
user_dict = json.loads(json_acceptable_string)
2020-12-07 04:23:14 +00:00
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 = {
2020-12-07 05:10:55 +00:00
'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'],
2020-12-07 04:23:14 +00:00
}
2020-12-07 05:18:48 +00:00
cu = CustomUser.objects.get(username=user.get('username'))
2020-12-07 04:23:14 +00:00
# Create OpenNebulaManager
2020-12-07 05:22:06 +00:00
self.stdout.write(
self.style.SUCCESS(
'Connecting using %s' % (cu.username)
)
2020-12-07 05:14:20 +00:00
)
2020-12-07 05:18:48 +00:00
manager = OpenNebulaManager(email=cu.username, password=cu.password)
2020-12-07 04:23:14 +00:00
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)
)
)
)