useful_scripts/get_relation_info.py

83 lines
2.3 KiB
Python
Raw Normal View History

2022-02-26 16:53:04 +00:00
#!/usr/bin/env python3
import argparse
2022-12-15 12:04:25 +00:00
import asyncio
2022-02-26 16:53:04 +00:00
from juju.model import Model
2022-11-03 22:50:11 +00:00
2022-02-26 16:53:04 +00:00
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()
2022-11-03 22:50:11 +00:00
2022-02-26 16:53:04 +00:00
async def _check_binding(unit, binding):
rel_ids = await _get_rel_ids(unit, binding)
seperator = "============================================================"
for rel in rel_ids:
2022-11-03 22:50:11 +00:00
rel_info = await unit.run('relation-get -r {} - {}'.format(
rel, unit.name))
2022-12-15 12:04:25 +00:00
await rel_info.fetch_output()
if 'stdout' in rel_info.results:
2022-02-26 16:53:04 +00:00
print(seperator)
print('Relation info for {} - {}\n'.format(unit.name, rel))
2022-12-15 12:04:25 +00:00
print(rel_info.results['stdout'])
2022-02-26 16:53:04 +00:00
print(seperator)
async def _get_rel_ids(unit, binding):
rel_ids = []
2022-12-15 12:04:25 +00:00
2022-02-26 16:53:04 +00:00
relation_ids = await unit.run('relation-ids {}'.format(binding))
2022-12-15 12:04:25 +00:00
await relation_ids.fetch_output()
2022-02-26 16:53:04 +00:00
2022-12-15 12:04:25 +00:00
if 'stdout' in relation_ids.results:
rel_ids = relation_ids.results['stdout'].split()
2022-02-26 16:53:04 +00:00
return rel_ids
def _parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--unit', metavar="<unit>",
help="Unit to run against", dest="unit_name",
required=True)
2022-11-03 22:50:11 +00:00
# parser.add_argument('-a', '--application', metavar="<application>",
2022-02-26 16:53:04 +00:00
# help="Application to run against",
# dest="app_name")
parser.add_argument('-b', '--binding', metavar="<binding>",
help="Only check <binding> for the relational data",
dest="bind_check")
return parser.parse_args()
2022-11-03 22:50:11 +00:00
2022-02-26 16:53:04 +00:00
if __name__ == '__main__':
2022-12-15 12:04:25 +00:00
asyncio.run(main(_parse_args()))