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