diff --git a/get_relation_info.py b/get_relation_info.py new file mode 100755 index 0000000..bbbc336 --- /dev/null +++ b/get_relation_info.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +import argparse + +from juju.model import Model + +from juju import jasyncio + +async def main(args): + + unit_name = args.unit_name + if args.bind_check: + bind_check = args.bind_check + + model = Model() + await model.connect_current() + + try: + app = unit_name.split('/')[0] + + juju_status = await model.get_status() + + units = model.applications[app].units + + bindings = juju_status.applications[app].endpoint_bindings + + for binding in bindings: + if ((binding is not None and "bind_check" not in locals()) or + ("bind_check" in locals() and binding == bind_check)): + for unit in units: + if unit.name == unit_name: + await _check_binding(unit, binding) + + finally: + await model.disconnect() + +async def _check_binding(unit, binding): + rel_ids = await _get_rel_ids(unit, binding) + + seperator = "============================================================" + + for rel in rel_ids: + + rel_info = await unit.run('relation-get -r {} - {}'.format(rel, unit.name)) + if 'Stdout' in rel_info.results: + print(seperator) + print('Relation info for {} - {}\n'.format(unit.name, rel)) + print(rel_info.results['Stdout']) + print(seperator) + + +async def _get_rel_ids(unit, binding): + rel_ids = [] + relation_ids = await unit.run('relation-ids {}'.format(binding)) + + if 'Stdout' in relation_ids.results: + rel_ids = relation_ids.results['Stdout'].split() + + return rel_ids + + +def _parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('-u', '--unit', metavar="", + help="Unit to run against", dest="unit_name", + required=True) + #parser.add_argument('-a', '--application', metavar="", + # help="Application to run against", + # dest="app_name") + parser.add_argument('-b', '--binding', metavar="", + help="Only check for the relational data", + dest="bind_check") + return parser.parse_args() + +if __name__ == '__main__': + jasyncio.run(main(_parse_args()))