Merge commands into the "vm" command

This commit is contained in:
Nico Schottelius 2020-03-05 17:52:01 +01:00
parent b8c2f80e45
commit efbe1c0596
3 changed files with 85 additions and 47 deletions

View file

@ -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

View file

@ -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")

View file

@ -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")