Add agent and workload status properties to Unit
[osm/N2VC.git] / juju / unit.py
1 import logging
2 from datetime import datetime
3
4 from . import model
5 from .client import client
6
7 log = logging.getLogger(__name__)
8
9
10 class Unit(model.ModelEntity):
11 @property
12 def agent_status(self):
13 """Returns the current agent status string.
14
15 """
16 return self.data['agent-status']['current']
17
18 @property
19 def agent_status_since(self):
20 """Get the time when the `agent_status` was last updated.
21
22 """
23 since = self.data['agent-status']['since']
24 # Juju gives us nanoseconds, but Python only supports microseconds
25 since = since[:26]
26 return datetime.strptime(since, "%Y-%m-%dT%H:%M:%S.%f")
27
28 @property
29 def agent_status_message(self):
30 """Get the agent status message.
31
32 """
33 return self.data['agent-status']['message']
34
35 @property
36 def workload_status(self):
37 """Returns the current workload status string.
38
39 """
40 return self.data['workload-status']['current']
41
42 @property
43 def workload_status_since(self):
44 """Get the time when the `workload_status` was last updated.
45
46 """
47 since = self.data['workload-status']['since']
48 # Juju gives us nanoseconds, but Python only supports microseconds
49 since = since[:26]
50 return datetime.strptime(since, "%Y-%m-%dT%H:%M:%S.%f")
51
52 @property
53 def workload_status_message(self):
54 """Get the workload status message.
55
56 """
57 return self.data['workload-status']['message']
58
59 def add_storage(self, name, constraints=None):
60 """Add unit storage dynamically.
61
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'
65
66 """
67 pass
68
69 def collect_metrics(self):
70 """Collect metrics on this unit.
71
72 """
73 pass
74
75 async def destroy(self):
76 """Destroy this unit.
77
78 """
79 app_facade = client.ApplicationFacade()
80 app_facade.connect(self.connection)
81
82 log.debug(
83 'Destroying %s', self.name)
84
85 return await app_facade.DestroyUnits([self.name])
86 remove = destroy
87
88 def get_resources(self, details=False):
89 """Return resources for this unit.
90
91 :param bool details: Include detailed info about resources used by each
92 unit
93
94 """
95 pass
96
97 def resolved(self, retry=False):
98 """Mark unit errors resolved.
99
100 :param bool retry: Re-execute failed hooks
101
102 """
103 pass
104
105 async def run(self, command, timeout=None):
106 """Run command on this unit.
107
108 :param str command: The command to run
109 :param int timeout: Time to wait before command is considered failed
110
111 """
112 action = client.ActionFacade()
113 action.connect(self.connection)
114
115 log.debug(
116 'Running `%s` on %s', command, self.name)
117
118 return await action.Run(
119 [],
120 command,
121 [],
122 timeout,
123 [self.name],
124 )
125
126 def run_action(self, action_name, **params):
127 """Run action on this unit.
128
129 :param str action_name: Name of action to run
130 :param \*\*params: Action parameters
131
132 """
133 pass
134
135 def scp(
136 self, source_path, user=None, destination_path=None, proxy=False,
137 scp_opts=None):
138 """Transfer files to this unit.
139
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
143 remote machine
144 :param bool proxy: Proxy through the Juju API server
145 :param str scp_opts: Additional options to the `scp` command
146
147 """
148 pass
149
150 def set_meter_status(self):
151 """Set the meter status on this unit.
152
153 """
154 pass
155
156 def ssh(
157 self, command, user=None, proxy=False, ssh_opts=None):
158 """Execute a command over SSH on this unit.
159
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
164
165 """
166 pass
167
168 def status_history(self, num=20, utc=False):
169 """Get status history for this unit.
170
171 :param int num: Size of history backlog
172 :param bool utc: Display time as UTC in RFC3339 format
173
174 """
175 pass