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