~landscape/landscape-client/landscape-client-12.04-0ubuntu0.12.04.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from landscape.ui.constants import NOT_MANAGED, CANONICAL_MANAGED
import logging

from landscape.ui.model.registration.proxy import RegistrationProxy
from landscape.ui.model.configuration.state import StateError


class ConfigControllerLockError(Exception):
    pass


class ConfigController(object):
    """
    L{ConfigContoller} defines actions to take against a configuration object,
    providing starting values from the file, allowing them to be changed
    transiently, reverted or committed.
    """

    DEFAULT_DEDICATED_ACCOUNT_NAME = "standalone"

    def __init__(self, configuration):
        self._observers = []
        self._configuration = configuration
        self._initialised = True

    def __getattr__(self, name):
        if name in self.__dict__:
            return self.__dict__[name]
        else:
            return getattr(self._configuration, name)

    def __setattr__(self, name, value):
        # this test allows attributes to be set in the __init__ method
        if not '_initialised' in self.__dict__:
            return object.__setattr__(self, name, value)
        if name in ConfigController.__dict__:
            return object.__setattr__(self, name, value)
        else:
            try:
                setattr(self._configuration, name, value)
                self._configuration.modify()
            except AttributeError:
                return object.__setattr__(self, name, value)
            else:
                self._configuration.modify()

    def load(self):
        """
        Load the initial data from the configuration.
        """
        return self._configuration.load_data()

    def revert(self):
        """
        Revert settings to those the configuration object originally found.
        """
        try:
            self._configuration.revert()
        except StateError:
            # We probably don't care.
            logging.info("landscape-client-settings-ui reverted with no "
                         "changes to revert.")

    def persist(self, on_notify, on_error, on_succeed, on_fail):
        "Persist settings via the configuration object."
        try:
            self._configuration.persist()
        except StateError:
            # We probably don't care.
            logging.info("landscape-client-settings-ui committed with no "
                         "changes to commit.")
        if self._configuration.management_type == NOT_MANAGED:
            self.disable(on_notify, on_succeed, on_fail)
        else:
            self.register(on_notify, on_error, on_succeed, on_fail)

    def register(self, notify_method, error_method, succeed_method,
                 fail_method):
        """
        Perform registration using the L{RegistrationProxy}.
        """

        def registration_fail_wrapper():
            fail_method(action="Registering client")

        def registration_succeed_wrapper():
            succeed_method(action="Registering client")

        registration = RegistrationProxy(
            on_register_notify=notify_method,
            on_register_error=error_method,
            on_register_succeed=registration_succeed_wrapper,
            on_register_fail=registration_fail_wrapper)
        if self._configuration.management_type == CANONICAL_MANAGED:
            notify_method("Attempting to register at %s" %
                          self._configuration.hosted_landscape_host)
        else:
            notify_method("Attempting to register at %s" %
                          self._configuration.local_landscape_host)
        registration.register(self._configuration.get_config_filename())
        registration.exit()

    def disable(self, notify_method, succeed_method, fail_method):
        """
        Disable landscape client via the L{RegistrationProxy}.
        """

        def disabling_fail_wrapper():
            fail_method(action="Disabling client")

        def disabling_succeed_wrapper():
            succeed_method(action="Disabling client")

        registration = RegistrationProxy(
            on_disable_succeed=disabling_succeed_wrapper,
            on_disable_fail=disabling_fail_wrapper)
        notify_method("Attempting to disable landscape client.")
        registration.disable()
        registration.exit()