Merge commit '19031b24b523c872c1ac367821dc60c950a09755' as 'modules/libjuju'
[osm/N2VC.git] / modules / libjuju / examples / unitrun.py
diff --git a/modules/libjuju/examples/unitrun.py b/modules/libjuju/examples/unitrun.py
new file mode 100644 (file)
index 0000000..3dfacd6
--- /dev/null
@@ -0,0 +1,47 @@
+"""
+This example:
+
+1. Connects to current model and resets it.
+2. Deploys one ubuntu unit.
+3. Runs an action against the unit.
+4. Waits for the action results to come back, then exits.
+
+"""
+import asyncio
+import logging
+
+from juju.model import Model
+from juju import loop
+
+
+async def run_command(unit):
+    logging.debug('Running command on unit %s', unit.name)
+
+    # 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 main():
+    model = Model()
+    await model.connect_current()
+    await model.reset(force=True)
+
+    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()
+
+
+if __name__ == '__main__':
+    logging.basicConfig(level=logging.DEBUG)
+    ws_logger = logging.getLogger('websockets.protocol')
+    ws_logger.setLevel(logging.INFO)
+    loop.run(main())