Skip to content

Instantly share code, notes, and snippets.

@mjs
Created July 27, 2017 05:32
Show Gist options
  • Save mjs/484dd863dd6bc593bb2690f7450d7cc6 to your computer and use it in GitHub Desktop.
Save mjs/484dd863dd6bc593bb2690f7450d7cc6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3.5
import asyncio
import logging
from juju import loop
from juju.model import Model, ModelObserver
class ModelWatcher(ModelObserver):
async def on_machine_add(self, delta, old, new, model):
self._print("ADD", delta.data)
async def on_machine_change(self, delta, old, new, model):
self._print("CHANGE", delta.data)
def _print(self, label, data):
machine_id = data["id"]
status = data.get("instance-status", {})
print("machine {} {}: {} ({})".format(
machine_id,
label,
status["current"],
status["message"]))
async def setup():
model = Model()
await model.connect_current()
model.add_observer(ModelWatcher())
logging.basicConfig(level=logging.WARNING)
loop = asyncio.get_event_loop()
loop.run_until_complete(setup())
loop.run_forever()
@mjs
Copy link
Author

mjs commented Jul 27, 2017

Example output:

$ python allwatcher-client.py
machine 16 ADD: pending ()
machine 16 CHANGE: pending ()
machine 16 CHANGE: running (container started)
machine 16 CHANGE: pending ()
machine 16 CHANGE: running (Running)
machine 16 CHANGE: pending ()
machine 16 CHANGE: pending ()
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)
machine 16 CHANGE: running (Running)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment