5 from .client
import client
7 log
= logging
.getLogger(__name__
)
10 class Application(model
.ModelEntity
):
12 def _unit_match_pattern(self
):
13 return r
'^{}.*$'.format(self
.entity_id
)
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.
20 self
.model
.add_observer(
21 callable_
, 'unit', 'add', self
._unit
_match
_pattern
)
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.
28 self
.model
.add_observer(
29 callable_
, 'unit', 'remove', self
._unit
_match
_pattern
)
34 unit
for unit
in self
.model
.units
.values()
35 if unit
.application
== self
.name
40 """Get the application status, as set by the charm's leader.
43 return self
.data
['status']['current']
46 def status_message(self
):
47 """Get the application status message, as set by the charm's leader.
50 return self
.data
['status']['message']
54 return 'application-%s' % self
.name
56 async def add_relation(self
, local_relation
, remote_relation
):
57 """Add a relation to another application.
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>]'
64 if ':' not in local_relation
:
65 local_relation
= '{}:{}'.format(self
.name
, local_relation
)
67 return await self
.model
.add_relation(local_relation
, remote_relation
)
69 async def add_unit(self
, count
=1, to
=None):
70 """Add one or more units to this application.
72 :param int count: Number of units to add
73 :param str to: Placement directive, e.g.::
75 'lxc:7' - new lxc container on machine 7
76 '24/lxc/3' - lxc container 3 or machine 24
78 If None, a new machine is provisioned.
81 app_facade
= client
.ApplicationFacade()
82 app_facade
.connect(self
.connection
)
85 'Adding %s unit%s to %s',
86 count
, '' if count
== 1 else 's', self
.name
)
88 result
= await app_facade
.AddUnits(
89 application
=self
.name
,
94 return await asyncio
.gather(*[
95 asyncio
.ensure_future(self
.model
._wait
_for
_new
('unit', unit_id
))
96 for unit_id
in result
.units
101 def allocate(self
, budget
, value
):
102 """Allocate budget to this application.
104 :param str budget: Name of budget
105 :param int value: Budget limit
110 def attach(self
, resource_name
, file_path
):
111 """Upload a file as a resource for this application.
113 :param str resource: Name of the resource
114 :param str file_path: Path to the file to upload
119 def collect_metrics(self
):
120 """Collect metrics on this application.
125 async def destroy_relation(self
, local_relation
, remote_relation
):
126 """Remove a relation to another application.
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>]'
133 if ':' not in local_relation
:
134 local_relation
= '{}:{}'.format(self
.name
, local_relation
)
136 app_facade
= client
.ApplicationFacade()
137 app_facade
.connect(self
.connection
)
140 'Destroying relation %s <-> %s', local_relation
, remote_relation
)
142 return await app_facade
.DestroyRelation([
143 local_relation
, remote_relation
])
144 remove_relation
= destroy_relation
146 async def destroy_unit(self
, *unit_names
):
147 """Destroy units by name.
150 return await self
.model
.destroy_units(*unit_names
)
151 destroy_units
= destroy_unit
153 async def destroy(self
):
154 """Remove this application from the model.
157 app_facade
= client
.ApplicationFacade()
158 app_facade
.connect(self
.connection
)
161 'Destroying %s', self
.name
)
163 return await app_facade
.Destroy(self
.name
)
166 async def expose(self
):
167 """Make this application publicly available over the network.
170 app_facade
= client
.ApplicationFacade()
171 app_facade
.connect(self
.connection
)
174 'Exposing %s', self
.name
)
176 return await app_facade
.Expose(self
.name
)
178 async def get_config(self
):
179 """Return the configuration settings dict for this application.
182 app_facade
= client
.ApplicationFacade()
183 app_facade
.connect(self
.connection
)
186 'Getting config for %s', self
.name
)
188 return (await app_facade
.Get(self
.name
)).config
190 async def get_constraints(self
):
191 """Return the machine constraints dict for this application.
194 app_facade
= client
.ApplicationFacade()
195 app_facade
.connect(self
.connection
)
198 'Getting constraints for %s', self
.name
)
200 result
= (await app_facade
.Get(self
.name
)).constraints
201 return vars(result
) if result
else result
203 def get_actions(self
, schema
=False):
204 """Get actions defined for this application.
206 :param bool schema: Return the full action schema
211 def get_resources(self
, details
=False):
212 """Return resources for this application.
214 :param bool details: Include detailed info about resources used by each
220 async def run(self
, command
, timeout
=None):
221 """Run command on all units for this application.
223 :param str command: The command to run
224 :param int timeout: Time to wait before command is considered failed
227 action
= client
.ActionFacade()
228 action
.connect(self
.connection
)
231 'Running `%s` on all units of %s', command
, self
.name
)
233 # TODO this should return a list of Actions
234 return await action
.Run(
242 async def set_annotations(self
, annotations
):
243 """Set annotations on this application.
245 :param annotations map[string]string: the annotations as key/value
249 log
.debug('Updating annotations on application %s', self
.name
)
251 self
.ann_facade
= client
.AnnotationsFacade()
252 self
.ann_facade
.connect(self
.connection
)
254 ann
= client
.EntityAnnotations(
256 annotations
=annotations
,
258 return await self
.ann_facade
.Set([ann
])
260 async def set_config(self
, config
, to_default
=False):
261 """Set configuration options for this application.
263 :param config: Dict of configuration to set
264 :param bool to_default: Set application options to default values
267 app_facade
= client
.ApplicationFacade()
268 app_facade
.connect(self
.connection
)
271 'Setting config for %s: %s', self
.name
, config
)
273 return await app_facade
.Set(self
.name
, config
)
275 async def set_constraints(self
, constraints
):
276 """Set machine constraints for this application.
278 :param dict constraints: Dict of machine constraints
281 app_facade
= client
.ApplicationFacade()
282 app_facade
.connect(self
.connection
)
285 'Setting constraints for %s: %s', self
.name
, constraints
)
287 return await app_facade
.SetConstraints(self
.name
, constraints
)
289 def set_meter_status(self
, status
, info
=None):
290 """Set the meter status on this status.
292 :param str status: Meter status, e.g. 'RED', 'AMBER'
293 :param str info: Extra info message
298 def set_plan(self
, plan_name
):
299 """Set the plan for this application, effective immediately.
301 :param str plan_name: Name of plan
306 async def unexpose(self
):
307 """Remove public availability over the network for this application.
310 app_facade
= client
.ApplicationFacade()
311 app_facade
.connect(self
.connection
)
314 'Unexposing %s', self
.name
)
316 return await app_facade
.Unexpose(self
.name
)
318 def update_allocation(self
, allocation
):
319 """Update existing allocation for this application.
321 :param int allocation: The allocation to set
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.
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
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
345 async def get_metrics(self
):
346 return await self
.model
.get_metrics(self
.tag
)