.. code:: python
- ubuntu_app = await model.deploy(
- 'ubuntu',
- application_name='ubuntu',
- series='trusty',
- channel='stable',
- )
-
- unit_a, unit_b = await ubuntu_app.add_units(count=2)
+ ubuntu_app = await model.deploy(
+ 'ubuntu',
+ application_name='ubuntu',
+ series='trusty',
+ channel='stable',
+ )
+
+ unit_a, unit_b = await ubuntu_app.add_units(count=2)
Updating Config and Constraints
controller
model
application
+ unit
--- /dev/null
+Units
+=====
+
+Running Commands
+----------------
+Run arbitrary commands on a unit with the
+:meth:`juju.unit.Unit.run` method. This method blocks
+the current coroutine until a result is available, and
+returns a :class:`juju.action.Action` instance.
+
+
+.. code:: python
+
+ from juju.model import Model
+
+ model = Model()
+ await model.connect_current()
+
+ ubuntu_app = await model.deploy(
+ 'ubuntu',
+ application_name='ubuntu',
+ series='trusty',
+ channel='stable',
+ )
+
+ for unit in app.units:
+ action = await unit.run('unit-get public-address')
+ print(action.results)
+
+ action = await unit.run('uname -a')
+ print(action.results)
+
+
import asyncio
import logging
-from juju.model import Model, ModelObserver
+from juju.model import Model
-async def run_stuff_on_unit(unit):
- print('Running command on unit', unit.name)
-
- # unit.run() returns a client.ActionResults instance
- action = await unit.run('unit-get public-address')
-
- print("Action results: {}".format(action.results))
-
- # Inform asyncio that we're done.
- await unit.model.disconnect()
- unit.model.loop.stop()
+async def run_command(unit):
+ logging.debug('Running command on unit %s', unit.name)
-class MyModelObserver(ModelObserver):
- async def on_unit_add(self, delta, old, new, model):
- loop.create_task(run_stuff_on_unit(new))
+ # unit.run() returns a juju.action.Action instance
+ action = await unit.run('unit-get public-address')
+ logging.debug("Action results: %s", action.results)
async def run():
model = Model()
await model.connect_current()
await model.reset(force=True)
- model.add_observer(MyModelObserver())
- await model.deploy(
+ app = await model.deploy(
'ubuntu-0',
application_name='ubuntu',
series='trusty',
channel='stable',
)
+ for unit in app.units:
+ await run_command(unit)
+
+ await model.disconnect()
+ model.loop.stop()
+
logging.basicConfig(level=logging.DEBUG)
ws_logger = logging.getLogger('websockets.protocol')
:param str command: The command to run
:param int timeout: Time to wait before command is considered failed
+ :returns: A :class:`juju.action.Action` instance.
Returns a tuple containing the stdout, stderr, and return code
from the command.
:param str action_name: Name of action to run
:param \*\*params: Action parameters
- :returns: An `juju.action.Action` instance.
+ :returns: A :class:`juju.action.Action` instance.
Note that this only enqueues the action. You will need to call
``action.wait()`` on the resulting `Action` instance if you wish
to block until the action is complete.
+
"""
action_facade = client.ActionFacade()
action_facade.connect(self.connection)