-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_aliases
432 lines (409 loc) · 17.6 KB
/
.bash_aliases
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# Remember, alias are only interactive, not in scriupts w/o: shopt -s expand_aliases
# bash -i -c 'myalias' will work because makes the shell interactive
# and use -t to force a terminal session (needed to insert interactive input like a password), -tt to force a tesminal also when ssh doesn't have it
# e.g. alias fclremotesettoken="ssh -t -K openstackuigpvm01.fnal.gov 'bash -i -c fclsettoken' 2> /dev/null"
# "$PWD" resolved at definition, '$PWD' resolved at use, 'part1 '"'"'quoted'"'"' part2' adds a single quoted part in a single quote string (string concatenation in shells)
# shellcheck shell=bash
# Setup GWMS_DEV_USER in the environment as your desired fermicloud UI (openstackuigpvm01) user or substitute here (not needed if it is $USER)
export GWMS_DEV_USER=${GWMS_DEV_USER:-$USER}
# Modify the Git repo if desired
export GWMS_DEV_REPO="https://raw.githubusercontent.com/glideinwms/dev-tools/master"
export GWMS_DEV_REPO_GIT="https://github.com/glideinWMS/dev-tools.git"
# Setup (init and update) add ~/.bash_aliases ~/.bashcache/fclhosts and some files in ~/bin/
#alias mvim="/Applications/MacVim.app/contents/MacOS/MacVim"
alias mvim="open -a MacVim.app"
#alias lt='ls --human-readable --size'
alias lt='du -sh * | sort -h'
alias cpv='rsync -ah --info=progress2'
alias ve='python3 -m venv ./venv'
alias va='source ./venv/bin/activate'
alias dfh='df -h -T hfs,apfs,exfat,ntfs,noowners'
# git
alias gpo='git push origin'
# shellcheck disable=SC2142 # the parameter is part of awk syntax
alias gitmodified="git status | grep modified | awk '{print \$2}' | tr $'\n' ' '"
alias gitgraph='git log --all --decorate --oneline --graph'
alias cg='cd `git rev-parse --show-toplevel`'
alias cdgwms='cd prog/repos/git-gwms/'
alias cdm='cd_with_memory'
alias pushdm='cd_with_memory pushd'
alias dictlist='curl dict://dict.org/show:db'
# From https://gist.github.com/angelo-v/e0208a18d455e2e6ea3c40ad637aac53
alias jwtdecode='jq -R '"'"'gsub("-";"+") | gsub("_";"/") | split(".") | .[0],.[1] | @base64d | fromjson'"'"
alias infoalias='
echo -e "Aliases defined:\n General: lt cpv ve va dfh cl cdm pushdm cg dict dictlist"
echo " To connect to fermicloud: fcl... slv slf sgweb fcl_fe_certs (proxy-creds renewal)"
echo " GWMS: gv.. fe.. fa.."
echo " HTCondor: cv.. cc.. htc_.."
echo " infoalias, fclinit"
'
## For laptop
# Fermicloud
alias fcltokenlocal='OST_PROJECT=${OST_PROJECT:-glideinwms} ; export OS_TOKEN=$(openstack --os-username=$USER --os-user-domain-name=services --os-project-domain-name=services --os-project-name $OST_PROJECT --os-auth-url http://131.225.153.227:5000/v3 --os-system-scope all token issue --format json | jq -r '"'"'.id'"'"') && rm -f "$HOME"/.fclcache/token && echo "OST_PROJECT=$OST_PROJECT" > "$HOME"/.fclcache/token && chmod 600 "$HOME"/.fclcache/token && echo "OS_TOKEN_DATE=$(date +%Y-%m-%dT%H:%M:%S%z)" >> "$HOME"/.fclcache/token && echo "OS_TOKEN=$OS_TOKEN" >> "$HOME"/.fclcache/token'
# Will ask for service password
#alias fclremotesettoken="ssh -t -K [email protected] 'bash -i -c fclsettoken' 2> /dev/null"
alias fcltoken="ssh -t -K [email protected] 'bash -i -c fcltokenlocal' 2> /dev/null"
alias fcltokendelete="ssh -t -K [email protected] 'rm -f ~/.fclcache/token' 2> /dev/null"
#alias fclrefreshhosts="ssh -K [email protected] '. /etc/profile.d/one4x.sh; . /etc/profile.d/one4x_user_credentials.sh; ~marcom/bin/myhosts' > ~/.bashcache/fclhosts"
#alias fclrefreshhosts="ssh -K [email protected] '~marcom/bin/myhosts -r' > ~/.bashcache/fclhosts"
#alias fclrefreshhosts="ssh -K [email protected] '~marcom/bin/myhosts -r' > ~/.bashcache/fclhosts"
alias fclrefreshhosts="ssh -K [email protected] '~/bin/myhosts.sh' > ~/.bashcache/fclhosts"
alias fclhosts='cat ~/.bashcache/fclhosts'
alias fclinit='ssh_init_host'
alias fclinfo='gwms-what.sh'
#alias fclui='ssh [email protected]'
alias fclui="ssh [email protected]"
alias fclvofrontend='ssh [email protected]'
alias fclfactory='ssh [email protected]'
alias fclweb='ssh [email protected]'
alias slv='ssh_last ssh root U_vofrontend'
alias slf='ssh_last ssh root U_factory'
alias slce='ssh_last ssh root U_ce'
alias fcl='ssh_last ssh root'
alias fcl025='ssh [email protected]'
#alias sgweb='ssh [email protected]'
# htgettoken options: (-r pilot group pilot for wlcg, default) -i fermilab , -i fermilab-test, -i cms
alias fclhtgettoken='htgettoken --minsecs=3580 -v -a fermicloud543.fnal.gov -o '
alias fclhtgettokenfnal='htgettoken --minsecs=3580 -v -a fermicloud543.fnal.gov -i fermilab -o '
## For fermicloud hosts
# GWMS log files (use [N]:n :p to navigate files)
alias gvmain='less /var/log/gwms-frontend/group_main/main.*.log'
alias gvfe='less /var/log/gwms-frontend/frontend/frontend.*.log'
alias gvfa='less /var/log/gwms-factory/server/factory/factory.*.log'
alias gvg0='less /var/log/gwms-factory/server/factory/group_0.*.log'
alias gvel='ls /var/log/gwms-factory/server/ | grep entry'
# gve is a function
# HTCondor CondorView some log file, CondorCommand...
alias cvcoll='less /var/log/condor/CollectorLog'
alias cvsched='less /var/log/condor/SchedLog'
alias cvmaster='less /var/log/condor/MasterLog'
alias cvgm='less /var/log/condor/GridManagerLog.schedd_glideins*'
alias cvcerts='less /etc/condor/certs/condor_mapfile'
alias ccs='condor_status -any'
alias ccsf='condor_status -any -af MyType Name'
alias ccsl="condor_status -any -constraint 'MyType == \"glidefactoryclient\"' -af GlideinMonitorRequestedIdle GlideinMonitorRequestedMaxGlideins GlideinMonitorRequestedIdleCores GlideinMonitorRequestedMaxCores ReqEntryName Name"
alias ccq='condor_q -global -allusers'
#alias ccql='htc_foreach_schedd condor_q -af ClusterId ProcId GlideinEntryName GlideinClient JobStatus Cmd -name'
alias ccql='htc_foreach_schedd -f1 condor_q -allusers -af ClusterId ProcId GlideinEntryName GlideinClient JobStatus Cmd -name'
alias ccqlv='htc_foreach_schedd -v -f1 condor_q -allusers -af ClusterId ProcId GlideinEntryName GlideinClient JobStatus Cmd -name'
alias ccrm='condor_rm -all -name'
alias ccrmg2='su -c "condor_rm -name schedd_glideins2@$HOSTNAME -all" - gfactory'
alias ccrmg3='su -c "condor_rm -name schedd_glideins3@$HOSTNAME -all" - gfactory'
alias ccrmg4='su -c "condor_rm -name schedd_glideins4@$HOSTNAME -all" - gfactory'
alias ccrma='htc_foreach_schedd condor_rm -all -name'
## These are for root on fermicloud hosts
# GWMS manage and troubleshoot
# /bin/systemctl is not working in the containers, use systemctl
alias festart='systemctl start gwms-frontend'
alias festartall1='for s in fetch-crl-cron httpd condor gwms-frontend fetch-crl-boot; do echo "Starting $s"; systemctl start $s; done'
alias festop='systemctl stop gwms-frontend'
alias fereconfig='systemctl stop gwms-frontend; /usr/sbin/gwms-frontend reconfig; systemctl start gwms-frontend'
alias feupgrade='systemctl stop gwms-frontend; /usr/sbin/gwms-frontend upgrade; systemctl start gwms-frontend'
alias fecredrenewal='fcl_fe_certs' # alias to make it easy to find - renew proxy from certs/creds
alias fetest='su -c "cd condor-test/; condor_submit test-vanilla.sub" -' # the user to use for the test will be specified after the alias (fetest). condor-test/test-vanilla.sub is assumed
# the followinf 2 commands myst run as the frontend user to have the correct token ownership and location
alias feccq='su -c "CONDOR_CONFIG=/var/lib/gwms-frontend/vofrontend/frontend.condor_config _CONDOR_TOOL_DEBUG=D_FULLDEBUG,D_SECURITY condor_q -debug -global -allusers" -s /bin/bash - frontend'
alias fecca='su -c "CONDOR_CONFIG=/var/lib/gwms-frontend/vofrontend/frontend.condor_config _CONDOR_TOOL_DEBUG=D_FULLDEBUG,D_SECURITY condor_advertise -debug" -s /bin/bash - frontend'
alias fastart='systemctl start gwms-factory'
alias fastartall='for s in fetch-crl-cron httpd condor gwms-factory fetch-crl-boot; do echo "Starting $s"; systemctl start $s; done'
alias fastop='systemctl stop gwms-factory'
alias faupgrade='systemctl stop gwms-factory; /usr/sbin/gwms-factory upgrade ; systemctl start gwms-factory'
alias fareconfig='systemctl stop gwms-factory; /usr/sbin/gwms-factory reconfig; systemctl start gwms-factory'
## Functions
dict() {
# dict word [dictionary (as from dictlist)] OR dict word:dictionary
if [ -n "$2" ]; then
curl dict://dict.org/d:"${1}:${2}"
else
curl dict://dict.org/d:"${1}"
fi
}
translate() {
# translate word [to [from]]
# using 3 letters languages as in FreeDict
local lan_from=${3:-eng}
local lan_to=${2:-ita}
dict "$1":fd-"${lan_from}"-"${lan_to}"
}
cl() {
# cd and list files
DIR="$*";
[ $# -lt 1 ] && DIR=$HOME
builtin cd "${DIR}" && ls -F --color=auto
}
ts2date() {
# Using function so it can be both on the line or piped
# Unix timestamp to date conversion. Requires jq
[[ -n "$1" ]] && { jq 'todate' <<< $1 ; true;} || jq 'todate'
}
gwms_test_job() {
[[ "$1" = "-h" ]] && { echo -e "gwms_test_job [-h | USER [-l | SUBMIT_FILE]]\nSubmitting condor jobs from the USER's ~/condor-test/ directory"; return; }
local juser=${1:-$GWMS_DEV_USER}
[[ "$2" = "-l" ]] && { su -c "cd condor-test/; ls *sub" - $juser; return; }
local job=${2:-test-vanilla.sub}
if [ "$(id -u)" -eq 0 ]; then
local juserdir
juserdir=$(eval echo "~$juser")
[[ -e "$job" || -e ${juserdir}/condor-test/$job ]] && su -c "cd ${juserdir}/condor-test/; condor_submit $job" - $juser || su -c "cd ${juserdir}/condor-test/; ls *${job}*" - $juser
else
[[ "$PWD" = */condor-test ]] || cd condor-test/
[[ -e "$job" ]] && condor_submit $job || ls *${job}*
fi
}
gve() {
less /var/log/gwms-factory/server/entry_${1#entry_}/${1#entry_}.*.log
}
cd_with_memory() {
# use cd or pushd and record the directory in bash_aliases_aux BA_LASTDIR
local cmd=cd
if [[ "$1" = pushd ]]; then
cmd=pushd
shift
fi
[ ! -e ~/.bash_aliases_aux ] && touch ~/.bash_aliases_aux
if [ -n "$1" ]; then
grep -v "^BA_LASTDIR=" ~/.bash_aliases_aux > ~/.bash_aliases_aux.new
echo "BA_LASTDIR=\"$1\"" >> ~/.bash_aliases_aux.new
command mv ~/.bash_aliases_aux.new ~/.bash_aliases_aux
$cmd "$1"
else
# cache script with wariables to load
. ~/.bash_aliases_aux
local lastdir=${BA_LASTDIR}
if [ -n "$lastdir" ]; then
[ "$cmd" = cd ] && echo "cd to $lastdir"
$cmd "$lastdir"
else
echo "No last dir available"
false
fi
fi
}
get_glidein_dir_last() {
# -u user -r root_dir (/tmp) -l
local OPTIND option user root_dir=/tmp
local do_list=false
while getopts u:r:lh option ; do
case "${option}" in
h) echo "get_glidein_dir_last -l"; echo "get_glidein_dir_last [-u user][-r root_dir (default:/tmp)]"; return;;
r) root_dir="${OPTARG}";;
u) user="${OPTARG}";;
l) do_list=true;;
*) echo "Bad option: ${option}"; false; return;
esac
done
shift $((OPTIND-1))
if $do_list; then
ls -ldt "$root_dir"/glide_*
return
fi
# echo added to compress spaces?
if [[ -z "$user" ]]; then
echo "$(ls -dt "$root_dir"/glide_* | head -n 1)"
else
echo "$(find "$root_dir" -maxdepth 1 -name 'glide_*' -user $user -printf "%T@,%p\n" | sort -r | head -n1 | cut -d, -f2)"
fi
}
ssh_last() {
# return the full hostname of the last host of the requested type (or do partial name matches), optionally ssh to it
# valid types: fact, factory, vofe, frontend, vofrontend, web, ce (fermicloud025), INT (fermicloudINT)
# OpenStack presents all hosts of the project, "U_" prefix to search only user VMs
# VMs are in reverse order, most recent first
local dossh=false
local asroot=false
local filteruser=
if [ "$1" = "ssh" ]; then
dossh=true
shift
fi
if [ "$1" = "root" ]; then
asroot=true
shift
fi
local sel="$1"
if [[ "$sel" == U_* ]]; then
filteruser="$GWMS_DEV_USER"
sel="${sel#U_}"
fi
[ "$sel" == "factory" ] && sel=fact
[ "$sel" == "frontend" ] && sel=vofe
[ "$sel" == "vofrontend" ] && sel=vofe
[ "$sel" == "web" ] && sel=gwms-web
#[ "$sel" == "ce" ] && sel=fermicloud025
[[ "$sel" =~ ^[0-9]+$ ]] && sel="fermicloud$sel"
if [ -n "$filteruser" ]; then
myhost=$(grep "$filteruser" ~/.bashcache/fclhosts | grep "$sel" | head -n 1 | cut -d ' ' -f 3 )
else
myhost=$(grep "$sel" ~/.bashcache/fclhosts | head -n 1 | cut -d ' ' -f 3 )
fi
if [ -z "$myhost" ]; then
[[ ! "$sel" =~ ^fermicloud[0-9]+\.fnal\.gov$ ]] && { echo "Host $1 ($sel) not found on fermicloud list."; return 1; }
myhost=$sel
fi
shift
echo "$myhost"
if $dossh; then
if $asroot; then
# shellcheck disable=SC2029 # client expansion desired, these are ssh options
ssh root@"$myhost" "$@"
else
# shellcheck disable=SC2029 # client expansion desired, these are ssh options
ssh "$myhost" "$@"
fi
fi
}
ssh_init_host() {
# init a fermicloud node
local hname
hname=$(ssh_last "$1")
local huser=${2:-root}
echo "Initializing ${huser}@${hname}"
#scp "$HOME"/prog/repos/git-gwms/gwms-tools/.bash_aliases ${huser}@${hname}: >/dev/null && ssh ${huser}@${hname} ". .bash_aliases && aliases-update"
#ssh ${huser}@${hname} "curl -L -o $HOME/.bash_aliases $GWMS_DEV_REPO/.bash_aliases 2>/dev/null" && ssh ${huser}@${hname} ". .bash_aliases && aliases-update"
# shellcheck disable=SC2029 # client expansion desired
ssh "${huser}@${hname}" "curl -L -o ~/.bash_aliases $GWMS_DEV_REPO/.bash_aliases 2>/dev/null && . ~/.bash_aliases && aliases-update"
}
fcldownload() {
# download from repo and make executable if -x is first parameter
if [ "$1" == clone ]; then
git clone $GWMS_DEV_REPO_GIT
return 0
fi
local make_exe=false
[ "$1" == "-x" ] && { make_exe=true; shift; }
local dfile="$1"
if [ -n "$dfile" ]; then
curl -L -o "$dfile" "$GWMS_DEV_REPO/$(basename "$dfile")"
if head -n 1 "$dfile" | grep -q "404: Not Found" ; then
echo "URL not found ($GWMS_DEV_REPO/$(basename "$dfile")). Removing file ($dfile)."
rm "$dfile"
else
$make_exe && chmod +x "$dfile"
fi
fi
}
fcl_fe_certs() {
# 1. pilot proxy path or file name (default: /etc/gwms-frontend/mm_proxy)
# some checks to avoid running as regular user or on a host that is not the frontend
command -v voms-proxy-init >/dev/null || { echo "voms-proxy-init not found. aborting"; return 1; }
[[ $(id -u) -ne 0 ]] && { echo "must run as root"; return 1; }
local pilot_proxy=$1
[[ -n "$pilot_proxy" ]] && pilot_proxy=/etc/gwms-frontend/mm_proxy
[[ "$pilot_proxy" = /* ]] || pilot_proxy=/etc/gwms-frontend/"$pilot_proxy"
pushd /etc/grid-security/ || return 1
grid-proxy-init -cert hostcert.pem -key hostkey.pem -valid 999:0 -out /etc/gwms-frontend/fe_proxy
popd || return 1
/bin/cp /etc/gwms-frontend/fe_proxy /etc/gwms-frontend/vo_proxy
kx509
voms-proxy-init -rfc -dont-verify-ac -noregen -voms fermilab -valid 500:0
/bin/cp /tmp/x509up_u0 /etc/gwms-frontend/mm_proxy
chown frontend: /etc/gwms-frontend/*
# Check all proxies
echo "Proxy renewed (/etc/gwms-frontend/vo_proxy, /etc/gwms-frontend/mm_proxy), now checking..."
if command -v gwms-check-proxies.sh >/dev/null; then
gwms-check-proxies.sh
elif [ -x ~marcom/bin/gwms-check-proxies.sh ]; then
~marcom/bin/gwms-check-proxies.sh
else
echo "No gwms-check-proxies found"
fi
}
aliases_update() {
[ -e "$HOME/.bash_aliases" ] && command cp -f "$HOME"/.bash_aliases "$HOME"/.bash_aliases.bck
if ! curl -L -o "$HOME"/.bash_aliases $GWMS_DEV_REPO/.bash_aliases 2>/dev/null; then
echo "Download from github.com failed. Update failed."
return 1
fi
if ! grep "# Added by alias-update" "$HOME"/.bashrc >/dev/null; then
cat >> "$HOME"/.bashrc << EOF
# Added by alias-update
export PATH="\$PATH:\$HOME/bin"
if [ -e \$HOME/.bash_aliases ]; then
source \$HOME/.bash_aliases
fi
# End from alias-update
EOF
fi
# copy also some binaries
mkdir -p "$HOME"/.fclcache
mkdir -p "$HOME"/bin
for i in gwms-clean-logs.sh gwms-setup-script.py gwms-what.sh gwms-check-auth.sh gwms-check-proxies.sh myhosts.sh 99_debug.config; do
curl -L -o "$HOME/bin/$i" "$GWMS_DEV_REPO/$i" 2>/dev/null && chmod +x "$HOME/bin/$i"
[[ $? -ne 0 ]] && echo "Error downloading $i. Continuing."
done
# If root, update some system files. This only for fermicloud hosts
if [[ -w /etc/profile && "$(hostname)" = fermicloud* ]]; then
if ! grep "# Added by alias-update" /etc/profile >/dev/null; then
cat >> /etc/profile << EOF
# Added by alias-update
[ -f /etc/motd.local ] && { tput setaf 2; cat /etc/motd.local; tput sgr0; }
tput setaf 2
if [ -x /root/bin/gwms-what.sh ]; then
/root/bin/gwms-what.sh
elif [ -x "$HOME/bin/gwms-what.sh" ]; then
"$HOME/bin/gwms-what.sh"
fi
tput sgr0
# End from alias-update
EOF
fi
fi
# source alias definitions to load updates
. $HOME/.bash_aliases
}
# HTC functions
htc_job_status() {
local htc_short=true
if $htc_short; then
case $1 in
0) echo U;;
1) echo I;;
2) echo R;;
3) echo X;;
4) echo C;;
5) echo H;;
6) echo E;;
esac
else
case $1 in
0) echo Unexpanded;;
1) echo Idle;;
2) echo Running;;
3) echo Removed;;
4) echo Completed;;
5) echo Held;;
6) echo Submission_err;;
esac
fi
}
htc_filter1() {
while read -r a b c d e rest; do
if [[ "$a" == "#"* ]]; then
echo "$a $b $c $d $e $rest"
else
printf '%i.%i\t%s %s %s %s\n' "$a" "$b" "$c" "${d%%-fn*}" "$(htc_job_status "$e")" "$rest"
fi
done
}
htc_foreach_schedd() {
local verbose=false
local filter=
if [[ "$1" = "-v" ]]; then
verbose=true
shift
fi
if [[ "$1" = "-f1" ]]; then
filter=htc_filter1
shift
fi
local sc_list
sc_list="$(condor_status -schedd -af Name)"
for i in $sc_list; do
$verbose && echo "# $i"
if [[ -z "$filter" ]]; then
"$@" "$i"
else
"$@" "$i" | $filter
fi
done
}