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