Skip to content

Add Eucalyptus Account and User Names to Your BASH Prompt

November 14, 2012

At Eucalyptus, IT tends to have a bunch of different accounts for the clouds that we run. Most of the work we do with these clouds happens from the Cloud Controller of each cloud and it can become confusing determining which user’s credentials are currently sourced when you have 10 accounts and 15 users. We need a method for easily determining which account and user we are currently accessing the cloud as so that we don’t launch instances or create keypairs using the incorrect user as then we’ll need to do it again and clean up the mistake as well (i.e. doing twice the work).

So, one method that we’ve come up with is to put the account and user name of the currently sourced credentials directly into the BASH prompt. Now there is little doubt as to which user’s credentials we are currently using.

The following function will print out (<account>:<user>) for the currently source Eucalyptus credentials:

parse_euca_user() {
  if [[ ! -z ${EC2_USER_ID} ]]; then
    if [[ ! -e ${HOME}/.my_ec2_user_id || $(cat ${HOME}/.my_ec2_user_id | cut -d' ' -f1) -ne ${EC2_USER_ID} ]]; then
      EC2_ACCOUNT_NAME=$(euare-accountlist | grep $EC2_USER_ID | cut -d' ' -f1)
      EC2_USER_NAME=$(basename $EC2_CERT | cut -d'-' -f2)
      echo ${EC2_USER_ID} ${EC2_ACCOUNT_NAME} >${HOME}/.my_ec2_user_id
      EC2_ACCOUNT_NAME=$(cat $HOME/.my_ec2_user_id | cut -d' ' -f2)
      EC2_USER_NAME=$(basename $EC2_CERT | cut -d'-' -f2)

  if [[ ! -z ${EC2_ACCOUNT_NAME} ]]; then
    echo "(${EC2_ACCOUNT_NAME}:${EC2_USER_NAME})"
    echo ""

Add the above function to your ${HOME}/.bashrc file. Next update your prompt by adding a call to the parse_euca_user() function in it such as the following:

export PS1="\u@\h:\W \$(parse_euca_user)\$ "

Now you should see something like the following after running source eucarc on the eucalyptus account’s admin user.

root@prod-frontend:admin (eucalyptus:admin)$

From → Cloud, Eucalyptus

  1. Great post!

  2. Nice, I will definitely use this. Thanks for the idea.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: