Having to test code in multiple Juju providers one can forget which environment you’re currently working on. So yesterday I came up with this little snippet in my $HOME/.bashrc file:

.bashrc snippet # Print the current juju environment name in the prompt function show_juju_env { local jujuHome local currentEnv [[ -n "$JUJU_HOME" ]] && jujuHome="${JUJU_HOME}" || jujuHome="${HOME}/.juju" if [[ -n "$JUJU_ENV" ]]; then currentEnv="${JUJU_ENV}" else local envFile="${jujuHome}/current-environment" if [[ ! -e "$envFile" ]]; then echo `juju switch` > "${envFile}" fi currentEnv=`cat $envFile` fi local currentEnvJenv="${jujuHome}/environments/$currentEnv.jenv" if [[ -e "$currentEnvJenv" ]]; then printf "[\e[38;5;70m%s\e[0m] " "$currentEnv" else printf "[\e[38;5;7m%s\e[0m] " "$currentEnv" fi } export PS1="\$(show_juju_env)${PS1}"; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # Print the current juju environment name in the prompt function show_juju_env { local jujuHome local currentEnv [ [ - n "$JUJU_HOME" ] ] && jujuHome = "${JUJU_HOME}" || jujuHome = "${HOME}/.juju" if [ [ - n "$JUJU_ENV" ] ] ; then currentEnv = "${JUJU_ENV}" else local envFile = "${jujuHome}/current-environment" if [ [ ! - e "$envFile" ] ] ; then echo ` juju switch ` > "${envFile}" fi currentEnv = ` cat $envFile ` fi local currentEnvJenv = "${jujuHome}/environments/$currentEnv.jenv" if [ [ - e "$currentEnvJenv" ] ] ; then printf "[\e[38;5;70m%s\e[0m] " "$currentEnv" else printf "[\e[38;5;7m%s\e[0m] " "$currentEnv" fi } export PS1 = "\$(show_juju_env)${PS1}" ;

This gives you a nice prompt like this:

It assumes you have your Juju home directory in $HOME/.juju or set using the $JUJU_HOME environment variable. Then it checks whether $JUJU_ENV is set and uses that. Otherwise, it looks for the current-environment file juju switch command creates. Finally, as a fallback it calls calls juju switch .

A nice feature is that you’ll see local in light gray if the environment is not bootstrapped, or in green othewise (thanks for the tip Simon Davy and for the bug fix!). You can tell whether it’s bootstrapped by the existence of a $JUJU_HOME/environments/<name>.jenv file, which is created after bootstrap.

Now I just need to set up AWS billing alert on my Amazon account, so that next time I forget to destroy a live testing environment on EC2, the bill won’t be outrageous :/