14
14
DepartedRelationHookContext, HookContext, RelationChange)
15
15
from juju.state.errors import StopWatcher, UnitRelationStateNotFound
16
16
from juju.state.relation import RelationStateManager, UnitRelationState
17
from juju.state.service import ServiceStateManager
18
19
from juju.unit.charm import download_charm
20
from juju.unit.deploy import UnitDeployer
19
21
from juju.unit.workflow import RelationWorkflowState
391
393
# Process new relations.
392
394
for relation_id in added:
393
395
service_relation = new_relations[relation_id]
394
yield self._add_relation(service_relation)
396
if service_relation.relation_scope == "container":
397
self._deploy_subordinate(service_relation)
399
yield self._add_relation(service_relation)
395
400
yield self._store_relations()
398
def _add_relation(self, service_relation):
403
def _add_relation(self, service_relation, unit=None):
400
unit_relation = yield service_relation.get_unit_state(
408
unit_relation = yield service_relation.get_unit_state(unit)
402
409
except UnitRelationStateNotFound:
403
410
# This unit has not yet been assigned a unit relation state,
404
411
# Go ahead and add one.
405
412
unit_relation = yield service_relation.add_unit_state(
415
lifecycle = UnitRelationLifecycle(
416
self._client, unit.unit_name, unit_relation,
417
service_relation.relation_name, self._unit_dir, self._state_dir,
420
workflow = RelationWorkflowState(
421
self._client, unit_relation, service_relation.relation_name,
422
lifecycle, self._state_dir)
424
self._relations[service_relation.internal_relation_id] = workflow
425
with (yield workflow.lock()):
426
yield workflow.synchronize()
428
def _get_unit_deployer(self, machine_id, charm_dir):
429
# this method exists to aid testing rather than being an
431
return UnitDeployer(self._client, machine_id, charm_dir)
434
def _deploy_subordinate(self, service_relation):
435
service_states = yield service_relation.get_service_states()
436
sub_service = [s for s in service_states if
437
s.service_name != self._unit.service_name][0]
439
#print "add unit of", sub_service, sub_service.service_name, "to", self._unit
440
sub_unit = yield sub_service.add_unit_state(container=self._unit)
441
machine_id = yield self._unit.get_assigned_machine_id()
443
charm_dir = os.path.join(os.path.dirname(self._unit_dir),
444
sub_unit.service_name.replace("/", "-"))
445
os.makedirs(charm_dir)
446
os.makedirs(os.path.join(charm_dir, "charms"))
448
unit_relation = yield service_relation.add_unit_state(sub_unit)
449
yield service_relation.add_unit_state(self._unit)
408
451
lifecycle = UnitRelationLifecycle(
409
452
self._client, self._unit.unit_name, unit_relation,
417
460
self._relations[service_relation.internal_relation_id] = workflow
418
461
with (yield workflow.lock()):
419
462
yield workflow.synchronize()
463
# with the relation in place and the units added to the
464
# container we can start the unit agent
465
unit_deployer = self._get_unit_deployer(machine_id, charm_dir)
466
yield unit_deployer.start("machine")
467
yield unit_deployer.start_service_unit(sub_unit.unit_name)
422
470
def _known_relations_path(self):