Skip to content

Instantly share code, notes, and snippets.

@yogeek
Created March 14, 2019 09:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogeek/c68041410bd4c084f93ce9aec04b2056 to your computer and use it in GitHub Desktop.
Save yogeek/c68041410bd4c084f93ce9aec04b2056 to your computer and use it in GitHub Desktop.
ALIASES
# Personnal aliases
#
######
# Go
######
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
####################
# K8S
####################
# Update kubectl
function kubectl-up() {
echo "Updating kubectl to $(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt) version..."
sudo curl -sSL https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl > /usr/bin/kubectl
sudo chmod +x /usr/bin/kubectl
echo "Done !"
kubectl version -o json
}
funcion kubeops() {
echo "Searching kube config..."
if [ -z "$1" ]; then
if [[ -e $KUBECONFIG ]]; then
echo "No argument => loading current KUBECONFIG ($KUBECONFIG) !"
CONFIGFILE=$(basename $KUBECONFIG)
else
echo "No KUBECONFIG : Either set KUBECONFIG env var or pass a kubeconfig name as argument"
echo ""
echo "Usage: ${0} <kube-config-name> "
echo ""
echo "------------------------------------"
echo "List of available kube config :"
echo "------------------------------------"
for i in $(find ~/.kube/ -maxdepth 1 -name "*config*"); do echo $(basename $i); done
return
fi
else
CONFIGFILE=${1}
fi
echo "Using kube config : ${CONFIGFILE}..."
docker run -it --net=host -v ${HOME}:${HOME} hjacobs/kube-ops-view --kubeconfig-path=${HOME}/.kube/${CONFIGFILE}
}
function k() {
kubectl "$@"
}
function kk() {
[[ -z KUBECONFIG ]] && export KUBECONFIG=~/.kube/config
export BULLETTRAIN_KCTX_KCONFIG=$KUBECONFIG
}
# kubefwd
function kf() {
# Get current context
ctx="$(kubectl config view -o=jsonpath='{.current-context}')"
# Get namespace
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${ctx}\")].context.namespace}")"
echo "#################################################################"
echo "Forwarding services from $ns namespace in $ctx context..."
echo "#################################################################"
# Forward services of the current namespace
sudo kubefwd services -c $KUBECONFIG -n $ns
}
function kubespy_install() {
KUBESPY_VERSION=${1:-v0.4.0}
curl -sSL https://github.com/pulumi/kubespy/releases/download/${KUBESPY_VERSION}/kubespy-linux-amd64.tar.gz | sudo tar -xzf - -C /usr/local/bin/ --strip-components=2 releases/kubespy-linux-amd64/kubespy && sudo chmod +x /usr/local/bin/kubespy && kubespy version
}
# Docker
alias portainer='docker run -d -p 9001:9000 --name portainer -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer'
alias portainer_stop='docker rm -f portainer'
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
# Reload and Edit this file
alias ra='. ~/.zshrc'
alias ea='code ~/.zsh_aliases'
function mk_start() {
export MINIKUBE_WANTUPDATENOTIFICATION=false
export MINIKUBE_WANTREPORTERRORPROMPT=false
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true
mkdir -p $HOME/.kube
mkdir -p $HOME/.minikube
touch $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
sudo -E minikube start --vm-driver=none
# this for loop waits until kubectl can access the api server that Minikube has created
for i in {1..150}; do # timeout for 5 minutes
kubectl get po &> /dev/null
if [ $? -ne 1 ]; then
break
fi
sleep 2
done
echo "Deleting kube-dns... (https://github.com/kubernetes/minikube/issues/3233)"
kubectl delete deployment kube-dns --namespace kube-system
}
# Minikube
alias minikube_start='mk_start'
# alias minikube_start='sudo minikube start --vm-driver=none && sudo chown -R guillaume ~/.minikube'
alias minikube_update='curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo cp minikube /usr/local/bin/ && rm minikube'
# VPN aliases
alias vpn-up='sudo systemctl stop cntlm redsocks && sudo wg-quick up wg0'
alias vpn-status='sudo wg'
alias vpn-down='sudo wg-quick down wg0 && sudo systemctl start cntlm redsocks'
####################
# Proxy
####################
DEFAULT="\e[39m"
GREEN="\e[32m"
RED="\e[31m"
export CORPORATE_URL="http://<CORPORATE_TEST_URL>"
# Check internet access, fail after 15s
function check_internet_connection() {
[[ "$(curl -m 5 -L -s -o /dev/null -I -w '%{http_code}' http://www.google.fr)" == "200" ]] && { echo -e "${GREEN}OK"; return 0 } || { echo -e "${RED}KO"; return 1 }
}
# Check corporate access, fail after 15s
function check_corporate_connection() {
[[ "$(curl -m 5 -L -s -o /dev/null -I -w '%{http_code}' $CORPORATE_URL)" == "200" ]] && { echo -e "${GREEN}OK"; return 0 } || { echo -e "${RED}KO"; return 1 }
}
# Check DNS
function check_dns() {
dig +short www.google.fr +time=5 +tries=3 > /dev/null && { echo -e "${GREEN}OK"; return 0 } || { echo -e "${RED}KO"; return 1 }
}
# Network healthcheck
function proxy_status() {
echo -en "${DEFAULT}CNTLM : ${GREEN}"; sudo systemctl is-active cntlm.service
echo -en "${DEFAULT}REDSOCKS : ${GREEN}"; sudo systemctl is-active redsocks.service
echo -en "${DEFAULT}INTERNET CONNECTION : "; check_internet_connection
echo -en "${DEFAULT}CORPORATE_CONNECTION : "; check_corporate_connection
echo -en "${DEFAULT}DNS_CONNECTION : "; check_dns
}
# Proxy management
alias proxy_unset='unset http{s,}_proxy && unset HTTP{S,}_PROXY && unset FTP_PROXY && unset ftp_proxy && unset ALL_PROXY && unset all_proxy'
alias proxy_start='sudo systemctl daemon-reload && sudo systemctl start cntlm.service && sudo systemctl start redsocks.service; proxy_status'
alias proxy_stop='sudo systemctl stop redsocks.service cntlm.service; proxy_status'
function proxy_restart() {
sudo systemctl daemon-reload
sudo systemctl restart cntlm
sudo systemctl restart redsocks
sudo systemctl restart systemd-resolved resolvconf.service
proxy_status
}
function dns_restart() {
sudo systemctl daemon-reload
sudo systemctl restart systemd-resolved resolvconf.service
proxy_status
}
alias prs='proxy_status'
alias rep='proxy_status'
alias red='dns_restart'
# Curl useful options
function curl_code() {
curl -s -o /dev/null -w "%{http_code}" "$@"
}
# TMUX cheat sheet
alias tcs='google-chrome https://tmuxcheatsheet.com/'
alias ts='tmux ls'
# function ta() {
# ARG=${1:+"-t $1"}
# tmux a ${ARG:-}
# }
# Update VS code
alias codeup='sudo apt-get update && sudo apt-get install code'
alias curl_nocache='curl -H "Cache-Control: no-cache"'
# ---------------------------------------------------------------------------------
# Principle : we use 'deploy-toolbox' docker image with DevOps tools preinstalled
# ---------------------------------------------------------------------------------
export TOOLBOX_IMAGE="devops-toolbox"
export TOOLBOX_IMAGE_NAME="yogeek/${TOOLBOX_IMAGE}"
# Launch an interactive session on a docker image
# ex: drun alpine
function drun() {
docker run --rm -it ${1} "${@:2}"
}
# Toolbox
function toolbox() {
docker run --rm -it --network=host -v ${HOME}/.ssh:/home/devops/.ssh -v ${HOME}/.aws:/home/devops/.aws -v $(pwd):/app -e AWS_DEFAULT_REGION -e AWS_PROFILE -w /app ${TOOLBOX_IMAGE_NAME} $@
}
# function packer() {
# toolbox packer "$@"
# }
# # Terraform
# function terraform() {
# toolbox terraform "$@"
# }
# # Ansible
# function ansible() {
# toolbox ansible "$@"
# }
# function ansible-playbook() {
# toolbox ansible-playbook "$@"
# }
# AWS
function awstool() {
toolbox aws "$@"
}
#--------------------
# Ansible + java
#--------------------
function ansible-java() {
docker run --rm -it --network=host -v ${HOME}/.ssh:/home/devops/.ssh -v $(pwd):/app -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION -w /app yogeek/ansible-java-docker "$@"
}
# Docker images sorted by size
alias dis="docker images --format '{{.Size}}\t{{.Repository}}\t{{.Tag}}\t{{.ID}}' | sed 's/ //' | sort -h | column -t"
# ---------------------------------------------------------------------------------
# AWS environment shortcuts
# ---------------------------------------------------------------------------------
function adl() {
ADFS_HOST=XXXX
unset AWS_PROFILE
echo "Usage : adl <PROFILE> (e.g. adl 1116)"
echo "WARNING : uid = uia*****@cw01 / passwd = <ldap one>"
PROFILE=$1
if [[ ! -z $PROFILE ]]; then
echo "aws-adfs login --adfs-host=$ADFS_HOST --profile=$PROFILE"
aws-adfs login --adfs-host=$ADFS_HOST --profile=$PROFILE
export AWS_PROFILE=$PROFILE
else
echo "aws-adfs login --adfs-host=$ADFS_HOST"
aws-adfs login --adfs-host=$ADFS_HOST
echo "Set AWS_PROFILE !"
fi
export AWS_DEFAULT_REGION="eu-central-1"
}
# ---------------------------------------------------------------------------------
# AWS useful commands
# ---------------------------------------------------------------------------------
function listec2() {
aws ec2 describe-instances
}
function listec2_by_tag() {
aws ec2 describe-instances --filters 'Name=tag:'"$1"',Values='"$2"'' 'Name=instance-state-name,Values=running'
}
function listec2_by_name() {
listec2_by_tag "Name" "$1"
}
# AMI
function ami_print_all() {
#aws ec2 describe-images --owners self --query 'Images[*].{ID: ImageId}'
#aws ec2 describe-images --owners self --query 'Images[*].[CreationDate, ImageId, Tags[name]' --output table
aws ec2 describe-images --owners self --query 'Images[*].[ImageId,CreationDate,Name]' --output text \
| sort -k2 #-r
# | head -n1
}
function ami_print_by_id() {
[[ -z "$1" ]] && echo "Need to pass an ami ID" || aws ec2 describe-images --owner self --image-ids ${1}
}
function ami_print_by_tag() {
#echo "aws ec2 describe-images --filters Name=${1},Values=${2}"
aws ec2 describe-images --owners self --filters Name=tag:${1},Values=${2}
}
function ami_print_by_name() {
aws ec2 describe-images --owners self --filters Name=name,Values=${1}
}
function listec2_newer_than() {
#echo "aws ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=\`${1}\`][].{id: InstanceId, type: InstanceType, launched: LaunchTime}'"
aws ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=`'"${1}"'`][].{id: InstanceId, name: Name, type: InstanceType, launched: LaunchTime}'
}
# AWS tips
function atips() {
echo ""
echo "-----------> Custom commands"
echo "listec2"
echo "listec2_by_name <NAME>"
echo "listec2_by_tag <TAG> <VALUE>"
echo ""
echo "-----------> Find out information about an EC2 instance from AMI id"
echo "aws ec2 describe-instances --filters \"Name=image-id,Values=ami-xxxxxxx\""
echo ""
echo "-----------> Recursively copy a directory and its subfolders from PC to Amazon S3"
echo "aws s3 cp MyFolder s3://bucket-name --recursive [--region eu-central-1]"
echo ""
echo "-----------> List users in a table format"
echo "aws iam list-users --output table"
echo ""
echo "-----------> List the size and content of a S3 bucket"
echo "aws s3api list-objects --bucket BUCKETNAME --output json --query \"[sum(Contents[].Size), length(Contents[])]\""
echo ""
echo "----------> List users by Arn"
echo "aws iam list-users --output json | jq -r '.Users[].Arn'"
echo ""
echo "----------> List currently stopped instances and the reason"
echo "aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped --region eu-west-1 --output json | jq -r '.Reservations[].Instances[].StateReason.Message'"
echo ""
echo "----------> Find instances newer than a date"
echo "aws ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=`2018-05-025`][].{id: InstanceId, type: InstanceType, launched: LaunchTime}'"
echo ""
echo "----------> Describe a Security Group by its name (e.g. VPC_ID = vpc-f8bdb291 / SG NAME = its-jenkins-master"
echo "aws ec2 describe-security-groups --filters \"Name=vpc-id,Values=vpc-f8bdb291\" \"Name=group-name,Values=its-jenkins-master\""
echo ""
echo "----------> Wait for a condition to be fullfilled :"
echo "aws ec2 wait instance-terminated --filters "Name=tag:Name,Values=WaitTest""
echo ""
}
command -v ncdu > /dev/null && alias space-used='ncdu -x -r'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment