Last active
August 29, 2015 14:14
-
-
Save 0atman/b1bb5d5e61e3a0149c60 to your computer and use it in GitHub Desktop.
juju status prettifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
concicely prints the output of `juju status`. | |
Good for `watch`ing. i.e. `watch -d juju-status` | |
""" | |
import json | |
from subprocess import Popen, PIPE, STDOUT | |
import argparse | |
import sys | |
parser = argparse.ArgumentParser( | |
description=__doc__ | |
) | |
parser.add_argument('--verbose', dest='verbose', action='store_true') | |
parser.add_argument('--quiet', dest='verbose', action='store_false') | |
parser.set_defaults(verbose=False) | |
args = parser.parse_args() | |
process = Popen( | |
['juju', 'status', '--format=json'], | |
stdout=PIPE, | |
stderr=STDOUT | |
) | |
raw_json = process.stdout.read() | |
try: | |
status = json.loads(raw_json) | |
except: | |
print raw_json | |
sys.exit(1) | |
services = status.get('services') | |
machines = status.get('machines') | |
if len(machines) <= 1: | |
print "Juju environment is bootstraped, no services deployed yet." | |
for name, service in services.iteritems(): | |
if service.get('units'): | |
units = service['units'] | |
unit_machines = ", ".join([u['machine'] for u in units.values()]) | |
num_of_units = str(len(service['units'].values())) | |
open_ports = " ".join( | |
[p.split('/')[0] for p in service['units'] | |
.values()[0] | |
.get('open-ports', [])] | |
) or "none" | |
machine = machines[service['units'].values()[0]['machine']] | |
agent_state = machine['agent-state'] | |
public_address = service['units'].values()[0].get('public-address') | |
instance_id = machine['instance-id'] | |
machine_number = units.values()[0]['machine'] | |
exposed = "[exposed]" if service['exposed'] else "" | |
unit_plural = "s" if len(units) > 1 else "" | |
relations = ", ".join(service.get('relations', {}).keys()) | |
print "{name} [{num_of_units} unit{unit_plural} on machine {unit_machines}]"\ | |
.format(**locals()) | |
print "\t{agent_state}, {public_address}{exposed}, open ports: {open_ports}"\ | |
.format(**locals()) | |
if args.verbose: | |
print "\trelations: {relations}".format(**locals()) | |
print "\tmachine {machine_number}: {instance_id}"\ | |
.format(**locals()) | |
print "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment