Also updated the README.
Made a few other tweaks to make them more consistent in style.
import asyncio
import logging
+ from juju import loop
from juju.model import Model
- async def run():
+ async def deploy():
# Create a Model instance. We need to connect our Model to a Juju api
# server before we can use it.
model = Model()
# Disconnect from the api server and cleanup.
model.disconnect()
- # Stop the asyncio event loop.
- model.loop.stop()
-
def main():
# Set logging level to debug so we can see verbose output from the
ws_logger = logging.getLogger('websockets.protocol')
ws_logger.setLevel(logging.INFO)
- # Create the asyncio event loop
- loop = asyncio.get_event_loop()
-
- # Queue up our `run()` coroutine for execution
- loop.create_task(run())
-
- # Start the event loop
- loop.run_forever()
+ # Run the deploy coroutine in an asyncio event loop, using a helper
+ # that abstracts loop creation and teardown.
+ loop.run(deploy())
if __name__ == '__main__':
import asyncio
import logging
+from juju import loop
from juju.model import Model
logging.debug("Action results: %s", action.results)
-async def run():
+async def main():
model = Model()
await model.connect_current()
await model.reset(force=True)
await run_action(unit)
await model.disconnect()
- model.loop.stop()
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(False)
-loop.create_task(run())
-loop.run_forever()
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())
await model.disconnect()
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
-loop.run(main())
+ loop.run(main())
await controller.disconnect()
-loop.run(main())
+if __name__ == '__main__':
+ loop.run(main())
from juju.client.connection import Connection
from juju.client import watcher
+from juju import loop
async def watch():
allwatcher = watcher.AllWatcher()
+ conn = await Connection.connect_current()
allwatcher.connect(conn)
while True:
change = await allwatcher.Next()
print(delta.deltas)
-logging.basicConfig(level=logging.DEBUG)
-loop = asyncio.get_event_loop()
-conn = loop.run_until_complete(Connection.connect_current())
-loop.run_until_complete(watch())
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ # Run loop until the process is manually stopped (watch will loop
+ # forever).
+ loop.run(watch())
import logging
from juju.model import Model
+from juju import loop
log = logging.getLogger(__name__)
MB = 1
-async def run():
+async def main():
model = Model()
await model.connect_current()
await model.reset(force=True)
assert(constraints['mem'] == 512 * MB)
await model.disconnect()
- model.loop.stop()
-
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(False)
-loop.create_task(run())
-loop.run_forever()
+
+
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())
import logging
from juju.controller import Controller
+from juju import loop
-async def run():
+async def main():
controller = Controller()
await controller.connect_current()
model = await controller.add_model(
await model.disconnect()
await controller.destroy_model(model.info.uuid)
await controller.disconnect()
- model.loop.stop()
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(False)
-loop.create_task(run())
-loop.run_forever()
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())
await model.disconnect()
-loop.run(main())
+if __name__ == '__main__':
+ loop.run(main())
from juju.client.connection import Connection
from juju.client.client import ClientFacade
-
-
-loop = asyncio.get_event_loop()
-conn = loop.run_until_complete(Connection.connect_current())
-
+from juju import loop
async def status():
client = ClientFacade()
+ conn = await Connection.connect_current()
client.connect(conn)
patterns = None
return status
-loop.run_until_complete(status())
-loop.stop()
+if __name__ == '__main__':
+ loop.run(status())
+
import logging
from juju.model import Model
+from juju import loop
-async def run():
+async def main():
model = Model()
await model.connect_current()
await model.reset(force=True)
)
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.create_task(run())
-loop.run_forever()
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())
import asyncio
from juju.model import Model
+from juju import loop
async def report_leadership():
model = Model()
await model.disconnect()
-loop = asyncio.get_event_loop()
-loop.run_until_complete(report_leadership())
+
+if __name__ == '__main__':
+ loop.run(report_leadership())
import asyncio
from juju.model import Model
+from juju import loop
async def on_model_change(delta, old, new, model):
model.add_observer(on_model_change)
-loop = asyncio.get_event_loop()
-loop.create_task(watch_model())
-loop.run_forever()
+if __name__ == '__main__':
+ # Run loop until the process is manually stopped (watch_model will loop
+ # forever).
+ loop.run(watch_model())
import logging
from juju.model import Model
+from juju import loop
-async def run():
+async def main():
model = Model()
await model.connect_current()
)
await model.disconnect()
- model.loop.stop()
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(False)
-loop.create_task(run())
-loop.run_forever()
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())
import logging
from juju.model import Model, ModelObserver
+from juju import loop
class MyRemoveObserver(ModelObserver):
logging.debug('All units idle, disconnecting')
await model.reset(force=True)
await model.disconnect()
- model.loop.stop()
-async def run():
+async def main():
model = Model()
await model.connect_current()
print('Relation removed: {}'.format(old_rel.endpoints))
))
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(True)
-loop.create_task(run())
-loop.run_forever()
+
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())
import logging
from juju.model import Model
+from juju import loop
async def run_command(unit):
logging.debug("Action results: %s", action.results)
-async def run():
+async def main():
model = Model()
await model.connect_current()
await model.reset(force=True)
await run_command(unit)
await model.disconnect()
- model.loop.stop()
-logging.basicConfig(level=logging.DEBUG)
-ws_logger = logging.getLogger('websockets.protocol')
-ws_logger.setLevel(logging.INFO)
-loop = asyncio.get_event_loop()
-loop.set_debug(False)
-loop.create_task(run())
-loop.run_forever()
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.DEBUG)
+ ws_logger = logging.getLogger('websockets.protocol')
+ ws_logger.setLevel(logging.INFO)
+ loop.run(main())