Skip to content

Instantly share code, notes, and snippets.

@carlevans719
Last active December 3, 2020 14:00
Show Gist options
  • Save carlevans719/ccda6fa265838aec15b728545822e51e to your computer and use it in GitHub Desktop.
Save carlevans719/ccda6fa265838aec15b728545822e51e to your computer and use it in GitHub Desktop.
Bash Aliases / helper functions
# Bash helpers
alias '...'='cd ../..'
alias '....'='cd ../../..'
alias '.....'='cd ../../../..'
function announce () {
printf "\n###### $1 ######\n\n"
}
# SSH helpers
alias sshtun='ssh -D 1337 -f -C -q -N user@host -p port'
# Git helpers
ga () {
announce "Running git add -A $@"
git add -A
}
gs () {
announce "Running git status"
git status
}
gc () {
announce "Running git commit -m $1"
git commit -m "$1"
}
gl () {
announce "Running git log --pretty=oneline"
git log --pretty=oneline
}
gd () {
announce "Running git diff $@"
git diff $@
}
gr () {
announce "Running git reset $@"
git reset $@
}
gpl () {
if [ $# -gt 0 ]; then
local BRANCH=$1
shift
fi
if [ -n $BRANCH ]; then
announce "Running git pull origin ${BRANCH:-master} $@"
git pull origin ${BRANCH:-master} $@
else
announce "Running git pull $@"
git pull $@
fi
}
gpu () {
if [ $# -gt 0 ]; then
local BRANCH=$1
shift
fi
if [ -n $BRANCH ]; then
announce "Running git push origin ${BRANCH:-master} $@"
git push origin ${BRANCH:-master} $@
else
announce "Running git push $@"
git push $@
fi
}
gst () {
announce "Running git stash $@"
git stash $@
}
gplr () {
if [ $# -gt 0 ]; then
local BRANCH=$1
shift
fi
if [ -n $BRANCH ]; then
announce "Running git pull --rebase origin ${BRANCH:-master} $@"
git pull --rebase origin ${BRANCH:-master} $@
else
announce "Running git pull --rebase $@"
git pull --rebase $@
fi
}
# Kubectl helpers
ksn () {
announce "Running kubectl config set-context --current --namespace=\"$@\""
kubectl config set-context --current --namespace=$@
}
kgp () {
announce "Running kubectl get pods $@"
kubectl get pods $@
}
kgs () {
announce "Running kubectl get services $@"
kubectl get services $@
}
kcg () {
announce "Running kubectl config get-contexts $@"
kubectl config get-contexts $@
}
kcs () {
announce "Running kubectl config use-context $@"
kubectl config use-context $@
}
kl () {
announce "Running kubectl logs -f $@"
kubectl logs -f $@
}
ke () {
POD=`shift`
announce "Running kubectl exec -it $POD $@"
kubectl exec -it $POD $@
}
ka () {
announce "Running kubectl apply -f $@"
kubectl apply -f $@
}
kd () {
announce "Running kubectl delete -f $@"
kubectl delete -f $@
}
kdep () {
local OPTIND opt NAMESPACE IMAGE CMD
while getopts ":hn:i:c:" opt; do
case "${opt}" in
n)
NAMESPACE=$OPTARG
[ -z "$NAMESPACE" ] && echo "Invalid value for -n" && exit 1
;;
i)
IMAGE=$OPTARG
[ -z "$IMAGE" ] && echo "Invalid value for -i" && exit 1
;;
c)
CMD=$OPTARG
[ -z "$CMD" ] && echo "Invalid value for -c" && exit 1
;;
h)
echo "Deploy a bastion pod to kubernetes, exec into it, then remove it again."
echo
echo "Usage: kdep [-h|-n|-c|-i]"
echo
echo "-h show this help message"
echo "-n=namespace [default]"
echo "-i=docker_image [byrnedo/alpine-curl]"
echo "-c=command_to_run [sh]"
return 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
esac
done
announce "Deploying bastion pod to Kubernetes. Namespace: $NAMESPACE. Image: $IMAGE. Cmd: $CMD."
printf "apiVersion: v1\nkind: Pod\nmetadata:\n name: bastion-pod\n namespace: ${NAMESPACE:-default}\nspec:\n containers:\n - image: ${IMAGE:-byrnedo/alpine-curl}\n command:\n - sleep\n - \"3600\"\n imagePullPolicy: IfNotPresent\n name: bastion-pod\n restartPolicy: Always" | kubectl create -f -
sleep 10
kubectl exec -it --namespace ${NAMESPACE:-default} bastion-pod ${CMD:-sh}
kubectl delete pod --namespace ${NAMESPACE:-default} bastion-pod
}
kenv () {
local OPTIND opt NAMESPACE DEPLOYMENT ENVVAR
help () {
echo "Fetch the env vars from a running pod"
echo
echo "Usage: kenv [-h|-n|-d|-e]"
echo
echo "-h show this help message"
echo "-n=namespace [default]"
echo "-d=deployment_name"
echo "-e=env_var_filter"
}
while getopts ":hn:d:e:" opt; do
case "${opt}" in
n)
NAMESPACE=$OPTARG
[ -z "$NAMESPACE" ] && echo "Invalid value for -n" && return 1
;;
d)
DEPLOYMENT=$OPTARG
[ -z "$DEPLOYMENT" ] && echo "Invalid value for -d" && return 1
;;
e)
ENVVAR=$OPTARG
[ -z "$ENVVAR" ] && echo "Invalid value for -e" && return 1
;;
h)
help
return 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
esac
done
if [ -z "$DEPLOYMENT" ]; then
echo "ERROR: Must provide -d option to specify the deployment name"
echo ""
help
return 1
fi
local POD_NAME=`kubectl get pods -n ${NAMESPACE:-default} | grep ${DEPLOYMENT} | awk '{print $1}' | tail -n 1`
if [ -z "$POD_NAME" ] || [ $POD_NAME == "No resources found." ]; then
return 1
fi
if [ -z "$NAMESPACE" ]; then
NAMESPACE=default
fi
local ENVS=`kubectl exec -it -n $NAMESPACE ${POD_NAME} env`
if [ -n "$ENVVAR" ]; then
ENVS=`echo "$ENVS" | grep ${ENVVAR}`
fi
echo "$ENVS"
}
# Docker helpers
dkillall () {
announce "Killing all running docker containers"
local RUNNING_CONTAINERS=$(docker ps -q)
[ -z "$RUNNING_CONTAINERS" ] && return 0
docker kill "$RUNNING_CONTAINERS"
}
dcleanc () {
announce "Removing all stopped docker containers"
local STOPPED_CONTAINERS=$(docker ps -a -q)
[ -z "$STOPPED_CONTAINERS" ] && return 0
docker rm "$STOPPED_CONTAINERS"
}
dcleani () {
announce "Removing untagged docker images"
local UNTAGGED_IMAGES=$(docker images -q -f dangling=true)
[ -z "$UNTAGGED_IMAGES" ] && return 0
docker rmi "$UNTAGGED_IMAGES"
}
dcleanv () {
announce "Removing docker volumes"
local VOLUMES=$(docker volume ls -q)
[ -z "$VOLUMES" ] && return 0
docker volume rm "$VOLUMES"
}
dclean () {
dcleanc
dcleani
dcleanv
}
shortcuts () {
echo "Bash helpers
============
... | move up 2 folders
.... | move up 3 folders
announce | print a message
SSH helpers
===========
sshtun | tunnel port 1337 through the RapidSwitch server
Git helpers
===========
ga | git add -A
gs | git status
gc | git commit -m
gl | git log
gpl | git pull
gpu | git push
gst | git stash
gplr | git pull --rebase
Kubectl helpers
===============
ksn | kubectl config set-context --current --namespace=
kgp | kubectl get pods
kgs | kubectl get services
kcg | kubectl config get
kcs | kubectl config use
kl | kubectl logs -f
ke | kubectl exec
ka | kubectl apply
kd | kubectl delete
kdep | deploy bastion. use -h
kenv | Get env vars from deployment. use -h
Docker helpers
==============
dkillall | kill running containers
dcleanc | remove stopped containers
dcleani | remove untagged images
dcleanv | remove volumes
dclean | run all the above
"
}
alias john='/home/carl/john/john'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment