~fwereade/pyjuju/fix-cluster-instances

« back to all changes in this revision

Viewing changes to juju/agents/unit.py

merge parent

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
log = logging.getLogger("juju.agents.unit")
23
23
 
24
24
 
 
25
def unit_path(juju_path, unit_state):
 
26
    return os.path.join(
 
27
        juju_path, "units", unit_state.unit_name.replace("/", "-"))
 
28
 
 
29
 
25
30
class UnitAgent(BaseAgent):
26
31
    """An juju Unit Agent.
27
32
 
62
67
    def start(self):
63
68
        """Start the unit agent process."""
64
69
        self.service_state_manager = ServiceStateManager(self.client)
65
 
        self.executor.start()
66
70
 
67
71
        # Retrieve our unit and configure working directories.
68
72
        service_name = self.unit_name.split("/")[0]
72
76
        self.unit_state = yield service_state.get_unit_state(
73
77
            self.unit_name)
74
78
        self.unit_directory = os.path.join(
75
 
            self.config["juju_directory"],
76
 
            "units",
 
79
            self.config["juju_directory"], "units",
77
80
            self.unit_state.unit_name.replace("/", "-"))
78
 
 
79
81
        self.state_directory = os.path.join(
80
82
            self.config["juju_directory"], "state")
81
83
 
92
94
        yield self.unit_state.set_private_address(
93
95
            (yield address.get_private_address()))
94
96
 
 
97
        if self.get_watch_enabled():
 
98
            yield self.unit_state.watch_hook_debug(self.cb_watch_hook_debug)
 
99
 
95
100
        # Inform the system, we're alive.
96
101
        yield self.unit_state.connect_agent()
97
102
 
98
103
        self.lifecycle = UnitLifecycle(
99
 
            self.client,
100
 
            self.unit_state,
101
 
            service_state,
102
 
            self.unit_directory,
103
 
            self.executor)
 
104
            self.client, self.unit_state, service_state, self.unit_directory,
 
105
            self.state_directory, self.executor)
104
106
 
105
107
        self.workflow = UnitWorkflowState(
106
 
            self.client,
107
 
            self.unit_state,
108
 
            self.lifecycle,
109
 
            self.state_directory)
 
108
            self.client, self.unit_state, self.lifecycle, self.state_directory)
 
109
 
 
110
        # Set up correct lifecycle and executor state given the persistent
 
111
        # unit workflow state, and fire any starting transitions if necessary.
 
112
        yield self.workflow.synchronize(self.executor)
110
113
 
111
114
        if self.get_watch_enabled():
112
115
            yield self.unit_state.watch_resolved(self.cb_watch_resolved)
113
 
            yield self.unit_state.watch_hook_debug(self.cb_watch_hook_debug)
114
116
            yield service_state.watch_config_state(
115
117
                self.cb_watch_config_changed)
116
 
 
117
 
        # Fire initial transitions, only if successful
118
 
        if (yield self.workflow.transition_state("installed")):
119
 
            yield self.workflow.transition_state("started")
120
 
 
121
 
        # Upgrade can only be processed if we're in a running state so
122
 
        # for case of a newly started unit, do it after the unit is started.
123
 
        if self.get_watch_enabled():
124
118
            yield self.unit_state.watch_upgrade_flag(
125
119
                self.cb_watch_upgrade_flag)
126
120
 
127
121
    @inlineCallbacks
128
122
    def stop(self):
129
123
        """Stop the unit agent process."""
130
 
        if self.workflow:
131
 
            yield self.workflow.transition_state("stopped")
 
124
        if self.lifecycle.running:
 
125
            yield self.lifecycle.stop(fire_hooks=False, stop_relations=False)
 
126
        yield self.executor.stop()
132
127
        if self.api_socket:
133
128
            yield self.api_socket.stopListening()
134
129
        yield self.api_factory.stopFactory()
232
227
 
233
228
        # Verify the workflow state
234
229
        workflow_state = yield self._agent.workflow.get_state()
235
 
        if not workflow_state in ("started",):
 
230
        if workflow_state != "started":
236
231
            self._log.warning(
237
232
                "Unit not in an upgradeable state: %s", workflow_state)
238
233
            # Upgrades can only be supported while the unit is