2017-05-09 14:33:56 +00:00
|
|
|
import oca
|
|
|
|
import socket
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from oca.pool import WrongNameError
|
|
|
|
|
|
|
|
class VirtualMachineTemplate(models.Model):
|
|
|
|
"""This class represents an opennebula template."""
|
|
|
|
opennebula_id = models.IntegerField()
|
|
|
|
base_price = models.FloatField()
|
|
|
|
memory_price = models.FloatField()
|
|
|
|
core_price = models.FloatField()
|
|
|
|
disk_size_price = models.FloatField()
|
|
|
|
|
2017-05-10 00:36:11 +00:00
|
|
|
def calculate_price(self):
|
|
|
|
manager = OpenNebulaManager()
|
|
|
|
template = manger._get_template(self.opennebula_id).template
|
|
|
|
|
|
|
|
price = int(template.vpcu) * self.core_price
|
|
|
|
price += int(template.memory) / 1024 * self.memory_price
|
|
|
|
try:
|
|
|
|
price += int(template.disk.size) / 1024 * self.disk_size_price
|
|
|
|
except AttributeError:
|
|
|
|
for disk in template.disks:
|
|
|
|
price += int(disk.size) / 1024 * self.disk_size_price
|
|
|
|
return price
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-05-10 02:06:12 +00:00
|
|
|
def get_name(self):
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
manager = OpenNebulaManager(create_user=False)
|
|
|
|
template = manager._get_template(template_id=self.opennebula_id)
|
2017-05-10 02:06:12 +00:00
|
|
|
return template.name
|
|
|
|
|
|
|
|
def get_cores(self):
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
manager = OpenNebulaManager(create_user=False)
|
|
|
|
template = manager._get_template(template_id=self.opennebula_id).template
|
2017-05-10 02:06:12 +00:00
|
|
|
return int(template.vcpu)
|
|
|
|
|
|
|
|
def get_disk_size(self):
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
manager = OpenNebulaManager(create_user=False)
|
|
|
|
template = manager._get_template(template_id=self.opennebula_id).template
|
|
|
|
disk_size = 0
|
|
|
|
for disk in template.disks:
|
|
|
|
disk_size += int(disk.size)
|
|
|
|
return disk_size / 1024
|
2017-05-10 02:06:12 +00:00
|
|
|
|
|
|
|
def get_memory(self):
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
manager = OpenNebulaManager(create_user=False)
|
|
|
|
template = manager._get_template(template_id=self.opennebula_id).template
|
2017-05-10 02:06:12 +00:00
|
|
|
return int(template.memory) / 1024
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
|
|
class VirtualMachine(models.Model):
|
|
|
|
"""This class represents an opennebula virtual machine."""
|
|
|
|
opennebula_id = models.IntegerField()
|
|
|
|
template = models.ForeignKey(VirtualMachineTemplate)
|
|
|
|
|
2017-05-10 02:06:12 +00:00
|
|
|
VM_STATE = {
|
|
|
|
'0': 'INIT',
|
|
|
|
'1': 'PENDING',
|
|
|
|
'2': 'HOLD',
|
|
|
|
'3': 'ACTIVE',
|
|
|
|
'4': 'STOPPED',
|
|
|
|
'5': 'SUSPENDED',
|
|
|
|
'6': 'DONE',
|
|
|
|
'8': 'POWEROFF',
|
|
|
|
'9': 'UNDEPLOYED',
|
|
|
|
'10': 'CLONING',
|
|
|
|
'11': 'CLONING_FAILURE',
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
vm = self.manager._get_vm(vm_id=self.opennebula_id)
|
|
|
|
return vm.name
|
|
|
|
|
|
|
|
def get_cores(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
template = self.manager._get_vm(vm_id=self.opennebula_id).template
|
|
|
|
return int(template.vcpu)
|
|
|
|
|
|
|
|
def get_disk_size(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
template = self.manager._get_vm(vm_id=self.opennebula_id).template
|
|
|
|
try:
|
|
|
|
return template.disk.size
|
|
|
|
except AttributeError:
|
|
|
|
disk_size = 0
|
|
|
|
for disk in template.disks:
|
|
|
|
disk_size += disk.size
|
|
|
|
return disk_size / 1024
|
|
|
|
|
|
|
|
|
|
|
|
def get_memory(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
template = self.manager._get_vm(vm_id=self.opennebula_id).template
|
|
|
|
return int(template.memory)
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
vm = self.manager._get_vm(vm_id=self.opennebula_id)
|
|
|
|
return vm.id
|
|
|
|
|
|
|
|
def get_ip(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
vm = self.manager._get_vm(vm_id=self.opennebula_id)
|
|
|
|
try:
|
|
|
|
return vm.user_template.ungleich_public_ip
|
|
|
|
except AttributeError:
|
|
|
|
return '-'
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
if self.manager is None:
|
|
|
|
self.manager = OpenNebulaManager()
|
|
|
|
|
|
|
|
vm = self.manager._get_vm(vm_id=self.opennebula_id)
|
|
|
|
return self.VM_STATE.get(str(vm.state))
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
def get_price(self):
|
2017-05-10 02:06:12 +00:00
|
|
|
return 0.0
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
class OpenNebulaManager():
|
|
|
|
"""This class represents an opennebula manager."""
|
|
|
|
|
|
|
|
def __init__(self, email=None, password=None, create_user=True):
|
|
|
|
|
|
|
|
# Get oneadmin client
|
|
|
|
self.oneadmin_client = self._get_opennebula_client(
|
|
|
|
settings.OPENNEBULA_USERNAME,
|
|
|
|
settings.OPENNEBULA_PASSWORD
|
|
|
|
)
|
2017-05-09 22:39:41 +00:00
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
if not create_user or email is None:
|
2017-05-09 14:33:56 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Get or create oppenebula user using given credentials
|
|
|
|
self.opennebula_user = self._get_or_create_user(
|
|
|
|
email,
|
|
|
|
password
|
|
|
|
)
|
|
|
|
|
|
|
|
# If opennebula user was created/obtained, get his client
|
|
|
|
if self.opennebula_user:
|
|
|
|
self.client = self._get_opennebula_client(
|
|
|
|
email,
|
|
|
|
password
|
|
|
|
)
|
|
|
|
|
|
|
|
def _get_opennebula_client(self, username, password):
|
|
|
|
return oca.Client("{0}:{1}".format(
|
|
|
|
username,
|
|
|
|
password),
|
|
|
|
"{protocol}://{domain}:{port}{endpoint}".format(
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL,
|
|
|
|
domain=settings.OPENNEBULA_DOMAIN,
|
|
|
|
port=settings.OPENNEBULA_PORT,
|
|
|
|
endpoint=settings.OPENNEBULA_ENDPOINT
|
|
|
|
))
|
|
|
|
|
|
|
|
def _get_or_create_user(self, email, password):
|
|
|
|
try:
|
2017-05-09 22:39:41 +00:00
|
|
|
user_pool = self._get_user_pool()
|
2017-05-09 14:33:56 +00:00
|
|
|
opennebula_user = user_pool.get_by_name(email)
|
|
|
|
return opennebula_user
|
|
|
|
except WrongNameError as wrong_name_err:
|
|
|
|
opennebula_user = self.oneadmin_client.call(oca.User.METHODS['allocate'], email,
|
|
|
|
password, 'core')
|
2017-05-10 00:49:03 +00:00
|
|
|
return opennebula_user
|
2017-05-09 22:39:41 +00:00
|
|
|
except ConnectionRefusedError:
|
|
|
|
print('Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
|
|
|
)
|
2017-05-11 02:05:58 +00:00
|
|
|
raise ConnectionRefusedError
|
2017-05-09 15:15:12 +00:00
|
|
|
def _get_user_pool(self):
|
2017-05-09 22:39:41 +00:00
|
|
|
try:
|
|
|
|
user_pool = oca.UserPool(self.oneadmin_client)
|
|
|
|
user_pool.info()
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
print('Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
|
|
|
)
|
2017-05-11 02:05:58 +00:00
|
|
|
raise ConnectionRefusedError
|
2017-05-09 15:15:12 +00:00
|
|
|
return user_pool
|
2017-05-09 23:31:27 +00:00
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
def _get_vm_pool(self):
|
|
|
|
try:
|
|
|
|
vm_pool = oca.VmPool(self.oneadmin_client)
|
|
|
|
vm_pool.info()
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
print('Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
|
|
|
)
|
|
|
|
raise ConnectionRefusedError
|
|
|
|
return vm_pool
|
|
|
|
|
|
|
|
|
|
|
|
def _get_vm(self, vm_id):
|
|
|
|
vm_pool = self._get_vm_pool()
|
|
|
|
# Get virtual machines from all users
|
|
|
|
vm_pool.info(filter=-2)
|
|
|
|
return vm_pool.get_by_id(vm_id)
|
|
|
|
|
|
|
|
def create_vm(self, template_id):
|
|
|
|
template_pool = self._get_template_pool()
|
2017-05-09 23:31:27 +00:00
|
|
|
|
|
|
|
template = template_pool.get_by_id(template_id)
|
|
|
|
|
|
|
|
vm_id = template.instantiate()
|
|
|
|
self.oneadmin.call(
|
|
|
|
oca.VirtualMachine.METHODS['chown'],
|
|
|
|
vm_id,
|
|
|
|
self.opennebula_user.id,
|
|
|
|
self.opennebula_user.group_ids[0]
|
|
|
|
)
|
|
|
|
return vm_id
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
def delete_vm(self, vm_id):
|
|
|
|
vm = self._get_vm(vm_id)
|
|
|
|
vm.delete()
|
|
|
|
|
|
|
|
def _get_template_pool(self):
|
|
|
|
try:
|
|
|
|
template_pool = oca.VmTemplatePool(self.oneadmin_client)
|
|
|
|
template_pool.info()
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
print('Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
|
|
|
)
|
|
|
|
raise ConnectionRefusedError
|
|
|
|
return template_pool
|
|
|
|
|
|
|
|
def _get_template(self, template_id):
|
|
|
|
template_pool = self._get_template_pool()
|
|
|
|
return template_pool.get_by_id(template_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-09 23:11:49 +00:00
|
|
|
def create_template(self, name, cores, memory, disk_size):
|
2017-05-10 00:49:03 +00:00
|
|
|
"""Create and add a new template to opennebula.
|
|
|
|
:param name: A string representation describing the template.
|
|
|
|
Used as label in view.
|
|
|
|
:param cores: Amount of virtual cpu cores for the VM.
|
|
|
|
:param memory: Amount of RAM for the VM (MB)
|
|
|
|
:param disk_size: Amount of disk space for VM (MB)
|
|
|
|
"""
|
2017-05-09 23:11:49 +00:00
|
|
|
template_string_formatter = """<TEMPLATE>
|
|
|
|
<NAME>{name}</NAME>
|
|
|
|
<MEMORY>{memory}</MEMORY>
|
|
|
|
<VCPU>{vcpu}</VCPU>
|
|
|
|
<CPU>{cpu}</CPU>
|
|
|
|
<DISK>
|
|
|
|
<TYPE>fs</TYPE>
|
|
|
|
<SIZE>{size}</SIZE>
|
|
|
|
<DEV_PREFIX>vd</DEV_PREFIX>
|
|
|
|
</DISK>
|
|
|
|
</TEMPLATE>
|
|
|
|
"""
|
|
|
|
template_id = oca.VmTemplate.allocate(
|
|
|
|
self.oneadmin_client,
|
|
|
|
template_string_formatter.format(
|
|
|
|
name=name,
|
|
|
|
vcpu=cores,
|
|
|
|
cpu=0.1*cores,
|
|
|
|
size=1024 * disk_size,
|
|
|
|
memory=1024 * memory
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return template_id
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
|
|
def delete_template(self, template_id):
|
|
|
|
self.oneadmin_client.call(oca.VmTemplate.METHODS['delete'], template_id, False)
|
|
|
|
|
|
|
|
|
|
|
|
|