Add new relation python script

This commit is contained in:
Arif Ali 2022-02-26 16:53:04 +00:00
parent a90392cc89
commit 5051513034
Signed by: arif
GPG Key ID: 369608FBA1353A70

76
get_relation_info.py Executable file
View File

@ -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="<unit>",
help="Unit to run against", dest="unit_name",
required=True)
#parser.add_argument('-a', '--application', metavar="<application>",
# 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()
if __name__ == '__main__':
jasyncio.run(main(_parse_args()))