2020-06-15 11:52:05 +00:00
|
|
|
from django.db.models import CharField, IntegerField, Model
|
|
|
|
from django.utils.functional import cached_property
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-07-13 09:33:09 +00:00
|
|
|
from libvirt import virConnect
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2020-06-15 11:52:05 +00:00
|
|
|
from vrtManager.connection import connection_manager
|
2020-07-13 09:33:09 +00:00
|
|
|
from vrtManager.hostdetails import wvmHostDetails
|
2020-06-15 11:52:05 +00:00
|
|
|
|
2020-06-15 06:57:17 +00:00
|
|
|
|
2020-03-16 13:59:45 +00:00
|
|
|
class Compute(Model):
|
2022-11-02 05:54:35 +00:00
|
|
|
name = CharField(_("name"), max_length=64, unique=True)
|
|
|
|
hostname = CharField(_("hostname"), max_length=64)
|
|
|
|
login = CharField(_("login"), max_length=20)
|
|
|
|
password = CharField(_("password"), max_length=14, blank=True, null=True)
|
|
|
|
details = CharField(_("details"), max_length=64, null=True, blank=True)
|
2020-03-16 13:59:45 +00:00
|
|
|
type = IntegerField()
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2020-06-15 11:52:05 +00:00
|
|
|
@cached_property
|
|
|
|
def status(self):
|
2020-07-13 09:33:09 +00:00
|
|
|
# return connection_manager.host_is_up(self.type, self.hostname)
|
|
|
|
# TODO: looks like socket has problems connecting via VPN
|
|
|
|
if isinstance(self.connection, virConnect):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return self.connection
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def connection(self):
|
|
|
|
try:
|
|
|
|
return connection_manager.get_connection(
|
|
|
|
self.hostname,
|
|
|
|
self.login,
|
|
|
|
self.password,
|
|
|
|
self.type,
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
return e
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def proxy(self):
|
|
|
|
return wvmHostDetails(
|
|
|
|
self.hostname,
|
|
|
|
self.login,
|
|
|
|
self.password,
|
|
|
|
self.type,
|
|
|
|
)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def cpu_count(self):
|
|
|
|
return self.proxy.get_node_info()[3]
|
2023-05-10 10:59:13 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def cpu_usage(self):
|
2023-05-18 11:35:19 +00:00
|
|
|
return round(self.proxy.get_cpu_usage(diff=False).get('usage'))
|
2020-07-13 09:33:09 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ram_size(self):
|
|
|
|
return self.proxy.get_node_info()[2]
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ram_usage(self):
|
2022-11-02 05:54:35 +00:00
|
|
|
return self.proxy.get_memory_usage()["percent"]
|
2020-06-15 11:52:05 +00:00
|
|
|
|
2020-06-15 06:57:17 +00:00
|
|
|
def __str__(self):
|
2020-06-15 11:52:05 +00:00
|
|
|
return self.name
|