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