From 890a83cfa69335564964c421473cb2f5d23d72fb Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 10 Dec 2020 08:34:17 +0530 Subject: [PATCH 1/6] Add check_vm_templates management command --- .../management/commands/check_vm_templates.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 datacenterlight/management/commands/check_vm_templates.py diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py new file mode 100644 index 00000000..ee86f15d --- /dev/null +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -0,0 +1,62 @@ +from django.core.management.base import BaseCommand +from opennebula_api.models import OpenNebulaManager +from datacenterlight.models import VMTemplate +from membership.models import CustomUser + +from django.conf import settings +from time import sleep +import datetime +import json +import logging +import os + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = '''Checks all VM templates to find if they can be instantiated''' + + def add_arguments(self, parser): + parser.add_argument('user_email', type=str) + + def handle(self, *args, **options): + result_dict = {} + user_email = options['user_email'] if 'user_email' in options else "" + + if user_email: + cu = CustomUser.objects.get(email=user_email) + specs = {'cpu': 1, 'memory': 1, 'disk_size': 10} + manager = OpenNebulaManager(email=user_email, password=cu.password) + pub_keys = [settings.TEST_MANAGE_SSH_KEY_PUBKEY] + if not os.path.exists("outputs"): + os.mkdir("outputs") + for vm_template in VMTemplate.objects.all(): + vm_name = 'test-%s' % vm_template.name + vm_id = manager.create_vm( + template_id=vm_template.opennebula_vm_template_id, + specs=specs, + ssh_key='\n'.join(pub_keys), + vm_name=vm_name + ) + if vm_id > 0: + result_dict[vm_name] = "%s OK, created VM %s" % ( + '%s %s' % (vm_template.opennebula_vm_template_id, + vm_template.name), + vm_id + ) + self.stdout.write(self.style.SUCCESS(result_dict[vm_name])) + manager.delete_vm(vm_id) + else: + result_dict[vm_name] = '''Error creating VM %s, template_id + %s ''' % (vm_name, vm_template.opennebula_vm_template_id) + self.stdout.write(self.style.ERROR(result_dict[vm_name])) + sleep(1) + date_str = datetime.datetime.strftime( + datetime.datetime.now(), '%Y%m%d%H%M%S' + ) + with open("output/check_vm_templates_%s.txt" % date_str, 'w', + encoding='utf-8') as f: + f.write(json.dumps(result_dict)) + else: + self.stdout.write(self.style.ERROR("user_email not supplied")) + self.stdout.write(self.style.SUCCESS("Done")) From 785091e4ff80ab17e439165600ad0fa75ceac395 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 10 Dec 2020 08:40:40 +0530 Subject: [PATCH 2/6] Handle vm_id is None case --- datacenterlight/management/commands/check_vm_templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py index ee86f15d..b75e9751 100644 --- a/datacenterlight/management/commands/check_vm_templates.py +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -38,7 +38,7 @@ class Command(BaseCommand): ssh_key='\n'.join(pub_keys), vm_name=vm_name ) - if vm_id > 0: + if vm_id and vm_id > 0: result_dict[vm_name] = "%s OK, created VM %s" % ( '%s %s' % (vm_template.opennebula_vm_template_id, vm_template.name), From 01d8cc1b9bfebf5b5369cf4e79933e24ac5ba0c8 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 10 Dec 2020 08:56:34 +0530 Subject: [PATCH 3/6] Use absolute paths --- .../management/commands/check_vm_templates.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py index b75e9751..6346be50 100644 --- a/datacenterlight/management/commands/check_vm_templates.py +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -28,8 +28,9 @@ class Command(BaseCommand): specs = {'cpu': 1, 'memory': 1, 'disk_size': 10} manager = OpenNebulaManager(email=user_email, password=cu.password) pub_keys = [settings.TEST_MANAGE_SSH_KEY_PUBKEY] - if not os.path.exists("outputs"): - os.mkdir("outputs") + PROJECT_PATH = os.path.abspath(os.path.dirname(__name__)) + if not os.path.exists("%s/outputs" % PROJECT_PATH): + os.mkdir("%s/outputs" % PROJECT_PATH) for vm_template in VMTemplate.objects.all(): vm_name = 'test-%s' % vm_template.name vm_id = manager.create_vm( @@ -54,7 +55,9 @@ class Command(BaseCommand): date_str = datetime.datetime.strftime( datetime.datetime.now(), '%Y%m%d%H%M%S' ) - with open("output/check_vm_templates_%s.txt" % date_str, 'w', + with open("%s/outputs/check_vm_templates_%s.txt" % + (PROJECT_PATH, date_str), + 'w', encoding='utf-8') as f: f.write(json.dumps(result_dict)) else: From 22d3b1f83c465dd1bbda416c731de1b3692dfba4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 10 Dec 2020 09:05:04 +0530 Subject: [PATCH 4/6] Add vm_type to the log info --- datacenterlight/management/commands/check_vm_templates.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py index 6346be50..6424df80 100644 --- a/datacenterlight/management/commands/check_vm_templates.py +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -41,15 +41,17 @@ class Command(BaseCommand): ) if vm_id and vm_id > 0: result_dict[vm_name] = "%s OK, created VM %s" % ( - '%s %s' % (vm_template.opennebula_vm_template_id, - vm_template.name), + '%s %s %s' % (vm_template.opennebula_vm_template_id, + vm_template.name, vm_template.vm_type), vm_id ) self.stdout.write(self.style.SUCCESS(result_dict[vm_name])) manager.delete_vm(vm_id) else: result_dict[vm_name] = '''Error creating VM %s, template_id - %s ''' % (vm_name, vm_template.opennebula_vm_template_id) + %s %s''' % (vm_name, + vm_template.opennebula_vm_template_id, + vm_template.vm_type) self.stdout.write(self.style.ERROR(result_dict[vm_name])) sleep(1) date_str = datetime.datetime.strftime( From 57b6b18243306b534db1306054996c6fe1d93cc2 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 10 Dec 2020 10:04:04 +0530 Subject: [PATCH 5/6] Fix PEP warning --- datacenterlight/management/commands/check_vm_templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py index 6424df80..be0a1957 100644 --- a/datacenterlight/management/commands/check_vm_templates.py +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -42,7 +42,7 @@ class Command(BaseCommand): if vm_id and vm_id > 0: result_dict[vm_name] = "%s OK, created VM %s" % ( '%s %s %s' % (vm_template.opennebula_vm_template_id, - vm_template.name, vm_template.vm_type), + vm_template.name, vm_template.vm_type), vm_id ) self.stdout.write(self.style.SUCCESS(result_dict[vm_name])) From 504681107af92d60d566a33eda7635aa49e7b496 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 10 Dec 2020 10:04:16 +0530 Subject: [PATCH 6/6] Remove unreachable code --- datacenterlight/management/commands/check_vm_templates.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py index be0a1957..db36fde8 100644 --- a/datacenterlight/management/commands/check_vm_templates.py +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -62,6 +62,4 @@ class Command(BaseCommand): 'w', encoding='utf-8') as f: f.write(json.dumps(result_dict)) - else: - self.stdout.write(self.style.ERROR("user_email not supplied")) self.stdout.write(self.style.SUCCESS("Done"))