Merge "Fix bug #502"
[osm/N2VC.git] / modules / libjuju / 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 Deploying a Local Charm
36 -----------------------
37 To deploy a local charm, pass the charm directory path to
38 `Model.deploy()`.
39
40 .. code:: python
41
42   from juju.model import Model
43
44   model = Model()
45   await model.connect_current()
46
47   # Deploy a local charm using a path to the charm directory
48   await model.deploy(
49       '/home/tvansteenburgh/src/charms/ubuntu',
50       application_name='ubuntu',
51       series='trusty',
52   )
53
54
55 Adding Units
56 ------------
57 To add units to a deployed application, use the
58 :meth:`juju.application.Application.add_units` method. A list of the newly
59 added units (:class:`~juju.unit.Unit` objects) is returned.
60
61 .. code:: python
62
63   ubuntu_app = await model.deploy(
64       'ubuntu',
65       application_name='ubuntu',
66       series='trusty',
67       channel='stable',
68   )
69
70   unit_a, unit_b = await ubuntu_app.add_units(count=2)
71
72
73 Updating Config and Constraints
74 -------------------------------
75 Example showing how to update configuration and constraints on a deployed
76 application. The `mysql_app` object is an instance of
77 :class:`juju.application.Application`.
78
79 .. code:: python
80
81   MB = 1024 * 1024
82
83   # Update and check app config
84   await mysql_app.set_config({'tuning-level': 'fast'})
85   config = await mysql_app.get_config()
86
87   assert(config['tuning-level']['value'] == 'fast')
88
89   # update and check app constraints
90   await mysql_app.set_constraints({'mem': 512 * MB})
91   constraints = await mysql_app.get_constraints()
92
93   assert(constraints['mem'] == 512 * MB)
94
95
96 Adding and Removing Relations
97 -----------------------------
98 The :meth:`juju.application.Application.add_relation` method returns a
99 :class:`juju.relation.Relation` instance.
100
101 .. code:: python
102
103   from juju.model import Model
104
105   model = Model()
106   await model.connect_current()
107
108   # Deploy mysql-master application
109   mysql_master = await model.deploy(
110       'cs:mysql-55',
111       application_name='mysql-master',
112       series='trusty',
113       channel='stable',
114   )
115
116   # Deploy mysql-slave application
117   mysql_slave = await model.deploy(
118       'cs:mysql-55',
119       application_name='mysql-slave',
120       series='trusty',
121       channel='stable',
122   )
123
124   # Add the master-slave relation
125   relation = await mysql_master.add_relation(
126       # Name of the relation on the local (mysql-master) side
127       'master',
128       # Name of the app:relation on the remote side
129       'mysql-slave:slave',
130   )
131
132   # Remove the relation
133   await mysql_master.remove_relation(
134       'master',
135       'mysql-slave:slave',
136   )