Add get metrics
[osm/N2VC.git] / juju / application.py
1 import asyncio
2 import logging
3
4 from . import model
5 from .client import client
6
7 log = logging.getLogger(__name__)
8
9
10 class Application(model.ModelEntity):
11 @property
12 def _unit_match_pattern(self):
13 return r'^{}.*$'.format(self.entity_id)
14
15 def on_unit_add(self, callable_):
16 """Add a "unit added" observer to this entity, which will be called
17 whenever a unit is added to this application.
18
19 """
20 self.model.add_observer(
21 callable_, 'unit', 'add', self._unit_match_pattern)
22
23 def on_unit_remove(self, callable_):
24 """Add a "unit removed" observer to this entity, which will be called
25 whenever a unit is removed from this application.
26
27 """
28 self.model.add_observer(
29 callable_, 'unit', 'remove', self._unit_match_pattern)
30
31 @property
32 def units(self):
33 return [
34 unit for unit in self.model.units.values()
35 if unit.application == self.name
36 ]
37
38 @property
39 def status(self):
40 """Get the application status, as set by the charm's leader.
41
42 """
43 return self.data['status']['current']
44
45 @property
46 def status_message(self):
47 """Get the application status message, as set by the charm's leader.
48
49 """
50 return self.data['status']['message']
51
52 @property
53 def tag(self):
54 return 'application-%s' % self.name
55
56 async def add_relation(self, local_relation, remote_relation):
57 """Add a relation to another application.
58
59 :param str local_relation: Name of relation on this application
60 :param str remote_relation: Name of relation on the other
61 application in the form '<application>[:<relation_name>]'
62
63 """
64 if ':' not in local_relation:
65 local_relation = '{}:{}'.format(self.name, local_relation)
66
67 return await self.model.add_relation(local_relation, remote_relation)
68
69 async def add_unit(self, count=1, to=None):
70 """Add one or more units to this application.
71
72 :param int count: Number of units to add
73 :param str to: Placement directive, e.g.::
74 '23' - machine 23
75 'lxc:7' - new lxc container on machine 7
76 '24/lxc/3' - lxc container 3 or machine 24
77
78 If None, a new machine is provisioned.
79
80 """
81 app_facade = client.ApplicationFacade()
82 app_facade.connect(self.connection)
83
84 log.debug(
85 'Adding %s unit%s to %s',
86 count, '' if count == 1 else 's', self.name)
87
88 result = await app_facade.AddUnits(
89 application=self.name,
90 placement=to,
91 num_units=count,
92 )
93
94 return await asyncio.gather(*[
95 asyncio.ensure_future(self.model._wait_for_new('unit', unit_id))
96 for unit_id in result.units
97 ])
98
99 add_units = add_unit
100
101 def allocate(self, budget, value):
102 """Allocate budget to this application.
103
104 :param str budget: Name of budget
105 :param int value: Budget limit
106
107 """
108 pass
109
110 def attach(self, resource_name, file_path):
111 """Upload a file as a resource for this application.
112
113 :param str resource: Name of the resource
114 :param str file_path: Path to the file to upload
115
116 """
117 pass
118
119 def collect_metrics(self):
120 """Collect metrics on this application.
121
122 """
123 pass
124
125 async def destroy_relation(self, local_relation, remote_relation):
126 """Remove a relation to another application.
127
128 :param str local_relation: Name of relation on this application
129 :param str remote_relation: Name of relation on the other
130 application in the form '<application>[:<relation_name>]'
131
132 """
133 if ':' not in local_relation:
134 local_relation = '{}:{}'.format(self.name, local_relation)
135
136 app_facade = client.ApplicationFacade()
137 app_facade.connect(self.connection)
138
139 log.debug(
140 'Destroying relation %s <-> %s', local_relation, remote_relation)
141
142 return await app_facade.DestroyRelation([
143 local_relation, remote_relation])
144 remove_relation = destroy_relation
145
146 async def destroy_unit(self, *unit_names):
147 """Destroy units by name.
148
149 """
150 return await self.model.destroy_units(*unit_names)
151 destroy_units = destroy_unit
152
153 async def destroy(self):
154 """Remove this application from the model.
155
156 """
157 app_facade = client.ApplicationFacade()
158 app_facade.connect(self.connection)
159
160 log.debug(
161 'Destroying %s', self.name)
162
163 return await app_facade.Destroy(self.name)
164 remove = destroy
165
166 async def expose(self):
167 """Make this application publicly available over the network.
168
169 """
170 app_facade = client.ApplicationFacade()
171 app_facade.connect(self.connection)
172
173 log.debug(
174 'Exposing %s', self.name)
175
176 return await app_facade.Expose(self.name)
177
178 async def get_config(self):
179 """Return the configuration settings dict for this application.
180
181 """
182 app_facade = client.ApplicationFacade()
183 app_facade.connect(self.connection)
184
185 log.debug(
186 'Getting config for %s', self.name)
187
188 return (await app_facade.Get(self.name)).config
189
190 async def get_constraints(self):
191 """Return the machine constraints dict for this application.
192
193 """
194 app_facade = client.ApplicationFacade()
195 app_facade.connect(self.connection)
196
197 log.debug(
198 'Getting constraints for %s', self.name)
199
200 result = (await app_facade.Get(self.name)).constraints
201 return vars(result) if result else result
202
203 def get_actions(self, schema=False):
204 """Get actions defined for this application.
205
206 :param bool schema: Return the full action schema
207
208 """
209 pass
210
211 def get_resources(self, details=False):
212 """Return resources for this application.
213
214 :param bool details: Include detailed info about resources used by each
215 unit
216
217 """
218 pass
219
220 async def run(self, command, timeout=None):
221 """Run command on all units for this application.
222
223 :param str command: The command to run
224 :param int timeout: Time to wait before command is considered failed
225
226 """
227 action = client.ActionFacade()
228 action.connect(self.connection)
229
230 log.debug(
231 'Running `%s` on all units of %s', command, self.name)
232
233 # TODO this should return a list of Actions
234 return await action.Run(
235 [self.name],
236 command,
237 [],
238 timeout,
239 [],
240 )
241
242 async def set_annotations(self, annotations):
243 """Set annotations on this application.
244
245 :param annotations map[string]string: the annotations as key/value
246 pairs.
247
248 """
249 log.debug('Updating annotations on application %s', self.name)
250
251 self.ann_facade = client.AnnotationsFacade()
252 self.ann_facade.connect(self.connection)
253
254 ann = client.EntityAnnotations(
255 entity=self.name,
256 annotations=annotations,
257 )
258 return await self.ann_facade.Set([ann])
259
260 async def set_config(self, config, to_default=False):
261 """Set configuration options for this application.
262
263 :param config: Dict of configuration to set
264 :param bool to_default: Set application options to default values
265
266 """
267 app_facade = client.ApplicationFacade()
268 app_facade.connect(self.connection)
269
270 log.debug(
271 'Setting config for %s: %s', self.name, config)
272
273 return await app_facade.Set(self.name, config)
274
275 async def set_constraints(self, constraints):
276 """Set machine constraints for this application.
277
278 :param dict constraints: Dict of machine constraints
279
280 """
281 app_facade = client.ApplicationFacade()
282 app_facade.connect(self.connection)
283
284 log.debug(
285 'Setting constraints for %s: %s', self.name, constraints)
286
287 return await app_facade.SetConstraints(self.name, constraints)
288
289 def set_meter_status(self, status, info=None):
290 """Set the meter status on this status.
291
292 :param str status: Meter status, e.g. 'RED', 'AMBER'
293 :param str info: Extra info message
294
295 """
296 pass
297
298 def set_plan(self, plan_name):
299 """Set the plan for this application, effective immediately.
300
301 :param str plan_name: Name of plan
302
303 """
304 pass
305
306 async def unexpose(self):
307 """Remove public availability over the network for this application.
308
309 """
310 app_facade = client.ApplicationFacade()
311 app_facade.connect(self.connection)
312
313 log.debug(
314 'Unexposing %s', self.name)
315
316 return await app_facade.Unexpose(self.name)
317
318 def update_allocation(self, allocation):
319 """Update existing allocation for this application.
320
321 :param int allocation: The allocation to set
322
323 """
324 pass
325
326 def upgrade_charm(
327 self, channel=None, force_series=False, force_units=False,
328 path=None, resources=None, revision=-1, switch=None):
329 """Upgrade the charm for this application.
330
331 :param str channel: Channel to use when getting the charm from the
332 charm store, e.g. 'development'
333 :param bool force_series: Upgrade even if series of deployed
334 application is not supported by the new charm
335 :param bool force_units: Upgrade all units immediately, even if in
336 error state
337 :param str path: Uprade to a charm located at path
338 :param dict resources: Dictionary of resource name/filepath pairs
339 :param int revision: Explicit upgrade revision
340 :param str switch: Crossgrade charm url
341
342 """
343 pass
344
345 async def get_metrics(self):
346 return await self.model.get_metrics(self.tag)