f6129a5f8120bcaba62ecdf89adbde088cade0b0
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
38 def add_relation(self
, local_relation
, remote_relation
):
39 """Add a relation to another application.
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>]'
48 async def add_unit(self
, count
=1, to
=None):
49 """Add one or more units to this application.
51 :param int count: Number of units to add
52 :param str to: Placement directive, e.g.::
54 'lxc:7' - new lxc container on machine 7
55 '24/lxc/3' - lxc container 3 or machine 24
57 If None, a new machine is provisioned.
60 app_facade
= client
.ApplicationFacade()
61 app_facade
.connect(self
.connection
)
64 'Adding %s unit%s to %s',
65 count
, '' if count
== 1 else 's', self
.name
)
67 result
= await app_facade
.AddUnits(
68 application
=self
.name
,
73 return await asyncio
.gather(*[
74 asyncio
.ensure_future(self
.model
._wait
_for
_new
('unit', unit_id
))
75 for unit_id
in result
.units
80 def allocate(self
, budget
, value
):
81 """Allocate budget to this application.
83 :param str budget: Name of budget
84 :param int value: Budget limit
89 def attach(self
, resource_name
, file_path
):
90 """Upload a file as a resource for this application.
92 :param str resource: Name of the resource
93 :param str file_path: Path to the file to upload
98 def collect_metrics(self
):
99 """Collect metrics on this application.
104 def destroy_relation(self
, local_relation
, remote_relation
):
105 """Remove a relation to another application.
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>]'
113 remove_relation
= destroy_relation
115 async def destroy(self
):
116 """Remove this application from the model.
119 app_facade
= client
.ApplicationFacade()
120 app_facade
.connect(self
.connection
)
123 'Destroying %s', self
.name
)
125 return await app_facade
.Destroy(self
.name
)
128 async def expose(self
):
129 """Make this application publicly available over the network.
132 app_facade
= client
.ApplicationFacade()
133 app_facade
.connect(self
.connection
)
136 'Exposing %s', self
.name
)
138 return await app_facade
.Expose(self
.name
)
140 def get_config(self
):
141 """Return the configuration settings for this application.
146 def get_constraints(self
):
147 """Return the machine constraints for this application.
152 def get_actions(self
, schema
=False):
153 """Get actions defined for this application.
155 :param bool schema: Return the full action schema
160 def get_resources(self
, details
=False):
161 """Return resources for this application.
163 :param bool details: Include detailed info about resources used by each
169 def run(self
, command
, timeout
=None):
170 """Run command on all units for this application.
172 :param str command: The command to run
173 :param int timeout: Time to wait before command is considered failed
178 async def set_annotations(self
, annotations
):
179 """Set annotations on this application.
181 :param annotations map[string]string: the annotations as key/value
185 log
.debug('Updating annotations on application %s', self
.name
)
187 self
.ann_facade
= client
.AnnotationsFacade()
188 self
.ann_facade
.connect(self
.connection
)
190 ann
= client
.EntityAnnotations(
192 annotations
=annotations
,
194 return await self
.ann_facade
.Set([ann
])
196 def set_config(self
, to_default
=False, **config
):
197 """Set configuration options for this application.
199 :param bool to_default: Set application options to default values
200 :param \*\*config: Config key/values
205 def set_constraints(self
, constraints
):
206 """Set machine constraints for this application.
208 :param :class:`juju.Constraints` constraints: Machine constraints
213 def set_meter_status(self
, status
, info
=None):
214 """Set the meter status on this status.
216 :param str status: Meter status, e.g. 'RED', 'AMBER'
217 :param str info: Extra info message
222 def set_plan(self
, plan_name
):
223 """Set the plan for this application, effective immediately.
225 :param str plan_name: Name of plan
231 """Remove public availability over the network for this application.
236 def update_allocation(self
, allocation
):
237 """Update existing allocation for this application.
239 :param int allocation: The allocation to set
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.
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
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