~fwereade/pyjuju/rearrange-settings-nodes

« back to all changes in this revision

Viewing changes to juju/state/environment.py

  • Committer: William Reade
  • Date: 2011-09-21 10:24:06 UTC
  • Revision ID: fwereade@gmail.com-20110921102406-47371sz4jejosh1o
global settings now live as value nodes under /settings, instead of using /global_settings as a dict node

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
from juju.state.base import StateBase
11
11
 
12
12
 
13
 
GLOBAL_SETTINGS = "/global_settings"
 
13
GLOBAL_SETTINGS = "/settings"
14
14
 
15
15
 
16
16
class EnvironmentStateManager(StateBase):
78
78
    def set_default_series(self, series):
79
79
        return self._set_value("default_series", series)
80
80
 
 
81
    def _key_path(self, key):
 
82
        return "%s/%s" % (GLOBAL_SETTINGS, key)
 
83
 
81
84
    @inlineCallbacks
82
85
    def _get_value(self, key, default=None):
83
86
        try:
84
 
            content, stat = yield self._client.get(GLOBAL_SETTINGS)
 
87
            content, stat = yield self._client.get(self._key_path(key))
85
88
        except zookeeper.NoNodeException:
86
89
            returnValue(default)
87
 
        data = yaml.load(content)
88
 
        returnValue(data.get(key, default))
 
90
        returnValue(yaml.load(content))
89
91
 
 
92
    @inlineCallbacks
90
93
    def _set_value(self, key, value):
 
94
        exists = yield self._client.exists(GLOBAL_SETTINGS)
 
95
        if not exists:
 
96
            yield self._client.create(GLOBAL_SETTINGS)
91
97
 
92
98
        def set_value(old_content, stat):
93
 
            if not old_content:
94
 
                data = {}
95
 
            else:
96
 
                data = yaml.load(old_content)
97
 
            data[key] = value
98
 
            return yaml.safe_dump(data)
99
 
 
100
 
        return retry_change(self._client, GLOBAL_SETTINGS, set_value)
101
 
 
102
 
    def watch_settings_changes(self, callback, error_callback=None):
 
99
            return yaml.safe_dump(value)
 
100
 
 
101
        result = yield retry_change(
 
102
            self._client, self._key_path(key), set_value)
 
103
        returnValue(result)
 
104
 
 
105
    def watch_debug_log(self, callback, error_callback=None):
103
106
        """Register a callback to invoked when the runtime changes.
104
107
 
105
108
        This watch primarily serves to get a persistent watch of the
115
118
        consumed by the error callback.
116
119
        """
117
120
        assert callable(callback), "Invalid callback"
118
 
        watcher = _RuntimeWatcher(self._client, callback, error_callback)
 
121
        path = self._key_path("debug_log")
 
122
        watcher = _RuntimeWatcher(self._client, path, callback, error_callback)
119
123
        return watcher.start()
120
124
 
121
125
 
122
126
class _RuntimeWatcher(object):
123
127
 
124
 
    def __init__(self, client, callback, error_callback=None):
 
128
    def __init__(self, client, path, callback, error_callback=None):
125
129
        self._client = client
 
130
        self._path = path
126
131
        self._callback = callback
127
132
        self._watching = False
128
133
        self._error_callback = error_callback
145
150
        # This logic will break if the node is removed, and so will
146
151
        # the function below, but the internal logic never removes
147
152
        # it, so we do not handle this case.
148
 
        exists_d, watch_d = self._client.exists_and_watch(GLOBAL_SETTINGS)
 
153
        exists_d, watch_d = self._client.exists_and_watch(self._path)
149
154
        exists = yield exists_d
150
155
 
151
156
        if exists:
167
172
        if not self._watching or not self._client.connected:
168
173
            returnValue(False)
169
174
 
170
 
        exists_d, watch_d = self._client.exists_and_watch(GLOBAL_SETTINGS)
 
175
        exists_d, watch_d = self._client.exists_and_watch(self._path)
171
176
 
172
177
        try:
173
178
            yield self._callback(change_event)