1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Fixed servers basic information

This commit is contained in:
Adolfo Gómez García 2024-04-17 19:56:22 +02:00
parent 60c5f37591
commit 2822013b36
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
9 changed files with 76 additions and 40 deletions

View File

@ -37,7 +37,7 @@ from django.utils.translation import gettext_lazy as _
from uds import models
from uds.core import consts, types, ui
from uds.core.util import permissions, ensure
from uds.core.util import net, permissions, ensure
from uds.core.util.model import sql_datetime, process_uuid
from uds.core.exceptions.rest import NotFound, RequestError
from uds.REST.model import DetailHandler, ModelHandler
@ -127,7 +127,7 @@ class ServersServers(DetailHandler):
'hostname': i.hostname,
'ip': i.ip,
'listen_port': i.listen_port,
'mac': i.mac if not multi or i.mac != consts.MAC_UNKNOWN else '',
'mac': i.mac if not multi and i.mac != consts.MAC_UNKNOWN else '',
'maintenance_mode': i.maintenance_mode,
}
res.append(val)
@ -149,22 +149,32 @@ class ServersServers(DetailHandler):
def get_fields(self, parent: 'Model') -> list[typing.Any]:
parent = ensure.is_instance(parent, models.ServerGroup)
return [
{
'hostname': {
'title': _('Hostname'),
}
},
{'ip': {'title': _('Ip')}},
{'mac': {'title': _('Mac')}},
{
'maintenance_mode': {
'title': _('State'),
'type': 'dict',
'dict': {True: _('Maintenance'), False: _('Normal')},
}
},
]
return (
[
{
'hostname': {
'title': _('Hostname'),
}
},
{'ip': {'title': _('Ip')}},
] # If not managed, we can show mac, else listen port (related to UDS Server)
+ (
[
{'mac': {'title': _('Mac')}},
]
if not parent.is_managed()
else [{'listen_port': {'title': _('Port')}}]
)
+ [
{
'maintenance_mode': {
'title': _('State'),
'type': 'dict',
'dict': {True: _('Maintenance'), False: _('Normal')},
}
},
]
)
def get_row_style(self, parent: 'Model') -> types.ui.RowStyleInfo:
return types.ui.RowStyleInfo(prefix='row-maintenance-', field='maintenance_mode')
@ -194,11 +204,11 @@ class ServersServers(DetailHandler):
'order': 101, # At end
},
{
'name': 'listen_port',
'value': 0,
'label': gettext('Port'),
'tooltip': gettext('Port of server. 0 means "service default"'),
'type': types.ui.FieldType.NUMERIC,
'name': 'mac',
'value': '',
'label': gettext('Server MAC'),
'tooltip': gettext('Optional MAC address of the server'),
'type': types.ui.FieldType.TEXT,
'order': 102, # At end
},
{
@ -241,12 +251,17 @@ class ServersServers(DetailHandler):
if item is None:
# Create new, depending on server type
if parent.type == types.servers.ServerType.UNMANAGED:
# Ensure mac is emty or valid
mac: str = self._params['mac'].strip().upper()
if mac and not net.is_valid_mac(mac):
raise self.invalid_request_response('Invalid MAC address')
# Create a new one, and add it to group
server = models.Server.objects.create(
ip_from='::1',
ip=self._params['ip'],
hostname=self._params['hostname'],
listen_port=self._params['listen_port'] or 0,
listen_port=0,
mac=mac,
type=parent.type,
subtype=parent.subtype,
stamp=sql_datetime(),
@ -267,13 +282,25 @@ class ServersServers(DetailHandler):
raise self.invalid_item_response() from None
pass
else:
try:
server = models.Server.objects.get(uuid=process_uuid(item))
parent.servers.add(server)
except Exception:
raise self.invalid_item_response() from None
if parent.type == types.servers.ServerType.UNMANAGED:
mac: str = self._params['mac'].strip().upper()
if mac and not net.is_valid_mac(mac):
raise self.invalid_request_response('Invalid MAC address')
try:
models.Server.objects.filter(uuid=process_uuid(item)).update(
ip=self._params['ip'],
hostname=self._params['hostname'],
mac=mac,
)
except Exception:
raise self.invalid_item_response() from None
raise self.invalid_request_response() from None
else:
try:
server = models.Server.objects.get(uuid=process_uuid(item))
parent.servers.add(server)
except Exception:
raise self.invalid_item_response() from None
def delete_item(self, parent: 'Model', item: str) -> None:
parent = ensure.is_instance(parent, models.ServerGroup)

View File

@ -63,9 +63,10 @@ def restrain_server(func: collections.abc.Callable[..., typing.Any]) -> collecti
try:
return func(self, *args, **kwargs)
except Exception as e:
logger.error('Error executing %s: %s', func.__name__, e)
restrained_until = sql_datetime() + datetime.timedelta(seconds=consts.system.FAILURE_TIMEOUT)
logger.exception('Error executing %s: %s. Server restrained until %s', func.__name__, e, restrained_until)
self.server.set_restrained_until(
sql_datetime() + datetime.timedelta(seconds=consts.system.FAILURE_TIMEOUT)
restrained_until
) # Block server for a while
return False

View File

@ -278,6 +278,9 @@ def is_valid_host(value: str) -> bool:
return is_valid_ip(value) or is_valid_fqdn(value)
def is_valid_mac(value: str) -> bool:
return re.match(r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$', value) is not None
def test_connectivity(host: str, port: int, timeout: float = 4) -> bool:
try:
logger.debug('Checking connection to %s:%s with %s seconds timeout', host, port, timeout)

View File

@ -47,12 +47,12 @@ def resolve(hostname: str, rdtype: typing.Optional[str] = None) -> list[str]:
for i in ('A', 'AAAA'):
try:
ips.extend([str(ip) for ip in typing.cast(collections.abc.Iterable[typing.Any], dns.resolver.resolve(hostname, i))])
except dns.resolver.NXDOMAIN:
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
pass
else:
try:
ips.extend([str(ip) for ip in typing.cast(collections.abc.Iterable[typing.Any], dns.resolver.resolve(hostname, rdtype))])
except dns.resolver.NXDOMAIN:
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # Ignore NoAnswer, that is, the record does not exists
pass
return ips
@ -63,5 +63,5 @@ def reverse_resolve(ip: str) -> list[str]:
"""
try:
return[str(i).rstrip('.') for i in typing.cast(collections.abc.Iterable[typing.Any], dns.resolver.query(dns.reversename.from_address(ip).to_text(), 'PTR'))]
except dns.resolver.NXDOMAIN:
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
return []

View File

@ -396,10 +396,10 @@ class Storage:
for v in query: # @UndefinedVariable
yield (v.key, codecs.decode(v.data.encode(), 'base64'), v.attr1)
def filter_unpickle(
self, attr1: typing.Optional[str] = None, forUpdate: bool = False
def filter_unpickle_by_attr(
self, attr1: typing.Optional[str] = None, for_update: bool = False
) -> collections.abc.Iterable[tuple[str, typing.Any, 'str|None']]:
for v in self.filter(attr1, forUpdate):
for v in self.filter(attr1, for_update):
yield (v[0], pickle.loads(v[1]), v[2]) # nosec: secure pickle load
def clear(self) -> None:

View File

@ -107,6 +107,10 @@ class ServerGroup(UUIDModel, TaggingMixin, properties.PropertiesMixin):
def server_type(self, value: types.servers.ServerType) -> None:
"""Sets the server type of this server"""
self.type = value
def is_managed(self) -> bool:
"""Returns if this server group is managed or not"""
return self.server_type != types.servers.ServerType.UNMANAGED
def https_url(self, path: str) -> str:
if not path.startswith('/'):

File diff suppressed because one or more lines are too long

View File

@ -167,6 +167,7 @@ gettext("Exit maintenance mode?");
gettext("Enter maintenance mode?");
gettext("Maintenance mode for");
gettext("New server");
gettext("Edit server");
gettext("Remove server from server group");
gettext("Information");
gettext("New service");

View File

@ -102,6 +102,6 @@
</svg>
</div>
</uds-root>
<script src="/uds/res/admin/runtime.js?stamp=1710438355" type="module"></script><script src="/uds/res/admin/polyfills.js?stamp=1710438355" type="module"></script><script src="/uds/res/admin/main.js?stamp=1710438355" type="module"></script></body>
<script src="/uds/res/admin/runtime.js?stamp=1713376179" type="module"></script><script src="/uds/res/admin/polyfills.js?stamp=1713376179" type="module"></script><script src="/uds/res/admin/main.js?stamp=1713376179" type="module"></script></body>
</html>