-
Notifications
You must be signed in to change notification settings - Fork 0
/
devops.sh
150 lines (119 loc) · 4.45 KB
/
devops.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# https://github.com/lfromanini/luiz-at-machine
# docker aliases and functions
alias dk=docker
alias dki="docker images"
alias dkrm="docker rm"
alias dkrmi="docker rmi"
alias dkps="docker ps --all"
alias dkprune="docker system prune --all --force"
alias dktop="docker stats"
function dkclean()
{
docker rm $( docker ps --all --quiet --filter status=exited ) 2>/dev/null || true
docker volume rm $( docker volume ls --quiet --filter dangling=true ) 2>/dev/null || true
}
function dklog() { docker logs "${1}" ; }
function dksh() { docker exec --interactive --tty ${@:2} "${1}" sh -c '$( which -a bash ash sh | head -n 1 )' ; }
function dksu() { dksh "${1}" ${@:2} --user root ; }
function dkstats() { docker ps --quiet --filter "name=${1}" | xargs docker stats --no-stream ; }
# kubernetes aliases and functions
[ -n "$BASH_VERSION" ] && source <( kubectl completion bash )
[ -n "$ZSH_VERSION" ] && source <( kubectl completion zsh )
alias k=kubectl
function konf()
{
local returnCode=0
local kubeConfigPath="$HOME/.kube/"
local searchString="${1}"
local fzfResult=""
fzfResult=$( builtin cd "${kubeConfigPath}" && command find "${kubeConfigPath}" -maxdepth 1 -type f -iname '*'"${searchString}"'*' -printf "%f\n" | command sort | command fzf --exit-0 --select-1 --layout="reverse" --height=10 )
returnCode=$?
[ ${returnCode} -eq 0 ] && [ ! -z "${fzfResult}" ] && KUBECONFIG="${kubeConfigPath}${fzfResult}"
[ -z "${KUBECONFIG}" ] && printf '\033[4m'"KUBECONFIG not set"'\033[0m'"\n" || printf "KUBECONFIG[ "'\033[1m'$( echo "${KUBECONFIG}" | command awk -F '/' '{ print $NF }' )'\033[0m'" ]\n"
export KUBECONFIG
[ ${returnCode} -eq 130 ] && returnCode=0
return ${returnCode}
}
function kg()
{
local returnCode=0
local fzfPreview=$( whereis -b batcat bat cat | command awk '/: ./ { print $2 ; exit }' )
local k8sMaster=$( { kubectl get nodes | command awk '/master/ { print $1 }' ; } 2>/dev/null )
local fzfPrompt=" ${k8sMaster} > "
if [[ "${fzfPreview}" == */bat* ]] ; then fzfPreview+=" --color=always --decorations=never --paging=never --language=yaml" ; fi
kubectl get $* -o custom-columns="KIND:kind,NAMESPACE:metadata.namespace,NAME:metadata.name" | command fzf --exit-0 --layout="reverse" --height="100%" \
--preview='kubectl get {1} {3} --namespace {2} -o yaml | '"${fzfPreview}" \
--prompt="${fzfPrompt}" \
--header=$'Press : reload[ CTRL + R ] or edit[ ENTER ]\n\n' \
--header-lines=1 \
--bind="ctrl-r:reload( kubectl get $* -o custom-columns=\"KIND:kind,NAMESPACE:metadata.namespace,NAME:metadata.name\" )" \
--bind="enter:execute( kubectl edit {1} {3} --namespace {2} )"
returnCode=$?
[ ${returnCode} -eq 130 ] && returnCode=0
return ${returnCode}
}
function kgpod()
{
local returnCode=0
local arg=""
local k8sMaster=$( { kubectl get nodes | command awk '/master/ { print $1 }' ; } 2>/dev/null )
local fzfPrompt=" ${k8sMaster} > "
local fzfCmd="kubectl get pods --all-namespaces"
local bIsNamespace="false"
local bHasNamespace="false"
local bAllNamespace="false"
for arg in "$@" ; do
case "${arg}" in
-A|--all-namespaces)
bAllNamespace="true"
;;
-n*|--namespace*)
bHasNamespace="true"
;;
esac
done
# use default namespace if no namespace informed neither using all namespaces
[ "${bHasNamespace}" = "false" ] && [ "${bAllNamespace}" = "false" ] && fzfCmd+=" --field-selector metadata.namespace=default"
for arg in "$@" ; do
case "${arg}" in
-A|--all-namespaces)
;;
--namespace=*)
fzfCmd+=" --field-selector metadata.namespace=""${arg#*=}"
bIsNamespace="false"
;;
-n*|--namespace*)
bIsNamespace="true"
;;
*)
[ "${bIsNamespace}" = "true" ] && fzfCmd+=" --field-selector metadata.namespace=""${arg}"
bIsNamespace="false"
;;
esac
done
for arg in "$@" ; do
case "${arg}" in
-A|--all-namespaces)
;;
--namespace=*)
bIsNamespace="false"
;;
-n*|--namespace*)
bIsNamespace="true"
;;
*)
[ "${bIsNamespace}" = "false" ] && fzfCmd+=" | command grep --extended-regex \"NAME|${arg}\"" # NAME is used to keep header
bIsNamespace="false"
;;
esac
done
sh -c "${fzfCmd}" | command fzf --exit-0 --layout="reverse" --height="100%" \
--prompt="${fzfPrompt}" \
--header=$'Press : reload[ CTRL + R ] or delete pod[ CTRL + D ]\n\n' \
--header-lines=1 \
--bind="ctrl-r:reload( ${fzfCmd} )" \
--bind="ctrl-d:execute( kubectl delete pod --namespace {1} {2} )+reload( ${fzfCmd} )"
returnCode=$?
[ ${returnCode} -eq 130 ] && returnCode=0
return ${returnCode}
}