2022-02-26 16:53:04 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
from juju.model import Model
|
|
|
|
|
|
|
|
from juju import jasyncio
|
|
|
|
|
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-02-26 16:53:04 +00:00
|
|
|
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="<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__':
|
|
|
|
jasyncio.run(main(_parse_args()))
|