Last active
September 12, 2017 22:43
-
-
Save jamesbeedy/a5816a6ecd9f64e4bb96c8ba4a153ade to your computer and use it in GitHub Desktop.
python script to show juju controller:model in bash prompt
This file contains hidden or 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 python3 | |
| # For OS X | |
| # brew install python3 | |
| # sudo -H pip3 install pyyaml | |
| import yaml | |
| import os | |
| JUJU_HOME = os.path.join(os.environ['HOME'], '.local', 'share', 'juju') | |
| JUJU_MODELS_YAML = os.path.join(JUJU_HOME, 'models.yaml') | |
| JUJU_CONTROLLERS_YAML = os.path.join(JUJU_HOME, 'controllers.yaml') | |
| def parse_yaml_file(yaml_file): | |
| try: | |
| with open(yaml_file, 'r') as f: | |
| return yaml.load(f) | |
| except (FileNotFoundError, yaml.YAMLError) as exc: | |
| return {} | |
| def get_current_controller(): | |
| """Return current juju controller | |
| """ | |
| controllers = parse_yaml_file(JUJU_CONTROLLERS_YAML) | |
| return controllers.get("current-controller", "") | |
| def get_current_model(controller): | |
| """Return current model for current controller | |
| """ | |
| try: | |
| models = parse_yaml_file(JUJU_MODELS_YAML) | |
| model = models["controllers"][controller]["current-model"] | |
| except KeyError: | |
| model = "" | |
| return model | |
| if __name__ == "__main__": | |
| controller = get_current_controller() | |
| print("%s:%s" % (controller, get_current_model(controller))) |
Author
@galgalesh thanks!
@jamesbeedy
One last fix to handle the case where controllers.yaml isn't present yet (fresh installation of Juju)
def parse_yaml_file(yaml_file):
try:
with open(yaml_file, 'r') as f:
return yaml.load(f)
except (FileNotFoundError, yaml.YAMLError) as exc:
return {}
Author
@galgalesh talk about rebuilding the wheel lol
juju whoami --format json
Author
@galgalesh can you get it cleaner than this?
function show_juju_env {
local model
local controller
model=`juju whoami --format json | jq -r '.["model"]'`
controller=`juju whoami --format json | jq -r '.["controller"]'`
printf "%s:%s" "$controller" "$model"
}
export PS1="[\[\e\[\033[01;32m\]\$(show_juju_env)\[\e[0m\]]\n${PS1}";
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This errors out after destroying a controller. Fix: