From efbe1c0596d487208d867f6411bb762d625081ed Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Thu, 5 Mar 2020 17:52:01 +0100 Subject: [PATCH] Merge commands into the "vm" command --- .../management/commands/schedulevms.py | 21 ----- uncloud/uncloud_vm/management/commands/vm.py | 85 +++++++++++++++++++ .../management/commands/vmhealth.py | 26 ------ 3 files changed, 85 insertions(+), 47 deletions(-) delete mode 100644 uncloud/uncloud_vm/management/commands/schedulevms.py create mode 100644 uncloud/uncloud_vm/management/commands/vm.py delete mode 100644 uncloud/uncloud_vm/management/commands/vmhealth.py diff --git a/uncloud/uncloud_vm/management/commands/schedulevms.py b/uncloud/uncloud_vm/management/commands/schedulevms.py deleted file mode 100644 index 836e100..0000000 --- a/uncloud/uncloud_vm/management/commands/schedulevms.py +++ /dev/null @@ -1,21 +0,0 @@ -import json - -import uncloud.secrets as secrets - -from django.core.management.base import BaseCommand -from django.contrib.auth import get_user_model - -from uncloud_vm.models import VMProduct, VMHost - -class Command(BaseCommand): - help = 'Select VM Host for VMs' - - def add_arguments(self, parser): - pass - - def handle(self, *args, **options): - pending_vms = VMProduct.objects.filter(vmhost__isnull=True) - vmhosts = VMHost.objects.filter(status='active') - for vm in pending_vms: - print(vm) - # FIXME: implement smart placement diff --git a/uncloud/uncloud_vm/management/commands/vm.py b/uncloud/uncloud_vm/management/commands/vm.py new file mode 100644 index 0000000..c0e2783 --- /dev/null +++ b/uncloud/uncloud_vm/management/commands/vm.py @@ -0,0 +1,85 @@ +import json + +import uncloud.secrets as secrets + +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model + +from uncloud_vm.models import VMProduct, VMHost + +class Command(BaseCommand): + help = 'Select VM Host for VMs' + + def add_arguments(self, parser): + parser.add_argument('--schedule-vms', action='store_true') + parser.add_argument('--start-vms-here', action='store_true') + parser.add_argument('--check-health', action='store_true') + parser.add_argument('--vmhostname') + print(parser) + + + def handle(self, *args, **options): + print(args) + print(options) + + if options['schedule_vms']: + self.schedule_vms(args, option) + if options['start_vms_here']: + if not options['vmhostname']: + raise Exception("Argument vmhostname is required to know which vmhost we are on") + self.start_vms(args, options) + if options['check_health']: + self.check_health(args, option) + + def start_vms(self, *args, **options): + vmhost = VMHost.objects.get(status='active', + hostname=options['vmhostname']) + + if not vmhost: + print("No active vmhost {} exists".format(options['vmhostname'])) + return + + vms_to_start = VMProduct.objects.filter(vmhost=vmhost, + status='creating') + for vm in vms_to_start: + + """ run qemu: + check if VM is not already active / qemu running + prepare / create the Qemu arguments + + + """ + + def schedule_vms(self, *args, **options)): + pending_vms = VMProduct.objects.filter(vmhost__isnull=True) + vmhosts = VMHost.objects.filter(status='active') + + for vm in pending_vms: + print(vm) + + found_vmhost = False + for vmhost in vmhosts: + if vmhost.available_cores >= vm.cores and vmhost.available_ram_in_gb >= vm.ram_in_gb: + vm.vmhost = vmhost + vm.status = "creating" + vm.save() + found_vmhost = True + print("Scheduled VM {} on VMHOST {}".format(vm, vmhost)) + break + + if not found_vmhost: + print("Error: cannot schedule VM {}, no suitable host found".format(vm)) + + def check_health(self, *args, **options): + pending_vms = VMProduct.objects.filter(vmhost__isnull=True) + vmhosts = VMHost.objects.filter(status='active') + + # 1. Check that all active hosts reported back N seconds ago + # 2. Check that no VM is running on a dead host + # 3. Migrate VMs if necessary + # 4. Check that no VMs have been pending for longer than Y seconds + + # If VM snapshots exist without a VM -> notify user (?) + + + print("Nothing is good, you should implement me") diff --git a/uncloud/uncloud_vm/management/commands/vmhealth.py b/uncloud/uncloud_vm/management/commands/vmhealth.py deleted file mode 100644 index 9397b16..0000000 --- a/uncloud/uncloud_vm/management/commands/vmhealth.py +++ /dev/null @@ -1,26 +0,0 @@ -import json - -from django.core.management.base import BaseCommand -from django.contrib.auth import get_user_model - -from uncloud_vm.models import VMProduct, VMHost - -class Command(BaseCommand): - help = 'Check health of VMs and VMHosts' - - def add_arguments(self, parser): - pass - - def handle(self, *args, **options): - pending_vms = VMProduct.objects.filter(vmhost__isnull=True) - vmhosts = VMHost.objects.filter(status='active') - - # 1. Check that all active hosts reported back N seconds ago - # 2. Check that no VM is running on a dead host - # 3. Migrate VMs if necessary - # 4. Check that no VMs have been pending for longer than Y seconds - - # If VM snapshots exist without a VM -> notify user (?) - - - print("Nothing is good, you should implement me")