1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-02-02 09:47:13 +03:00

chore: Added capacity to put_back_to_cache to all Dynamic services

This commit is contained in:
Adolfo Gómez García 2024-09-03 23:36:55 +02:00
parent a47e5b2d93
commit ba0dd1f46a
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
9 changed files with 65 additions and 23 deletions

View File

@ -85,14 +85,8 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
order=104,
tab=types.ui.Tab.ADVANCED,
)
put_back_to_cache = gui.ChoiceField(
put_back_to_cache = fields.put_back_to_cache_field(
order=105,
label=_('Put back to cache'),
tooltip=_('On machine releasy by logout, put it back to cache instead of deleting if possible.'),
choices=[
{'id': 'no', 'text': _('No. Never put it back to cache')},
{'id': 'yes', 'text': _('Yes, try to put it back to cache')},
],
tab=types.ui.Tab.ADVANCED,
)
@ -107,9 +101,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
validators.validate_basename(self.basename.value, self.lenname.value)
def allow_putting_back_to_cache(self) -> bool:
if self.has_field('put_back_to_cache') and isinstance(
getattr(self, 'put_back_to_cache', None), gui.ChoiceField
):
if self.has_field('put_back_to_cache'):
return self.put_back_to_cache.value == 'yes'
return False

View File

@ -516,7 +516,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
def error_reason(self) -> str:
return self._reason
def remove_duplicated_names(self) -> None:
def remove_duplicates(self) -> None:
name = self.get_vmname()
try:
retry = False
@ -548,9 +548,9 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
By default, tries to locate duplicated machines and remove them.
If you override this method, you should take care yourself of removing duplicated machines
(maybe only calling "remove_duplicated_name" method)
(maybe only calling "super().op_initialize()" method)
"""
self.remove_duplicated_names()
self.remove_duplicates()
@abc.abstractmethod
def op_create(self) -> None:

View File

@ -440,7 +440,10 @@ def on_logout_field(
default='keep',
)
def allow_skip_mfa_from_networks_field(order: int = 52, tab: 'types.ui.Tab|str|None' = types.ui.Tab.CONFIG) -> ui.gui.MultiChoiceField:
def allow_skip_mfa_from_networks_field(
order: int = 52, tab: 'types.ui.Tab|str|None' = types.ui.Tab.CONFIG
) -> ui.gui.MultiChoiceField:
return ui.gui.MultiChoiceField(
label=_('Allow skip MFA from networks'),
readonly=False,
@ -453,7 +456,10 @@ def allow_skip_mfa_from_networks_field(order: int = 52, tab: 'types.ui.Tab|str|N
old_field_name='allow_networks_without_mfa',
)
def login_without_mfa_policy_networks_field(order: int = 51, tab: 'types.ui.Tab|str|None' = types.ui.Tab.CONFIG) -> ui.gui.MultiChoiceField:
def login_without_mfa_policy_networks_field(
order: int = 51, tab: 'types.ui.Tab|str|None' = types.ui.Tab.CONFIG
) -> ui.gui.MultiChoiceField:
return ui.gui.MultiChoiceField(
label=_('MFA policy networks'),
readonly=False,
@ -466,7 +472,10 @@ def login_without_mfa_policy_networks_field(order: int = 51, tab: 'types.ui.Tab|
old_field_name='networks',
)
def login_without_mfa_policy_field(order: int = 50, tab: 'types.ui.Tab|str|None' = types.ui.Tab.CONFIG) -> ui.gui.ChoiceField:
def login_without_mfa_policy_field(
order: int = 50, tab: 'types.ui.Tab|str|None' = types.ui.Tab.CONFIG
) -> ui.gui.ChoiceField:
return ui.gui.ChoiceField(
label=_('Policy for users without MFA support'),
order=order,
@ -479,6 +488,19 @@ def login_without_mfa_policy_field(order: int = 50, tab: 'types.ui.Tab|str|None'
)
def put_back_to_cache_field(order: int = 120, tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED) -> ui.gui.ChoiceField:
return ui.gui.ChoiceField(
order=order,
label=_('Put back to cache'),
tooltip=_('On machine releasy by logout, put it back to cache instead of deleting if possible.'),
choices=[
{'id': 'no', 'text': _('No. Never put it back to cache')},
{'id': 'yes', 'text': _('Yes, try to put it back to cache')},
],
tab=tab,
)
def onlogout_field_is_persistent(fld: ui.gui.ChoiceField) -> bool:
return fld.value == 'keep-always'

View File

@ -180,6 +180,7 @@ class OpenStackLiveService(DynamicService):
maintain_on_error = DynamicService.maintain_on_error
try_soft_shutdown = DynamicService.try_soft_shutdown
remove_duplicates = DynamicService.remove_duplicates
put_back_to_cache = DynamicService.put_back_to_cache
prov_uuid = gui.HiddenField()

View File

@ -100,6 +100,8 @@ class OpenStackServiceFixed(FixedService): # pylint: disable=too-many-public-me
machines = FixedService.machines
randomize = FixedService.randomize
maintain_on_error = FixedService.maintain_on_error
prov_uuid = gui.HiddenField()

View File

@ -93,6 +93,8 @@ class ProxmoxServiceFixed(FixedService): # pylint: disable=too-many-public-meth
machines = FixedService.machines
use_snapshots = FixedService.use_snapshots
maintain_on_error = FixedService.maintain_on_error
prov_uuid = gui.HiddenField(value=None)

View File

@ -31,6 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com
import logging
import re
import typing
import collections.abc
from django.utils.translation import gettext_noop as _
@ -124,6 +125,9 @@ class ProxmoxServiceLinked(DynamicService):
)
try_soft_shutdown = DynamicService.try_soft_shutdown
maintain_on_error = DynamicService.maintain_on_error
remove_duplicates = DynamicService.remove_duplicates
put_back_to_cache = DynamicService.put_back_to_cache
machine = gui.ChoiceField(
label=_("Base Machine"),
@ -207,6 +211,11 @@ class ProxmoxServiceLinked(DynamicService):
"""
return re.sub("[^a-zA-Z0-9_-]", "-", name)
def find_duplicates(self, name: str, mac: str) -> collections.abc.Iterable[str]:
for i in self.provider().api.list_vms():
if i.name and i.name.casefold() == name.casefold():
yield str(i.id)
def clone_vm(self, name: str, description: str, vmid: int = -1) -> 'prox_types.VmCreationResult':
name = self.sanitized_name(name)
pool = self.pool.value or None
@ -258,16 +267,22 @@ class ProxmoxServiceLinked(DynamicService):
def is_avaliable(self) -> bool:
return self.provider().is_available()
def get_ip(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> str:
def get_ip(
self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str
) -> str:
return self.provider().api.get_guest_ip_address(int(vmid))
def get_mac(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> str:
def get_mac(
self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str
) -> str:
# If vmid is empty, we are requesting a new mac
if not vmid:
return self.mac_generator().get(self.get_macs_range())
return self.provider().api.get_vm_config(int(vmid)).networks[0].macaddr.lower()
def start(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> None:
def start(
self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str
) -> None:
if isinstance(caller_instance, ProxmoxUserserviceLinked):
if self.is_running(caller_instance, vmid): # If running, skip
caller_instance._task = ''
@ -276,7 +291,9 @@ class ProxmoxServiceLinked(DynamicService):
else:
self.provider().api.start_vm(int(vmid))
def stop(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> None:
def stop(
self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str
) -> None:
if isinstance(caller_instance, ProxmoxUserserviceLinked):
if self.is_running(caller_instance, vmid):
caller_instance._store_task(self.provider().api.stop_vm(int(vmid)))
@ -285,7 +302,9 @@ class ProxmoxServiceLinked(DynamicService):
else:
self.provider().api.stop_vm(int(vmid))
def shutdown(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> None:
def shutdown(
self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str
) -> None:
if isinstance(caller_instance, ProxmoxUserserviceLinked):
if self.is_running(caller_instance, vmid):
caller_instance._store_task(self.provider().api.shutdown_vm(int(vmid)))
@ -294,7 +313,9 @@ class ProxmoxServiceLinked(DynamicService):
else:
self.provider().api.shutdown_vm(int(vmid)) # Just shutdown it, do not stores anything
def is_running(self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str) -> bool:
def is_running(
self, caller_instance: typing.Optional['DynamicUserService | DynamicPublication'], vmid: str
) -> bool:
# Raise an exception if fails to get machine info
return self.get_vm_info(int(vmid)).validate().status.is_running()

View File

@ -160,9 +160,9 @@ class XenLinkedService(DynamicService): # pylint: disable=too-many-public-metho
)
remove_duplicates = DynamicService.remove_duplicates
maintain_on_error = DynamicService.maintain_on_error
try_soft_shutdown = DynamicService.try_soft_shutdown
put_back_to_cache = DynamicService.put_back_to_cache
basename = DynamicService.basename
lenname = DynamicService.lenname

View File

@ -94,6 +94,8 @@ class XenFixedService(FixedService): # pylint: disable=too-many-public-methods
use_snapshots = FixedService.use_snapshots
randomize = FixedService.randomize
maintain_on_error = FixedService.maintain_on_error
prov_uuid = gui.HiddenField(value=None)
# Uses default FixedService.initialize