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.
42 return self
.data
['status']['current']
45 def status_message(self
):
46 """Get the application status message, as set by the charm's leader.
48 return self
.data
['status']['message']
50 def add_relation(self
, local_relation
, remote_relation
):
51 """Add a relation to another application.
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>]'
60 async def add_unit(self
, count
=1, to
=None):
61 """Add one or more units to this application.
63 :param int count: Number of units to add
64 :param str to: Placement directive, e.g.::
66 'lxc:7' - new lxc container on machine 7
67 '24/lxc/3' - lxc container 3 or machine 24
69 If None, a new machine is provisioned.
72 app_facade
= client
.ApplicationFacade()
73 app_facade
.connect(self
.connection
)
76 'Adding %s unit%s to %s',
77 count
, '' if count
== 1 else 's', self
.name
)
79 result
= await app_facade
.AddUnits(
80 application
=self
.name
,
85 return await asyncio
.gather(*[
86 asyncio
.ensure_future(self
.model
._wait
_for
_new
('unit', unit_id
))
87 for unit_id
in result
.units
92 def allocate(self
, budget
, value
):
93 """Allocate budget to this application.
95 :param str budget: Name of budget
96 :param int value: Budget limit
101 def attach(self
, resource_name
, file_path
):
102 """Upload a file as a resource for this application.
104 :param str resource: Name of the resource
105 :param str file_path: Path to the file to upload
110 def collect_metrics(self
):
111 """Collect metrics on this application.
116 def destroy_relation(self
, local_relation
, remote_relation
):
117 """Remove a relation to another application.
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>]'
125 remove_relation
= destroy_relation
127 async def destroy(self
):
128 """Remove this application from the model.
131 app_facade
= client
.ApplicationFacade()
132 app_facade
.connect(self
.connection
)
135 'Destroying %s', self
.name
)
137 return await app_facade
.Destroy(self
.name
)
140 async def expose(self
):
141 """Make this application publicly available over the network.
144 app_facade
= client
.ApplicationFacade()
145 app_facade
.connect(self
.connection
)
148 'Exposing %s', self
.name
)
150 return await app_facade
.Expose(self
.name
)
152 def get_config(self
):
153 """Return the configuration settings for this application.
158 def get_constraints(self
):
159 """Return the machine constraints for this application.
164 def get_actions(self
, schema
=False):
165 """Get actions defined for this application.
167 :param bool schema: Return the full action schema
172 def get_resources(self
, details
=False):
173 """Return resources for this application.
175 :param bool details: Include detailed info about resources used by each
181 def run(self
, command
, timeout
=None):
182 """Run command on all units for this application.
184 :param str command: The command to run
185 :param int timeout: Time to wait before command is considered failed
190 async def set_annotations(self
, annotations
):
191 """Set annotations on this application.
193 :param annotations map[string]string: the annotations as key/value
197 log
.debug('Updating annotations on application %s', self
.name
)
199 self
.ann_facade
= client
.AnnotationsFacade()
200 self
.ann_facade
.connect(self
.connection
)
202 ann
= client
.EntityAnnotations(
204 annotations
=annotations
,
206 return await self
.ann_facade
.Set([ann
])
208 def set_config(self
, to_default
=False, **config
):
209 """Set configuration options for this application.
211 :param bool to_default: Set application options to default values
212 :param \*\*config: Config key/values
217 def set_constraints(self
, constraints
):
218 """Set machine constraints for this application.
220 :param :class:`juju.Constraints` constraints: Machine constraints
225 def set_meter_status(self
, status
, info
=None):
226 """Set the meter status on this status.
228 :param str status: Meter status, e.g. 'RED', 'AMBER'
229 :param str info: Extra info message
234 def set_plan(self
, plan_name
):
235 """Set the plan for this application, effective immediately.
237 :param str plan_name: Name of plan
243 """Remove public availability over the network for this application.
248 def update_allocation(self
, allocation
):
249 """Update existing allocation for this application.
251 :param int allocation: The allocation to set
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.
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
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