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