Implement Application.run()
[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 # TODO this should return an Action
119 return await action.Run(
120 [],
121 command,
122 [],
123 timeout,
124 [self.name],
125 )
126
127 def run_action(self, action_name, **params):
128 """Run action on this unit.
129
130 :param str action_name: Name of action to run
131 :param \*\*params: Action parameters
132
133 """
134 pass
135
136 def scp(
137 self, source_path, user=None, destination_path=None, proxy=False,
138 scp_opts=None):
139 """Transfer files to this unit.
140
141 :param str source_path: Path of file(s) to transfer
142 :param str user: Remote username
143 :param str destination_path: Destination of transferred files on
144 remote machine
145 :param bool proxy: Proxy through the Juju API server
146 :param str scp_opts: Additional options to the `scp` command
147
148 """
149 pass
150
151 def set_meter_status(self):
152 """Set the meter status on this unit.
153
154 """
155 pass
156
157 def ssh(
158 self, command, user=None, proxy=False, ssh_opts=None):
159 """Execute a command over SSH on this unit.
160
161 :param str command: Command to execute
162 :param str user: Remote username
163 :param bool proxy: Proxy through the Juju API server
164 :param str ssh_opts: Additional options to the `ssh` command
165
166 """
167 pass
168
169 def status_history(self, num=20, utc=False):
170 """Get status history for this unit.
171
172 :param int num: Size of history backlog
173 :param bool utc: Display time as UTC in RFC3339 format
174
175 """
176 pass