55b9d1eb7ca796f043564ed98121e706fe39d654
5 from .client
import client
6 from .placement
import parse
as parse_placement
8 log
= logging
.getLogger(__name__
)
11 class Application(model
.ModelEntity
):
13 def _unit_match_pattern(self
):
14 return r
'^{}.*$'.format(self
.entity_id
)
16 def on_unit_add(self
, callable_
):
17 """Add a "unit added" observer to this entity, which will be called
18 whenever a unit is added to this application.
21 self
.model
.add_observer(
22 callable_
, 'unit', 'add', self
._unit
_match
_pattern
)
24 def on_unit_remove(self
, callable_
):
25 """Add a "unit removed" observer to this entity, which will be called
26 whenever a unit is removed from this application.
29 self
.model
.add_observer(
30 callable_
, 'unit', 'remove', self
._unit
_match
_pattern
)
35 unit
for unit
in self
.model
.units
.values()
36 if unit
.application
== self
.name
41 """Get the application status, as set by the charm's leader.
44 return self
.safe_data
['status']['current']
47 def status_message(self
):
48 """Get the application status message, as set by the charm's leader.
51 return self
.safe_data
['status']['message']
55 return 'application-%s' % self
.name
57 async def add_relation(self
, local_relation
, remote_relation
):
58 """Add a relation to another application.
60 :param str local_relation: Name of relation on this application
61 :param str remote_relation: Name of relation on the other
62 application in the form '<application>[:<relation_name>]'
65 if ':' not in local_relation
:
66 local_relation
= '{}:{}'.format(self
.name
, local_relation
)
68 return await self
.model
.add_relation(local_relation
, remote_relation
)
70 async def add_unit(self
, count
=1, to
=None):
71 """Add one or more units to this application.
73 :param int count: Number of units to add
74 :param str to: Placement directive, e.g.::
76 'lxc:7' - new lxc container on machine 7
77 '24/lxc/3' - lxc container 3 or machine 24
79 If None, a new machine is provisioned.
82 app_facade
= client
.ApplicationFacade()
83 app_facade
.connect(self
.connection
)
86 'Adding %s unit%s to %s',
87 count
, '' if count
== 1 else 's', self
.name
)
89 result
= await app_facade
.AddUnits(
90 application
=self
.name
,
91 placement
=parse_placement(to
) if to
else None,
95 return await asyncio
.gather(*[
96 asyncio
.ensure_future(self
.model
._wait
_for
_new
('unit', unit_id
))
97 for unit_id
in result
.units
102 def allocate(self
, budget
, value
):
103 """Allocate budget to this application.
105 :param str budget: Name of budget
106 :param int value: Budget limit
111 def attach(self
, resource_name
, file_path
):
112 """Upload a file as a resource for this application.
114 :param str resource: Name of the resource
115 :param str file_path: Path to the file to upload
120 def collect_metrics(self
):
121 """Collect metrics on this application.
126 async def destroy_relation(self
, local_relation
, remote_relation
):
127 """Remove a relation to another application.
129 :param str local_relation: Name of relation on this application
130 :param str remote_relation: Name of relation on the other
131 application in the form '<application>[:<relation_name>]'
134 if ':' not in local_relation
:
135 local_relation
= '{}:{}'.format(self
.name
, local_relation
)
137 app_facade
= client
.ApplicationFacade()
138 app_facade
.connect(self
.connection
)
141 'Destroying relation %s <-> %s', local_relation
, remote_relation
)
143 return await app_facade
.DestroyRelation([
144 local_relation
, remote_relation
])
145 remove_relation
= destroy_relation
147 async def destroy_unit(self
, *unit_names
):
148 """Destroy units by name.
151 return await self
.model
.destroy_units(*unit_names
)
152 destroy_units
= destroy_unit
154 async def destroy(self
):
155 """Remove this application from the model.
158 app_facade
= client
.ApplicationFacade()
159 app_facade
.connect(self
.connection
)
162 'Destroying %s', self
.name
)
164 return await app_facade
.Destroy(self
.name
)
167 async def expose(self
):
168 """Make this application publicly available over the network.
171 app_facade
= client
.ApplicationFacade()
172 app_facade
.connect(self
.connection
)
175 'Exposing %s', self
.name
)
177 return await app_facade
.Expose(self
.name
)
179 async def get_config(self
):
180 """Return the configuration settings dict for this application.
183 app_facade
= client
.ApplicationFacade()
184 app_facade
.connect(self
.connection
)
187 'Getting config for %s', self
.name
)
189 return (await app_facade
.Get(self
.name
)).config
191 async def get_constraints(self
):
192 """Return the machine constraints dict for this application.
195 app_facade
= client
.ApplicationFacade()
196 app_facade
.connect(self
.connection
)
199 'Getting constraints for %s', self
.name
)
201 result
= (await app_facade
.Get(self
.name
)).constraints
202 return vars(result
) if result
else result
204 def get_actions(self
, schema
=False):
205 """Get actions defined for this application.
207 :param bool schema: Return the full action schema
212 def get_resources(self
, details
=False):
213 """Return resources for this application.
215 :param bool details: Include detailed info about resources used by each
221 async def run(self
, command
, timeout
=None):
222 """Run command on all units for this application.
224 :param str command: The command to run
225 :param int timeout: Time to wait before command is considered failed
228 action
= client
.ActionFacade()
229 action
.connect(self
.connection
)
232 'Running `%s` on all units of %s', command
, self
.name
)
234 # TODO this should return a list of Actions
235 return await action
.Run(
243 async def set_annotations(self
, annotations
):
244 """Set annotations on this application.
246 :param annotations map[string]string: the annotations as key/value
250 log
.debug('Updating annotations on application %s', self
.name
)
252 self
.ann_facade
= client
.AnnotationsFacade()
253 self
.ann_facade
.connect(self
.connection
)
255 ann
= client
.EntityAnnotations(
257 annotations
=annotations
,
259 return await self
.ann_facade
.Set([ann
])
261 async def set_config(self
, config
, to_default
=False):
262 """Set configuration options for this application.
264 :param config: Dict of configuration to set
265 :param bool to_default: Set application options to default values
268 app_facade
= client
.ApplicationFacade()
269 app_facade
.connect(self
.connection
)
272 'Setting config for %s: %s', self
.name
, config
)
274 return await app_facade
.Set(self
.name
, config
)
276 async def set_constraints(self
, constraints
):
277 """Set machine constraints for this application.
279 :param dict constraints: Dict of machine constraints
282 app_facade
= client
.ApplicationFacade()
283 app_facade
.connect(self
.connection
)
286 'Setting constraints for %s: %s', self
.name
, constraints
)
288 return await app_facade
.SetConstraints(self
.name
, constraints
)
290 def set_meter_status(self
, status
, info
=None):
291 """Set the meter status on this status.
293 :param str status: Meter status, e.g. 'RED', 'AMBER'
294 :param str info: Extra info message
299 def set_plan(self
, plan_name
):
300 """Set the plan for this application, effective immediately.
302 :param str plan_name: Name of plan
307 async def unexpose(self
):
308 """Remove public availability over the network for this application.
311 app_facade
= client
.ApplicationFacade()
312 app_facade
.connect(self
.connection
)
315 'Unexposing %s', self
.name
)
317 return await app_facade
.Unexpose(self
.name
)
319 def update_allocation(self
, allocation
):
320 """Update existing allocation for this application.
322 :param int allocation: The allocation to set
328 self
, channel
=None, force_series
=False, force_units
=False,
329 path
=None, resources
=None, revision
=-1, switch
=None):
330 """Upgrade the charm for this application.
332 :param str channel: Channel to use when getting the charm from the
333 charm store, e.g. 'development'
334 :param bool force_series: Upgrade even if series of deployed
335 application is not supported by the new charm
336 :param bool force_units: Upgrade all units immediately, even if in
338 :param str path: Uprade to a charm located at path
339 :param dict resources: Dictionary of resource name/filepath pairs
340 :param int revision: Explicit upgrade revision
341 :param str switch: Crossgrade charm url
346 async def get_metrics(self
):
347 return await self
.model
.get_metrics(self
.tag
)