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']
52 async def add_relation(self
, local_relation
, remote_relation
):
53 """Add a relation to another application.
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>]'
60 if ':' not in local_relation
:
61 local_relation
= '{}:{}'.format(self
.name
, local_relation
)
63 return await self
.model
.add_relation(local_relation
, remote_relation
)
65 async def add_unit(self
, count
=1, to
=None):
66 """Add one or more units to this application.
68 :param int count: Number of units to add
69 :param str to: Placement directive, e.g.::
71 'lxc:7' - new lxc container on machine 7
72 '24/lxc/3' - lxc container 3 or machine 24
74 If None, a new machine is provisioned.
77 app_facade
= client
.ApplicationFacade()
78 app_facade
.connect(self
.connection
)
81 'Adding %s unit%s to %s',
82 count
, '' if count
== 1 else 's', self
.name
)
84 result
= await app_facade
.AddUnits(
85 application
=self
.name
,
90 return await asyncio
.gather(*[
91 asyncio
.ensure_future(self
.model
._wait
_for
_new
('unit', unit_id
))
92 for unit_id
in result
.units
97 def allocate(self
, budget
, value
):
98 """Allocate budget to this application.
100 :param str budget: Name of budget
101 :param int value: Budget limit
106 def attach(self
, resource_name
, file_path
):
107 """Upload a file as a resource for this application.
109 :param str resource: Name of the resource
110 :param str file_path: Path to the file to upload
115 def collect_metrics(self
):
116 """Collect metrics on this application.
121 async def destroy_relation(self
, local_relation
, remote_relation
):
122 """Remove a relation to another application.
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>]'
129 if ':' not in local_relation
:
130 local_relation
= '{}:{}'.format(self
.name
, local_relation
)
132 app_facade
= client
.ApplicationFacade()
133 app_facade
.connect(self
.connection
)
136 'Destroying relation %s <-> %s', local_relation
, remote_relation
)
138 return await app_facade
.DestroyRelation([
139 local_relation
, remote_relation
])
140 remove_relation
= destroy_relation
142 async def destroy_unit(self
, *unit_names
):
143 """Destroy units by name.
146 return await self
.model
.destroy_units(*unit_names
)
147 destroy_units
= destroy_unit
149 async def destroy(self
):
150 """Remove this application from the model.
153 app_facade
= client
.ApplicationFacade()
154 app_facade
.connect(self
.connection
)
157 'Destroying %s', self
.name
)
159 return await app_facade
.Destroy(self
.name
)
162 async def expose(self
):
163 """Make this application publicly available over the network.
166 app_facade
= client
.ApplicationFacade()
167 app_facade
.connect(self
.connection
)
170 'Exposing %s', self
.name
)
172 return await app_facade
.Expose(self
.name
)
174 async def get_config(self
):
175 """Return the configuration settings dict for this application.
178 app_facade
= client
.ApplicationFacade()
179 app_facade
.connect(self
.connection
)
182 'Getting config for %s', self
.name
)
184 return (await app_facade
.Get(self
.name
)).config
186 async def get_constraints(self
):
187 """Return the machine constraints dict for this application.
190 app_facade
= client
.ApplicationFacade()
191 app_facade
.connect(self
.connection
)
194 'Getting constraints for %s', self
.name
)
196 result
= (await app_facade
.Get(self
.name
)).constraints
197 return vars(result
) if result
else result
199 def get_actions(self
, schema
=False):
200 """Get actions defined for this application.
202 :param bool schema: Return the full action schema
207 def get_resources(self
, details
=False):
208 """Return resources for this application.
210 :param bool details: Include detailed info about resources used by each
216 async def run(self
, command
, timeout
=None):
217 """Run command on all units for this application.
219 :param str command: The command to run
220 :param int timeout: Time to wait before command is considered failed
223 action
= client
.ActionFacade()
224 action
.connect(self
.connection
)
227 'Running `%s` on all units of %s', command
, self
.name
)
229 # TODO this should return a list of Actions
230 return await action
.Run(
238 async def set_annotations(self
, annotations
):
239 """Set annotations on this application.
241 :param annotations map[string]string: the annotations as key/value
245 log
.debug('Updating annotations on application %s', self
.name
)
247 self
.ann_facade
= client
.AnnotationsFacade()
248 self
.ann_facade
.connect(self
.connection
)
250 ann
= client
.EntityAnnotations(
252 annotations
=annotations
,
254 return await self
.ann_facade
.Set([ann
])
256 async def set_config(self
, config
, to_default
=False):
257 """Set configuration options for this application.
259 :param config: Dict of configuration to set
260 :param bool to_default: Set application options to default values
263 app_facade
= client
.ApplicationFacade()
264 app_facade
.connect(self
.connection
)
267 'Setting config for %s: %s', self
.name
, config
)
269 return await app_facade
.Set(self
.name
, config
)
271 async def set_constraints(self
, constraints
):
272 """Set machine constraints for this application.
274 :param dict constraints: Dict of machine constraints
277 app_facade
= client
.ApplicationFacade()
278 app_facade
.connect(self
.connection
)
281 'Setting constraints for %s: %s', self
.name
, constraints
)
283 return await app_facade
.SetConstraints(self
.name
, constraints
)
285 def set_meter_status(self
, status
, info
=None):
286 """Set the meter status on this status.
288 :param str status: Meter status, e.g. 'RED', 'AMBER'
289 :param str info: Extra info message
294 def set_plan(self
, plan_name
):
295 """Set the plan for this application, effective immediately.
297 :param str plan_name: Name of plan
302 async def unexpose(self
):
303 """Remove public availability over the network for this application.
306 app_facade
= client
.ApplicationFacade()
307 app_facade
.connect(self
.connection
)
310 'Unexposing %s', self
.name
)
312 return await app_facade
.Unexpose(self
.name
)
314 def update_allocation(self
, allocation
):
315 """Update existing allocation for this application.
317 :param int allocation: The allocation to set
323 self
, channel
=None, force_series
=False, force_units
=False,
324 path
=None, resources
=None, revision
=-1, switch
=None):
325 """Upgrade the charm for this application.
327 :param str channel: Channel to use when getting the charm from the
328 charm store, e.g. 'development'
329 :param bool force_series: Upgrade even if series of deployed
330 application is not supported by the new charm
331 :param bool force_units: Upgrade all units immediately, even if in
333 :param str path: Uprade to a charm located at path
334 :param dict resources: Dictionary of resource name/filepath pairs
335 :param int revision: Explicit upgrade revision
336 :param str switch: Crossgrade charm url