2020-03-22 16:30:55 +00:00
|
|
|
import sys
|
2020-03-03 18:46:39 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils import timezone
|
2020-03-22 16:30:55 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-03-03 18:46:39 +00:00
|
|
|
|
|
|
|
from opennebula.models import VM as VMModel
|
|
|
|
from uncloud_vm.models import VMHost, VMProduct, VMNetworkCard, VMDiskImageProduct, VMDiskProduct
|
2020-03-17 15:03:41 +00:00
|
|
|
|
2020-03-03 18:46:39 +00:00
|
|
|
from uncloud_pay.models import Order
|
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2020-03-03 18:46:39 +00:00
|
|
|
|
2020-03-04 08:25:46 +00:00
|
|
|
def convert_mac_to_int(mac_address: str):
|
|
|
|
# Remove octet connecting characters
|
|
|
|
mac_address = mac_address.replace(':', '')
|
|
|
|
mac_address = mac_address.replace('.', '')
|
|
|
|
mac_address = mac_address.replace('-', '')
|
|
|
|
mac_address = mac_address.replace(' ', '')
|
|
|
|
|
|
|
|
# Parse the resulting number as hexadecimal
|
|
|
|
mac_address = int(mac_address, base=16)
|
|
|
|
|
|
|
|
return mac_address
|
|
|
|
|
|
|
|
|
2020-03-17 14:39:24 +00:00
|
|
|
def get_vm_price(core, ram, ssd_size, hdd_size, n_of_ipv4, n_of_ipv6):
|
|
|
|
total = 3 * core + 4 * ram + (3.5 * ssd_size/10.) + (1.5 * hdd_size/100.) + 8 * n_of_ipv4 + 0 * n_of_ipv6
|
2020-03-03 18:46:39 +00:00
|
|
|
|
|
|
|
# TODO: Find some reason about the following magical subtraction.
|
|
|
|
total -= 8
|
|
|
|
|
|
|
|
return total
|
|
|
|
|
|
|
|
|
|
|
|
def create_nics(one_vm, vm_product):
|
|
|
|
for nic in one_vm.nics:
|
2020-03-04 08:25:46 +00:00
|
|
|
mac_address = convert_mac_to_int(nic.get('MAC'))
|
2020-03-03 18:46:39 +00:00
|
|
|
ip_address = nic.get('IP', None) or nic.get('IP6_GLOBAL', None)
|
|
|
|
|
2020-03-04 08:25:46 +00:00
|
|
|
VMNetworkCard.objects.update_or_create(
|
|
|
|
mac_address=mac_address, vm=vm_product, defaults={'ip_address': ip_address}
|
2020-03-03 18:46:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
def sync_disk_and_image(one_vm, vm_product, disk_owner):
|
|
|
|
"""
|
|
|
|
a) Check all opennebula disk if they are in the uncloud VM, if not add
|
|
|
|
b) Check all uncloud disks and remove them if they are not in the opennebula VM
|
|
|
|
"""
|
|
|
|
|
|
|
|
vmdisknum = 0
|
|
|
|
|
|
|
|
one_disks_extra_data = []
|
|
|
|
|
2020-03-03 18:46:39 +00:00
|
|
|
for disk in one_vm.disks:
|
2020-03-22 16:30:55 +00:00
|
|
|
vmowner = one_vm.owner
|
2020-03-03 18:46:39 +00:00
|
|
|
name = disk.get('image')
|
2020-03-22 16:30:55 +00:00
|
|
|
vmdisknum += 1
|
2020-03-03 18:46:39 +00:00
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
log.info("Checking disk {} for VM {}".format(name, one_vm))
|
|
|
|
|
|
|
|
is_os_image, is_public, status = True, False, 'active'
|
2020-03-03 18:46:39 +00:00
|
|
|
|
|
|
|
image_size_in_gb = disk.get('image_size_in_gb')
|
|
|
|
disk_size_in_gb = disk.get('size_in_gb')
|
2020-03-22 16:30:55 +00:00
|
|
|
storage_class = disk.get('storage_class')
|
2020-03-03 18:46:39 +00:00
|
|
|
image_source = disk.get('source')
|
|
|
|
image_source_type = disk.get('source_type')
|
|
|
|
|
2020-03-04 08:25:46 +00:00
|
|
|
image, _ = VMDiskImageProduct.objects.update_or_create(
|
|
|
|
name=name,
|
|
|
|
defaults={
|
2020-03-22 16:30:55 +00:00
|
|
|
'owner': disk_owner,
|
2020-03-04 08:25:46 +00:00
|
|
|
'is_os_image': is_os_image,
|
|
|
|
'is_public': is_public,
|
|
|
|
'size_in_gb': image_size_in_gb,
|
|
|
|
'storage_class': storage_class,
|
|
|
|
'image_source': image_source,
|
|
|
|
'image_source_type': image_source_type,
|
|
|
|
'status': status
|
|
|
|
}
|
2020-03-03 18:46:39 +00:00
|
|
|
)
|
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
# identify vmdisk from opennebula - primary mapping key
|
|
|
|
extra_data = {
|
|
|
|
'opennebula_vm': one_vm.vmid,
|
|
|
|
'opennebula_size_in_gb': disk_size_in_gb,
|
|
|
|
'opennebula_source': disk.get('opennebula_source'),
|
|
|
|
'opennebula_disk_num': vmdisknum
|
|
|
|
}
|
|
|
|
# Save for comparing later
|
|
|
|
one_disks_extra_data.append(extra_data)
|
|
|
|
|
|
|
|
try:
|
|
|
|
vm_disk = VMDiskProduct.objects.get(extra_data=extra_data)
|
|
|
|
except VMDiskProduct.DoesNotExist:
|
|
|
|
vm_disk = VMDiskProduct.objects.create(
|
|
|
|
owner=vmowner,
|
|
|
|
vm=vm_product,
|
|
|
|
image=image,
|
|
|
|
size_in_gb=disk_size_in_gb,
|
|
|
|
extra_data=extra_data
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now remove all disks that are not in above extra_data list
|
|
|
|
for disk in VMDiskProduct.objects.filter(vm=vm_product):
|
|
|
|
extra_data = disk.extra_data
|
|
|
|
if not extra_data in one_disks_extra_data:
|
|
|
|
log.info("Removing disk {} from VM {}".format(disk, vm_product))
|
|
|
|
disk.delete()
|
|
|
|
|
|
|
|
disks = [ disk.extra_data for disk in VMDiskProduct.objects.filter(vm=vm_product) ]
|
|
|
|
log.info("VM {} has disks: {}".format(vm_product, disks))
|
2020-03-03 18:46:39 +00:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Migrate Opennebula VM to regular (uncloud) vm'
|
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('--disk-owner', required=True, help="The user who owns the the opennebula disks")
|
|
|
|
|
2020-03-03 18:46:39 +00:00
|
|
|
def handle(self, *args, **options):
|
2020-03-22 16:30:55 +00:00
|
|
|
log.debug("{} {}".format(args, options))
|
|
|
|
|
|
|
|
disk_owner = get_user_model().objects.get(username=options['disk_owner'])
|
|
|
|
|
2020-03-03 18:46:39 +00:00
|
|
|
for one_vm in VMModel.objects.all():
|
|
|
|
|
2020-03-17 15:03:41 +00:00
|
|
|
if not one_vm.last_host:
|
2020-03-22 16:30:55 +00:00
|
|
|
log.warning("No VMHost for VM {} - VM might be on hold - skipping".format(one_vm.vmid))
|
2020-03-17 15:03:41 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
vmhost = VMHost.objects.get(hostname=one_vm.last_host)
|
|
|
|
except VMHost.DoesNotExist:
|
2020-03-22 16:30:55 +00:00
|
|
|
log.error("VMHost {} does not exist, aborting".format(one_vm.last_host))
|
2020-03-17 15:03:41 +00:00
|
|
|
raise
|
|
|
|
|
2020-03-17 14:39:24 +00:00
|
|
|
cores = one_vm.cores
|
|
|
|
ram_in_gb = one_vm.ram_in_gb
|
|
|
|
owner = one_vm.owner
|
|
|
|
status = 'active'
|
2020-03-03 18:46:39 +00:00
|
|
|
|
2020-03-17 15:03:41 +00:00
|
|
|
ssd_size = sum([ disk['size_in_gb'] for disk in one_vm.disks if disk['pool_name'] in ['ssd', 'one'] ])
|
|
|
|
hdd_size = sum([ disk['size_in_gb'] for disk in one_vm.disks if disk['pool_name'] in ['hdd'] ])
|
2020-03-03 18:46:39 +00:00
|
|
|
|
|
|
|
# List of IPv4 addresses and Global IPv6 addresses
|
|
|
|
ipv4, ipv6 = one_vm.ips
|
|
|
|
|
|
|
|
# TODO: Insert actual/real creation_date, starting_date, ending_date
|
|
|
|
# instead of pseudo one we are putting currently
|
2020-03-17 15:03:41 +00:00
|
|
|
creation_date = starting_date = datetime.now(tz=timezone.utc)
|
2020-03-03 18:53:45 +00:00
|
|
|
|
2020-03-17 14:39:24 +00:00
|
|
|
# Price calculation based on datacenterlight.ch
|
2020-03-03 18:53:45 +00:00
|
|
|
one_time_price = 0
|
|
|
|
recurring_period = 'per_month'
|
2020-03-17 14:39:24 +00:00
|
|
|
recurring_price = get_vm_price(cores, ram_in_gb,
|
|
|
|
ssd_size, hdd_size,
|
|
|
|
len(ipv4), len(ipv6))
|
2020-03-03 18:53:45 +00:00
|
|
|
|
2020-03-04 08:25:46 +00:00
|
|
|
try:
|
2020-03-22 16:30:55 +00:00
|
|
|
vm_product = VMProduct.objects.get(extra_data__opennebula_id=one_vm.vmid)
|
2020-03-04 08:25:46 +00:00
|
|
|
except VMProduct.DoesNotExist:
|
|
|
|
order = Order.objects.create(
|
2020-03-17 14:39:24 +00:00
|
|
|
owner=owner,
|
2020-03-04 08:25:46 +00:00
|
|
|
creation_date=creation_date,
|
2020-03-17 15:03:41 +00:00
|
|
|
starting_date=starting_date
|
2020-03-04 08:25:46 +00:00
|
|
|
)
|
2020-03-22 16:30:55 +00:00
|
|
|
vm_product = VMProduct(
|
|
|
|
extra_data={ 'opennebula_id': one_vm.vmid },
|
2020-03-17 15:03:41 +00:00
|
|
|
name=one_vm.uncloud_name,
|
2020-03-22 16:30:55 +00:00
|
|
|
order=order
|
2020-03-04 08:25:46 +00:00
|
|
|
)
|
2020-03-03 18:46:39 +00:00
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
# we don't use update_or_create, as filtering by json AND setting json
|
|
|
|
# at the same time does not work
|
|
|
|
|
|
|
|
vm_product.vmhost = vmhost
|
|
|
|
vm_product.owner = owner
|
|
|
|
vm_product.cores = cores
|
|
|
|
vm_product.ram_in_gb = ram_in_gb
|
|
|
|
vm_product.status = status
|
|
|
|
|
|
|
|
vm_product.save()
|
|
|
|
|
2020-03-03 18:46:39 +00:00
|
|
|
# Create VMNetworkCards
|
|
|
|
create_nics(one_vm, vm_product)
|
|
|
|
|
|
|
|
# Create VMDiskImageProduct and VMDiskProduct
|
2020-03-22 16:30:55 +00:00
|
|
|
sync_disk_and_image(one_vm, vm_product, disk_owner=disk_owner)
|