""" This example shows how to reconnect to a model if you encounter an error 1. Connects to current model. 2. Attempts to get an application that doesn't exist. 3. Disconnect then reconnect. """ from juju import jasyncio from juju.model import Model from juju.errors import JujuEntityNotFoundError async def main(): model = Model() retryCount = 3 for i in range(0, retryCount): await model.connect_current() try: model.applications['foo'].relations except JujuEntityNotFoundError as e: print(e.entity_name) finally: await model.disconnect() # Everything worked out, continue on wards. if __name__ == '__main__': jasyncio.run(main())
async def test_run(self): assert jasyncio.get_running_loop() == self.loop async def _test(): return 'success' self.assertEqual(jasyncio.run(_test()), 'success')
from juju import jasyncio from juju.controller import Controller async def watch(): controller = Controller() # connect to current # controller with current user, per Juju CLI await controller.connect() # Need to call the WatchModelSummaries or WatchAllModelSummaries on the # controller. def callback(summary): print("-- change --\n{}\n".format(summary)) await controller.watch_model_summaries(callback) while True: await asyncio.sleep(1) if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) ws_logger = logging.getLogger('websockets.protocol') ws_logger.setLevel(logging.INFO) logging.getLogger('juju.client.connection').setLevel(logging.INFO) # Run loop until the process is manually stopped (watch will loop # forever). jasyncio.run(watch())
""" This example: 1. Connects to the current model. 2. Prints out leadership status for all deployed units in the model. 3. Cleanly disconnects. """ from juju import jasyncio from juju.model import Model async def report_leadership(): model = Model() await model.connect() print("Leadership: ") for app in model.applications.values(): for unit in app.units: print("{}: {}".format(unit.name, await unit.is_leader_from_status())) await model.disconnect() if __name__ == '__main__': jasyncio.run(report_leadership())
credential_name=credential_name) # verify credential print("Verify model's credential: {}".format( model.info.cloud_credential_tag)) # verify we can deploy print('Deploying ubuntu') app = await model.deploy('cs:ubuntu-10') print('Waiting for active') await model.block_until(lambda: app.units and all( unit.workload_status == 'active' for unit in app.units)) print('Removing ubuntu') await app.remove() finally: print('Cleaning up') if model: print('Removing model') model_uuid = model.info.uuid await model.disconnect() await controller.destroy_model(model_uuid) print('Disconnecting') await controller.disconnect() if __name__ == '__main__': assert len(sys.argv) > 2, 'Please provide a cloud and credential name' jasyncio.run(main(sys.argv[1], sys.argv[2]))
from juju import jasyncio from juju.model import Model async def status(): model = Model() await model.connect() status = await model.get_status() await model.disconnect() print('Applications:', list(status.applications.keys())) print('Machines:', list(status.machines.keys())) print('Relations:', status.relations) return status if __name__ == '__main__': jasyncio.run(status())