6
6
from txzookeeper.utils import retry_change
8
from ensemble.environment.config import EnvironmentsConfig
9
from ensemble.state.errors import EnvironmentStateNotFound
10
from ensemble.state.base import StateBase
13
GLOBAL_SETTINGS = "/global_settings"
8
from juju.environment.config import EnvironmentsConfig
9
from juju.state.errors import EnvironmentStateNotFound
10
from juju.state.base import StateBase
13
SETTINGS_PATH = "/settings"
16
16
class EnvironmentStateManager(StateBase):
58
58
This can't be stored directly in the environment, as that has access
59
59
restrictions. The runtime state can be accessed from all connected
63
def set_provider_type(self, provider_type):
64
return self._set_value("provider-type", provider_type)
66
def get_provider_type(self):
67
return self._get_value("provider-type")
63
69
def is_debug_log_enabled(self):
64
70
"""Find out if the debug log is enabled. Returns a boolean.
66
return self._get_value("debug_log", False)
72
return self._get_value("debug-log", False)
68
74
def set_debug_log(self, enabled):
69
75
"""Enable/Disable the debug log.
71
77
:param enabled: Boolean denoting whether the log should be enabled.
73
return self._set_value("debug_log", bool(enabled))
79
return self._set_value("debug-log", bool(enabled))
76
82
def _get_value(self, key, default=None):
78
content, stat = yield self._client.get(GLOBAL_SETTINGS)
84
content, stat = yield self._client.get(SETTINGS_PATH)
79
85
except zookeeper.NoNodeException:
80
86
returnValue(default)
81
87
data = yaml.load(content)
92
98
return yaml.safe_dump(data)
94
return retry_change(self._client, GLOBAL_SETTINGS, set_value)
100
return retry_change(self._client, SETTINGS_PATH, set_value)
96
102
def watch_settings_changes(self, callback, error_callback=None):
97
103
"""Register a callback to invoked when the runtime changes.
139
145
# This logic will break if the node is removed, and so will
140
146
# the function below, but the internal logic never removes
141
147
# it, so we do not handle this case.
142
exists_d, watch_d = self._client.exists_and_watch(GLOBAL_SETTINGS)
148
exists_d, watch_d = self._client.exists_and_watch(SETTINGS_PATH)
143
149
exists = yield exists_d
161
167
if not self._watching or not self._client.connected:
162
168
returnValue(False)
164
exists_d, watch_d = self._client.exists_and_watch(GLOBAL_SETTINGS)
170
exists_d, watch_d = self._client.exists_and_watch(SETTINGS_PATH)
167
173
yield self._callback(change_event)