Skip to content

Instantly share code, notes, and snippets.

@freyes
Last active February 17, 2017 17:43
Show Gist options
  • Select an option

  • Save freyes/40686eb06706e1354c6c2dfec246034e to your computer and use it in GitHub Desktop.

Select an option

Save freyes/40686eb06706e1354c6c2dfec246034e to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# example output:
# $ juju st
# unit workload agent message
# neutron-gateway/0* waiting executing Incomplete relations: network-service
# neutron-api/0* active idle Unit is ready
# nova-cloud-controller/0* waiting executing Incomplete relations: neutron-api, image, database, identity, messaging
# percona-cluster/0* active idle Unit is ready
# ceph-proxy/0* active idle Ready to proxy settings
# ceph/0* active idle Unit is ready and clustered
# ceph/1 active idle Unit is ready and clustered
# ceph/2 active idle Unit is ready and clustered
# rabbitmq-server/0* active executing Unit is ready
# glance/0* waiting executing Incomplete relations: messaging, identity, database
# keystone/0* active executing Unit is ready
# nova-compute/0* waiting idle Incomplete relations: image
# neutron-openvswitch/0* active idle Unit is ready
# nova-compute/1 waiting idle Incomplete relations: image
# neutron-openvswitch/1 active executing Unit is ready
# cinder/0* active idle Unit is ready
# cinder-ceph/0* active idle Unit is ready
#
# TODO: colorized output
import json
import subprocess
from pprint import pprint
from prettytable import PrettyTable, PLAIN_COLUMNS
table = PrettyTable()
table.field_names = ['unit', 'workload', 'agent', 'message']
table.set_style(PLAIN_COLUMNS)
table.align['unit'] = table.align['message'] = 'l'
st = subprocess.check_output(['juju', 'status', '--format', 'json'],
universal_newlines=True)
st = json.loads(st)
for app_name, app in st['applications'].items():
if 'units' not in app:
continue
for unit_id, unit in sorted(app['units'].items(), key=lambda a: a[0]):
table.add_row(["%s%s" % (unit_id, '*' if unit.get('leader') else ''),
unit['workload-status']['current'],
unit['juju-status']['current'],
unit['workload-status'].get('message', '')])
if 'subordinates' not in unit:
continue
for sub_id, subor in sorted(unit['subordinates'].items(), key=lambda a: a[0]):
table.add_row([" %s%s" % (sub_id,
'*' if subor.get('leader') else ''),
subor['workload-status']['current'],
subor['juju-status']['current'],
subor['workload-status'].get('message', '')])
print(table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment