01d5bc0d2d3cf22debe8f73f78c467b1699c94eb
[osm/N2VC.git] / docs / narrative / application.rst
1 Applications
2 ============
3 For api docs, see :class:`juju.application.Application`.
4
5
6 Deploying
7 ---------
8 To deploy a new application, connect a model and then call its
9 :meth:`~juju.model.Model.deploy` method. An
10 :class:`~juju.application.Application` instance is returned.
11
12 .. code:: python
13
14   from juju.model import Model
15
16   model = Model()
17   await model.connect_current()
18
19   mysql_app = await model.deploy(
20       # If a revision number is not included in the charm url,
21       # the latest revision from the Charm Store will be used.
22       'cs:mysql-55',
23       application_name='mysql',
24       series='trusty',
25       channel='stable',
26       config={
27           'tuning-level': 'safest',
28       },
29       constraints={
30           'mem': 256 * MB,
31       },
32   )
33
34
35 Adding Units
36 ------------
37 To add units to a deployed application, use the
38 :meth:`juju.application.Application.add_units` method. A list of the newly
39 added units (:class:`~juju.unit.Unit` objects) is returned.
40
41 .. code:: python
42
43   ubuntu_app = await model.deploy(
44       'ubuntu',
45       application_name='ubuntu',
46       series='trusty',
47       channel='stable',
48   )
49
50   unit_a, unit_b = await ubuntu_app.add_units(count=2)
51
52
53 Updating Config and Constraints
54 -------------------------------
55 Example showing how to update configuration and constraints on a deployed
56 application. The `mysql_app` object is an instance of
57 :class:`juju.application.Application`.
58
59 .. code:: python
60
61   MB = 1024 * 1024
62
63   # Update and check app config
64   await mysql_app.set_config({'tuning-level': 'fast'})
65   config = await mysql_app.get_config()
66
67   assert(config['tuning-level']['value'] == 'fast')
68
69   # update and check app constraints
70   await mysql_app.set_constraints({'mem': 512 * MB})
71   constraints = await mysql_app.get_constraints()
72
73   assert(constraints['mem'] == 512 * MB)
74
75
76 Adding and Removing Relations
77 -----------------------------
78 The :meth:`juju.application.Application.add_relation` method returns a
79 :class:`juju.relation.Relation` instance.
80
81 .. code:: python
82
83   from juju.model import Model
84
85   model = Model()
86   await model.connect_current()
87
88   # Deploy mysql-master application
89   mysql_master = await model.deploy(
90       'cs:mysql-55',
91       application_name='mysql-master',
92       series='trusty',
93       channel='stable',
94   )
95
96   # Deploy mysql-slave application
97   mysql_slave = await model.deploy(
98       'cs:mysql-55',
99       application_name='mysql-slave',
100       series='trusty',
101       channel='stable',
102   )
103
104   # Add the master-slave relation
105   relation = await mysql_master.add_relation(
106       # Name of the relation on the local (mysql-master) side
107       'master',
108       # Name of the app:relation on the remote side
109       'mysql-slave:slave',
110   )
111
112   # Remove the relation
113   await mysql_master.remove_relation(
114       'master',
115       'mysql-slave:slave',
116   )