From 7fc51ce513d1dc4ecf9edea4321548a2de1fd0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Tue, 18 Jan 2022 13:21:44 +0100 Subject: [PATCH] Added isAvailable for more services --- server/src/uds/core/transports/transport.py | 6 +++--- server/src/uds/core/util/connection.py | 16 ++-------------- server/src/uds/core/util/fuse.py | 12 ++++++------ server/src/uds/core/util/net.py | 16 ++++++++++++++++ server/src/uds/models/service.py | 6 +++--- server/src/uds/models/service_pool.py | 2 +- server/src/uds/services/OVirt/provider.py | 13 ++++++++++--- server/src/uds/services/OVirt/service.py | 3 +++ .../services/PhysicalMachines/service_multi.py | 4 +--- server/src/uds/services/Proxmox/provider.py | 2 ++ 10 files changed, 47 insertions(+), 33 deletions(-) diff --git a/server/src/uds/core/transports/transport.py b/server/src/uds/core/transports/transport.py index e9ac8bbc..538bcfeb 100644 --- a/server/src/uds/core/transports/transport.py +++ b/server/src/uds/core/transports/transport.py @@ -39,7 +39,7 @@ from django.utils.translation import gettext_noop as _ from uds.core.util import os_detector as OsDetector from uds.core import Module from uds.core.transports import protocols -from uds.core.util import connection +from uds.core.util import net # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: @@ -116,14 +116,14 @@ class Transport(Module): userService: 'models.UserService', ip: str, port: typing.Union[str, int], - timeout: int = 4, + timeout: float = 4, ) -> bool: proxy: typing.Optional[ 'models.Proxy' ] = userService.deployed_service.service.proxy if proxy: return proxy.doTestServer(ip, port, timeout) - return connection.testServer(ip, str(port), timeout) + return net.testConnection(ip, str(port), timeout) def isAvailableFor(self, userService: 'models.UserService', ip: str) -> bool: """ diff --git a/server/src/uds/core/util/connection.py b/server/src/uds/core/util/connection.py index 668172ee..d90050e1 100644 --- a/server/src/uds/core/util/connection.py +++ b/server/src/uds/core/util/connection.py @@ -36,17 +36,5 @@ import socket logger = logging.getLogger(__name__) - -def testServer(host: str, port: typing.Union[int, str], timeOut: float = 4) -> bool: - try: - logger.debug( - 'Checking connection to %s:%s with %s seconds timeout', host, port, timeOut - ) - sock = socket.create_connection((host, int(port)), timeOut) - sock.close() - except Exception as e: - logger.debug( - 'Exception checking %s:%s with %s timeout: %s', host, port, timeOut, e - ) - return False - return True +# import Alias +from .net import testConnection as testServer diff --git a/server/src/uds/core/util/fuse.py b/server/src/uds/core/util/fuse.py index a38943fc..a7fdcc21 100644 --- a/server/src/uds/core/util/fuse.py +++ b/server/src/uds/core/util/fuse.py @@ -88,19 +88,19 @@ if not _libfuse_path: def Reg32GetValue(rootkey, keyname, valname): key, val = None, None try: - key = reg.OpenKey( - rootkey, keyname, 0, reg.KEY_READ | reg.KEY_WOW64_32KEY + key = reg.OpenKey( # type: ignore + rootkey, keyname, 0, reg.KEY_READ | reg.KEY_WOW64_32KEY # type: ignore ) - val = str(reg.QueryValueEx(key, valname)[0]) + val = str(reg.QueryValueEx(key, valname)[0]) # type: ignore except WindowsError: # type: ignore pass finally: if key is not None: - reg.CloseKey(key) + reg.CloseKey(key) # type: ignore return val _libfuse_path = Reg32GetValue( - reg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WinFsp", r"InstallDir" + reg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WinFsp", r"InstallDir" # type: ignore ) if _libfuse_path: _libfuse_path += r"bin\winfsp-%s.dll" % ( @@ -713,7 +713,7 @@ class FuseOperations(ctypes.Structure): def time_of_timespec(ts: c_timespec, use_ns=False): - return ts.tv_sec * 10 ** 9 + ts.tv_nsec + return ts.tv_sec * 10 ** 9 + ts.tv_nsec # type: ignore def set_st_attrs(st: c_stat, attrs: typing.Mapping[str, int]) -> None: diff --git a/server/src/uds/core/util/net.py b/server/src/uds/core/util/net.py index 8cb54631..ca7de8f1 100644 --- a/server/src/uds/core/util/net.py +++ b/server/src/uds/core/util/net.py @@ -31,6 +31,7 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import re +import socket import logging import typing @@ -231,3 +232,18 @@ def isValidFQDN(value: str) -> bool: def isValidHost(value: str): return isValidIp(value) or isValidFQDN(value) + + +def testConnection(host: str, port: typing.Union[int, str], timeOut: float = 4) -> bool: + try: + logger.debug( + 'Checking connection to %s:%s with %s seconds timeout', host, port, timeOut + ) + sock = socket.create_connection((host, int(port)), timeOut) + sock.close() + except Exception as e: + logger.debug( + 'Exception checking %s:%s with %s timeout: %s', host, port, timeOut, e + ) + return False + return True diff --git a/server/src/uds/models/service.py b/server/src/uds/models/service.py index b33629aa..6d674775 100644 --- a/server/src/uds/models/service.py +++ b/server/src/uds/models/service.py @@ -38,7 +38,7 @@ from django.db import models from uds.core.environment import Environment from uds.core.util import log from uds.core.util import unique -from uds.core.util import connection +from uds.core.util import net from .managed_object_model import ManagedObjectModel from .tag import TaggingMixin @@ -188,11 +188,11 @@ class Service(ManagedObjectModel, TaggingMixin): # type: ignore return self.provider.isInMaintenance() if self.provider else True def testServer( - self, host: str, port: typing.Union[str, int], timeout: int = 4 + self, host: str, port: typing.Union[str, int], timeout: float = 4 ) -> bool: if self.proxy: return self.proxy.doTestServer(host, port, timeout) - return connection.testServer(host, port, timeout) + return net.testConnection(host, port, timeout) def __str__(self) -> str: return '{} of type {} (id:{})'.format(self.name, self.data_type, self.id) diff --git a/server/src/uds/models/service_pool.py b/server/src/uds/models/service_pool.py index ded24947..754bf1c1 100644 --- a/server/src/uds/models/service_pool.py +++ b/server/src/uds/models/service_pool.py @@ -675,7 +675,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore return 100 * cachedValue // maxs - def testServer(self, host, port, timeout=4) -> bool: + def testServer(self, host: str, port: typing.Union[str, int], timeout: float=4) -> bool: return self.service.testServer(host, port, timeout) # parent accessors diff --git a/server/src/uds/services/OVirt/provider.py b/server/src/uds/services/OVirt/provider.py index a46ac117..886e8b1e 100644 --- a/server/src/uds/services/OVirt/provider.py +++ b/server/src/uds/services/OVirt/provider.py @@ -33,14 +33,14 @@ import logging import typing from django.utils.translation import gettext_noop as _ - from uds.core import services from uds.core.ui import gui from uds.core.util import validators - -from .service import OVirtLinkedService +from uds.core.util.cache import Cache +from uds.core.util.decorators import allowCache from . import client +from .service import OVirtLinkedService # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: @@ -508,6 +508,13 @@ class OVirtProvider( ) -> typing.Optional[typing.MutableMapping[str, typing.Any]]: return self.__getApi().getConsoleConnection(machineId) + @allowCache('reachable', Cache.SHORT_VALIDITY) + def isAvailable(self) -> bool: + """ + Check if aws provider is reachable + """ + return self.testConnection() + @staticmethod def test(env: 'Environment', data: 'Module.ValuesType') -> typing.List[typing.Any]: """ diff --git a/server/src/uds/services/OVirt/service.py b/server/src/uds/services/OVirt/service.py index 9158f148..8b74228f 100644 --- a/server/src/uds/services/OVirt/service.py +++ b/server/src/uds/services/OVirt/service.py @@ -466,3 +466,6 @@ class OVirtLinkedService(Service): # pylint: disable=too-many-public-methods self, machineId: str ) -> typing.Optional[typing.MutableMapping[str, typing.Any]]: return self.parent().getConsoleConnection(machineId) + + def isAvailable(self) -> bool: + return self.parent().isAvailable() diff --git a/server/src/uds/services/PhysicalMachines/service_multi.py b/server/src/uds/services/PhysicalMachines/service_multi.py index dc931ec2..c8f510e2 100644 --- a/server/src/uds/services/PhysicalMachines/service_multi.py +++ b/server/src/uds/services/PhysicalMachines/service_multi.py @@ -40,8 +40,6 @@ from django.db import transaction from uds.models import getSqlDatetimeAsUnix from uds.core.ui import gui from uds.core.util import log -from uds.core.util import connection -from uds.core.util import config from uds.core.util import net from uds.core.services import types as serviceTypes @@ -271,7 +269,7 @@ class IPMachinesService(IPServiceBase): self._port > 0 and not wolENABLED ): # If configured WOL, check is a nonsense if ( - connection.testServer(theIP, self._port, timeOut=0.5) + net.testConnection(theIP, self._port, timeOut=0.5) is False ): # Log into logs of provider, so it can be "shown" on services logs diff --git a/server/src/uds/services/Proxmox/provider.py b/server/src/uds/services/Proxmox/provider.py index b4985211..5785ec85 100644 --- a/server/src/uds/services/Proxmox/provider.py +++ b/server/src/uds/services/Proxmox/provider.py @@ -36,6 +36,8 @@ from uds.core import services from uds.core.ui import gui from uds.core.util import validators from uds.core.util.unique_id_generator import UniqueIDGenerator +from uds.core.util.cache import Cache +from uds.core.util.decorators import allowCache from .service import ProxmoxLinkedService