1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-12 09:17:56 +03:00

updated tests and more refactoring

This commit is contained in:
Adolfo Gómez García 2024-01-05 01:02:11 +01:00
parent a7dc47dd6e
commit bac161b46c
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
60 changed files with 396 additions and 396 deletions

View File

@ -429,7 +429,7 @@ class Initialize(ActorV3Action):
dbFilter = UserService.objects.filter(deployed_service__service=service)
else:
# If not service provided token, use actor tokens
if not Server.validateToken(token, types.servers.ServerType.ACTOR):
if not Server.validate_token(token, types.servers.ServerType.ACTOR):
raise BlockAccess()
# Build the possible ids and make initial filter to match ANY userservice with provided MAC
idsList = [i['mac'] for i in self._params['id'][:5]]
@ -624,11 +624,11 @@ class Login(ActorV3Action):
if osManager: # For os managed services, let's check if we honor deadline
if osManager.ignoreDeadLine():
deadLine = userService.deployed_service.getDeadline()
deadLine = userService.deployed_service.get_deadline()
else:
deadLine = None
else: # For non os manager machines, process deadline as always
deadLine = userService.deployed_service.getDeadline()
deadLine = userService.deployed_service.get_deadline()
except (
Exception

View File

@ -174,11 +174,11 @@ class MetaAssignedService(DetailHandler):
k: v
for k, v in models.Properties.objects.filter(
owner_type='userservice',
owner_id__in=m.pool.assignedUserServices().values_list('uuid', flat=True),
owner_id__in=m.pool.assigned_user_services().values_list('uuid', flat=True),
).values_list('key', 'value')
}
for u in (
m.pool.assignedUserServices()
m.pool.assigned_user_services()
.filter(state__in=State.VALID_STATES)
.prefetch_related('deployed_service', 'publication')
):

View File

@ -205,7 +205,7 @@ class ServerEvent(Handler):
# * Logout: { 'username': 'username'}
# * Log: { 'level': 'level', 'message': 'message'}
try:
return ServerManager.manager().processEvent(server, self._params)
return ServerManager.manager().process_event(server, self._params)
except Exception as e:
logger.error('Error processing event %s: %s', self._params, e)
return rest_result('error', error='Error processing event')

View File

@ -356,7 +356,7 @@ class Services(DetailHandler): # pylint: disable=too-many-public-methods
'user_services_count': i.userServices.exclude(
state__in=(State.REMOVED, State.ERROR)
).count(),
'state': _('With errors') if i.isRestrained() else _('Ok'),
'state': _('With errors') if i.is_restrained() else _('Ok'),
}
)
except AccessDenied:

View File

@ -200,7 +200,7 @@ class ServicesPools(ModelHandler):
poolGroupThumb = item.servicesPoolGroup.image.thumb64
state = item.state
if item.isInMaintenance():
if item.is_in_maintenance():
state = State.MAINTENANCE
# This needs a lot of queries, and really does not apport anything important to the report
# elif UserServiceManager().canInitiateServiceFromDeployedService(item) is False:
@ -246,7 +246,7 @@ class ServicesPools(ModelHandler):
else:
valid_count = item.userServices.exclude(state__in=State.INFO_STATES).count()
preparing_count = item.userServices.filter(state=State.PREPARING).count()
restrained = item.isRestrained()
restrained = item.is_restrained()
usage_count = -1
poolGroupId = None

View File

@ -164,7 +164,7 @@ class System(Handler):
state__in=(State.REMOVED, State.ERROR)
).count()
restrained_services_pools: int = (
models.ServicePool.getRestrainedsQuerySet().count()
models.ServicePool.restraineds_queryset().count()
)
return {
'users': users,

View File

@ -81,7 +81,7 @@ class TunnelTicket(Handler):
# Take token from url
token = self._args[2][:48]
if not models.Server.validateToken(token, serverType=types.servers.ServerType.TUNNEL):
if not models.Server.validate_token(token, serverType=types.servers.ServerType.TUNNEL):
if self._args[1][:4] == 'stop':
# "Discard" invalid stop requests, because Applications does not like them.
# RDS connections keep alive for a while after the application is finished,
@ -123,7 +123,7 @@ class TunnelTicket(Handler):
logger.warning('Error logging tunnel close event: %s', e)
else:
if net.ipToLong(self._args[1][:32]).version == 0:
if net.ip_to_long(self._args[1][:32]).version == 0:
raise Exception('Invalid from IP')
events.addEvent(
userService.deployed_service,

View File

@ -128,17 +128,17 @@ class AssignedService(DetailHandler):
k: v
for k, v in models.Properties.objects.filter(
owner_type='userservice',
owner_id__in=parent.assignedUserServices().values_list('uuid', flat=True),
owner_id__in=parent.assigned_user_services().values_list('uuid', flat=True),
).values_list('key', 'value')
}
return [
AssignedService.itemToDict(k, properties.get(k.uuid, {}))
for k in parent.assignedUserServices()
for k in parent.assigned_user_services()
.all()
.prefetch_related('deployed_service', 'publication', 'user')
]
return AssignedService.itemToDict(
parent.assignedUserServices().get(processUuid(uuid=processUuid(item))),
parent.assigned_user_services().get(processUuid(uuid=processUuid(item))),
props={
k: v
for k, v in models.Properties.objects.filter(
@ -181,7 +181,7 @@ class AssignedService(DetailHandler):
def getLogs(self, parent: 'Model', item: str) -> list[typing.Any]:
parent = ensure.is_instance(parent, models.ServicePool)
try:
userService: models.UserService = parent.assignedUserServices().get(uuid=processUuid(item))
userService: models.UserService = parent.assigned_user_services().get(uuid=processUuid(item))
logger.debug('Getting logs for %s', userService)
return log.get_logs(userService)
except Exception as e:
@ -260,11 +260,11 @@ class CachedService(AssignedService):
if not item:
return [
AssignedService.itemToDict(k, is_cache=True)
for k in parent.cachedUserServices()
for k in parent.cached_users_services()
.all()
.prefetch_related('deployed_service', 'publication')
]
cachedService: models.UserService = parent.cachedUserServices().get(uuid=processUuid(item))
cachedService: models.UserService = parent.cached_users_services().get(uuid=processUuid(item))
return AssignedService.itemToDict(cachedService, is_cache=True)
except Exception as e:
logger.exception('getItems')
@ -294,7 +294,7 @@ class CachedService(AssignedService):
def getLogs(self, parent: 'Model', item: str) -> list[typing.Any]:
parent = ensure.is_instance(parent, models.ServicePool)
try:
userService = parent.cachedUserServices().get(uuid=processUuid(item))
userService = parent.cached_users_services().get(uuid=processUuid(item))
logger.debug('Getting logs for %s', item)
return log.get_logs(userService)
except Exception:
@ -511,7 +511,7 @@ class Publications(DetailHandler):
'revision': i.revision,
'publish_date': i.publish_date,
'state': i.state,
'reason': State.isErrored(i.state) and i.get_intance().error_reason() or '',
'reason': State.is_errored(i.state) and i.get_intance().error_reason() or '',
'state_date': i.state_date,
}
for i in parent.publications.all()

View File

@ -75,7 +75,7 @@ def getGroupsFromMeta(groups) -> collections.abc.Iterable[Group]:
def getPoolsForGroups(groups):
for servicePool in ServicePool.getDeployedServicesForGroups(groups):
for servicePool in ServicePool.get_pools_for_groups(groups):
yield servicePool
@ -305,7 +305,7 @@ class Users(DetailHandler):
'user_services_count': i.userServices.exclude(
state__in=(State.REMOVED, State.ERROR)
).count(),
'state': _('With errors') if i.isRestrained() else _('Ok'),
'state': _('With errors') if i.is_restrained() else _('Ok'),
}
)
@ -505,7 +505,7 @@ class Groups(DetailHandler):
'user_services_count': i.userServices.exclude(
state__in=(State.REMOVED, State.ERROR)
).count(),
'state': _('With errors') if i.isRestrained() else _('Ok'),
'state': _('With errors') if i.is_restrained() else _('Ok'),
}
)

View File

@ -250,7 +250,7 @@ def register_user(
usr = authenticator.get_or_create_user(username, username)
usr.real_name = authInstance.get_real_name(username)
usr.save()
if usr is not None and State.isActive(usr.state):
if usr is not None and State.is_active(usr.state):
# Now we update database groups for this user
usr.getManager().recreate_groups(usr)
# And add an login event

View File

@ -82,7 +82,7 @@ class PublicationOldMachinesCleaner(DelayedTask):
servicePoolPub.deployed_service.userServices.filter(in_use=True).update(
in_use=False, state_date=now
)
servicePoolPub.deployed_service.markOldUserServicesAsRemovables(activePub)
servicePoolPub.deployed_service.mark_old_userservices_as_removable(activePub)
except (
Exception
): # nosec: Removed publication, no problem at all, just continue
@ -119,7 +119,7 @@ class PublicationLauncher(DelayedTask):
state = pi.publish()
servicePool: ServicePool = servicePoolPub.deployed_service
servicePool.current_pub_revision += 1
servicePool.storeValue(
servicePool.set_value(
'toBeReplacedIn',
serialize(
now
@ -168,14 +168,14 @@ class PublicationFinishChecker(DelayedTask):
try:
prevState: str = publication.state
checkLater: bool = False
if State.isFinished(state):
if State.is_finished(state):
# Now we mark, if it exists, the previous usable publication as "Removable"
if State.isPreparing(prevState):
if State.is_preparing(prevState):
old: ServicePoolPublication
for old in publication.deployed_service.publications.filter(
state=State.USABLE
):
old.setState(State.REMOVABLE)
old.set_state(State.REMOVABLE)
osm = publication.deployed_service.osmanager
# If os manager says "machine is persistent", do not tray to delete "previous version" assigned machines
@ -192,26 +192,26 @@ class PublicationFinishChecker(DelayedTask):
'pclean-' + str(old.id),
True,
)
publication.deployed_service.markOldUserServicesAsRemovables(
publication.deployed_service.mark_old_userservices_as_removable(
publication
)
else: # Remove only cache services, not assigned
publication.deployed_service.markOldUserServicesAsRemovables(
publication.deployed_service.mark_old_userservices_as_removable(
publication, True
)
publication.setState(State.USABLE)
elif State.isRemoving(prevState):
publication.setState(State.REMOVED)
publication.set_state(State.USABLE)
elif State.is_removing(prevState):
publication.set_state(State.REMOVED)
else: # State is canceling
publication.setState(State.CANCELED)
publication.set_state(State.CANCELED)
# Mark all previous publications deployed services as removables
# and make this usable
publicationInstance.finish()
publication.updateData(publicationInstance)
elif State.isErrored(state):
elif State.is_errored(state):
publication.updateData(publicationInstance)
publication.setState(State.ERROR)
publication.set_state(State.ERROR)
else:
checkLater = True # The task is running
publication.updateData(publicationInstance)
@ -297,7 +297,7 @@ class PublicationManager(metaclass=singleton.Singleton):
)
)
if servicePool.isInMaintenance():
if servicePool.is_in_maintenance():
raise PublishException(
_('Service is in maintenance mode and new publications are not allowed')
)
@ -348,21 +348,21 @@ class PublicationManager(metaclass=singleton.Singleton):
'Forced cancel on publication, you must check uncleaned resources manually',
log.LogSource.ADMIN,
)
publication.setState(State.CANCELED)
publication.set_state(State.CANCELED)
publication.save()
return publication
raise PublishException(_('Can\'t cancel non running publication'))
if publication.state == State.LAUNCHING:
publication.state = State.CANCELED
publication.deployed_service.storeValue('toBeReplacedIn', None)
publication.deployed_service.set_value('toBeReplacedIn', None)
publication.save()
return publication
try:
pubInstance = publication.get_intance()
state = pubInstance.cancel()
publication.setState(State.CANCELING)
publication.set_state(State.CANCELING)
PublicationFinishChecker.state_updater(
publication, pubInstance, state
)
@ -378,8 +378,8 @@ class PublicationManager(metaclass=singleton.Singleton):
:param servicePoolPub: Publication to unpublish
"""
if (
State.isUsable(servicePoolPub.state) is False
and State.isRemovable(servicePoolPub.state) is False
State.is_usable(servicePoolPub.state) is False
and State.is_removable(servicePoolPub.state) is False
):
raise PublishException(_('Can\'t unpublish non usable publication'))
if servicePoolPub.userServices.exclude(state__in=State.INFO_STATES).count() > 0:
@ -389,7 +389,7 @@ class PublicationManager(metaclass=singleton.Singleton):
try:
pubInstance = servicePoolPub.get_intance()
state = pubInstance.destroy()
servicePoolPub.setState(State.REMOVING)
servicePoolPub.set_state(State.REMOVING)
PublicationFinishChecker.state_updater(
servicePoolPub, pubInstance, state
)

View File

@ -69,69 +69,69 @@ class ServerManager(metaclass=singleton.Singleton):
def manager() -> 'ServerManager':
return ServerManager() # Singleton pattern will return always the same instance
def cntStorage(self) -> 'StorageAccess':
def counter_storage(self) -> 'StorageAccess':
# If counters are too old, restart them
if datetime.datetime.now() - self.last_counters_clean > self.MAX_COUNTERS_AGE:
self.clearUnmanagedUsage()
return Storage(self.STORAGE_NAME).map(atomic=True, group='counters')
def propertyName(self, user: typing.Optional[typing.Union[str, 'models.User']]) -> str:
def property_name(self, user: typing.Optional[typing.Union[str, 'models.User']]) -> str:
"""Returns the property name for a user"""
if isinstance(user, str):
return ServerManager.BASE_PROPERTY_NAME + user
return ServerManager.BASE_PROPERTY_NAME + (str(user.uuid) if user else '_')
def clearUnmanagedUsage(self) -> None:
with self.cntStorage() as counters:
with self.counter_storage() as counters:
counters.clear()
self.last_counters_clean = datetime.datetime.now()
def getUnmanagedUsage(self, uuid: str) -> int:
def get_unmanaged_usage(self, uuid: str) -> int:
uuid = 'c' + uuid
with self.cntStorage() as counters:
with self.counter_storage() as counters:
return counters.get(uuid, 0)
def decreaseUnmanagedUsage(self, uuid: str, forceReset: bool = False) -> None:
def decrement_unmanaged_usage(self, uuid: str, force_reset: bool = False) -> None:
uuid = 'c' + uuid
with self.cntStorage() as counters:
with self.counter_storage() as counters:
if uuid in counters:
counters[uuid] -= 1
if counters[uuid] <= 0 or forceReset:
if counters[uuid] <= 0 or force_reset:
del counters[uuid]
def increaseUnmanagedUsage(self, uuid: str, onlyIfExists: bool = False) -> None:
def increment_unmanaged_usage(self, uuid: str, only_if_exists: bool = False) -> None:
uuid = 'c' + uuid
with self.cntStorage() as counters:
if not onlyIfExists or uuid in counters:
with self.counter_storage() as counters:
if not only_if_exists or uuid in counters:
counters[uuid] = counters.get(uuid, 0) + 1
def getServerStats(
def get_server_stats(
self, serversFltr: 'QuerySet[models.Server]'
) -> list[tuple[typing.Optional['types.servers.ServerStats'], 'models.Server']]:
"""
Returns a list of stats for a list of servers
"""
# Paralelize stats retrieval
retrievedStats: list[tuple[typing.Optional['types.servers.ServerStats'], 'models.Server']] = []
retrieved_stats: list[tuple[typing.Optional['types.servers.ServerStats'], 'models.Server']] = []
def _retrieveStats(server: 'models.Server') -> None:
def _retrieve_stats(server: 'models.Server') -> None:
try:
retrievedStats.append(
(requester.ServerApiRequester(server).getStats(), server)
retrieved_stats.append(
(requester.ServerApiRequester(server).get_stats(), server)
) # Store stats for later use
except Exception:
retrievedStats.append((None, server))
retrieved_stats.append((None, server))
# Retrieve, in parallel, stats for all servers (not restrained)
with ThreadPoolExecutor(max_workers=10) as executor:
for server in serversFltr.select_for_update():
if server.isRestrained():
if server.is_restrained():
continue # Skip restrained servers
executor.submit(_retrieveStats, server)
executor.submit(_retrieve_stats, server)
return retrievedStats
return retrieved_stats
def _findBestServer(
def _find_best_server(
self,
userService: 'models.UserService',
serverGroup: 'models.ServerGroup',
@ -149,7 +149,7 @@ class ServerManager(metaclass=singleton.Singleton):
if excludeServersUUids:
fltrs = fltrs.exclude(uuid__in=excludeServersUUids)
serversStats = self.getServerStats(fltrs)
serversStats = self.get_server_stats(fltrs)
# Now, cachedStats has a list of tuples (stats, server), use it to find the best server
for stats, server in serversStats:
@ -173,10 +173,10 @@ class ServerManager(metaclass=singleton.Singleton):
# So we have to provide a way to "reset" the server usage, and this is done by
# Get counter with less usage
best_with_counter = sorted(
[(s, self.getUnmanagedUsage(s.uuid)) for s in unmanaged_list], key=lambda x: x[1]
[(s, self.get_unmanaged_usage(s.uuid)) for s in unmanaged_list], key=lambda x: x[1]
)[0]
# Update counter
self.increaseUnmanagedUsage(best_with_counter[0].uuid)
self.increment_unmanaged_usage(best_with_counter[0].uuid)
best = (
best_with_counter[0],
types.servers.ServerStats.empty(),
@ -184,7 +184,7 @@ class ServerManager(metaclass=singleton.Singleton):
# If best was locked, notify it (will be notified again on assign)
if best[0].locked_until is not None:
requester.ServerApiRequester(best[0]).notifyRelease(userService)
requester.ServerApiRequester(best[0]).notify_release(userService)
return best
@ -218,7 +218,7 @@ class ServerManager(metaclass=singleton.Singleton):
raise exceptions.UDSException(_('No user assigned to service'))
# Look for existing user asignation through properties
prop_name = self.propertyName(userService.user)
prop_name = self.property_name(userService.user)
now = model_utils.sql_datetime()
excludeServersUUids = excludeServersUUids or set()
@ -236,7 +236,7 @@ class ServerManager(metaclass=singleton.Singleton):
raise exceptions.UDSException(_('Server is not part of the group'))
elif server.maintenance_mode:
raise exceptions.UDSException(_('Server is in maintenance mode'))
elif server.isRestrained():
elif server.is_restrained():
raise exceptions.UDSException(_('Server is restrained'))
# if server.uuid is stored uuid, increase counter (and counters if exits), else store it
@ -246,25 +246,25 @@ class ServerManager(metaclass=singleton.Singleton):
props[prop_name] = (server.uuid, 0)
info = types.servers.ServerCounter(server.uuid, 0)
self.increaseUnmanagedUsage(server.uuid, onlyIfExists=True)
self.increment_unmanaged_usage(server.uuid, only_if_exists=True)
else:
if info and info.server_uuid:
# If server does not exists, or it is in maintenance, or it is in exclude list or it is restrained,
# remove it from saved and use look for another one
svr = models.Server.objects.filter(uuid=info.server_uuid).first()
if not svr or (
svr.maintenance_mode or svr.uuid in excludeServersUUids or svr.isRestrained()
svr.maintenance_mode or svr.uuid in excludeServersUUids or svr.is_restrained()
):
info = None
del props[prop_name]
else:
# Increase "local" counters for RR if needed
self.increaseUnmanagedUsage(info.server_uuid, onlyIfExists=True)
self.increment_unmanaged_usage(info.server_uuid, only_if_exists=True)
# If no existing assignation, check for a new one
if info is None:
try:
with transaction.atomic():
best = self._findBestServer(
best = self._find_best_server(
userService=userService,
serverGroup=serverGroup,
now=now,
@ -289,11 +289,11 @@ class ServerManager(metaclass=singleton.Singleton):
# Ensure next assignation will have updated stats
# This is a simple simulation on cached stats, will be updated on next stats retrieval
# (currently, cache time is 1 minute)
bestServer.newAssignation()
bestServer.interpolate_new_assignation()
# Notify assgination in every case, even if reassignation to same server is made
# This lets the server to keep track, if needed, of multi-assignations
self.notifyAssign(bestServer, userService, serviceType, info.counter)
self.notify_assign(bestServer, userService, serviceType, info.counter)
return info
def release(
@ -317,7 +317,7 @@ class ServerManager(metaclass=singleton.Singleton):
if userUuid is None:
return types.servers.ServerCounter.null() # No user is assigned to this service, nothing to do
prop_name = self.propertyName(userService.user)
prop_name = self.property_name(userService.user)
with serverGroup.properties as props:
with transaction.atomic():
resetCounter = False
@ -350,14 +350,14 @@ class ServerManager(metaclass=singleton.Singleton):
# If unmanaged, decrease usage
if server.type == types.servers.ServerType.UNMANAGED:
self.decreaseUnmanagedUsage(server.uuid, forceReset=resetCounter)
self.decrement_unmanaged_usage(server.uuid, force_reset=resetCounter)
# Ensure next assignation will have updated stats
# This is a simple simulation on cached stats, will be updated on next stats retrieval
# (currently, cache time is 1 minute)
server.newRelease()
server.interpolate_new_release()
self.notifyRelease(server, userService)
self.notify_release(server, userService)
return types.servers.ServerCounter(serverCounter.server_uuid, serverCounter.counter - 1)
@ -374,12 +374,12 @@ class ServerManager(metaclass=singleton.Singleton):
Notifies preconnect to server
"""
if not server:
server = self.getServerAssignation(userService, serverGroup)
server = self.server_assignation_for(userService, serverGroup)
if server:
requester.ServerApiRequester(server).notify_preconnect(userService, info)
def notifyAssign(
def notify_assign(
self,
server: 'models.Server',
userService: 'models.UserService',
@ -389,9 +389,9 @@ class ServerManager(metaclass=singleton.Singleton):
"""
Notifies assign to server
"""
requester.ServerApiRequester(server).notifyAssign(userService, serviceType, counter)
requester.ServerApiRequester(server).notify_assign(userService, serviceType, counter)
def notifyRelease(
def notify_release(
self,
server: 'models.Server',
userService: 'models.UserService',
@ -399,9 +399,9 @@ class ServerManager(metaclass=singleton.Singleton):
"""
Notifies release to server
"""
requester.ServerApiRequester(server).notifyRelease(userService)
requester.ServerApiRequester(server).notify_release(userService)
def getAssignInformation(self, serverGroup: 'models.ServerGroup') -> dict[str, int]:
def assignation_info(self, serverGroup: 'models.ServerGroup') -> dict[str, int]:
"""
Get usage information for a server group
@ -419,7 +419,7 @@ class ServerManager(metaclass=singleton.Singleton):
res[kk] = res.get(kk, 0) + v[1]
return res
def getServerAssignation(
def server_assignation_for(
self,
userService: 'models.UserService',
serverGroup: 'models.ServerGroup',
@ -437,7 +437,7 @@ class ServerManager(metaclass=singleton.Singleton):
if not userService.user:
raise exceptions.UDSException(_('No user assigned to service'))
prop_name = self.propertyName(userService.user)
prop_name = self.property_name(userService.user)
with serverGroup.properties as props:
info: typing.Optional[types.servers.ServerCounter] = types.servers.ServerCounter.from_iterable(
props.get(prop_name)
@ -446,7 +446,7 @@ class ServerManager(metaclass=singleton.Singleton):
return None
return models.Server.objects.get(uuid=info.server_uuid)
def getSortedServers(
def sorted_server_list(
self,
serverGroup: 'models.ServerGroup',
excludeServersUUids: typing.Optional[typing.Set[str]] = None,
@ -468,11 +468,11 @@ class ServerManager(metaclass=singleton.Singleton):
fltrs = fltrs.exclude(uuid__in=excludeServersUUids)
# Get the stats for all servers, but in parallel
serverStats = self.getServerStats(fltrs)
serverStats = self.get_server_stats(fltrs)
# Sort by weight, lower first (lower is better)
return [s[1] for s in sorted(serverStats, key=lambda x: x[0].weight() if x[0] else 999999999)]
def doMaintenance(self, serverGroup: 'models.ServerGroup') -> None:
def perform_maintenance(self, serverGroup: 'models.ServerGroup') -> None:
"""Realizes maintenance on server group
Maintenace operations include:
@ -490,7 +490,7 @@ class ServerManager(metaclass=singleton.Singleton):
# User does not exists, remove it from counters
del serverGroup.properties[k]
def processEvent(self, server: 'models.Server', data: dict[str, typing.Any]) -> typing.Any:
def process_event(self, server: 'models.Server', data: dict[str, typing.Any]) -> typing.Any:
"""
Processes a notification FROM server
That is, this is not invoked directly unless a REST request is received from

View File

@ -90,7 +90,7 @@ def process_login(server: 'models.Server', data: dict[str, typing.Any]) -> typin
data['userservice_uuid'] = data.get('userservice_uuid', ticket['userservice_uuid'])
userService = models.UserService.objects.get(uuid=data['userservice_uuid'])
server.setActorVersion(userService)
server.set_actor_version(userService)
if not userService.in_use: # If already logged in, do not add a second login (windows does this i.e.)
osmanagers.OSManager.loggedIn(userService, data['username'])
@ -105,7 +105,7 @@ def process_login(server: 'models.Server', data: dict[str, typing.Any]) -> typin
logger.debug('Max idle: %s', maxIdle)
deadLine = (
userService.deployed_service.getDeadline() if not osManager or osManager.ignoreDeadLine() else None
userService.deployed_service.get_deadline() if not osManager or osManager.ignoreDeadLine() else None
)
result = {
'ip': src.ip,

View File

@ -57,14 +57,14 @@ AUTH_TOKEN = 'X-TOKEN-AUTH'
# If exception is raised, it will restrain the server and return False
def restrain_server(func: collections.abc.Callable[..., typing.Any]) -> collections.abc.Callable[..., typing.Any]:
def inner(self: 'ServerApiRequester', *args: typing.Any, **kwargs: typing.Any) -> typing.Any:
if self.server.isRestrained():
if self.server.is_restrained():
return False
try:
return func(self, *args, **kwargs)
except Exception as e:
logger.error('Error executing %s: %s', func.__name__, e)
self.server.setRestrainedUntil(
self.server.set_restrained_until(
sql_datetime() + datetime.timedelta(seconds=consts.system.FAILURE_TIMEOUT)
) # Block server for a while
return False
@ -166,7 +166,7 @@ class ServerApiRequester:
return response.json()
@restrain_server
def notifyAssign(
def notify_assign(
self, userService: 'models.UserService', service_type: 'types.services.ServiceType', count: int
) -> bool:
"""
@ -228,7 +228,7 @@ class ServerApiRequester:
return True
@restrain_server
def notifyRelease(self, userService: 'models.UserService') -> bool:
def notify_release(self, userService: 'models.UserService') -> bool:
"""
Notifies removal of user service to server
"""
@ -237,7 +237,7 @@ class ServerApiRequester:
return True
def getStats(self) -> typing.Optional['types.servers.ServerStats']:
def get_stats(self) -> typing.Optional['types.servers.ServerStats']:
"""
Returns the stats of a server
"""

View File

@ -290,8 +290,8 @@ class UserServiceManager(metaclass=singleton.Singleton):
State.as_str(cache.state),
State.as_str(cache.os_state),
)
if State.isRuning(state) and cache.isUsable():
cache.setState(State.PREPARING)
if State.is_runing(state) and cache.isUsable():
cache.set_state(State.PREPARING)
# Data will be serialized on makeUnique process
UserServiceOpChecker.make_unique(cache, cacheInstance, state)
@ -316,7 +316,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
# State is kept, just mark it for destroy after finished preparing
userService.destroy_after = True
else:
userService.setState(State.CANCELING)
userService.set_state(State.CANCELING)
# We simply notify service that it should cancel operation
state = userServiceInstance.cancel()
@ -334,9 +334,9 @@ class UserServiceManager(metaclass=singleton.Singleton):
with transaction.atomic():
userService = UserService.objects.select_for_update().get(id=userService.id)
operationsLogger.info('Removing userService %a', userService.name)
if userService.isUsable() is False and State.isRemovable(userService.state) is False:
if userService.isUsable() is False and State.is_removable(userService.state) is False:
raise OperationException(_('Can\'t remove a non active element'))
userService.setState(State.REMOVING)
userService.set_state(State.REMOVING)
logger.debug("***** The state now is %s *****", State.as_str(userService.state))
userService.setInUse(False) # For accounting, ensure that it is not in use right now
userService.save()
@ -350,7 +350,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
return userService
def removeOrCancel(self, userService: UserService):
if userService.isUsable() or State.isRemovable(userService.state):
if userService.isUsable() or State.is_removable(userService.state):
return self.remove(userService)
if userService.isPreparing():
@ -363,7 +363,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
def getExistingAssignationForUser(
self, servicePool: ServicePool, user: User
) -> typing.Optional[UserService]:
existing = servicePool.assignedUserServices().filter(
existing = servicePool.assigned_user_services().filter(
user=user, state__in=State.VALID_STATES
) # , deployed_service__visible=True
if existing.exists():
@ -383,7 +383,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if assignedUserService:
return assignedUserService
if servicePool.isRestrained():
if servicePool.is_restrained():
raise InvalidServiceException(_('The requested service is restrained'))
cache: typing.Optional[UserService] = None
@ -391,7 +391,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
with transaction.atomic():
caches = typing.cast(
list[UserService],
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(
cache_level=services.UserService.L1_CACHE,
@ -405,7 +405,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
cache = caches[0]
# Ensure element is reserved correctly on DB
if (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(user=None, uuid=typing.cast(UserService, cache).uuid)
.update(user=user, cache_level=0)
@ -420,7 +420,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
with transaction.atomic():
caches = typing.cast(
list[UserService],
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(cache_level=services.UserService.L1_CACHE, state=State.USABLE)[
:1 # type: ignore # Slicing is not supported by pylance right now
@ -429,7 +429,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if cache:
cache = caches[0]
if (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(user=None, uuid=typing.cast(UserService, cache).uuid)
.update(user=user, cache_level=0)
@ -453,7 +453,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
events.addEvent(
servicePool,
events.ET_CACHE_HIT,
fld1=servicePool.cachedUserServices()
fld1=servicePool.cached_users_services()
.filter(cache_level=services.UserService.L1_CACHE, state=State.USABLE)
.count(),
)
@ -464,7 +464,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
# Now find if there is a preparing one
with transaction.atomic():
caches = (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(cache_level=services.UserService.L1_CACHE, state=State.PREPARING)[
:1 # type: ignore # Slicing is not supported by pylance right now
@ -473,7 +473,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if caches:
cache = caches[0] # type: ignore # Slicing is not supported by pylance right now
if (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(user=None, uuid=typing.cast(UserService, cache).uuid)
.update(user=user, cache_level=0)
@ -496,7 +496,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
events.addEvent(
servicePool,
events.ET_CACHE_MISS,
fld1=servicePool.cachedUserServices()
fld1=servicePool.cached_users_services()
.filter(cache_level=services.UserService.L1_CACHE, state=State.PREPARING)
.count(),
)
@ -506,7 +506,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
serviceType = servicePool.service.get_type()
if serviceType.usesCache:
inAssigned = (
servicePool.assignedUserServices().filter(self.getStateFilter(servicePool.service)).count()
servicePool.assigned_user_services().filter(self.getStateFilter(servicePool.service)).count()
)
if (
inAssigned >= servicePool.max_srvs
@ -583,7 +583,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
userService.updateData(userServiceInstance)
return True
userService.setState(State.PREPARING)
userService.set_state(State.PREPARING)
UserServiceOpChecker.make_unique(userService, userServiceInstance, state)
return False
@ -666,12 +666,12 @@ class UserServiceManager(metaclass=singleton.Singleton):
State.USABLE,
State.PREPARING,
): # We don't want to get active deleting or deleted machines...
userService.setState(State.PREPARING)
userService.set_state(State.PREPARING)
UserServiceOpChecker.make_unique(userService, userServiceInstance, state)
userService.save(update_fields=['os_state'])
except Exception as e:
logger.exception('Unhandled exception on notyfyReady: %s', e)
userService.setState(State.ERROR)
userService.set_state(State.ERROR)
return
def locateUserService(
@ -685,13 +685,13 @@ class UserServiceManager(metaclass=singleton.Singleton):
if kind in 'A': # This is an assigned service
logger.debug('Getting A service %s', uuidService)
userService = UserService.objects.get(uuid=uuidService, user=user)
typing.cast(UserService, userService).deployed_service.validateUser(user)
typing.cast(UserService, userService).deployed_service.validate_user(user)
else:
try:
servicePool: ServicePool = ServicePool.objects.get(uuid=uuidService)
# We first do a sanity check for this, if the user has access to this service
# If it fails, will raise an exception
servicePool.validateUser(user)
servicePool.validate_user(user)
# Now we have to locate an instance of the service, so we can assign it to user.
if create: # getAssignation, if no assignation is found, tries to create one
@ -744,7 +744,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if userService.isInMaintenance():
raise ServiceInMaintenanceMode()
if not userService.deployed_service.isAccessAllowed():
if not userService.deployed_service.is_access_allowed():
raise ServiceAccessDeniedByCalendar()
if not idTransport: # Find a suitable transport
@ -877,7 +877,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool)
# Get pool members. Just pools "visible" and "usable"
pools = [p.pool for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable()]
pools = [p.pool for p in meta.members.all() if p.pool.is_visible() and p.pool.is_usable()]
# look for an existing user service in the pool
try:
return UserService.objects.filter(
@ -914,7 +914,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
raise ServiceAccessDeniedByCalendar()
# Get pool members. Just pools "visible" and "usable"
poolMembers = [p for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable()]
poolMembers = [p for p in meta.members.all() if p.pool.is_visible() and p.pool.is_usable()]
# Sort pools array. List of tuples with (priority, pool)
sortPools: list[tuple[int, ServicePool]]
# Sort pools based on meta selection
@ -940,7 +940,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
pools: list[ServicePool] = []
poolsFull: list[ServicePool] = []
for p in sortedPools:
if not p[1].isUsable():
if not p[1].is_usable():
continue
if p[1].usage().percent == 100:
poolsFull.append(p[1])
@ -1024,7 +1024,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
# Stop if a pool-transport is found and can be assigned to user
if usable:
try:
usable[0].validateUser(user)
usable[0].validate_user(user)
return self.getService(
user,
os,

View File

@ -70,7 +70,7 @@ class StateUpdater:
def save(self, newState: typing.Optional[str] = None):
if newState:
self.user_service.setState(newState)
self.user_service.set_state(newState)
self.user_service.updateData(self.user_service_instance)
@ -124,7 +124,7 @@ class UpdateFromPreparing(StateUpdater):
# and make this usable if os manager says that it is usable, else it pass to configuring state
# This is an "early check" for os manager, so if we do not have os manager, or os manager
# already notifies "ready" for this, we
if osManager is not None and State.isPreparing(self.user_service.os_state):
if osManager is not None and State.is_preparing(self.user_service.os_state):
logger.debug('Has valid osmanager for %s', self.user_service.friendly_name)
stateOs = osManager.check_state(self.user_service)
@ -253,7 +253,7 @@ class UserServiceOpChecker(DelayedTask):
except Exception as e:
logger.exception('Checking service state')
log.log(userService, log.LogLevel.ERROR, f'Exception: {e}', log.LogSource.INTERNAL)
userService.setState(State.ERROR)
userService.set_state(State.ERROR)
userService.save(update_fields=['data'])
@staticmethod
@ -293,7 +293,7 @@ class UserServiceOpChecker(DelayedTask):
if user_service:
log.log(user_service, log.LogLevel.ERROR, f'Exception: {e}', log.LogSource.INTERNAL)
try:
user_service.setState(State.ERROR)
user_service.set_state(State.ERROR)
user_service.save(update_fields=['data'])
except Exception:
logger.error('Can\'t update state of uService object')

View File

@ -186,9 +186,9 @@ class OSManager(Module):
Helper method that informs if the os manager transforms the username and/or the password.
This is used from ServicePool
"""
return cls.processUserPassword != OSManager.processUserPassword
return cls.process_user_password != OSManager.process_user_password
def processUserPassword(
def process_user_password(
self,
userService: 'UserService', # pylint: disable=unused-argument
username: str,

View File

@ -204,9 +204,9 @@ class Transport(Module):
is done, because there is no relation at that level between user and service.
"""
if isinstance(userService, models.ServicePool):
username, password = userService.processUserPassword(user.name, password)
username, password = userService.process_user_password(user.name, password)
else:
username = self.processedUser(userService, user)
username = self.processed_username(userService, user)
return types.connections.ConnectionData(
protocol=self.protocol,
username=username,
@ -215,7 +215,7 @@ class Transport(Module):
domain='',
)
def processedUser(self, userService: 'models.UserService', user: 'models.User') -> str:
def processed_username(self, userService: 'models.UserService', user: 'models.User') -> str:
"""
Used to "transform" username that will be sent to service
This is used to make the "user" that will receive the service match with that sent in notification

View File

@ -52,22 +52,22 @@ class NetworkType(typing.NamedTuple):
logger = logging.getLogger(__name__)
# Test patters for networks IPv4
reCIDRIPv4 = re.compile(
RECIDRIPV4: typing.Final[re.Pattern] = re.compile(
r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/([0-9]{1,2})$'
)
reMaskIPv4 = re.compile(
REMASKIPV4: typing.Final[re.Pattern] = re.compile(
r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})netmask([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$'
)
re1AsteriskIPv4 = re.compile(r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.\*$')
re2AsteriskIPv4 = re.compile(r'^([0-9]{1,3})\.([0-9]{1,3})\.\*\.?\*?$')
re3AsteriskIPv4 = re.compile(r'^([0-9]{1,3})\.\*\.?\*?\.?\*?$')
reRangeIPv4 = re.compile(
RE1ASTERISKIPV4: typing.Final[re.Pattern] = re.compile(r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.\*$')
RE2ASTERISKIPV4: typing.Final[re.Pattern] = re.compile(r'^([0-9]{1,3})\.([0-9]{1,3})\.\*\.?\*?$')
RE3ASTERISKIPV4: typing.Final[re.Pattern] = re.compile(r'^([0-9]{1,3})\.\*\.?\*?\.?\*?$')
RERANGEIPV4: typing.Final[re.Pattern] = re.compile(
r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})-([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$'
)
reSingleIPv4 = re.compile(r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$')
RESINGLEIPV4: typing.Final[re.Pattern] = re.compile(r'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$')
def ipToLong(ip: str) -> IpType:
def ip_to_long(ip: str) -> IpType:
"""
Convert an ipv4 or ipv6 address to its long representation
"""
@ -85,7 +85,7 @@ def ipToLong(ip: str) -> IpType:
return IpType(0, 0) # Invalid values will map to "0.0.0.0" --> 0
def longToIp(n: int, version: typing.Literal[0, 4, 6] = 0) -> str:
def long_to_ip(n: int, version: typing.Literal[0, 4, 6] = 0) -> str:
"""
convert long int to ipv4 or ipv6 address, depending on size
"""
@ -94,7 +94,7 @@ def longToIp(n: int, version: typing.Literal[0, 4, 6] = 0) -> str:
return str(ipaddress.IPv4Address(n))
def networkFromStringIPv4(strNets: str) -> NetworkType:
def network_from_str_ipv4(nets_string: str) -> NetworkType:
'''
Parses the network from strings in this forms:
- A.* (or A.*.* or A.*.*.*)
@ -107,15 +107,15 @@ def networkFromStringIPv4(strNets: str) -> NetworkType:
returns a named tuple with networks start and network end
'''
inputString = strNets
logger.debug('Getting network from %s', strNets)
input_string = nets_string
logger.debug('Getting network from %s', nets_string)
def check(*args) -> None:
for n in args:
if int(n) < 0 or int(n) > 255:
raise Exception()
def toNum(*args) -> int:
def to_num(*args) -> int:
start = 256 * 256 * 256
val = 0
for n in args:
@ -123,74 +123,74 @@ def networkFromStringIPv4(strNets: str) -> NetworkType:
start >>= 8
return val
def maskFromBits(nBits: int) -> int:
def mask_from_bits(nBits: int) -> int:
v = 0
for n in range(nBits):
v |= 1 << (31 - n)
return v
strNets = strNets.replace(' ', '')
nets_string = nets_string.replace(' ', '')
if strNets == '*':
if nets_string == '*':
return NetworkType(0, 2**32 - 1, 4)
try:
# Test patterns
m = reCIDRIPv4.match(strNets)
m = RECIDRIPV4.match(nets_string)
if m is not None:
logger.debug('Format is CIDR')
check(*m.groups())
bits = int(m.group(5))
if bits < 0 | bits > 32:
raise Exception()
val = toNum(*m.groups())
bits = maskFromBits(bits)
val = to_num(*m.groups())
bits = mask_from_bits(bits)
noBits = ~bits & 0xFFFFFFFF
return NetworkType(val & bits, val | noBits, 4)
m = reMaskIPv4.match(strNets)
m = REMASKIPV4.match(nets_string)
if m is not None:
logger.debug('Format is network mask')
check(*m.groups())
val = toNum(*(m.groups()[0:4]))
bits = toNum(*(m.groups()[4:8]))
val = to_num(*(m.groups()[0:4]))
bits = to_num(*(m.groups()[4:8]))
noBits = ~bits & 0xFFFFFFFF
return NetworkType(val & bits, val | noBits, 4)
m = reRangeIPv4.match(strNets)
m = RERANGEIPV4.match(nets_string)
if m is not None:
logger.debug('Format is network range')
check(*m.groups())
val = toNum(*(m.groups()[0:4]))
val2 = toNum(*(m.groups()[4:8]))
val = to_num(*(m.groups()[0:4]))
val2 = to_num(*(m.groups()[4:8]))
if val2 < val:
raise Exception()
return NetworkType(val, val2, 4)
m = reSingleIPv4.match(strNets)
m = RESINGLEIPV4.match(nets_string)
if m is not None:
logger.debug('Format is a single host')
check(*m.groups())
val = toNum(*m.groups())
val = to_num(*m.groups())
return NetworkType(val, val, 4)
for v in ((re1AsteriskIPv4, 3), (re2AsteriskIPv4, 2), (re3AsteriskIPv4, 1)):
m = v[0].match(strNets)
for v in ((RE1ASTERISKIPV4, 3), (RE2ASTERISKIPV4, 2), (RE3ASTERISKIPV4, 1)):
m = v[0].match(nets_string)
if m is not None:
check(*m.groups())
val = toNum(*(m.groups()[0 : v[1] + 1]))
bits = maskFromBits(v[1] * 8)
val = to_num(*(m.groups()[0 : v[1] + 1]))
bits = mask_from_bits(v[1] * 8)
noBits = ~bits & 0xFFFFFFFF
return NetworkType(val & bits, val | noBits, 4)
# No pattern recognized, invalid network
raise Exception()
except Exception as e:
logger.error('Invalid network found: %s %s', strNets, e)
raise ValueError(inputString) from e
logger.error('Invalid network found: %s %s', nets_string, e)
raise ValueError(input_string) from e
def networkFromStringIPv6(strNets: str) -> NetworkType:
def network_from_str_ipv6(strNets: str) -> NetworkType:
'''
returns a named tuple with networks start and network end
'''
@ -209,20 +209,20 @@ def networkFromStringIPv6(strNets: str) -> NetworkType:
raise ValueError(strNets) from e
def networkFromString(
def network_from_str(
strNets: str,
version: typing.Literal[0, 4, 6] = 0,
) -> NetworkType:
if not ':' in strNets and version != 6:
return networkFromStringIPv4(strNets)
return network_from_str_ipv4(strNets)
# ':' in strNets or version == 6:
# If is in fact an IPv4 address, return None network, this will not be used
if '.' in strNets:
return NetworkType(0, 0, 0)
return networkFromStringIPv6(strNets)
return network_from_str_ipv6(strNets)
def networksFromString(
def networks_from_str(
nets: str,
version: typing.Literal[0, 4, 6] = 0,
) -> list[NetworkType]:
@ -233,7 +233,7 @@ def networksFromString(
res = []
for strNet in re.split('[;,]', nets):
if strNet:
res.append(networkFromString(strNet, version))
res.append(network_from_str(strNet, version))
return res
@ -243,11 +243,11 @@ def contains(
version: typing.Literal[0, 4, 6] = 0,
) -> bool:
if isinstance(ip, str):
ip, version = ipToLong(ip) # Ip overrides protocol version
ip, version = ip_to_long(ip) # Ip overrides protocol version
if isinstance(networks, str):
if networks == '*':
return True # All IPs are in the * network
networks = networksFromString(networks, version)
networks = networks_from_str(networks, version)
elif isinstance(networks, NetworkType):
networks = [networks]
@ -258,7 +258,7 @@ def contains(
return False
def isValidIp(value: str, version: typing.Literal[0, 4, 6] = 0) -> bool:
def is_valid_ip(value: str, version: typing.Literal[0, 4, 6] = 0) -> bool:
# Using ipaddress module
try:
addr = ipaddress.ip_address(value)
@ -267,7 +267,7 @@ def isValidIp(value: str, version: typing.Literal[0, 4, 6] = 0) -> bool:
return False
def isValidFQDN(value: str) -> bool:
def is_valid_fqdn(value: str) -> bool:
return (
re.match(
r'^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$',
@ -277,8 +277,8 @@ def isValidFQDN(value: str) -> bool:
)
def isValidHost(value: str):
return isValidIp(value) or isValidFQDN(value)
def is_valid_host(value: str):
return is_valid_ip(value) or is_valid_fqdn(value)
def test_connectivity(host: str, port: int, timeOut: float = 4) -> bool:

View File

@ -137,7 +137,7 @@ class PropertyAccessor:
class PropertiesMixin:
"""Mixin to add properties to a model"""
def ownerIdAndType(self) -> tuple[str, str]:
def get_owner_id_and_type(self) -> tuple[str, str]:
"""Returns the owner id and type of this object
The owner id and type is used to identify the owner in the properties table
@ -149,7 +149,7 @@ class PropertiesMixin:
@property
def properties(self) -> PropertyAccessor:
owner_id, owner_type = self.ownerIdAndType()
owner_id, owner_type = self.get_owner_id_and_type()
return PropertyAccessor(owner_id=owner_id, owner_type=owner_type)
@staticmethod

View File

@ -99,59 +99,59 @@ class State:
PUBLISH_STATES = [LAUNCHING, PREPARING]
@staticmethod
def isActive(state: str) -> bool:
def is_active(state: str) -> bool:
return state == State.ACTIVE
@staticmethod
def isInactive(state: str) -> bool:
def is_inactive(state: str) -> bool:
return state == State.INACTIVE
@staticmethod
def isBlocked(state: str) -> bool:
def is_blocked(state: str) -> bool:
return state == State.BLOCKED
@staticmethod
def isPreparing(state: str) -> bool:
def is_preparing(state: str) -> bool:
return state == State.PREPARING
@staticmethod
def isUsable(state : str) -> bool:
def is_usable(state : str) -> bool:
return state == State.USABLE
@staticmethod
def isRemovable(state: str) -> bool:
def is_removable(state: str) -> bool:
return state == State.REMOVABLE
@staticmethod
def isRemoving(state: str) -> bool:
def is_removing(state: str) -> bool:
return state == State.REMOVING
@staticmethod
def isRemoved(state: str) -> bool:
def is_removed(state: str) -> bool:
return state == State.REMOVED
@staticmethod
def isCanceling(state: str) -> bool:
def is_canceling(state: str) -> bool:
return state == State.CANCELING
@staticmethod
def isCanceled(state: str) -> bool:
def is_canceled(state: str) -> bool:
return state == State.CANCELED
@staticmethod
def isErrored(state: str) -> bool:
def is_errored(state: str) -> bool:
return state == State.ERROR
@staticmethod
def isFinished(state: str) -> bool:
def is_finished(state: str) -> bool:
return state == State.FINISHED
@staticmethod
def isRuning(state: str) -> bool:
def is_runing(state: str) -> bool:
return state == State.RUNNING
@staticmethod
def isForExecute(state: str) -> bool:
def is_for_execute(state: str) -> bool:
return state == State.FOR_EXECUTE
@staticmethod

View File

@ -67,9 +67,9 @@ class AssignedAndUnused(Job):
).filter(outdated__gt=0, state=State.ACTIVE)
for ds in outdatedServicePools:
# Skips checking deployed services in maintenance mode or ignores assigned and unused
if ds.isInMaintenance() or ds.ignores_unused:
if ds.is_in_maintenance() or ds.ignores_unused:
continue
unusedMachines = ds.assignedUserServices().filter(
unusedMachines = ds.assigned_user_services().filter(
in_use=False,
state_date__lt=since_state,
state=State.USABLE,

View File

@ -177,7 +177,7 @@ class DeployedServiceRemover(Job):
for servicePool in removableServicePools:
try:
# Skips checking deployed services in maintenance mode
if servicePool.isInMaintenance() is False:
if servicePool.is_in_maintenance() is False:
self.startRemovalOf(servicePool)
except Exception as e1:
logger.error('Error removing service pool %s: %s', servicePool.name, e1)
@ -203,7 +203,7 @@ class DeployedServiceRemover(Job):
self.forceRemovalOf(servicePool) # Force removal
# Skips checking deployed services in maintenance mode
if servicePool.isInMaintenance() is False:
if servicePool.is_in_maintenance() is False:
self.continueRemovalOf(servicePool)
except Exception:
logger.exception('Removing deployed service')

View File

@ -115,7 +115,7 @@ class ServiceCacheUpdater(Job):
)
continue
if servicePool.isRestrained():
if servicePool.is_restrained():
logger.debug(
'StopSkippingped cache generation for restrained service pool: %s',
servicePool.name,
@ -127,17 +127,17 @@ class ServiceCacheUpdater(Job):
# Before we were removing the elements marked to be destroyed after creation, but this makes us
# to create new items over the limit stablisshed, so we will not remove them anymore
inCacheL1: int = (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.filter(UserServiceManager().getCacheStateFilter(servicePool, services.UserService.L1_CACHE))
.count()
)
inCacheL2: int = (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.filter(UserServiceManager().getCacheStateFilter(servicePool, services.UserService.L2_CACHE))
.count()
) if spServiceInstance.usesCache_L2 else 0
inAssigned: int = (
servicePool.assignedUserServices()
servicePool.assigned_user_services()
.filter(UserServiceManager().getStateFilter(servicePool.service)) # type: ignore
.count()
)
@ -216,7 +216,7 @@ class ServiceCacheUpdater(Job):
valid = None
with transaction.atomic():
for n in (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.select_for_update()
.filter(
UserServiceManager().getCacheStateFilter(servicePool, services.UserService.L2_CACHE)
@ -224,7 +224,7 @@ class ServiceCacheUpdater(Job):
.order_by('creation_date')
):
if n.needsOsManager():
if State.isUsable(n.state) is False or State.isUsable(n.os_state):
if State.is_usable(n.state) is False or State.is_usable(n.os_state):
valid = n
break
else:
@ -296,7 +296,7 @@ class ServiceCacheUpdater(Job):
# Here, we will take into account the "remove_after" marked user services, so we don't try to remove them
cacheItems: list[UserService] = [
i
for i in servicePool.cachedUserServices()
for i in servicePool.cached_users_services()
.filter(UserServiceManager().getCacheStateFilter(servicePool, services.UserService.L1_CACHE))
.order_by('-creation_date')
.iterator()
@ -313,7 +313,7 @@ class ServiceCacheUpdater(Job):
valid = None
for n in cacheItems:
if n.needsOsManager():
if State.isUsable(n.state) is False or State.isUsable(n.os_state):
if State.is_usable(n.state) is False or State.is_usable(n.os_state):
valid = n
break
else:
@ -337,7 +337,7 @@ class ServiceCacheUpdater(Job):
logger.debug("Reducing L2 cache erasing a service in cache for %s", servicePool.name)
if cacheL2 > 0:
cacheItems = (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.filter(UserServiceManager().getCacheStateFilter(servicePool, services.UserService.L2_CACHE))
.order_by('creation_date')
)

View File

@ -64,14 +64,14 @@ class DeployedServiceStatsCollector(Job):
totalAssigned, totalInUse, totalCached = 0, 0, 0
for servicePool in servicePoolsToCheck:
try:
fltr = servicePool.assignedUserServices().exclude(
fltr = servicePool.assigned_user_services().exclude(
state__in=State.INFO_STATES
)
assigned = fltr.count()
inUse = fltr.filter(in_use=True).count()
# Cached user services
cached = (
servicePool.cachedUserServices()
servicePool.cached_users_services()
.exclude(state__in=State.INFO_STATES)
.count()
)

View File

@ -99,5 +99,5 @@ class StuckCleaner(Job):
log.LogLevel.ERROR,
f'User service {stuck.name} has been hard removed because it\'s stuck',
)
# stuck.setState(State.ERROR)
# stuck.set_state(State.ERROR)
stuck.delete()

View File

@ -61,7 +61,7 @@ def dict2resp(dct: collections.abc.Mapping[typing.Any, typing.Any]) -> str:
@auth.trustedSourceRequired
def guacamole(request: ExtendedHttpRequestWithUser, token: str, tunnelId: str) -> HttpResponse:
if not Server.validateToken(token, serverType=types.servers.ServerType.TUNNEL):
if not Server.validate_token(token, serverType=types.servers.ServerType.TUNNEL):
logger.error('Invalid token %s from %s', token, request.ip)
return HttpResponse(ERROR, content_type=CONTENT_TYPE)
logger.debug('Received credentials request for tunnel id %s', tunnelId)

View File

@ -270,10 +270,10 @@ class StatsFS(types.UDSFSInterface):
size,
)
# Compose the csv file from what we now of service pools
virtualFile = models.ServicePool.getCSVHeader().encode() + b'\n'
virtualFile = models.ServicePool.get_cvs_header().encode() + b'\n'
# First, get the list of service pools
for pool in models.ServicePool.objects.all().order_by('name'):
virtualFile += pool.toCsv().encode() + b'\n'
virtualFile += pool.as_cvs().encode() + b'\n'
return virtualFile
def _read_auths(self, interval: StatInterval, extension: str, size: int, offset: int) -> bytes:

View File

@ -197,7 +197,7 @@ class Authenticator(ManagedObjectModel, TaggingMixin):
"""
try:
usr: 'User' = self.users.get(name=username)
return State.isActive(usr.state)
return State.is_active(usr.state)
except Exception:
return returnValueIfNot
@ -223,7 +223,7 @@ class Authenticator(ManagedObjectModel, TaggingMixin):
"""
if self.net_filtering == consts.auth.NO_FILTERING:
return True
ip, version = net.ipToLong(ipStr)
ip, version = net.ip_to_long(ipStr)
# Allow
exists = self.networks.filter(
start__lte=Network.hexlify(ip), end__gte=Network.hexlify(ip), version=version

View File

@ -295,7 +295,7 @@ class CalendarAction(UUIDModel):
"""
logger.debug('Executing action')
# If restrained pool, skip this execution (will rery later, not updated)
if not self.service_pool.isUsable():
if not self.service_pool.is_usable():
logger.info(
'Execution of task for %s due to contained state (restrained, in maintenance or removing)',
self.service_pool.name,
@ -334,13 +334,13 @@ class CalendarAction(UUIDModel):
def remove_userservices() -> None:
# 1.- Remove usable assigned services (Ignore "creating ones", just for created)
for userService in self.service_pool.assignedUserServices().filter(state=state.State.USABLE):
for userService in self.service_pool.assigned_user_services().filter(state=state.State.USABLE):
userService.remove()
def remove_stuck_userservice() -> None:
# 1.- Remove stuck assigned services (Ignore "creating ones", just for created)
since = sql_datetime() - datetime.timedelta(hours=numVal('hours'))
for userService in self.service_pool.assignedUserServices().filter(
for userService in self.service_pool.assigned_user_services().filter(
state_date__lt=since, state=state.State.USABLE
):
userService.remove()
@ -355,7 +355,7 @@ class CalendarAction(UUIDModel):
def clear_cache() -> None:
# 4.- Remove all cache_l1_srvs
for i in self.service_pool.cachedUserServices().filter(
for i in self.service_pool.cached_users_services().filter(
UserServiceManager().getCacheStateFilter(
self.service_pool,
services.UserService.L1_CACHE

View File

@ -136,7 +136,7 @@ class MetaPool(UUIDModel, TaggingMixin): # type: ignore
p: 'MetaPoolMember'
for p in self.members.all():
total += 1
if p.pool.isInMaintenance():
if p.pool.is_in_maintenance():
maintenance += 1
return total == maintenance

View File

@ -103,7 +103,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
"""
Returns the networks that are valid for specified ip in dotted quad (xxx.xxx.xxx.xxx)
"""
ipInt, version = net.ipToLong(ip)
ipInt, version = net.ip_to_long(ip)
hex_value = Network.hexlify(ipInt)
# hexlify is used to convert to hex, and then decode to convert to string
return Network.objects.filter(
@ -128,7 +128,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
netRange: Network range in any supported format
"""
nr = net.networkFromString(netRange)
nr = net.network_from_str(netRange)
return Network.objects.create(
name=name,
start=Network.hexlify(nr.start),
@ -173,7 +173,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
Returns:
string representing the dotted quad of this network start
"""
return net.longToIp(self.net_start)
return net.long_to_ip(self.net_start)
@property
def str_net_end(self) -> str:
@ -183,7 +183,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
Returns:
string representing the dotted quad of this network end
"""
return net.longToIp(self.net_end)
return net.long_to_ip(self.net_end)
def contains(self, ip: str) -> bool:
"""
@ -192,7 +192,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
# if net_string is '*', then we are in all networks, return true
if self.net_string == '*':
return True
ipInt, version = net.ipToLong(ip)
ipInt, version = net.ip_to_long(ip)
return self.net_start <= ipInt <= self.net_end and self.version == version
__contains__ = contains
@ -201,7 +201,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
"""
Overrides save to update the start, end and version fields
"""
rng = net.networkFromString(self.net_string)
rng = net.network_from_str(self.net_string)
self.start = Network.hexlify(rng.start)
self.end = Network.hexlify(rng.end)
self.version = rng.version

View File

@ -83,7 +83,7 @@ class ServerGroup(UUIDModel, TaggingMixin, properties.PropertiesMixin):
servers: 'models.manager.RelatedManager[Server]'
# For properties
def ownerIdAndType(self) -> tuple[str, str]:
def get_owner_id_and_type(self) -> tuple[str, str]:
return self.uuid, 'servergroup'
class Meta:
@ -203,7 +203,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
app_label = 'uds'
# For properties
def ownerIdAndType(self) -> tuple[str, str]:
def get_owner_id_and_type(self) -> tuple[str, str]:
return self.uuid, 'server'
@property
@ -225,7 +225,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
Host returns first the IP if it exists, and if not, the hostname (resolved)
"""
if net.isValidIp(self.ip):
if net.is_valid_ip(self.ip):
return self.ip
# If hostname exists, try to resolve it
if self.hostname:
@ -258,21 +258,21 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
statsDict['stamp'] = sql_stamp()
self.properties['stats'] = statsDict
def newAssignation(self) -> None:
"""Simulates, with current stats, the addition of a new user"""
def interpolate_new_assignation(self) -> None:
"""Interpolates, with current stats, the addition of a new user"""
stats = self.stats
if stats and stats.is_valid: # If rae invalid, do not waste time recalculating
# Avoid replacing current "stamp" value, this is just a "simulation"
self.properties['stats'] = stats.adjust(users_increment=1).as_dict()
def newRelease(self) -> None:
"""Simulates, with current stats, the release of a user"""
def interpolate_new_release(self) -> None:
"""Interpolates, with current stats, the release of a user"""
stats = self.stats
if stats and stats.is_valid:
# Avoid replacing current "stamp" value, this is just a "simulation"
self.properties['stats'] = stats.adjust(users_increment=-1).as_dict()
def isRestrained(self) -> bool:
def is_restrained(self) -> bool:
"""Returns if this server is restrained or not
For this, we get the property (if available) "available" (datetime) and compare it with current time
@ -281,7 +281,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
restrainedUntil = datetime.datetime.fromtimestamp(self.properties.get('available', consts.NEVER_UNIX))
return restrainedUntil > sql_datetime()
def setRestrainedUntil(self, value: typing.Optional[datetime.datetime] = None) -> None:
def set_restrained_until(self, value: typing.Optional[datetime.datetime] = None) -> None:
"""Sets the availability of this server
If value is None, it will be available right now
"""
@ -306,7 +306,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
return create_token() # Return global function
@staticmethod
def validateToken(
def validate_token(
token: str,
serverType: typing.Union[collections.abc.Iterable[types.servers.ServerType], types.servers.ServerType],
request: typing.Optional[ExtendedHttpRequest] = None,
@ -353,7 +353,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
Returns:
The server found, or None if not found
"""
if net.isValidIp(ip_or_host):
if net.is_valid_ip(ip_or_host):
found = Server.objects.filter(ip=ip_or_host).first()
if not found: # Try reverse dns lookup
found = Server.objects.filter(hostname__in=resolver.reverse(ip_or_host)).first()
@ -361,7 +361,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin):
found = Server.objects.filter(hostname=ip_or_host).first()
return found
def setActorVersion(self, userService: 'UserService') -> None:
def set_actor_version(self, userService: 'UserService') -> None:
"""Sets the actor version of this server to the userService"""
userService.actor_version = f'Server {self.version or "unknown"}'

View File

@ -185,7 +185,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return self.osmanager.get_type().transformsUserOrPasswordForService()
return False
def processUserPassword(self, username: str, password: str) -> tuple[str, str]:
def process_user_password(self, username: str, password: str) -> tuple[str, str]:
"""
This method is provided for consistency between UserService and ServicePool
There is no posibility to check the username and password that a user will use to
@ -197,7 +197,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return username, password
@staticmethod
def getRestrainedsQuerySet() -> 'models.QuerySet[ServicePool]':
def restraineds_queryset() -> 'models.QuerySet[ServicePool]':
from uds.models.user_service import ( # pylint: disable=import-outside-toplevel
UserService,
)
@ -226,8 +226,8 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return ServicePool.objects.filter(pk__in=res)
@staticmethod
def getRestraineds() -> typing.Iterator['ServicePool']:
return ServicePool.getRestrainedsQuerySet().iterator()
def restrained_pools() -> typing.Iterator['ServicePool']:
return ServicePool.restraineds_queryset().iterator()
@property
def owned_by_meta(self) -> bool:
@ -240,7 +240,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return str(self.short_name.strip())
return str(self.name)
def isRestrained(self) -> bool:
def is_restrained(self) -> bool:
"""
Maybe this deployed service is having problems, and that may block some task in some
situations.
@ -270,18 +270,18 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return False
def isInMaintenance(self) -> bool:
def is_in_maintenance(self) -> bool:
return self.service.isInMaintenance() if self.service else True
def isVisible(self) -> bool:
def is_visible(self) -> bool:
return self.visible # type: ignore
def isUsable(self) -> bool:
def is_usable(self) -> bool:
return (
self.state == states.servicePool.ACTIVE and not self.isInMaintenance() and not self.isRestrained()
self.state == states.servicePool.ACTIVE and not self.is_in_maintenance() and not self.is_restrained()
)
def toBeReplaced(self, forUser: 'User') -> typing.Optional[datetime]:
def when_will_be_replaced(self, forUser: 'User') -> typing.Optional[datetime]:
activePub: typing.Optional['ServicePoolPublication'] = self.activePublication()
# If no publication or current revision, it's not going to be replaced
if activePub is None:
@ -295,12 +295,12 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
try:
found = typing.cast(
'UserService',
self.assignedUserServices().filter(user=forUser, state__in=states.userService.VALID_STATES)[
self.assigned_user_services().filter(user=forUser, state__in=states.userService.VALID_STATES)[
0
], # type: ignore # Slicing is not supported by pylance right now
)
if activePub and found.publication and activePub.id != found.publication.id:
ret = self.recoverValue('toBeReplacedIn')
ret = self.get_value('toBeReplacedIn')
if ret:
return serializer.deserialize(ret)
@ -310,7 +310,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return None
def isAccessAllowed(self, chkDateTime=None) -> bool:
def is_access_allowed(self, chkDateTime=None) -> bool:
"""
Checks if the access for a service pool is allowed or not (based esclusively on associated calendars)
"""
@ -326,7 +326,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return access == states.action.ALLOW
def getDeadline(self, chkDateTime: typing.Optional[datetime] = None) -> typing.Optional[int]:
def get_deadline(self, chkDateTime: typing.Optional[datetime] = None) -> typing.Optional[int]:
"""Gets the deadline for an access on chkDateTime in seconds
Keyword Arguments:
@ -338,7 +338,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
if chkDateTime is None:
chkDateTime = typing.cast(datetime, sql_datetime())
if self.isAccessAllowed(chkDateTime) is False:
if self.is_access_allowed(chkDateTime) is False:
return -1
deadLine = None
@ -360,7 +360,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
return int((deadLine - chkDateTime).total_seconds())
def storeValue(self, name: str, value: typing.Any):
def set_value(self, name: str, value: typing.Any):
"""
Stores a value inside custom storage
@ -370,7 +370,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
"""
self.get_environment().storage.put(name, value)
def recoverValue(self, name: str) -> typing.Any:
def get_value(self, name: str) -> typing.Any:
"""
Recovers a value from custom storage
@ -382,7 +382,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
"""
return typing.cast(str, self.get_environment().storage.get(name))
def setState(self, state: str, save: bool = True) -> None:
def set_state(self, state: str, save: bool = True) -> None:
"""
Updates the state of this object and, optionally, saves it
@ -403,7 +403,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
The background worker will be the responsible for removing the deployed service
"""
self.setState(states.servicePool.REMOVABLE)
self.set_state(states.servicePool.REMOVABLE)
def removed(self) -> None:
"""
@ -414,10 +414,10 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
# self.assignedGroups.clear()
# self.osmanager = None
# self.service = None
# self.setState(State.REMOVED)
# self.set_state(State.REMOVED)
self.delete()
def markOldUserServicesAsRemovables(
def mark_old_userservices_as_removable(
self,
activePub: typing.Optional['ServicePoolPublication'],
skipAssigned: bool = False,
@ -454,7 +454,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
cache_level=0, state=states.userService.USABLE, in_use=False
).update(state=states.userService.REMOVABLE, state_date=now)
def validateGroups(self, groups: collections.abc.Iterable['Group']) -> None:
def validate_groups(self, groups: collections.abc.Iterable['Group']) -> None:
"""
Ensures that at least a group of groups (database groups) has access to this Service Pool
raise an InvalidUserException if fails check
@ -466,7 +466,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
): # pylint: disable=no-member
raise exceptions.auth.InvalidUserException()
def validatePublication(self) -> None:
def validate_publication(self) -> None:
"""
Ensures that, if this service has publications, that a publication is active
raises an IvalidServiceException if check fails
@ -478,14 +478,14 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
):
raise InvalidServiceException()
def validateTransport(self, transport) -> None:
def validate_transport(self, transport) -> None:
if (
self.transports.filter(id=transport.id).count() # pylint: disable=no-member
== 0 # pylint: disable=no-member
): # pylint: disable=no-member
raise InvalidServiceException()
def validateUser(self, user: 'User') -> None:
def validate_user(self, user: 'User') -> None:
"""
Validates that the user has access to this deployed service
@ -502,11 +502,11 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
logger.debug('User: %s', user.id)
logger.debug('ServicePool: %s', self.id)
self.validateGroups(user.getGroups())
self.validatePublication()
self.validate_groups(user.getGroups())
self.validate_publication()
@staticmethod
def getDeployedServicesForGroups(
def get_pools_for_groups(
groups: collections.abc.Iterable['Group'], user: typing.Optional['User'] = None
) -> collections.abc.Iterable['ServicePool']:
"""
@ -603,7 +603,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
if pub:
pub.unpublish()
def cachedUserServices(self) -> 'models.QuerySet[UserService]':
def cached_users_services(self) -> 'models.QuerySet[UserService]':
"""
':rtype uds.models.user_service.UserService'
Utility method to access the cached user services (level 1 and 2)
@ -613,7 +613,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
"""
return self.userServices.exclude(cache_level=0)
def assignedUserServices(self) -> 'models.QuerySet[UserService]':
def assigned_user_services(self) -> 'models.QuerySet[UserService]':
"""
Utility method to access the assigned user services
@ -622,7 +622,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
"""
return self.userServices.filter(cache_level=0)
def erroneousUserServices(self) -> 'models.QuerySet[UserService]':
def erroneous_user_services(self) -> 'models.QuerySet[UserService]':
"""
Utility method to locate invalid assigned user services.
@ -643,7 +643,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
maxs = self.service.get_instance().maxUserServices
if cachedValue == -1:
cachedValue = self.assignedUserServices().filter(state__in=states.userService.VALID_STATES).count()
cachedValue = self.assigned_user_services().filter(state__in=states.userService.VALID_STATES).count()
if maxs == 0 or max == consts.UNLIMITED:
return types.pools.UsageInfo(cachedValue, consts.UNLIMITED)
@ -684,7 +684,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
# returns CSV header
@staticmethod
def getCSVHeader(sep: str = ',') -> str:
def get_cvs_header(sep: str = ',') -> str:
return sep.join(
[
'name',
@ -698,7 +698,7 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
)
# Return record as csv line using separator (default: ',')
def toCsv(self, sep: str = ',') -> str:
def as_cvs(self, sep: str = ',') -> str:
return sep.join(
[
self.name,
@ -706,8 +706,8 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
str(self.cache_l1_srvs),
str(self.cache_l2_srvs),
str(self.max_srvs),
str(self.assignedUserServices().count()),
str(self.cachedUserServices().count()),
str(self.assigned_user_services().count()),
str(self.cached_users_services().count()),
]
)

View File

@ -169,7 +169,7 @@ class ServicePoolPublication(UUIDModel):
self.data = publication.serialize()
self.save(update_fields=['data'])
def setState(self, state: str) -> None:
def set_state(self, state: str) -> None:
"""
Updates the state of this object and, optionally, saves it

View File

@ -132,7 +132,7 @@ class Transport(ManagedObjectModel, TaggingMixin):
if self.net_filtering == Transport.NO_FILTERING:
return True
ip, version = net.ipToLong(ipStr)
ip, version = net.ip_to_long(ipStr)
# Allow
exists = self.networks.filter(
start__lte=Network.hexlify(ip), end__gte=Network.hexlify(ip), version=version

View File

@ -101,7 +101,7 @@ class User(UUIDModel, properties.PropertiesMixin):
]
# For properties
def ownerIdAndType(self) -> tuple[str, str]:
def get_owner_id_and_type(self) -> tuple[str, str]:
return self.uuid, 'user'
def getUsernameForAuth(self) -> str:

View File

@ -125,7 +125,7 @@ class UserService(UUIDModel, properties.PropertiesMixin):
]
# For properties
def ownerIdAndType(self) -> tuple[str, str]:
def get_owner_id_and_type(self) -> tuple[str, str]:
return self.uuid, 'userservice'
@property
@ -358,7 +358,7 @@ class UserService(UUIDModel, properties.PropertiesMixin):
"""
return self.deployed_service.transformsUserOrPasswordForService()
def processUserPassword(self, username: str, password: str) -> tuple[str, str]:
def process_user_password(self, username: str, password: str) -> tuple[str, str]:
"""
Before accessing a service by a transport, we can request
the service to "transform" the username & password that the transport
@ -384,9 +384,9 @@ class UserService(UUIDModel, properties.PropertiesMixin):
if serviceInstance.needsManager is False or not servicePool.osmanager:
return (username, password)
return servicePool.osmanager.get_instance().processUserPassword(self, username, password)
return servicePool.osmanager.get_instance().process_user_password(self, username, password)
def setState(self, state: str) -> None:
def set_state(self, state: str) -> None:
"""
Updates the state of this object and, optionally, saves it
@ -498,13 +498,13 @@ class UserService(UUIDModel, properties.PropertiesMixin):
"""
Returns if this service is usable
"""
return State.isUsable(self.state)
return State.is_usable(self.state)
def isPreparing(self) -> bool:
"""
Returns if this service is in preparation (not ready to use, but in its way to be so...)
"""
return State.isPreparing(self.state)
return State.is_preparing(self.state)
def isReady(self) -> bool:
"""
@ -517,13 +517,13 @@ class UserService(UUIDModel, properties.PropertiesMixin):
return UserServiceManager().isReady(self)
def isInMaintenance(self) -> bool:
return self.deployed_service.isInMaintenance()
return self.deployed_service.is_in_maintenance()
def remove(self) -> None:
"""
Mark this user deployed service for removal
"""
self.setState(State.REMOVABLE)
self.set_state(State.REMOVABLE)
def release(self) -> None:
"""

View File

@ -85,7 +85,7 @@ class LinuxRandomPassManager(LinuxOsManager):
else:
self._userAccount = ''
def processUserPassword(
def process_user_password(
self, userService: 'UserService', username: str, password: str
) -> tuple[str, str]:
if username == self._userAccount:

View File

@ -165,7 +165,7 @@ class WindowsOsManager(osmanagers.OSManager):
) -> collections.abc.MutableMapping[str, typing.Any]:
return {'action': 'rename', 'name': userService.getName()} # No custom data
def processUserPassword(
def process_user_password(
self, userService: 'UserService', username: str, password: str
) -> tuple[str, str]:
if userService.properties.get('sso_available') == '1':
@ -182,7 +182,7 @@ class WindowsOsManager(osmanagers.OSManager):
) # , owner=SECURE_OWNER, secure=True)
return ticket, ''
return osmanagers.OSManager.processUserPassword(
return osmanagers.OSManager.process_user_password(
self, userService, username, password
)

View File

@ -96,13 +96,13 @@ class WinRandomPassManager(WindowsOsManager):
self._userAccount = ''
self._password = '' # nosec: not a password (empty)
def processUserPassword(
def process_user_password(
self, userService: 'UserService', username: str, password: str
) -> tuple[str, str]:
if username == self._userAccount:
password = userService.recoverValue('winOsRandomPass')
return WindowsOsManager.processUserPassword(self, userService, username, password)
return WindowsOsManager.process_user_password(self, userService, username, password)
def genPassword(self, userService: 'UserService'):
randomPass = userService.recoverValue('winOsRandomPass')

View File

@ -149,7 +149,7 @@ class OGDeployment(services.UserService):
status = self.__checkMachineReady()
if status == State.FINISHED:
self.service().notifyDeadline(
self._machineId, dbs.deployed_service.getDeadline()
self._machineId, dbs.deployed_service.get_deadline()
)
return State.FINISHED

View File

@ -74,7 +74,7 @@ class IPMachineDeployed(services.UserService, AutoAttributes):
# If multiple and has a ';' on IP, the values is IP;MAC
ip = self._ip.split('~')[0].split(';')[0]
# If ip is in fact a hostname...
if not net.ipToLong(ip).version:
if not net.ip_to_long(ip).version:
# Try to resolve name...
try:
# Prefer ipv4 first

View File

@ -102,7 +102,7 @@ class PhysicalMachinesProvider(services.ServiceProvider):
# wol section
for key in config['wol']:
try:
net.networksFromString(key) # Raises exception if net is invalid
net.networks_from_str(key) # Raises exception if net is invalid
except Exception:
raise exceptions.validation.ValidationError(
_('Invalid network in advanced configuration: ') + key
@ -133,7 +133,7 @@ class PhysicalMachinesProvider(services.ServiceProvider):
return ''
# If ip is in fact a hostname...
if not net.ipToLong(ip).version:
if not net.ip_to_long(ip).version:
# Try to resolve name...
try:
# Prefer ipv4

View File

@ -161,7 +161,7 @@ class IPMachinesService(IPServiceBase):
else:
# Check that ips are valid
for v in values['ipList']:
if not net.isValidHost(v.split(';')[0]): # Get only IP/hostname
if not net.is_valid_host(v.split(';')[0]): # Get only IP/hostname
raise exceptions.validation.ValidationError(
gettext('Invalid value detected on servers list: "{}"').format(v)
)

View File

@ -80,7 +80,7 @@ class IPSingleMachineService(IPServiceBase):
if values is None:
return
if not net.isValidHost(self.ip.value):
if not net.is_valid_host(self.ip.value):
raise exceptions.validation.ValidationError(
gettext('Invalid server used: "{}"'.format(self.ip.value))
)

View File

@ -309,7 +309,7 @@ class HTML5RDPTransport(transports.Transport):
self.cache.put(ip, 'N', READY_CACHE_TIMEOUT)
return ready == 'Y'
def processedUser(self, userService: 'models.UserService', user: 'models.User') -> str:
def processed_username(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.getConnectionInfo(userService, user, '')
return v.username
@ -363,7 +363,7 @@ class HTML5RDPTransport(transports.Transport):
username = 'AzureAD\\' + username
# Fix username/password acording to os manager
username, password = userService.processUserPassword(username, password)
username, password = userService.process_user_password(username, password)
return types.connections.ConnectionData(
protocol=self.protocol,

View File

@ -346,11 +346,11 @@ class BaseRDPTransport(transports.Transport):
self.cache.put(ip, 'N', READY_CACHE_TIMEOUT)
return ready == 'Y'
def processedUser(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.processUserPassword(userService, user, '', altUsername=None)
def processed_username(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.process_user_password(userService, user, '', altUsername=None)
return v.username
def processUserPassword(
def process_user_password(
self,
userService: 'models.UserService',
user: 'models.User',
@ -388,12 +388,12 @@ class BaseRDPTransport(transports.Transport):
if '.' in domain: # Dotter domain form
username = username + '@' + domain
domain = ''
else: # In case of a NETBIOS domain (not recomended), join it so processUserPassword can deal with it
else: # In case of a NETBIOS domain (not recomended), join it so process_user_password can deal with it
username = domain + '\\' + username
domain = ''
# Fix username/password acording to os manager
username, password = userService.processUserPassword(username, password)
username, password = userService.process_user_password(username, password)
# Recover domain name if needed
if '\\' in username:
@ -427,7 +427,7 @@ class BaseRDPTransport(transports.Transport):
username = cdata[1] or username
password = cdata[2] or password
return self.processUserPassword(
return self.process_user_password(
typing.cast('models.UserService', userService),
user,
password,

View File

@ -191,11 +191,11 @@ class BaseSpiceTransport(transports.Transport):
return transports.Transport.getCustomAvailableErrorMsg(self, userService, ip)
return msg
def processedUser(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.processUserPassword(userService, user, '')
def processed_username(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.process_user_password(userService, user, '')
return v.username
def processUserPassword(
def process_user_password(
self,
userService: typing.Union['models.UserService', 'models.ServicePool'],
user: 'models.User',
@ -213,7 +213,7 @@ class BaseSpiceTransport(transports.Transport):
username, password = '', ''
# Fix username/password acording to os manager
username, password = userService.processUserPassword(username, password)
username, password = userService.process_user_password(username, password)
return types.connections.ConnectionData(
protocol=self.protocol,
@ -228,4 +228,4 @@ class BaseSpiceTransport(transports.Transport):
user: 'models.User',
password: str,
) -> types.connections.ConnectionData:
return self.processUserPassword(userService, user, password)
return self.process_user_password(userService, user, password)

View File

@ -112,7 +112,7 @@ class TestTransport(transports.Transport):
# Fix username/password acording to os manager
username: str = user.getUsernameForAuth()
username, password = userService.processUserPassword(username, password)
username, password = userService.process_user_password(username, password)
url = self.testURL.value.replace('_IP_', ip).replace('_USER_', username)

View File

@ -113,7 +113,7 @@ class URLCustomTransport(transports.Transport):
# Fix username/password acording to os manager
username: str = user.getUsernameForAuth()
username, password = userService.processUserPassword(username, password)
username, password = userService.process_user_password(username, password)
url = self.urlPattern.value.replace('_IP_', ip).replace('_USER_', username)

View File

@ -219,11 +219,11 @@ class BaseX2GOTransport(transports.Transport):
def getScreenSize(self) -> tuple[int, int]:
return CommonPrefs.get_wh(self.screenSize.value)
def processedUser(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.processUserPassword(userService, user, '')
def processed_username(self, userService: 'models.UserService', user: 'models.User') -> str:
v = self.process_user_password(userService, user, '')
return v.username
def processUserPassword(
def process_user_password(
self,
userService: typing.Union['models.UserService', 'models.ServicePool'],
user: 'models.User',
@ -243,7 +243,7 @@ class BaseX2GOTransport(transports.Transport):
username = self.fixedName.value
# Fix username/password acording to os manager
username, password = userService.processUserPassword(username, password)
username, password = userService.process_user_password(username, password)
return types.connections.ConnectionData(
protocol=self.protocol,
@ -258,7 +258,7 @@ class BaseX2GOTransport(transports.Transport):
user: 'models.User',
password: str,
) -> types.connections.ConnectionData:
return self.processUserPassword(userService, user, password)
return self.process_user_password(userService, user, password)
def genKeyPairForSsh(self) -> tuple[str, str]:
"""

View File

@ -122,7 +122,7 @@ def getServicesData(
# We look for services for this authenticator groups. User is logged in in just 1 authenticator, so his groups must coincide with those assigned to ds
groups = list(request.user.getGroups())
availServicePools = list(
ServicePool.getDeployedServicesForGroups(groups, request.user)
ServicePool.get_pools_for_groups(groups, request.user)
) # Pass in user to get "number_assigned" to optimize
availMetaPools = list(
MetaPool.getForGroups(groups, request.user)
@ -371,7 +371,7 @@ def getServicesData(
# Only add toBeReplaced info in case we allow it. This will generate some "overload" on the services
toBeReplacedDate = (
sPool.toBeReplaced(request.user)
sPool.when_will_be_replaced(request.user)
if typing.cast(typing.Any, sPool).pubs_active > 0
and GlobalConfig.NOTIFY_REMOVAL_BY_PUB.getBool(False)
else None
@ -399,8 +399,8 @@ def getServicesData(
show_transports=sPool.show_transports,
allow_users_remove=sPool.allow_users_remove,
allow_users_reset=sPool.allow_users_reset,
maintenance=sPool.isInMaintenance(),
not_accesible=not sPool.isAccessAllowed(now),
maintenance=sPool.is_in_maintenance(),
not_accesible=not sPool.is_access_allowed(now),
in_use=in_use,
to_be_replaced=toBeReplaced,
to_be_replaced_text=toBeReplacedTxt,

View File

@ -229,7 +229,7 @@ def ticket_auth(
raise Exception('Invalid ticket authentication')
usr = auth.get_or_create_user(username, realname)
if usr is None or State.isActive(usr.state) is False: # If user is inactive, raise an exception
if usr is None or State.is_active(usr.state) is False: # If user is inactive, raise an exception
raise exceptions.auth.InvalidUserException()
# Add groups to user (replace existing groups)

View File

@ -197,6 +197,6 @@ class UsersTest(rest.test.RESTActorTestCase):
response = self.client.rest_get(url)
self.assertEqual(response.status_code, 200)
groups = self.plain_users[0].groups.all()
count = len(list(models.ServicePool.getDeployedServicesForGroups(groups)))
count = len(list(models.ServicePool.get_pools_for_groups(groups)))
self.assertEqual(len(response.json()), count)

View File

@ -105,27 +105,27 @@ class ServerManagerManagedServersTest(UDSTestCase):
@contextmanager
def create_mock_api_requester(
self,
getStats: typing.Optional[
get_stats: typing.Optional[
collections.abc.Callable[['models.Server'], typing.Optional['types.servers.ServerStats']]
] = None,
) -> typing.Iterator[mock.Mock]:
with mock.patch('uds.core.managers.servers_api.requester.ServerApiRequester') as mockServerApiRequester:
def _getStats() -> typing.Optional[types.servers.ServerStats]:
def _get_stats() -> typing.Optional[types.servers.ServerStats]:
# Get first argument from call to init on serverApiRequester
server = mockServerApiRequester.call_args[0][0]
logger.debug('Getting stats for %s', server.host)
return (getStats or (lambda x: self.server_stats.get(x.uuid)))(server)
return (get_stats or (lambda x: self.server_stats.get(x.uuid)))(server)
# return_value returns the instance of the mock
mockServerApiRequester.return_value.getStats.side_effect = _getStats
mockServerApiRequester.return_value.get_stats.side_effect = _get_stats
yield mockServerApiRequester
def test_auto_assign(self) -> None:
with self.create_mock_api_requester() as mockServerApiRequester:
for elementNumber, userService in enumerate(self.user_services):
expected_getStats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notifyAssign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
expected_get_stats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notify_assign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
assignation = self.assign(userService)
if assignation is None:
self.fail('Assignation returned None')
@ -136,33 +136,33 @@ class ServerManagerManagedServersTest(UDSTestCase):
self.server_stats[uuid], current_users=self.server_stats[uuid].current_users + 1
)
prop_name = self.manager.propertyName(userService.user)
prop_name = self.manager.property_name(userService.user)
# uuid shuld be one on registered servers
self.assertTrue(uuid in self.all_uuids)
# Server locked should be None
self.assertIsNone(models.Server.objects.get(uuid=uuid).locked_until)
# mockServer.getStats has been called NUM_REGISTEREDSERVERS times
# mockServer.get_stats has been called NUM_REGISTEREDSERVERS times
self.assertEqual(
mockServerApiRequester.return_value.getStats.call_count,
expected_getStats_calls,
mockServerApiRequester.return_value.get_stats.call_count,
expected_get_stats_calls,
f'Error on loop {elementNumber}',
)
# notifyAssign should has been called once for each user service
# notify_assign should has been called once for each user service
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_count, expected_notifyAssign_calls + 1
mockServerApiRequester.return_value.notify_assign.call_count, expected_notify_assign_calls + 1
)
# notifyAssign paramsh should have been
# request.ServerApiRequester(bestServer).notifyAssign(userService, serviceType, uuid_counter[1])
# notify_assign paramsh should have been
# request.ServerApiRequester(bestServer).notify_assign(userService, serviceType, uuid_counter[1])
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_args[0][0], userService
mockServerApiRequester.return_value.notify_assign.call_args[0][0], userService
) # userService
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_args[0][1],
mockServerApiRequester.return_value.notify_assign.call_args[0][1],
types.services.ServiceType.VDI,
)
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_args[0][2], counter
mockServerApiRequester.return_value.notify_assign.call_args[0][2], counter
) # counter
# Server storage should contain the assignation
@ -185,14 +185,14 @@ class ServerManagerManagedServersTest(UDSTestCase):
self.assertTrue(uuid2 in self.all_uuids)
self.assertIsNone(models.Server.objects.get(uuid=uuid).locked_until) # uuid is uuid2
# mockServer.getStats has been called NUM_REGISTEREDSERVERS times, because no new requests has been done
# mockServer.get_stats has been called NUM_REGISTEREDSERVERS times, because no new requests has been done
self.assertEqual(
mockServerApiRequester.return_value.getStats.call_count, expected_getStats_calls
mockServerApiRequester.return_value.get_stats.call_count, expected_get_stats_calls
)
# notifyAssign should has been called twice
# notify_assign should has been called twice
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_count,
expected_notifyAssign_calls + i + 2,
mockServerApiRequester.return_value.notify_assign.call_count,
expected_notify_assign_calls + i + 2,
)
# Server storage should have elementNumber + 1 elements
@ -204,8 +204,8 @@ class ServerManagerManagedServersTest(UDSTestCase):
# Now, remove all asignations..
for elementNumber, userService in enumerate(self.user_services):
expected_getStats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notifyAssign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
expected_get_stats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notify_assign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
# # Remove it, should decrement counter
for i in range(32, -1, -1): # Deletes 33 times
@ -238,8 +238,8 @@ class ServerManagerManagedServersTest(UDSTestCase):
time.sleep(1)
self.assign(self.user_services[NUM_REGISTEREDSERVERS], lockTime=datetime.timedelta(seconds=1))
# notifyRelease should has been called once
self.assertEqual(mockServerApiRequester.return_value.notifyRelease.call_count, 1)
# notify_release should has been called once
self.assertEqual(mockServerApiRequester.return_value.notify_release.call_count, 1)
def test_assign_release_max(self) -> None:
with self.create_mock_api_requester() as mockServerApiRequester:
@ -249,7 +249,7 @@ class ServerManagerManagedServersTest(UDSTestCase):
# Ensure locking server, so we have to use every server only once
assignation = self.assign(userService, lockTime=datetime.timedelta(seconds=32))
self.assertEqual(
serverApiRequester.notifyAssign.call_count,
serverApiRequester.notify_assign.call_count,
assignations * NUM_REGISTEREDSERVERS + elementNumber + 1,
)
if assignation is None:
@ -275,7 +275,7 @@ class ServerManagerManagedServersTest(UDSTestCase):
# Storage should have NUM_REGISTEREDSERVERS elements
self.assertEqual(len(self.registered_servers_group.properties), NUM_REGISTEREDSERVERS)
with self.manager.cntStorage() as stor:
with self.manager.counter_storage() as stor:
self.assertEqual(len(stor), 0) # No counter storage for managed servers
# Now release all, twice
@ -291,7 +291,7 @@ class ServerManagerManagedServersTest(UDSTestCase):
else:
self.fail('Release returned None')
self.assertEqual(
serverApiRequester.notifyRelease.call_count,
serverApiRequester.notify_release.call_count,
release * NUM_REGISTEREDSERVERS + elementNumber + 1,
f'Error on loop {release} - {elementNumber}',
)
@ -302,7 +302,7 @@ class ServerManagerManagedServersTest(UDSTestCase):
self.assertEqual(len(self.registered_servers_group.properties), 0)
with self.manager.cntStorage() as stor:
with self.manager.counter_storage() as stor:
self.assertEqual(len(stor), 0)
# Trying to release again should return '', 0
@ -314,6 +314,6 @@ class ServerManagerManagedServersTest(UDSTestCase):
# Number of lasting assignations should be one less than before
self.assertEqual(counter, 0)
self.assertEqual(
serverApiRequester.notifyRelease.call_count,
2 * NUM_REGISTEREDSERVERS, # No release if all are released already, so no notifyRelease
serverApiRequester.notify_release.call_count,
2 * NUM_REGISTEREDSERVERS, # No release if all are released already, so no notify_release
)

View File

@ -88,46 +88,46 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
@contextmanager
def createMockApiRequester(self) -> typing.Iterator[mock.Mock]:
with mock.patch('uds.core.managers.servers_api.requester.ServerApiRequester') as mockServerApiRequester:
mockServerApiRequester.return_value.getStats.return_value = None
mockServerApiRequester.return_value.get_stats.return_value = None
yield mockServerApiRequester
def testAssignAuto(self) -> None:
with self.createMockApiRequester() as mockServerApiRequester:
for elementNumber, userService in enumerate(self.user_services[:NUM_REGISTEREDSERVERS]):
expected_getStats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notifyAssign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
expected_get_stats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notify_assign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
assignation = self.assign(userService)
if assignation is None:
self.fail('Assignation returned None')
return # For mypy
uuid, counter = assignation
prop_name = self.manager.propertyName(userService.user)
prop_name = self.manager.property_name(userService.user)
# uuid shuld be one on registered servers
self.assertTrue(uuid in self.all_uuids)
# Server locked should be None
self.assertIsNone(models.Server.objects.get(uuid=uuid).locked_until)
# mockServer.getStats has been called NUM_REGISTEREDSERVERS times
# mockServer.get_stats has been called NUM_REGISTEREDSERVERS times
self.assertEqual(
mockServerApiRequester.return_value.getStats.call_count,
expected_getStats_calls,
mockServerApiRequester.return_value.get_stats.call_count,
expected_get_stats_calls,
f'Error on loop {elementNumber}',
)
# notifyAssign should has been called once for each user service
# notify_assign should has been called once for each user service
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_count, expected_notifyAssign_calls + 1
mockServerApiRequester.return_value.notify_assign.call_count, expected_notify_assign_calls + 1
)
# notifyAssign paramsh should have been
# request.ServerApiRequester(bestServer).notifyAssign(userService, serviceType, uuid_counter[1])
# notify_assign paramsh should have been
# request.ServerApiRequester(bestServer).notify_assign(userService, serviceType, uuid_counter[1])
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_args[0][0], userService
mockServerApiRequester.return_value.notify_assign.call_args[0][0], userService
) # userService
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_args[0][1],
mockServerApiRequester.return_value.notify_assign.call_args[0][1],
types.services.ServiceType.VDI,
)
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_args[0][2], counter
mockServerApiRequester.return_value.notify_assign.call_args[0][2], counter
) # counter
# Server storage should contain the assignation
@ -147,14 +147,14 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
self.assertTrue(uuid2 in self.all_uuids)
self.assertIsNone(models.Server.objects.get(uuid=uuid).locked_until) # uuid is uuid2
# mockServer.getStats has been called NUM_REGISTEREDSERVERS times, because no new requests has been done
# mockServer.get_stats has been called NUM_REGISTEREDSERVERS times, because no new requests has been done
self.assertEqual(
mockServerApiRequester.return_value.getStats.call_count, expected_getStats_calls
mockServerApiRequester.return_value.get_stats.call_count, expected_get_stats_calls
)
# notifyAssign should has been called twice
# notify_assign should has been called twice
self.assertEqual(
mockServerApiRequester.return_value.notifyAssign.call_count,
expected_notifyAssign_calls + i + 2,
mockServerApiRequester.return_value.notify_assign.call_count,
expected_notify_assign_calls + i + 2,
)
self.assertEqual(len(self.registered_servers_group.properties), elementNumber + 1)
@ -162,9 +162,9 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
# Now, remove all asignations..
for elementNumber, userService in enumerate(self.user_services[:NUM_REGISTEREDSERVERS]):
expected_getStats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notifyAssign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
prop_name = self.manager.propertyName(userService.user)
expected_get_stats_calls = NUM_REGISTEREDSERVERS * (elementNumber + 1)
expected_notify_assign_calls = elementNumber * 33 # 32 in loop + 1 in first assign
prop_name = self.manager.property_name(userService.user)
# # Remove it, should decrement counter
for i in range(32, -1, -1): # Deletes 33 times
@ -198,8 +198,8 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
time.sleep(1)
self.assign(self.user_services[NUM_REGISTEREDSERVERS], lockTime=datetime.timedelta(seconds=1))
# notifyRelease should has been called once
self.assertEqual(mockServerApiRequester.return_value.notifyRelease.call_count, 1)
# notify_release should has been called once
self.assertEqual(mockServerApiRequester.return_value.notify_release.call_count, 1)
def testAssignReleaseMax(self) -> None:
with self.createMockApiRequester() as mockServerApiRequester:
@ -208,7 +208,7 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
for elementNumber, userService in enumerate(self.user_services[:NUM_REGISTEREDSERVERS]):
assign = self.assign(userService)
self.assertEqual(
serverApiRequester.notifyAssign.call_count,
serverApiRequester.notify_assign.call_count,
assignation * NUM_REGISTEREDSERVERS + elementNumber + 1,
)
if assign is None:
@ -221,7 +221,7 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
self.assertTrue(counter, 1)
# Server locked should be None
self.assertIsNone(models.Server.objects.get(uuid=uuid).locked_until)
self.assertEqual(self.manager.getUnmanagedUsage(uuid), assignation + 1)
self.assertEqual(self.manager.get_unmanaged_usage(uuid), assignation + 1)
self.assertEqual(len(self.registered_servers_group.properties), NUM_REGISTEREDSERVERS)
@ -239,12 +239,12 @@ class ServerManagerUnmanagedServersTest(UDSTestCase):
self.assertIsNone(models.Server.objects.get(uuid=uuid).locked_until)
# 3 - release -1 because we have released it already
self.assertEqual(
self.manager.getUnmanagedUsage(uuid),
self.manager.get_unmanaged_usage(uuid),
3 - release - 1,
f'Error on {elementNumber}/{release}',
)
self.assertEqual(
serverApiRequester.notifyRelease.call_count,
serverApiRequester.notify_release.call_count,
release * NUM_REGISTEREDSERVERS + elementNumber + 1,
f'Error on loop {release} - {elementNumber}',
)

View File

@ -58,7 +58,7 @@ class NetTest(UDSTestCase):
('192.168.0.1-192.168.0.87', 3232235521, 3232235607),
('192.168.0.1 netmask 255.255.255.0', 3232235520, 3232235775),
):
multiple_net: list[net.NetworkType] = net.networksFromString(n[0])
multiple_net: list[net.NetworkType] = net.networks_from_str(n[0])
self.assertEqual(
len(multiple_net),
1,
@ -75,7 +75,7 @@ class NetTest(UDSTestCase):
'Incorrect network end value for {0}'.format(n[0]),
)
single_net: net.NetworkType = net.networkFromString(n[0])
single_net: net.NetworkType = net.network_from_str(n[0])
self.assertEqual(
len(single_net),
3,
@ -94,10 +94,10 @@ class NetTest(UDSTestCase):
for n1 in ('192.168.0', '192.168.0.5-192.168.0.3', 'no net'):
with self.assertRaises(ValueError):
net.networksFromString(n1)
net.networks_from_str(n1)
self.assertEqual(net.ipToLong('192.168.0.5').ip, 3232235525)
self.assertEqual(net.longToIp(3232235525, 4), '192.168.0.5')
self.assertEqual(net.ip_to_long('192.168.0.5').ip, 3232235525)
self.assertEqual(net.long_to_ip(3232235525, 4), '192.168.0.5')
for n2 in range(0, 255):
self.assertTrue(net.contains('192.168.0.0/24', '192.168.0.{}'.format(n2)))
@ -115,7 +115,7 @@ class NetTest(UDSTestCase):
for n6 in range(0, 256, 13):
for n7 in range(0, 256, 11):
ip = '{0}.{1}.{2}.{3}'.format(n4, n5, n6, n7)
self.assertEqual(net.longToIp(net.ipToLong(ip).ip, 4), ip)
self.assertEqual(net.long_to_ip(net.ip_to_long(ip).ip, 4), ip)
def testNetworkFromStringIPv6(self) -> None:
# IPv6 only support standard notation, and '*', but not "netmask" or "range"
@ -151,7 +151,7 @@ class NetTest(UDSTestCase):
338620831926207318622244848606417780735,
),
):
multiple_net: list[net.NetworkType] = net.networksFromString(
multiple_net: list[net.NetworkType] = net.networks_from_str(
n[0], version=(6 if n[0] == '*' else 0)
)
self.assertEqual(
@ -170,7 +170,7 @@ class NetTest(UDSTestCase):
'Incorrect network end value for {0}'.format(n[0]),
)
single_net: net.NetworkType = net.networkFromString(
single_net: net.NetworkType = net.network_from_str(
n[0], version=(6 if n[0] == '*' else 0)
)
self.assertEqual(
@ -199,4 +199,4 @@ class NetTest(UDSTestCase):
'2001:2:3:4:5:6:0:1',
):
# Ensure converting back to string ips works
self.assertEqual(net.longToIp(net.ipToLong(n6).ip, 6), n6)
self.assertEqual(net.long_to_ip(net.ip_to_long(n6).ip, 6), n6)

View File

@ -55,7 +55,7 @@ class AssignedAndUnusedTest(UDSTestCase):
def test_assigned_unused(self):
for us in self.userServices: # Update state date to now
us.setState(State.USABLE)
us.set_state(State.USABLE)
# Set now, should not be removed
count = models.UserService.objects.filter(state=State.REMOVABLE).count()
cleaner = AssignedAndUnused(Environment.getTempEnv())

View File

@ -155,7 +155,7 @@ class TestGetServicesData(UDSTransactionTestCase):
user_service['maintenance'], found.service.provider.maintenance_mode
)
self.assertEqual(
user_service['not_accesible'], not found.isAccessAllowed(now)
user_service['not_accesible'], not found.is_access_allowed(now)
)
self.assertEqual(
user_service['in_use'], found.userServices.filter(in_use=True).count()