4b24331a54fad487e58eb4d0136b81a774894a28
2 from datetime
import datetime
5 from .client
import client
7 log
= logging
.getLogger(__name__
)
10 class Unit(model
.ModelEntity
):
12 def agent_status(self
):
13 """Returns the current agent status string.
16 return self
.data
['agent-status']['current']
19 def agent_status_since(self
):
20 """Get the time when the `agent_status` was last updated.
23 since
= self
.data
['agent-status']['since']
24 # Juju gives us nanoseconds, but Python only supports microseconds
26 return datetime
.strptime(since
, "%Y-%m-%dT%H:%M:%S.%f")
29 def agent_status_message(self
):
30 """Get the agent status message.
33 return self
.data
['agent-status']['message']
36 def workload_status(self
):
37 """Returns the current workload status string.
40 return self
.data
['workload-status']['current']
43 def workload_status_since(self
):
44 """Get the time when the `workload_status` was last updated.
47 since
= self
.data
['workload-status']['since']
48 # Juju gives us nanoseconds, but Python only supports microseconds
50 return datetime
.strptime(since
, "%Y-%m-%dT%H:%M:%S.%f")
53 def workload_status_message(self
):
54 """Get the workload status message.
57 return self
.data
['workload-status']['message']
59 def add_storage(self
, name
, constraints
=None):
60 """Add unit storage dynamically.
62 :param str name: Storage name, as specified by the charm
63 :param str constraints: Comma-separated list of constraints in the
64 form 'POOL,COUNT,SIZE'
69 def collect_metrics(self
):
70 """Collect metrics on this unit.
75 async def destroy(self
):
79 app_facade
= client
.ApplicationFacade()
80 app_facade
.connect(self
.connection
)
83 'Destroying %s', self
.name
)
85 return await app_facade
.DestroyUnits([self
.name
])
88 def get_resources(self
, details
=False):
89 """Return resources for this unit.
91 :param bool details: Include detailed info about resources used by each
97 def resolved(self
, retry
=False):
98 """Mark unit errors resolved.
100 :param bool retry: Re-execute failed hooks
105 async def run(self
, command
, timeout
=None):
106 """Run command on this unit.
108 :param str command: The command to run
109 :param int timeout: Time to wait before command is considered failed
112 action
= client
.ActionFacade()
113 action
.connect(self
.connection
)
116 'Running `%s` on %s', command
, self
.name
)
118 return await action
.Run(
126 def run_action(self
, action_name
, **params
):
127 """Run action on this unit.
129 :param str action_name: Name of action to run
130 :param \*\*params: Action parameters
136 self
, source_path
, user
=None, destination_path
=None, proxy
=False,
138 """Transfer files to this unit.
140 :param str source_path: Path of file(s) to transfer
141 :param str user: Remote username
142 :param str destination_path: Destination of transferred files on
144 :param bool proxy: Proxy through the Juju API server
145 :param str scp_opts: Additional options to the `scp` command
150 def set_meter_status(self
):
151 """Set the meter status on this unit.
157 self
, command
, user
=None, proxy
=False, ssh_opts
=None):
158 """Execute a command over SSH on this unit.
160 :param str command: Command to execute
161 :param str user: Remote username
162 :param bool proxy: Proxy through the Juju API server
163 :param str ssh_opts: Additional options to the `ssh` command
168 def status_history(self
, num
=20, utc
=False):
169 """Get status history for this unit.
171 :param int num: Size of history backlog
172 :param bool utc: Display time as UTC in RFC3339 format