-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.functions
366 lines (314 loc) · 9.82 KB
/
.functions
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
# Simple calculator
calc() {
local result=""
result="$(printf "scale=10;%s\\n" "$*" | bc --mathlib | tr -d '\\\n')"
if [[ "$result" == *.* ]]; then
printf "%s" "$result" |
sed -e 's/^\./0./' \
-e 's/^-\./-0./' \
-e 's/0*$//;s/\.$//'
else
printf "%s" "$result"
fi
printf "\\n"
}
# Create a new directory and enter it
mkd() {
mkdir -p "$@"
cd "$@" || exit
}
# Decrypt a file
reveal() {
output=$(echo "${1}" | rev | cut -c16- | rev)
gpg --decrypt --output "${output}" "${1}" \
&& echo "${1} -> ${output}"
}
# Encrypt a file
secret() {
output=~/"${1}".$(date +%s).enc
gpg --encrypt --armor \
--output "${output}" \
-r 0x8C649FA0D2396E42 \
-r [email protected] \
"${1}" && echo "${1} -> ${output}"
}
# Encrypt a file in same directory
secrethere() {
output="${1}".$(date +%s).enc
gpg --encrypt --armor \
--output "${output}" \
-r 0x8C649FA0D2396E42 \
-r [email protected] \
"${1}" && echo "${1} -> ${output}"
}
# check if uri is up
isup() {
local uri=$1
if curl -s --head --request GET "$uri" | grep "200 OK" > /dev/null ; then
notify-send --urgency=critical "$uri is down"
else
notify-send --urgency=low "$uri is up"
fi
}
# Check if uri is up
isup() {
local uri=$1
if curl -s --head --request GET "$uri" | grep "200 OK" > /dev/null ; then
notify-send --urgency=critical "$uri is down"
else
notify-send --urgency=low "$uri is up"
fi
}
# Get colors in manual pages
man() {
env \
LESS_TERMCAP_mb="$(printf '\e[1;31m')" \
LESS_TERMCAP_md="$(printf '\e[1;31m')" \
LESS_TERMCAP_me="$(printf '\e[0m')" \
LESS_TERMCAP_se="$(printf '\e[0m')" \
LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \
LESS_TERMCAP_ue="$(printf '\e[0m')" \
LESS_TERMCAP_us="$(printf '\e[1;32m')" \
man "$@"
}
# Call from a local repo to open the repository on github/bitbucket in browser
# Modified version of https://github.com/zeke/ghwd
repo() {
# Figure out github repo base URL
local base_url
base_url=$(git config --get remote.origin.url)
base_url=${base_url%\.git} # remove .git from end of string
# Fix [email protected]: URLs
base_url=${base_url//git@github\.com:/https:\/\/github\.com\/}
# Fix git://github.com URLS
base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/}
# Fix [email protected]: URLs
base_url=${base_url//[email protected]:/https:\/\/bitbucket\.org\/}
# Fix [email protected]: URLs
base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/}
# Validate that this folder is a git folder
if ! git branch 2>/dev/null 1>&2 ; then
echo "Not a git repo!"
exit $?
fi
# Find current directory relative to .git parent
full_path=$(pwd)
git_base_path=$(cd "./$(git rev-parse --show-cdup)" || exit 1; pwd)
relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory
# If filename argument is present, append it
if [ "$1" ]; then
relative_path="$relative_path/$1"
fi
# Figure out current git branch
# git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
# Remove cruft from branchname
branch=${git_where#refs\/heads\/}
[[ $base_url == *bitbucket* ]] && tree="src" || tree="tree"
url="$base_url/$tree/$branch$relative_path"
echo "Calling $(type open) for $url"
open "$url" &> /dev/null || (echo "Using $(type open) to open URL failed." && exit 1);
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
targz() {
local tmpFile="${1%/}.tar"
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${1}" || return 1
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null # GNU `stat`
)
local cmd=""
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli"
else
if hash pigz 2> /dev/null; then
cmd="pigz"
else
cmd="gzip"
fi
fi
echo "Compressing .tar using \`${cmd}\`…"
"${cmd}" -v "${tmpFile}" || return 1
[ -f "${tmpFile}" ] && rm "${tmpFile}"
echo "${tmpFile}.gz created successfully."
}
# Determine size of a file or total size of a directory
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
# shellcheck disable=SC2199
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg -- .[^.]* *
fi
}
github-print() {
cat << EndOfMessage
document.querySelector('#readme').setAttribute('style', 'position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 100; background-color: white')
document.querySelector('body').appendChild(document.querySelector('#readme'))
window.print()
EndOfMessage
}
git-fetch-all() {
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
}
git-prune-all() {
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
}
cleanhistory() {
# history -n has to be there before history -w to read from
# .bash_history the commands saved from any other terminal,
history -n # Read in entries that are not in current history
history -w # Write history, trigger erasedups
#history -a # Append history; does not trigger erasedups
history -c # Clear current history
history -r # Read history from $HISTFILE
}
# Use Git’s colored diff when available
if hash git &>/dev/null ; then
diff() {
git diff --no-index --color-words "$@"
}
fi
extract () {
local PV=$(command -v pv || echo cat);
if [ -f "${1}" ]; then
case "${1}" in
*.tar.xz)
tar xvf "${1}"
;;
*.tar.gz)
"${PV}" "${1}" | tar xzf - --totals
;;
*.tar.bz2 | *.tbz)
tar xvjf "${1}"
;;
*.bz2)
bunzip2 "${1}"
;;
*.rar)
unrar x "${1}"
;;
*.gz)
gunzip "${1}"
;;
*.tar)
tar xvf "${1}"
;;
*.tbz2)
tar xvjf "${1}"
;;
*.tgz)
tar xvzf "${1}"
;;
*.zip)
unzip "${1}"
;;
*.Z)
uncompress "${1}"
;;
*.7z)
7z x "${1}"
;;
*)
echo "don't know how to extract '$1'..."
;;
esac;
else
echo "'$1' is not a valid file";
fi
}
# _ _ _
# | | ___ _| |__ ___ ___(_)_ __ ___
# | |/ / | | | '_ \ / _ \/ __| | '_ ` _ \
# | <| |_| | |_) | __/\__ \ | | | | | |
# |_|\_\\__,_|_.__/ \___||___/_|_| |_| |_|
ks-root ()
{
( _bash_safe_mode;
local __KS_USER="${__KS_USER:-root}";
local SSH_BRUTE="-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null";
DIR=$(mktemp -d);
cd "${DIR}";
export -f extract;
ls --color=tty -t1 ~/Downloads/simulator-*.tar.gz | head -n1 | xargs -I{} bash -c "tar xzvf '{}'";
local SHORTCUTS;
SHORTCUTS=$(awk "/^Host/{print \"ssh ${SSH_BRUTE} root@\"\$3\"\t # \" \$2}" ./cp_simulator_config | grep -v bastion || true);
if [[ "${SHORTCUTS:-}" == "" ]]; then
echo "# shortcuts not possible, is root trust bundle downloaded?" 1>&2;
else
echo "${SHORTCUTS}";
fi;
cat <<EOF
kubectl get pods --all-namespaces
EOF
ssh -A -o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -F ./cp_simulator_config "${__KS_USER}"@bastion -i cp_simulator_rsa ${@:-} )
}
_bash_safe_mode() {
set -Eeuo pipefail
IFS=$'\n\t' # Split words on \n\t rather than spaces
}
ks-play () {
__KS_USER=ubuntu ks-root
}
# _ _ _
# | | ___ _| |__ ___ _ __ _ __ ___| |_ ___ ___
# | |/ / | | | '_ \ / _ \ '__| '_ \ / _ \ __/ _ \/ __|
# | <| |_| | |_) | __/ | | | | | __/ || __/\__ \
# |_|\_\\__,_|_.__/ \___|_| |_| |_|\___|\__\___||___/
highlight() {
local IFS="|"
local IPV6_REGEX="[\w:]+:+[\w:]+"
grep -iE --color=auto '^|((error|fail|info|warning|warn|unauthorized|start|success|succeed|'${IPV6_REGEX}'|'${*:-$}')\w*)' 2>/dev/null \
|| cat
}
klog () {
{
set -o pipefail;
local NAMESPACE;
if [[ "${*}" =~ --?n(amespace)?\s*=* ]]; then
NAMESPACE="";
else
if [[ "${K8S_NAMESPACE:-}" != "" ]]; then
NAMESPACE="--namespace=${K8S_NAMESPACE}";
else
NAMESPACE="--all-namespaces";
fi;
fi;
local EXCLUDE="(panics.go:76|leaderelection.go:247)";
stern ${NAMESPACE} --color always --since 25m "${@:-.}" \
--exclude="${EXCLUDE}" | highlight
}
}
kev () {
{
set -o pipefail;
local OUTPUT='--output="custom-columns=NAMESPACE:.metadata.namespace,NAME:.involvedObject.name,KIND:.involvedObject.kind,LASTSEEN:.lastTimestamp,COUNT:.count,REASON:.reason,MESSAGE:.message"';
local NAMESPACE;
if [[ "${@}" =~ --?n(amespace)?[\ =] ]]; then
NAMESPACE="";
else
if [[ "${K8S_NAMESPACE:-}" != "" ]]; then
NAMESPACE="--namespace=${K8S_NAMESPACE}";
else
NAMESPACE="--all-namespaces";
fi;
fi;
echo kubectl get events ${NAMESPACE} ${OUTPUT} "${@}" | source /dev/stdin | head -n1;
echo kubectl get events ${NAMESPACE} ${OUTPUT} --watch --no-headers=true "${@}" | source /dev/stdin | stdbuf -e0 -i0 -o0 tail -n+2
}
}
# https://dns.toys
function dy { dig +noall +answer +additional "$1" @dns.toys; }
function pulseaudio-reset {
pulseaudio -k
pulseaudio --start
}