Skip to content

Instantly share code, notes, and snippets.

@mitechie
Last active May 25, 2017 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mitechie/3076bd165611c5afea7f06ebea534280 to your computer and use it in GitHub Desktop.
Save mitechie/3076bd165611c5afea7f06ebea534280 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
### Loops through your controllers and outputs the model version for each
### model on each controller.
### JAAS NOTE: if you're not actively logged into JAAS the script will fail
### until you auth. Rerun after authenticating and it should work on the
### second go.
import json
from subprocess import check_output
import sys
status_cmd = "juju status -m {}:{} --format=json"
res = check_output(['juju controllers --format=json'], shell=True)
if res:
controllers_json = res
else:
print("juju controllers --format=json failed.")
sys.exit(1)
controllers = json.loads(res.decode('utf-8'))
for controller in controllers['controllers']:
print("Controller: {}".format(controller))
res = check_output(['juju models -c {} --format=json'.format(controller)],
shell=True)
if res:
models_json = res
else:
print("juju models for the controller failed.")
sys.exit(1)
models = json.loads(models_json.decode('utf-8'))
for model in models['models']:
if 'external' in model['owner']:
model_name = "{}/{}".format(model['owner'], model['name'])
else:
model_name = model['name']
res = check_output([status_cmd.format(controller, model_name)],
shell=True)
if res:
status_json = json.loads(res.decode('utf-8'))
else:
print("juju status for the model failed.")
sys.exit(1)
print(" Model {} - {}".format(model['name'],
status_json['model']['version']))
@jrwren
Copy link

jrwren commented May 25, 2017

Traceback (most recent call last):
File "check-juju-version.py", line 24, in
controllers = json.loads(controllers_json)
File "/usr/lib/python3.5/json/init.py", line 312, in loads
s.class.name))
TypeError: the JSON object must be str, not 'bytes'

@jcsackett
Copy link

jc@alice ~> python check-juju-version.py
Traceback (most recent call last):
File "check-juju-version.py", line 7, in
from subprocess import (
ImportError: cannot import name run

@jcsackett
Copy link

Ignore that; I ran it via python rather than just executing, missed the py3 req.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment