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 service.
41 :param str local_relation: Name of relation on this service
42 :param str remote_relation: Name of relation on the other service in
43 the form '<service>[:<relation_name>]'
48 async def add_unit(self
, count
=1, to
=None):
49 """Add one or more units to this service.
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 service.
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 service.
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 service.
104 def destroy_relation(self
, local_relation
, remote_relation
):
105 """Remove a relation to another service.
107 :param str local_relation: Name of relation on this service
108 :param str remote_relation: Name of relation on the other service in
109 the form '<service>[:<relation_name>]'
113 remove_relation
= destroy_relation
115 async def destroy(self
):
116 """Remove this service 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 service 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 service.
146 def get_constraints(self
):
147 """Return the machine constraints for this service.
152 def get_actions(self
, schema
=False):
153 """Get actions defined for this service.
155 :param bool schema: Return the full action schema
160 def get_resources(self
, details
=False):
161 """Return resources for this service.
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 service.
172 :param str command: The command to run
173 :param int timeout: Time to wait before command is considered failed
178 def set_config(self
, to_default
=False, **config
):
179 """Set configuration options for this service.
181 :param bool to_default: Set service options to default values
182 :param \*\*config: Config key/values
187 def set_constraints(self
, constraints
):
188 """Set machine constraints for this service.
190 :param :class:`juju.Constraints` constraints: Machine constraints
195 def set_meter_status(self
, status
, info
=None):
196 """Set the meter status on this status.
198 :param str status: Meter status, e.g. 'RED', 'AMBER'
199 :param str info: Extra info message
204 def set_plan(self
, plan_name
):
205 """Set the plan for this service, effective immediately.
207 :param str plan_name: Name of plan
213 """Remove public availability over the network for this service.
218 def update_allocation(self
, allocation
):
219 """Update existing allocation for this service.
221 :param int allocation: The allocation to set
227 self
, channel
=None, force_series
=False, force_units
=False,
228 path
=None, resources
=None, revision
=-1, switch
=None):
229 """Upgrade the charm for this service.
231 :param str channel: Channel to use when getting the charm from the
232 charm store, e.g. 'development'
233 :param bool force_series: Upgrade even if series of deployed service
234 is not supported by the new charm
235 :param bool force_units: Upgrade all units immediately, even if in
237 :param str path: Uprade to a charm located at path
238 :param dict resources: Dictionary of resource name/filepath pairs
239 :param int revision: Explicit upgrade revision
240 :param str switch: Crossgrade charm url