Skip to content

Instantly share code, notes, and snippets.

@x
Last active February 25, 2016 16:49
Show Gist options
  • Save x/36e99c2a9ba52c630880 to your computer and use it in GitHub Desktop.
Save x/36e99c2a9ba52c630880 to your computer and use it in GitHub Desktop.
.bash_chartbeat, some bash functions I wrote that make my job easier
# How To Use
# In order to use .bash_chartbeat, you need to do two things
# 1. set the PATH_TO_CHARTBEAT_REPO environment variable in your ~/.bash_profile or ~/.bash_rc
# 2. source this file in your ~/.bash_profile or ~/.bash_rc
#
# By default, OSX only sources your ~/.bash_profile when you start a new
# terminal session. Add these two things to that file.
# export PATH_TO_CHARTBEAT_REPO=$HOME/chartbeat # Or wherever your repo is
# source $PATH_TO_CHARTBEAT_REPO/.bash_chartbeat
if ! [ $PATH_TO_CHARTBEAT_REPO ]; then
echo "ERROR - Please set environment variable PATH_TO_CHARTBEAT_REPO to use functions in .bash_chartbeat"
fi
# this seems necessry for most of our scripts to run
export PYTHONPATH=$PATH_TO_CHARTBEAT_REPO:$PYTHONPATH
# get a space seperated list of server names based on the role.
# example:
# $ gsl cellarreplicaset
# cron02.chartbeat.net cron01.chartbeat.net
gsl() {
$PATH_TO_CHARTBEAT_REPO/external/tools/get_server_list.py $@
}
# get a comma seperated list of server names
# example:
# $ gslc cellarreplicaset
# cron02.chartbeat.net,cron01.chartbeat.net
gslc() {
gsl $@ | tr -s ' ' ','
}
# polysh into all servers of a type
# example:
# $ psh cron
psh() {
polysh --ssh="exec ssh -oLogLevel=Quiet -t %(host)s exec bash --noprofile" `gsl $1`
}
# polysh into all servers of a type without checking host keys
# WARNING: SUSCEPTIBLE TO MAN-IN-THE-MIDDLE ATTACKS
psh_evil() {
local hosts=`gsl $1`
polysh --ssh="exec ssh -oLogLevel=Quiet -oStrictHostKeyChecking=no -t %(host)s exec bash --noprofile" $hosts
}
# this is necesary for generate_configs
export SUPERFLY_ROOT=$PATH_TO_CHARTBEAT_REPO
# Runs both generate_configs functions
generate_configs() {
cd $PATH_TO_CHARTBEAT_REPO/private && ./generate_configs.py && cd puppet && ./generate_puppet_configs.py
}
# if you create a virtual environment called ".env" in the chartbeat repo root
# then every time you enter anywhere in the chartbeat repo, the enviornment will
# be activated. And whenever you aren't in the chartbeat root and the
# environment is active it will be deactivated.
#
# virtualenv wrapper can do something similar but I never figured it out. This
# only requires virtualenv to be installed (see: sudo pip install virtualenv)
function virtualenv_update {
local CHARTBEAT_VENV="${PATH_TO_CHARTBEAT_REPO}/.env"
# if the environment exists
if [ -d $CHARTBEAT_VENV ]; then
# if we're in the repo
if [ "${PWD:0:${#PATH_TO_CHARTBEAT_REPO}}" = $PATH_TO_CHARTBEAT_REPO ]; then
# and no virtual environment is active
if [ -z $VIRTUAL_ENV ]; then
. "${CHARTBEAT_VENV}/bin/activate"
fi
# if we're not in the repo, and a virtual environment _is active_
elif [ ! -z $VIRTUAL_ENV ]; then
# and it's the chartbeat one
if [ $VIRTUAL_ENV = $CHARTBEAT_VENV ]; then
deactivate
fi
fi
fi
}
function virtualenv_cd {
\cd $@ && virtualenv_update
}
alias cd="virtualenv_cd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment