6 from .client
import client
8 log
= logging
.getLogger(__name__
)
11 class Unit(model
.ModelEntity
):
13 def agent_status(self
):
14 """Returns the current agent status string.
17 return self
.safe_data
['agent-status']['current']
20 def agent_status_since(self
):
21 """Get the time when the `agent_status` was last updated.
24 return pyrfc3339
.parse(self
.safe_data
['agent-status']['since'])
27 def agent_status_message(self
):
28 """Get the agent status message.
31 return self
.safe_data
['agent-status']['message']
34 def workload_status(self
):
35 """Returns the current workload status string.
38 return self
.safe_data
['workload-status']['current']
41 def workload_status_since(self
):
42 """Get the time when the `workload_status` was last updated.
45 return pyrfc3339
.parse(self
.safe_data
['workload-status']['since'])
48 def workload_status_message(self
):
49 """Get the workload status message.
52 return self
.safe_data
['workload-status']['message']
56 """Get the machine object for this unit.
59 machine_id
= self
.safe_data
['machine-id']
61 return self
.model
.machines
.get(machine_id
, None)
66 def public_address(self
):
67 """ Get the public address.
70 return self
.safe_data
['public-address'] or None
74 return 'unit-%s' % self
.name
.replace('/', '-')
76 def add_storage(self
, name
, constraints
=None):
77 """Add unit storage dynamically.
79 :param str name: Storage name, as specified by the charm
80 :param str constraints: Comma-separated list of constraints in the
81 form 'POOL,COUNT,SIZE'
84 raise NotImplementedError()
86 def collect_metrics(self
):
87 """Collect metrics on this unit.
90 raise NotImplementedError()
92 async def destroy(self
):
96 app_facade
= client
.ApplicationFacade
.from_connection(self
.connection
)
99 'Destroying %s', self
.name
)
101 return await app_facade
.DestroyUnits([self
.name
])
104 def get_resources(self
, details
=False):
105 """Return resources for this unit.
107 :param bool details: Include detailed info about resources used by each
111 raise NotImplementedError()
113 def resolved(self
, retry
=False):
114 """Mark unit errors resolved.
116 :param bool retry: Re-execute failed hooks
119 raise NotImplementedError()
121 async def run(self
, command
, timeout
=None):
122 """Run command on this unit.
124 :param str command: The command to run
125 :param int timeout: Time, in seconds, to wait before command is
127 :returns: A :class:`juju.action.Action` instance.
130 action
= client
.ActionFacade
.from_connection(self
.connection
)
133 'Running `%s` on %s', command
, self
.name
)
136 # Convert seconds to nanoseconds
137 timeout
= int(timeout
* 1000000000)
139 res
= await action
.Run(
146 return await self
.model
.wait_for_action(res
.results
[0].action
.tag
)
148 async def run_action(self
, action_name
, **params
):
149 """Run an action on this unit.
151 :param str action_name: Name of action to run
152 :param **params: Action parameters
153 :returns: A :class:`juju.action.Action` instance.
155 Note that this only enqueues the action. You will need to call
156 ``action.wait()`` on the resulting `Action` instance if you wish
157 to block until the action is complete.
160 action_facade
= client
.ActionFacade
.from_connection(self
.connection
)
162 log
.debug('Starting action `%s` on %s', action_name
, self
.name
)
164 res
= await action_facade
.Enqueue([client
.Action(
169 action
= res
.results
[0].action
170 error
= res
.results
[0].error
171 if error
and error
.code
== 'not found':
172 raise ValueError('Action `%s` not found on %s' % (action_name
,
175 raise Exception('Unknown action error: %s' % error
.serialize())
176 action_id
= action
.tag
[len('action-'):]
177 log
.debug('Action started as %s', action_id
)
178 # we mustn't use wait_for_action because that blocks until the
179 # action is complete, rather than just being in the model
180 return await self
.model
._wait
_for
_new
('action', action_id
)
182 async def scp_to(self
, source
, destination
, user
='ubuntu', proxy
=False,
184 """Transfer files to this unit.
186 :param str source: Local path of file(s) to transfer
187 :param str destination: Remote destination of transferred files
188 :param str user: Remote username
189 :param bool proxy: Proxy through the Juju API server
190 :param scp_opts: Additional options to the `scp` command
191 :type scp_opts: str or list
193 await self
.machine
.scp_to(source
, destination
, user
=user
, proxy
=proxy
,
196 async def scp_from(self
, source
, destination
, user
='ubuntu', proxy
=False,
198 """Transfer files from this unit.
200 :param str source: Remote path of file(s) to transfer
201 :param str destination: Local destination of transferred files
202 :param str user: Remote username
203 :param bool proxy: Proxy through the Juju API server
204 :param scp_opts: Additional options to the `scp` command
205 :type scp_opts: str or list
207 await self
.machine
.scp_from(source
, destination
, user
=user
,
208 proxy
=proxy
, scp_opts
=scp_opts
)
210 def set_meter_status(self
):
211 """Set the meter status on this unit.
214 raise NotImplementedError()
217 self
, command
, user
=None, proxy
=False, ssh_opts
=None):
218 """Execute a command over SSH on this unit.
220 :param str command: Command to execute
221 :param str user: Remote username
222 :param bool proxy: Proxy through the Juju API server
223 :param str ssh_opts: Additional options to the `ssh` command
226 raise NotImplementedError()
228 def status_history(self
, num
=20, utc
=False):
229 """Get status history for this unit.
231 :param int num: Size of history backlog
232 :param bool utc: Display time as UTC in RFC3339 format
235 raise NotImplementedError()
237 async def is_leader_from_status(self
):
239 Check to see if this unit is the leader. Returns True if so, and
240 False if it is not, or if leadership does not make sense
241 (e.g., there is no leader in this application.)
243 This method is a kluge that calls FullStatus in the
244 ClientFacade to get its information. Once
245 https://bugs.launchpad.net/juju/+bug/1643691 is resolved, we
246 should add a simple .is_leader property, and deprecate this
250 app
= self
.name
.split("/")[0]
252 c
= client
.ClientFacade
.from_connection(self
.connection
)
254 status
= await c
.FullStatus(None)
256 # FullStatus may be more up to date than our model, and the
257 # unit may have gone away, or we may be doing something silly,
258 # like trying to fetch leadership for a subordinate, which
259 # will not be filed where we expect in the model. In those
260 # cases, we may simply return False, as a nonexistent or
261 # subordinate unit is not a leader.
262 if not status
.applications
.get(app
):
265 if not status
.applications
[app
].get('units'):
268 if not status
.applications
[app
]['units'].get(self
.name
):
271 return status
.applications
[app
]['units'][self
.name
].get('leader',
274 async def get_metrics(self
):
275 """Get metrics for the unit.
277 :return: Dictionary of metrics for this unit.
280 metrics
= await self
.model
.get_metrics(self
.tag
)
281 return metrics
[self
.name
]