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