diff --git a/roles/shell-dotfiles/files/functions b/roles/shell-dotfiles/files/functions index f022a77..2383bce 100644 --- a/roles/shell-dotfiles/files/functions +++ b/roles/shell-dotfiles/files/functions @@ -27,29 +27,6 @@ function always(){ done } -# Actualiza kubectl -function upgrade_kubectl (){ - echo "Downloading kubectl..." - version=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt); - curl -s -L https://storage.googleapis.com/kubernetes-release/release/$version/bin/linux/amd64/kubectl -o /tmp/kubectl - chmod +x /tmp/kubectl - mv /tmp/kubectl ~/Scripts/bin/kubectl-$version - ln -fs ~/Scripts/bin/kubectl-$version ~/Scripts/bin/kubectl - echo "kubectl version: $version" -} - -# Actualiza kops -function upgrade_kops (){ - echo "Downloading kops..." - uri=$(curl -s https://github.com/kubernetes/kops/releases/latest -L | grep "kops-linux-amd64" | grep -v strong | cut -d '"' -f2 | head -n1) - version=$(echo $uri | cut -d'/' -f6) - curl -s -L https://github.com/$uri -o /tmp/kops - chmod +x /tmp/kops - mv /tmp/kops ~/Scripts/bin/kops-$version - ln -fs ~/Scripts/bin/kops-$version ~/Scripts/bin/kops - echo "kops version: $version" -} - # Limpiar ramas mergeadas function delete_old_branches(){ git checkout master @@ -70,17 +47,6 @@ function enter(){ docker exec -ti $1 $command } -# Entra en un contenedor k8s -function kenter(){ - if [[ -z $4 ]] - then - command=bash - else - command="$4" - fi - kubectl exec -ti $1 $2 $3 $command -} - # Check that the directories of the main directory are updated git repos function git_updated() { local ROOT=`pwd` @@ -108,54 +74,6 @@ function git_updated() { done } - -# Autocompletados -## kubectl -KUBECTL_BIN=`which kubectl` -function kubectl (){ - if [[ -z $KUBECTL_SHOW_CONTEXT ]] - then - source <($KUBECTL_BIN completion zsh) - # Add support to completion when using the kubectl alias ku - source <($KUBECTL_BIN completion zsh | sed "s/kubectl/ku/g") - fi - KUBECTL_SHOW_CONTEXT="k8s-$($KUBECTL_BIN config current-context)" - $KUBECTL_BIN "$@" -} - -## kops -KOPS_BIN=`which kops` -function kops(){ - if [[ -z $KOPS_SHOW_CONTEXT ]] - then - source <($KOPS_BIN completion zsh) - fi - KOPS_SHOW_CONTEXT="k8s-$($KOPS_BIN config current-context)" - $KOPS_BIN "$@" -} - - -## molecule -MOLECULE_BIN=`which molecule` -function molecule() { - if [[ -z $MOLECULE_SET ]] - then - MOLECULE_SET=True - eval "$(_MOLECULE_COMPLETE=source molecule)" - fi - $MOLECULE_BIN "$@" -} - -## helm -HELM_BIN=`which helm` -function helm (){ - if [[ -z $HELM_SHOW_CONTEXT ]] - then - source <($HELM_BIN completion zsh) - fi - $HELM_BIN "$@" -} - # fzf functions ## insert located file fzf-locate-widget() { @@ -265,17 +183,13 @@ function warn_me (){ echo "notify-send --urgency=critical \"$1\"" | at $2 } -# TODO -# function ansible-playbook-2.5 (){ -# docker run -ti -v `pwd`:/ansible \ -# -v /var/run/docker.sock:/var/run/docker.sock \ -# -v `pwd`/config:/home/docker/.ssh/config ansible:2.5 "$@" -# } - +# Kubectl functions function kube_dump_logs(){ local deploy="$1" - local time="$2" + local namespace="$2" + local time="$3" + if [[ -z "$deploy" ]] then echo "You must pass a kubernetes deployment as an argument." @@ -285,22 +199,76 @@ function kube_dump_logs(){ then time=1h fi + + if [[ -z "$namespace" ]] + then + namespace_param="-n default" + else + namespace_param="-n $namespace" + fi + mkdir -p "$deploy" - for pod in $(ku get pods | grep "$deploy" | awk '{print $1}') + + for pod in $(kube_get_deploy_pods $deploy $namespace) do - ku logs "$pod" --since="$time" > "$deploy"/"$pod" + echo Dumping "$pod" ... + eval kubectl logs "$pod" --since="$time" "$namespace_param" > "$deploy"/"$pod" done echo "Dumped $deploy logs." } function kube_get_deploy_pods(){ local deploy="$1" + local namespace="$2" + if [[ -z "$deploy" ]] then echo "You must pass a kubernetes deployment as an argument." return 1 fi - ku get pods | grep "$deploy" | awk '{print $1}' + if [[ -z "$namespace" ]] + then + namespace="-n default" + else + namespace="-n $namespace" + fi + + eval kubectl get pods $namespace | grep "$deploy" | cut -d ' ' -f1 +} + +# Entra en un contenedor k8s +function kenter(){ + if [[ -z $4 ]] + then + command=bash + else + command="$4" + fi + kubectl exec -ti $1 $2 $3 $command +} + + +# Actualiza kubectl +function upgrade_kubectl (){ + echo "Downloading kubectl..." + version=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt); + curl -s -L https://storage.googleapis.com/kubernetes-release/release/$version/bin/linux/amd64/kubectl -o /tmp/kubectl + chmod +x /tmp/kubectl + mv /tmp/kubectl ~/Scripts/bin/kubectl-$version + ln -fs ~/Scripts/bin/kubectl-$version ~/Scripts/bin/kubectl + echo "kubectl version: $version" +} + +# Actualiza kops +function upgrade_kops (){ + echo "Downloading kops..." + uri=$(curl -s https://github.com/kubernetes/kops/releases/latest -L | grep "kops-linux-amd64" | grep -v strong | cut -d '"' -f2 | head -n1) + version=$(echo $uri | cut -d'/' -f6) + curl -s -L https://github.com/$uri -o /tmp/kops + chmod +x /tmp/kops + mv /tmp/kops ~/Scripts/bin/kops-$version + ln -fs ~/Scripts/bin/kops-$version ~/Scripts/bin/kops + echo "kops version: $version" } # Docker @@ -318,3 +286,58 @@ function reveal () { function docker-tox () { docker run -ti -v `pwd`:/tox registry.daemons.it/tox:latest tox "$@" } + +# Autocompletados +## kubectl +KUBECTL_BIN=`which kubectl` +function kubectl (){ + if [[ -z $KUBECTL_SHOW_CONTEXT ]] + then + source <($KUBECTL_BIN completion zsh) + fi + KUBECTL_SHOW_CONTEXT="k8s-$($KUBECTL_BIN config current-context)" + $KUBECTL_BIN "$@" +} + +function ku (){ + if [[ -z $KUBECTL_SHOW_CONTEXT ]] + then + source <($KUBECTL_BIN completion zsh | sed "s/kubectl/ku/g") + fi + KUBECTL_SHOW_CONTEXT="k8s-$($KUBECTL_BIN config current-context)" + $KUBECTL_BIN "$@" +} + +## kops +KOPS_BIN=`which kops` +function kops(){ + if [[ -z $KOPS_SHOW_CONTEXT ]] + then + source <($KOPS_BIN completion zsh) + fi + KOPS_SHOW_CONTEXT="k8s-$($KOPS_BIN config current-context)" + $KOPS_BIN "$@" +} + + +## molecule +MOLECULE_BIN=`which molecule` +function molecule() { + if [[ -z $MOLECULE_SET ]] + then + MOLECULE_SET=True + eval "$(_MOLECULE_COMPLETE=source molecule)" + fi + $MOLECULE_BIN "$@" +} + +## helm +HELM_BIN=`which helm` +function helm (){ + if [[ -z $HELM_SHOW_CONTEXT ]] + then + source <($HELM_BIN completion zsh) + fi + $HELM_BIN "$@" +} +