From 3d02a99b428186eeffc9ce586349e6b87ed2a6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Sun, 4 Feb 2024 05:25:42 +0100 Subject: [PATCH] Remove last "autoattributes" use. Will remove the class probably on 5.0, when sure no data is left behind using it (Probably, first will rename it just in case it's needed on some environment can recover it fast, or import conditionally...) --- .../services/PhysicalMachines/deployment.py | 40 +++++++++--- .../PhysicalMachines/service_multi.py | 6 +- .../PhysicalMachines/service_single.py | 4 +- .../test_serialization_deployment.py | 61 +++++++++++++++++++ 4 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 server/tests/services/physical_machines/test_serialization_deployment.py diff --git a/server/src/uds/services/PhysicalMachines/deployment.py b/server/src/uds/services/PhysicalMachines/deployment.py index 302506d99..b5c15cf2e 100644 --- a/server/src/uds/services/PhysicalMachines/deployment.py +++ b/server/src/uds/services/PhysicalMachines/deployment.py @@ -38,9 +38,8 @@ import dns.resolver from uds.core import services from uds.core.types.states import State -from uds.core.util.auto_attributes import AutoAttributes from uds.core.util import net -from uds.core.util import log +from uds.core.util import log, autoserializable, auto_attributes from .types import HostInfo @@ -51,19 +50,25 @@ if typing.TYPE_CHECKING: logger = logging.getLogger(__name__) - -class IPMachineDeployed(services.UserService, AutoAttributes): - suggested_delay = 10 - +# This class is used for serialization of old data +class OldIPSerialData(auto_attributes.AutoAttributes): _ip: str _reason: str _state: str - - def __init__(self, environment, **kwargs): - AutoAttributes.__init__(self, ip=str, reason=str, state=str) - services.UserService.__init__(self, environment, **kwargs) + + def __init__(self): + auto_attributes.AutoAttributes.__init__(self, ip=str, reason=str, state=str) + self._ip = '' + self._reason = '' self._state = State.FINISHED +class IPMachineUserService(services.UserService, autoserializable.AutoSerializable): + suggested_delay = 10 + + _ip = autoserializable.StringField(default='') + _reason = autoserializable.StringField(default='') + _state = autoserializable.StringField(default=State.FINISHED) + # Utility overrides for type checking... def service(self) -> 'IPServiceBase': return typing.cast('IPServiceBase', super().service()) @@ -170,3 +175,18 @@ class IPMachineDeployed(services.UserService, AutoAttributes): def cancel(self) -> str: return self.destroy() + + def unmarshal(self, data: bytes) -> None: + if autoserializable.is_autoserializable_data(data): + return super().unmarshal(data) + + _auto_data = OldIPSerialData() + _auto_data.unmarshal(data) + + # Fill own data from restored data + self._ip = _auto_data._ip + self._reason = _auto_data._reason + self._state = _auto_data._state + + # Flag for upgrade + self.mark_for_upgrade(True) diff --git a/server/src/uds/services/PhysicalMachines/service_multi.py b/server/src/uds/services/PhysicalMachines/service_multi.py index 32e9233b4..716ed2777 100644 --- a/server/src/uds/services/PhysicalMachines/service_multi.py +++ b/server/src/uds/services/PhysicalMachines/service_multi.py @@ -44,7 +44,7 @@ from uds.core.ui import gui from uds.core.util import ensure, log, net from uds.core.util.model import sql_stamp_seconds -from .deployment import IPMachineDeployed +from .deployment import IPMachineUserService from .service_base import IPServiceBase from .types import HostInfo @@ -148,7 +148,7 @@ class IPMachinesService(IPServiceBase): needs_osmanager = False # If the service needs a s.o. manager (managers are related to agents provided by services itselfs, i.e. virtual machines with agent) must_assign_manually = False # If true, the system can't do an automatic assignation of a deployed user service from this service - user_service_type = IPMachineDeployed + user_service_type = IPMachineUserService services_type_provided = types.services.ServiceType.VDI @@ -331,7 +331,7 @@ class IPMachinesService(IPServiceBase): user: 'models.User', userDeployment: 'services.UserService', ) -> str: - userservice_instance: IPMachineDeployed = typing.cast(IPMachineDeployed, userDeployment) + userservice_instance: IPMachineUserService = typing.cast(IPMachineUserService, userDeployment) host = HostInfo.from_str(assignable_id) now = sql_stamp_seconds() diff --git a/server/src/uds/services/PhysicalMachines/service_single.py b/server/src/uds/services/PhysicalMachines/service_single.py index d8033e348..ac04a1014 100644 --- a/server/src/uds/services/PhysicalMachines/service_single.py +++ b/server/src/uds/services/PhysicalMachines/service_single.py @@ -40,7 +40,7 @@ from uds.core.ui import gui from uds.core.util import net from uds.core import exceptions, types -from .deployment import IPMachineDeployed +from .deployment import IPMachineUserService from .service_base import IPServiceBase from .types import HostInfo @@ -73,7 +73,7 @@ class IPSingleMachineService(IPServiceBase): needs_osmanager = False # If the service needs a s.o. manager (managers are related to agents provided by services itselfs, i.e. virtual machines with agent) must_assign_manually = False # If true, the system can't do an automatic assignation of a deployed user service from this service - user_service_type = IPMachineDeployed + user_service_type = IPMachineUserService services_type_provided = types.services.ServiceType.VDI diff --git a/server/tests/services/physical_machines/test_serialization_deployment.py b/server/tests/services/physical_machines/test_serialization_deployment.py new file mode 100644 index 000000000..b8ca6e6cd --- /dev/null +++ b/server/tests/services/physical_machines/test_serialization_deployment.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2022 Virtual Cable S.L.U. +# All rights reserved. +# + +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +import typing + +# We use commit/rollback + +from tests.utils.test import UDSTestCase +from uds.core.util import autoserializable +from uds.core.environment import Environment +from uds.core.managers import crypto + +from django.conf import settings + + +from uds.services.PhysicalMachines import provider, deployment + + +class IPMachineUserServiceSerializationTest(UDSTestCase): + def test_marshalling(self): + obj = deployment.OldIPSerialData() + obj._ip = '1.1.1.1' + obj._state = 'state' + obj._reason = 'reason' + + def _check_fields(instance: deployment.IPMachineUserService) -> None: + self.assertEqual(instance._ip, '1.1.1.1') + self.assertEqual(instance._state, 'state') + self.assertEqual(instance._reason, 'reason') + + data = obj.marshal() + + instance = deployment.IPMachineUserService(environment=Environment.testing_environment(), service=None) + instance.unmarshal(data) + + marshaled_data = instance.marshal() + + # Ensure remarshalled flag is set + self.assertTrue(instance.needs_upgrade()) + instance.mark_for_upgrade(False) # reset flag + + # Ensure fields has been marshalled using new format + self.assertTrue(autoserializable.is_autoserializable_data(marshaled_data)) + + # Check fields + _check_fields(instance) + + # Reunmarshall again and check that remarshalled flag is not set + instance = deployment.IPMachineUserService(environment=Environment.testing_environment(), service=None) + instance.unmarshal(marshaled_data) + self.assertFalse(instance.needs_upgrade()) + + # Check fields again + _check_fields(instance)