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

feat: Add "soft reset" (reboot) functionality to fields

The commit adds the "soft reset" functionality to the fields module, allowing for a reboot operation. This feature enhances the flexibility and control of the fields module.

Note: This commit message follows the established convention of using a prefix to indicate the type of change (feat for feature).
This commit is contained in:
Adolfo Gómez García 2024-06-13 18:27:35 +02:00
parent 61689ad638
commit 7db960ca92
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
10 changed files with 28 additions and 21 deletions

View File

@ -196,6 +196,15 @@ class AutoSerializable(UDSTestCase):
b.list_field.append(1)
b.list_field.append(2)
self.assertEqual(b.list_field, [1, 2])
b.list_field = [0]
self.assertEqual(b.list_field, [0])
b.list_field.append(1)
marshalled_data = b.marshal()
bb = SimpleListFieldDefault()
bb.unmarshal(marshalled_data)
self.assertEqual(bb.list_field, [0, 1])
def test_auto_serializable_derived(self) -> None:
instance = DerivedAutoSerializableClass()

View File

@ -155,7 +155,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
count = data.get('exec_count', 0) + 1
data['exec_count'] = count
if count > self.max_state_checks:
return self._error(f'Max checks reached on {info or "unknown"}')
return self.error(f'Max checks reached on {info or "unknown"}')
return None
@typing.final
@ -170,7 +170,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
data['retries'] = retries
if retries > self.max_retries: # get "own class" max retries
return self._error(f'Max retries reached')
return self.error(f'Max retries reached')
return None
@ -211,7 +211,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
return self.name_generator().get(self.service().get_basename(), self.service().get_lenname())
@typing.final
def _error(self, reason: typing.Union[str, Exception]) -> types.states.TaskState:
def error(self, reason: typing.Union[str, Exception]) -> types.states.TaskState:
"""
Internal method to set object as error state
@ -275,7 +275,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
return self.retry_later()
except Exception as e:
logger.exception('Unexpected FixedUserService exception: %s', e)
return self._error(e)
return self.error(e)
@typing.final
def retry_later(self) -> types.states.TaskState:
@ -287,7 +287,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
In any case, if we overpass the max retries, we will set the machine to error state
"""
if self._inc_retries_counter() is not None:
return self._error('Max retries reached')
return self.error('Max retries reached')
self._queue.insert(0, types.services.Operation.RETRY)
return types.states.TaskState.FINISHED
@ -390,7 +390,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
]
)
except Exception as e:
return self._error(f'Error on setReady: {e}')
return self.error(f'Error on setReady: {e}')
return self._execute_queue()
def reset(self) -> types.states.TaskState:
@ -452,7 +452,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
# And it has not been removed from the queue
return types.states.TaskState.RUNNING
except Exception as e:
return self._error(e)
return self.error(e)
@typing.final
def destroy(self) -> types.states.TaskState:
@ -463,7 +463,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
op = self._current_op()
if op == types.services.Operation.ERROR:
return self._error('Machine is already in error state!')
return self.error('Machine is already in error state!')
shutdown_operations: list[types.services.Operation] = (
[]

View File

@ -253,6 +253,13 @@ class FixedUserService(services.UserService, autoserializable.AutoSerializable,
"""
return self.error('Cache for fixed userservices not supported')
def process_ready_from_os_manager(self, data: typing.Any) -> types.states.TaskState:
"""
By default, process ready from os manager will return finished for most of the fixed services
So we provide a default implementation here
"""
return types.states.TaskState.FINISHED
def set_ready(self) -> types.states.TaskState:
# If already ready, return finished
try:

View File

@ -131,7 +131,7 @@ class Operation(enum.IntEnum):
CREATE_COMPLETED = 1002
START = 1003
START_COMPLETED = 1004
STOP = 1005 # This is a "hard" shutdown, likes a power off
STOP = 1005 # This is a "hard" shutdown, like a power off
STOP_COMPLETED = 1006
SHUTDOWN = 1007 # This is a "soft" shutdown
SHUTDOWN_COMPLETED = 1008

View File

@ -208,7 +208,7 @@ class _SerializableField(typing.Generic[T]):
instance {SerializableFields} -- Instance of class with field
"""
if hasattr(instance, '_fields'):
if hasattr(instance, '_fields'):
if self.name in getattr(instance, '_fields'):
return getattr(instance, '_fields')[self.name]

View File

@ -26,7 +26,7 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# pyright: reportArgumentType=false
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""

View File

@ -96,9 +96,6 @@ class OpenStackUserServiceFixed(FixedUserService, autoserializable.AutoSerializa
return types.states.TaskState.FINISHED
def process_ready_from_os_manager(self, data: typing.Any) -> types.states.TaskState:
return types.states.TaskState.FINISHED
def op_start(self) -> None:
try:
server_info = self.service().api.get_server(self._vmid)

View File

@ -102,9 +102,6 @@ class ProxmoxUserServiceFixed(FixedUserService, autoserializable.AutoSerializabl
return types.states.TaskState.FINISHED
def process_ready_from_os_manager(self, data: typing.Any) -> types.states.TaskState:
return types.states.TaskState.FINISHED
def op_start(self) -> None:
try:
vminfo = self.service().get_machine_info(int(self._vmid))

View File

@ -136,7 +136,7 @@ class ProxmoxUserserviceLinked(DynamicUserService):
return types.states.TaskState.RUNNING # Try again later
if task.is_errored():
return self._error(task.exitstatus)
return self.error(task.exitstatus)
if task.is_completed():
return types.states.TaskState.FINISHED

View File

@ -69,9 +69,6 @@ class XenFixedUserService(FixedUserService, autoserializable.AutoSerializable):
return types.states.TaskState.FINISHED
def process_ready_from_os_manager(self, data: typing.Any) -> types.states.TaskState:
return types.states.TaskState.FINISHED
def op_start(self) -> None:
self._task = self.service().start_vm(self._vmid)