Current Juju Environment Name in Bash Prompt

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:

# 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:

[local] user@host:~$
# when "local" is bootstrapped:
[local] user@host:~$

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 :/

Share

4 Comments on “Current Juju Environment Name in Bash Prompt

  1. Neat. Does this correctly detect the edge case when there is a current-environment file but it has been overridden by setting the $JUJU_ENV?

  2. Line 15 has a missing $ before the {

    Good work!

    I added ansi colors to mine to indicate bootstrapped or not
    [shell theme=”Classic”]
    if [[ -e “$currentEnvJenv” ]]; then
    printf “[\e[38;5;70m%s\e[0m] ” “$currentEnv”
    else
    printf “[\e[38;5;88m%s\e[0m] ” “$currentEnv”
    fi
    [/shell]

    • Thanks Simon!

      I’ve updated the snippet to include ANSI colors, although green and gray, rather than red (it reads better on my semi-translucent deep purple terminal background).

      I also fixed the missing $ – the plain text code was correct, but it turned out the Crayon syntax highlighter plugin somehow interpreted /$${currentEnv} specially and dropped the $ from the displayed text (it is even doing it now – I had to edit this comment a few times).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.