Add action example and docs
[osm/N2VC.git] / docs / narrative / unit.rst
1 Units
2 =====
3
4 Running Commands
5 ----------------
6 Run arbitrary commands on a unit with the
7 :meth:`juju.unit.Unit.run` method. This method blocks
8 the current coroutine until a result is available, and
9 returns a :class:`juju.action.Action` instance.
10
11
12 .. code:: python
13
14   from juju.model import Model
15
16   model = Model()
17   await model.connect_current()
18
19   app = await model.deploy(
20       'ubuntu',
21       application_name='ubuntu',
22       series='trusty',
23       channel='stable',
24   )
25
26   for unit in app.units:
27       action = await unit.run('unit-get public-address')
28       print(action.results)
29
30       action = await unit.run('uname -a')
31       print(action.results)
32
33
34 Running Actions
35 ---------------
36 Run actions on a unit with the
37 :meth:`juju.unit.Unit.run_action` method. This method
38 returns a :class:`juju.action.Action` instance immediately. To block until
39 the action completes, use the :meth:`juju.action.Action.wait` method, as
40 in the example below.
41
42
43 .. code:: python
44
45   from juju.model import Model
46
47   model = Model()
48   await model.connect_current()
49
50   app = await model.deploy(
51       'git',
52       application_name='git',
53       series='trusty',
54       channel='stable',
55   )
56
57   for unit in app.units:
58       # run the 'add-repo' action, passing a 'repo' param
59       action = await unit.run_action('add-repo', repo='myrepo')
60       # wait for the action to complete
61       action = await action.wait()
62
63       print(action.results)