3 from datetime
import datetime
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
.data
['agent-status']['current']
20 def agent_status_since(self
):
21 """Get the time when the `agent_status` was last updated.
24 since
= self
.data
['agent-status']['since']
25 # Juju gives us nanoseconds, but Python only supports microseconds
27 return datetime
.strptime(since
, "%Y-%m-%dT%H:%M:%S.%f")
30 def agent_status_message(self
):
31 """Get the agent status message.
34 return self
.data
['agent-status']['message']
37 def workload_status(self
):
38 """Returns the current workload status string.
41 return self
.data
['workload-status']['current']
44 def workload_status_since(self
):
45 """Get the time when the `workload_status` was last updated.
48 since
= self
.data
['workload-status']['since']
49 # Juju gives us nanoseconds, but Python only supports microseconds
51 return datetime
.strptime(since
, "%Y-%m-%dT%H:%M:%S.%f")
54 def workload_status_message(self
):
55 """Get the workload status message.
58 return self
.data
['workload-status']['message']
60 def add_storage(self
, name
, constraints
=None):
61 """Add unit storage dynamically.
63 :param str name: Storage name, as specified by the charm
64 :param str constraints: Comma-separated list of constraints in the
65 form 'POOL,COUNT,SIZE'
70 def collect_metrics(self
):
71 """Collect metrics on this unit.
76 async def destroy(self
):
80 app_facade
= client
.ApplicationFacade()
81 app_facade
.connect(self
.connection
)
84 'Destroying %s', self
.name
)
86 return await app_facade
.DestroyUnits([self
.name
])
89 def get_resources(self
, details
=False):
90 """Return resources for this unit.
92 :param bool details: Include detailed info about resources used by each
98 def resolved(self
, retry
=False):
99 """Mark unit errors resolved.
101 :param bool retry: Re-execute failed hooks
106 async def run(self
, command
, timeout
=None):
107 """Run command on this unit.
109 :param str command: The command to run
110 :param int timeout: Time to wait before command is considered failed
112 Returns a tuple containing the stdout, stderr, and return code
116 action
= client
.ActionFacade()
117 action
.connect(self
.connection
)
120 'Running `%s` on %s', command
, self
.name
)
122 action_status
= asyncio
.Queue(loop
=self
.model
.loop
)
125 async def wait_for_tag():
130 async def callback(delta
, old
, new
, model
):
131 # Wait until we have something to report
135 # Verify that we have the the right action.
136 tag
= await wait_for_tag()
137 if not new
.id in tag
:
140 # Wait until the action has completed, or errored out.
141 if new
.status
not in ['completed', 'error']:
144 # Put the action in our queue, so that we can fetch it
145 # with the await below.
146 await action_status
.put(new
)
148 self
.model
.add_observer(callback
, 'action', None)
150 res
= await action
.Run(
157 tag
= res
.results
[0].action
.tag
# Set the tag for our waiter above.
158 ret
= await action_status
.get() # Wait for our callback to fire
160 ret
.results
['Stdout'],
161 ret
.results
['Stderr'],
165 def run_action(self
, action_name
, **params
):
166 """Run action on this unit.
168 :param str action_name: Name of action to run
169 :param \*\*params: Action parameters
175 self
, source_path
, user
=None, destination_path
=None, proxy
=False,
177 """Transfer files to this unit.
179 :param str source_path: Path of file(s) to transfer
180 :param str user: Remote username
181 :param str destination_path: Destination of transferred files on
183 :param bool proxy: Proxy through the Juju API server
184 :param str scp_opts: Additional options to the `scp` command
189 def set_meter_status(self
):
190 """Set the meter status on this unit.
196 self
, command
, user
=None, proxy
=False, ssh_opts
=None):
197 """Execute a command over SSH on this unit.
199 :param str command: Command to execute
200 :param str user: Remote username
201 :param bool proxy: Proxy through the Juju API server
202 :param str ssh_opts: Additional options to the `ssh` command
207 def status_history(self
, num
=20, utc
=False):
208 """Get status history for this unit.
210 :param int num: Size of history backlog
211 :param bool utc: Display time as UTC in RFC3339 format