22
22
log = logging.getLogger("juju.agents.unit")
25
def unit_path(juju_path, unit_state):
27
juju_path, "units", unit_state.unit_name.replace("/", "-"))
25
30
class UnitAgent(BaseAgent):
26
31
"""An juju Unit Agent.
72
76
self.unit_state = yield service_state.get_unit_state(
74
78
self.unit_directory = os.path.join(
75
self.config["juju_directory"],
79
self.config["juju_directory"], "units",
77
80
self.unit_state.unit_name.replace("/", "-"))
79
81
self.state_directory = os.path.join(
80
82
self.config["juju_directory"], "state")
92
94
yield self.unit_state.set_private_address(
93
95
(yield address.get_private_address()))
97
if self.get_watch_enabled():
98
yield self.unit_state.watch_hook_debug(self.cb_watch_hook_debug)
95
100
# Inform the system, we're alive.
96
101
yield self.unit_state.connect_agent()
98
103
self.lifecycle = UnitLifecycle(
104
self.client, self.unit_state, service_state, self.unit_directory,
105
self.state_directory, self.executor)
105
107
self.workflow = UnitWorkflowState(
109
self.state_directory)
108
self.client, self.unit_state, self.lifecycle, self.state_directory)
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)
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)
117
# Fire initial transitions, only if successful
118
if (yield self.workflow.transition_state("installed")):
119
yield self.workflow.transition_state("started")
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)
129
123
"""Stop the unit agent process."""
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()
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