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