~hazmat/pyjuju/subordinate

« back to all changes in this revision

Viewing changes to juju/unit/lifecycle.py

  • Committer: Benjamin Saller
  • Date: 2012-03-09 10:34:28 UTC
  • Revision ID: bcsaller@gmail.com-20120309103428-b206jtf7o2w5ya2c
wip on unit deployment from lifecycle

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
17
18
 
18
19
from juju.unit.charm import download_charm
 
20
from juju.unit.deploy import UnitDeployer
19
21
from juju.unit.workflow import RelationWorkflowState
20
22
 
21
23
 
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)
 
398
            else:
 
399
                yield self._add_relation(service_relation)
395
400
            yield self._store_relations()
396
401
 
397
402
    @inlineCallbacks
398
 
    def _add_relation(self, service_relation):
 
403
    def _add_relation(self, service_relation, unit=None):
 
404
        if unit is None:
 
405
            unit = self._unit
 
406
 
399
407
        try:
400
 
            unit_relation = yield service_relation.get_unit_state(
401
 
                self._unit)
 
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(
406
 
                self._unit)
 
413
                unit)
 
414
 
 
415
        lifecycle = UnitRelationLifecycle(
 
416
            self._client, unit.unit_name, unit_relation,
 
417
            service_relation.relation_name, self._unit_dir, self._state_dir,
 
418
            self._executor)
 
419
 
 
420
        workflow = RelationWorkflowState(
 
421
            self._client, unit_relation, service_relation.relation_name,
 
422
            lifecycle, self._state_dir)
 
423
 
 
424
        self._relations[service_relation.internal_relation_id] = workflow
 
425
        with (yield workflow.lock()):
 
426
            yield workflow.synchronize()
 
427
 
 
428
    def _get_unit_deployer(self, machine_id, charm_dir):
 
429
        # this method exists to aid testing rather than being an
 
430
        # inline
 
431
        return UnitDeployer(self._client, machine_id, charm_dir)
 
432
 
 
433
    @inlineCallbacks
 
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]
 
438
 
 
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()
 
442
 
 
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"))
 
447
 
 
448
        unit_relation = yield service_relation.add_unit_state(sub_unit)
 
449
        yield service_relation.add_unit_state(self._unit)
407
450
 
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)
420
468
 
421
469
    @property
422
470
    def _known_relations_path(self):