/usr/share/pyshared/juju/state/hook.py is in juju-0.7 0.7-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | import logging
from collections import namedtuple
from twisted.internet.defer import inlineCallbacks, returnValue, succeed, fail
from juju.state.base import StateBase
from juju.state.errors import (
UnitRelationStateNotFound, StateNotFound, RelationBrokenContextError,
RelationStateNotFound, InvalidRelationIdentity, StateChanged)
from juju.state.relation import ServiceRelationState, UnitRelationState
from juju.state.service import ServiceStateManager, parse_service_name
from juju.state.utils import YAMLState
log = logging.getLogger("juju.state.hook")
class RelationChange(
namedtuple(
"RelationChange",
"relation_ident change_type unit_name")):
__slots__ = ()
@property
def relation_name(self):
return self.relation_ident.split(":")[0]
class HookContext(StateBase):
"""Context for hooks which don't depend on relation state. """
def __init__(self, client, unit_name, topology=None):
super(HookContext, self).__init__(client)
self._unit_name = unit_name
self._service = None
# A cache of retrieved nodes.
self._node_cache = {}
# A cache of node names to node ids.
self._name_cache = {}
# Service options
self._config_options = None
# Cached topology
self._topology = topology
@inlineCallbacks
def _resolve_id(self, unit_id):
"""Resolve a unit id to a unit name."""
if self._topology is None:
self._topology = yield self._read_topology()
unit_name = self._topology.get_service_unit_name_from_id(unit_id)
returnValue(unit_name)
@inlineCallbacks
def _resolve_name(self, unit_name):
"""Resolve a unit name to a unit id with caching."""
if unit_name in self._name_cache:
returnValue(self._name_cache[unit_name])
if self._topology is None:
self._topology = yield self._read_topology()
unit_id = self._topology.get_service_unit_id_from_name(unit_name)
self._name_cache[unit_name] = unit_id
returnValue(unit_id)
@inlineCallbacks
def get_local_unit_state(self):
"""Return ServiceUnitState for the local service unit."""
service_state_manager = ServiceStateManager(self._client)
unit_state = yield service_state_manager.get_unit_state(
self._unit_name)
returnValue(unit_state)
@inlineCallbacks
def get_local_service(self):
"""Return ServiceState for the local service."""
if self._service is None:
service_state_manager = ServiceStateManager(self._client)
self._service = yield(
service_state_manager.get_service_state(
parse_service_name(self._unit_name)))
returnValue(self._service)
@inlineCallbacks
def get_config(self):
"""Gather the configuration options.
Returns YAMLState for the service options of the current hook
and caches them internally.This state object's `write` method
must be called to publish changes to Zookeeper. `flush` will
do this automatically.
"""
if not self._config_options:
service = yield self.get_local_service()
self._config_options = yield service.get_config()
returnValue(self._config_options)
@inlineCallbacks
def get_relations(self):
"""Get the relations associated to the local service."""
relations = []
if self._topology is None:
self._topology = yield self._read_topology()
service = yield self.get_local_service()
internal_service_id = service.internal_id
service_unit_state = yield self.get_local_unit_state()
for info in self._topology.get_relations_for_service(
internal_service_id):
service_info = info["service"]
relation = ServiceRelationState(
self._client,
internal_service_id,
info["relation_id"],
info["scope"],
**service_info)
# Verify that it has unit relation state defined in ZK
try:
yield relation.get_unit_state(service_unit_state)
relations.append(relation)
except UnitRelationStateNotFound:
log.debug("Ignoring partially constructed relation: %s",
relation.relation_ident)
returnValue(relations)
@inlineCallbacks
def get_relation_idents(self, relation_name):
"""Return the relation idents for `relation_name`"""
relations = yield self.get_relations()
returnValue(sorted([r.relation_ident for r in relations if
not relation_name or
r.relation_name == relation_name]))
@inlineCallbacks
def get_relation_id_and_scope(self, relation_ident):
"""Return the (internal) relation id for `relation_ident`."""
parts = relation_ident.split(":")
if len(parts) != 2 or not parts[1].isdigit():
raise InvalidRelationIdentity(relation_ident)
relation_name, normalized_id = parts
relation_id = "%s-%s" % ("relation", normalized_id.zfill(10))
# Double check the internal relation id by looking it up
relations = yield self.get_relations()
for r in relations:
if (r.relation_name == relation_name and \
r.internal_relation_id == relation_id):
returnValue((relation_id, r.relation_scope))
else:
raise RelationStateNotFound()
@inlineCallbacks
def get_relation_hook_context(self, relation_ident):
"""Return a child hook context for `relation_ident`"""
service = yield self.get_local_service()
unit = yield self.get_local_unit_state()
relation_id, relation_scope = yield self.get_relation_id_and_scope(
relation_ident)
unit_relation = UnitRelationState(
self._client, service.internal_id, unit.internal_id,
relation_id, relation_scope)
# Ensure that the topology is shared so there's a consistent view
# between the parent and any children.
returnValue(RelationHookContext(
self._client, unit_relation,
relation_ident,
unit_name=self._unit_name,
topology=self._topology))
@inlineCallbacks
def flush(self):
"""Flush pending state."""
config = yield self.get_config()
yield config.write()
class RelationHookContext(HookContext):
"""A hook execution data cache and write buffer of relation settings.
Performs caching of any relation settings examined by the
hook. Also buffers all writes till the flush method is invoked.
"""
def __init__(self, client, unit_relation, relation_ident,
members=None, unit_name=None, topology=None):
"""
@param unit_relation: The unit relation state associated to the hook.
@param change: A C{RelationChange} instance.
"""
# Zookeeper client.
super(RelationHookContext, self).__init__(
client, unit_name=unit_name, topology=topology)
self._unit_relation = unit_relation
# A cache of related units in the relation.
self._members = members
# The relation ident of this context
self._relation_ident = relation_ident
# Whether settings have been modified (set/delete) for this context
self._needs_flushing = False
# A cache of the relation scope path
self._settings_scope_path = None
def get_settings_path(self, unit_id):
if self._unit_relation.relation_scope == "global":
return "/relations/%s/settings/%s" % (
self._unit_relation.internal_relation_id, unit_id)
if self._settings_scope_path:
return "%s/%s" % (self._settings_scope_path, unit_id)
def process(unit_settings_path):
self._settings_scope_path = "/".join(
unit_settings_path.split("/")[:-1])
return "%s/%s" % (self._settings_scope_path, unit_id)
d = self._unit_relation.get_settings_path()
return d.addCallback(process)
@property
def relation_ident(self):
"""Returns the relation ident corresponding to this context."""
return self._relation_ident
@property
def relation_name(self):
"""Returns the relation name corresponding to this context."""
return self._relation_ident.split(":")[0]
@inlineCallbacks
def get_members(self):
"""Gets the related unit members of the relation with caching."""
if self._members is not None:
returnValue(self._members)
try:
container = yield self._unit_relation.get_related_unit_container()
except StateChanged:
# The unit relation has vanished, so there are no members.
returnValue([])
unit_ids = yield self._client.get_children(container)
if self._unit_relation.internal_unit_id in unit_ids:
unit_ids.remove(self._unit_relation.internal_unit_id)
members = []
for unit_id in unit_ids:
unit_name = yield self._resolve_id(unit_id)
members.append(unit_name)
self._members = members
returnValue(members)
@inlineCallbacks
def _setup_relation_state(self, unit_name=None):
"""For a given unit name make sure we have YAMLState."""
if unit_name is None:
unit_name = yield self._resolve_id(
self._unit_relation.internal_unit_id)
if unit_name in self._node_cache:
returnValue(self._node_cache[unit_name])
unit_id = yield self._resolve_name(unit_name)
path = yield self.get_settings_path(unit_id)
# verify the unit relation path exists
relation_data = YAMLState(self._client, path)
try:
yield relation_data.read(required=True)
except StateNotFound:
raise UnitRelationStateNotFound(
self._unit_relation.internal_relation_id,
self.relation_name,
unit_name)
# cache the value
self._node_cache[unit_name] = relation_data
returnValue(relation_data)
@inlineCallbacks
def get(self, unit_name):
"""Get the relation settings for a unit.
Returns the settings as a dictionary.
"""
relation_data = yield self._setup_relation_state(unit_name)
returnValue(dict(relation_data))
@inlineCallbacks
def get_value(self, unit_name, key):
"""Get a relation setting value for a unit."""
settings = yield self.get(unit_name)
if not settings:
returnValue("")
returnValue(settings.get(key, ""))
@inlineCallbacks
def set(self, data):
"""Set the relation settings for a unit.
@param data: A dictionary containing the settings.
**Warning**, this method will replace existing values for the
unit relation with those from the ``data`` dictionary.
"""
if not isinstance(data, dict):
raise TypeError("A dictionary is required.")
self._needs_flushing = True
state = yield self._setup_relation_state()
state.update(data)
@inlineCallbacks
def set_value(self, key, value):
"""Set a relation value for a unit."""
self._needs_flushing = True
state = yield self._setup_relation_state()
state[key] = value
@inlineCallbacks
def delete_value(self, key):
"""Delete a relation value for a unit."""
self._needs_flushing = True
state = yield self._setup_relation_state()
try:
del state[key]
except KeyError:
# deleting a non-existent key is a no-op
pass
def has_read(self, unit_name):
"""Has the context been used to access the settings of the unit.
"""
return unit_name in self._node_cache
@inlineCallbacks
def flush(self):
"""Flush all writes to the unit settings.
A flush will attempt to intelligently merge values modified on
the context to the current state of the underlying settings
node. It supports externally modified or deleted values that
are unchanged on the context, to be preserved.
The change items to the relation YAMLState is returned (this
could also be done with config settings, but given their
usage model, doesn't seem to be worth logging).
"""
relation_setting_changes = []
if self._needs_flushing:
rel_state = yield self._setup_relation_state()
relation_setting_changes = yield rel_state.write()
yield super(RelationHookContext, self).flush()
returnValue(relation_setting_changes)
class DepartedRelationHookContext(HookContext):
"""A hook execution context suitable for running a relation-broken hook.
This context exposes the same interface as RelationHookContext, but:
* relation settings cannot be changed
* no remote units are reported to exist
* remote unit settings are not accessible
"""
def __init__(self, client, unit_name, unit_id, relation_name, relation_id):
super(DepartedRelationHookContext, self).__init__(client, unit_name)
self._relation_name = relation_name
self._relation_id = relation_id
self._settings_path = None
# Cache of relation settings for the local unit
self._relation_cache = None
def get_members(self):
return succeed([])
@property
def relation_ident(self):
"""Returns the external relation id corresponding to this context."""
return ServiceRelationState.get_relation_ident(
self._relation_name, self._relation_id)
@inlineCallbacks
def get_settings_path(self):
if self._settings_path:
returnValue(self._settings_path)
unit_id = yield self._resolve_name(self._unit_name)
topology = yield self._read_topology()
container = topology.get_service_unit_container(unit_id)
container_info = ""
if container:
container_info = "%s/" % container[-1]
self._settings_path = "/relations/%s/%ssettings/%s" % (
self._relation_id, container_info, unit_id)
returnValue(self._settings_path)
@inlineCallbacks
def get(self, unit_name):
# Only this unit's settings should be accessible.
if unit_name not in (None, self._unit_name):
raise RelationBrokenContextError(
"Cannot access other units in broken relation")
settings_path = yield self.get_settings_path()
if self._relation_cache is None:
relation_data = YAMLState(self._client, settings_path)
try:
yield relation_data.read(required=True)
self._relation_cache = dict(relation_data)
except StateNotFound:
self._relation_cache = {}
returnValue(self._relation_cache)
@inlineCallbacks
def get_value(self, unit_name, key):
settings = yield self.get(unit_name)
returnValue(settings.get(key, ""))
def set(self, data):
return fail(RelationBrokenContextError(
"Cannot change settings in broken relation"))
def set_value(self, key, value):
return fail(RelationBrokenContextError(
"Cannot change settings in broken relation"))
def delete_value(self, key):
return fail(RelationBrokenContextError(
"Cannot change settings in broken relation"))
def has_read(self, unit_name):
"""Has the context been used to access the settings of the unit.
"""
if unit_name in (None, self._unit_name):
return self._relation_cache is not None
return False
|