dot_bashrc 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  1. ## Copyright © 2018, 2019, 2020, 2021, 2022, 2023, 2024 Oleg Pykhalov <go.wigust@gmail.com>
  2. ## Released under the GNU GPLv3 or any later version.
  3. if [ -f "/etc/skel/.bashrc" ]
  4. then
  5. # Load the skel profile's settings.
  6. source "/etc/skel/.bashrc"
  7. fi
  8. if [ -f "$HOME/.bash_vterm" ] && [[ $INSIDE_EMACS == vterm ]]
  9. then
  10. source "$HOME/.bash_vterm"
  11. fi
  12. if [ -f "$HOME/.bash_aliases" ]
  13. then
  14. # Load the Bash aliases and functions.
  15. source "$HOME/.bash_aliases"
  16. fi
  17. if [ -f "$HOME/.bash_guix" ]
  18. then
  19. source "$HOME/.bash_guix"
  20. fi
  21. function dmesg()
  22. {
  23. if [[ -z ${@:1} ]] && [[ -e "${HOME}/.guix-profile/bin/spacer" ]]
  24. then
  25. sudo /run/current-system/profile/bin/dmesg \
  26. --reltime \
  27. --human \
  28. --nopager \
  29. --decode \
  30. --ctime \
  31. --follow \
  32. --color=always \
  33. | "${HOME}/.guix-profile/bin/spacer"
  34. else
  35. sudo /run/current-system/profile/bin/dmesg
  36. fi
  37. }
  38. alias ,h="${BROWSER} ~/.guix-profile/share/doc/concise-gnu-bash/concise-gnu-bash.pdf"
  39. alias ,c='command'
  40. # Count number of code lines
  41. alias ,l='tokei'
  42. # Show Git repository statics
  43. alias ,g='onefetch'
  44. alias rm="rm -i"
  45. alias cp="cp -i"
  46. alias mv="mv -i"
  47. alias mtr="sudo mtr --show-ips --aslookup"
  48. alias xclipp='xclip -selection secondary'
  49. alias svg=xdot
  50. alias hosts='hosts --auto-sudo'
  51. alias log='sudo lnav /var/log'
  52. alias lsns='lsns --notruncate --output-all'
  53. alias goaccess='goaccess --log-format=COMBINED'
  54. alias catj=gron
  55. alias sqlite-visualize=sqleton
  56. alias tree="tree --dirsfirst -C"
  57. alias free="free -mht"
  58. alias psc='ps xawf -eo pid,user,cgroup,args'
  59. alias markdown-github=grip
  60. alias iostat='iostat -xm 2'
  61. alias dhall-to-yaml='dhall-to-yaml --omit-empty'
  62. alias noise-brown="mpv --no-resume-playback --loop '/srv/video/metube/Smoothed Brown Noise.webm'"
  63. mem()
  64. {
  65. cat <<EOF
  66. The top 5 applications using most RAM:
  67. $(ps -eo pid,%mem,cmd --sort=-%mem | head -n 6)
  68. EOF
  69. }
  70. httping()
  71. {
  72. case "$1" in
  73. majordomo*)
  74. command httping -s --colors https://www.majordomo.ru
  75. ;;
  76. *)
  77. command httping -s --colors "$@"
  78. ;;
  79. esac
  80. }
  81. emacs()
  82. {
  83. case "$1" in
  84. signals)
  85. "$BROWSER" https://emacs.stackexchange.com/a/44087
  86. ;;
  87. tty)
  88. emacs -nw -Q "${@:2}"
  89. ;;
  90. *)
  91. command emacs "$@"
  92. esac
  93. }
  94. emc()
  95. {
  96. command emacsclient --create-frame --tty "$@"
  97. }
  98. guile()
  99. {
  100. (
  101. if [ "$INSIDE_EMACS" == "vterm" ]
  102. then
  103. unset INSIDE_EMACS
  104. fi
  105. case "$1" in
  106. wisp)
  107. exec -a "$0" guile -x .w --language=wisp "${@:2}"
  108. ;;
  109. *)
  110. exec -a "$0" guile "$@"
  111. ;;
  112. esac
  113. )
  114. }
  115. cat()
  116. {
  117. if [[ "${*:-1}" == /proc/*/environ ]]
  118. then
  119. command cat "${@: -1}" | tr '\000' '\n'
  120. elif [[ "${*:-1}" == *.md ]]
  121. then
  122. command glow "$@"
  123. else
  124. command cat "$@"
  125. fi
  126. }
  127. benchmark()
  128. {
  129. hyperfine "$@"
  130. }
  131. lsd()
  132. {
  133. if [ "$INSIDE_EMACS" == "vterm" ]
  134. then
  135. exa --all --long "$@"
  136. else
  137. ls "$@"
  138. fi
  139. }
  140. if [ -f "$HOME/.guix-profile/etc/profile.d/autojump.sh" ]
  141. then
  142. source "$HOME/.guix-profile/etc/profile.d/autojump.sh"
  143. fi
  144. # http://puzan.info/linux/2014-05-14-direnv.html
  145. if [ -f "$HOME/.guix-profile/bin/direnv" ]
  146. then
  147. eval "$(direnv hook bash)"
  148. fi
  149. man_in_emacs()
  150. {
  151. emacsclient --eval "(man \"$1\")"
  152. }
  153. man_to_pdf()
  154. {
  155. man -t "$1" | ps2pdf - "$1.pdf"
  156. }
  157. stat_link()
  158. {
  159. stat --format=%N "$@"
  160. }
  161. # Origin <https://www.fsf.org/blogs/directory/the-free-software-directory-needs-you-irc-meetups-every-friday-1>.
  162. #
  163. # Every Friday at 12:00-15:00 EDT (16:00 to 19:00 UTC)
  164. # meet on IRC in the #fsf channel on irc.freenode.org
  165. date_fsf()
  166. {
  167. date --date='TZ="America/New_York" 12:00 this Fri'
  168. }
  169. cl()
  170. {
  171. echo "$(tput cols)x$(tput lines)"
  172. }
  173. alias bedtime='sudo loginctl suspend'
  174. alias aria2c='aria2c --seed-time=0'
  175. ihs()
  176. {
  177. GUILE_INSTALL_LOCALE=0 \
  178. GUIX_BUILD_OPTIONS='' \
  179. IHS_USER=pyhalov \
  180. IHS_PASS="$(pass show majordomo/private/hms/pyhalov)" \
  181. CVM_USER=cron \
  182. CVM_PASS="$(pass show majordomo/private/cvm/cron)" \
  183. command ihs "$@"
  184. }
  185. if [[ -e "${HOME}/.bash.d/nix.bash" ]]
  186. then
  187. source "${HOME}/.bash.d/nix.bash"
  188. fi
  189. rg()
  190. {
  191. case "$1" in
  192. edit)
  193. last_command="$(history -w /dev/stdout | tail --lines=2 | head --lines=1)";
  194. if [[ "$last_command" =~ 'rg' ]] && [[ "$last_command" != *last_command* ]]
  195. then last_output="$(eval "$last_command")"
  196. emacsclient --no-wait +"$(echo "$last_output" | cut -d: -f 2)" "$(echo "$last_output" | cut -d: -f 1)"
  197. else
  198. emacsclient --no-wait "$@"
  199. fi
  200. stumpish emacsclient
  201. ;;
  202. *)
  203. command rg "$@"
  204. esac
  205. }
  206. # TODO: readlink could fail to enter `guix environment`
  207. # alias wi-pure-bash="env -i $(readlink $(which bash)) --noprofile --norc"
  208. # TODO: Escape window names with asterisks.
  209. # wi_x_resize_window ()
  210. # {
  211. # window_name=$(xwininfo | grep 'Window id' | awk '{ print $5 }')
  212. # xdotool search --name "$window_name" windowsize $1 $2
  213. # }
  214. iptables()
  215. {
  216. case "$1" in
  217. block)
  218. sudo --login command iptables -I INPUT -s "$2" -j DROP
  219. ;;
  220. --help)
  221. command iptables --help
  222. echo "\
  223. Extra commands:
  224. block block IP-ADDRESS on INPUT chain"
  225. ;;
  226. *)
  227. command sudo iptables "$@"
  228. ;;
  229. esac
  230. }
  231. ansible_host()
  232. {
  233. ansible --inventory "$1", "$1" --become --ask-become-pass "${@:2}"
  234. }
  235. yq()
  236. {
  237. command yq --width "$(tput cols)" "$@"
  238. }
  239. jc()
  240. {
  241. if [[ $# -eq 0 ]]
  242. then
  243. glow "https://github.com/kellyjonbrazil/jc"
  244. else
  245. command jc "$@"
  246. fi
  247. }
  248. jord_vm_ip()
  249. {
  250. gms vm ip "$1" | recsel -pip_address | awk '{ print $2 }'
  251. }
  252. jord_ansible_service_start()
  253. {
  254. vm="$(gms vm ip "$1" | recsel -pip_address | awk '{ print $2 }')"
  255. ansible --user sup --private-key="$HOME/.ssh/id_rsa_sup" --inventory "$vm", \
  256. "$vm" --module-name service --args "name=$2 state=started" \
  257. --become --ask-become-pass
  258. }
  259. jord_web_loadavg()
  260. {
  261. watch --color "seq -f 'web%gs' 15 37 | xargs guix environment -l $HOME/src/guile-loadavg/guix.scm -- $HOME/src/guile-loadavg/pre-inst-env loadavg weather"
  262. }
  263. parallel_curl()
  264. {
  265. parallel --will-cite -k 'printf "domain: %s\n" {1}; curl --max-time 10 -L -s -o /dev/null -w "ip-address: %{remote_ip}\nstatus_code: %{http_code}" {1}; printf "\n\n"' ::: "$@"
  266. }
  267. jord_web_account_check()
  268. {
  269. parallel --will-cite -k 'printf "domain: %s\n" {1}; curl --max-time 10 -L -s -o /dev/null -w "ip-address: %{remote_ip}\nstatus_code: %{http_code}" {1}; printf "\n\n"' \
  270. ::: "$(gms account website "$1" | recsel -pname | awk '{ print $2 }')"
  271. }
  272. guix_export_archive()
  273. {
  274. path="$1"
  275. destination="$2"
  276. guix archive --export -r "$path" \
  277. | ssh "$destination" guix archive --import
  278. }
  279. ssh_keygen_rsa()
  280. {
  281. file="$1"
  282. ssh-keygen -b 4096 -m pem -f "$HOME/.ssh/id_rsa_$file"
  283. }
  284. ssh_keygen_show_host_key()
  285. {
  286. ssh-keygen -lf <(ssh-keyscan -p 1022 "$1" 2>/dev/null)
  287. }
  288. activity()
  289. {
  290. ps -ef | awk '{ print $1 }' | sort | uniq | wc -l;
  291. }
  292. alias t5='TMOUT=5'
  293. alias close='TMOUT=5'
  294. # https://www.gnu.org/software/emacs/manual/html_node/efaq/Disabling-backups.html
  295. alias ls='ls -B -p --color=auto'
  296. if [ -f "$HOME/.nix-profile/share/bash-completion/completions/docker" ]
  297. then
  298. source "$HOME/.nix-profile/share/bash-completion/completions/docker"
  299. fi
  300. if [ -f "$HOME/.bash_tmp" ]
  301. then
  302. source "$HOME/.bash_tmp"
  303. fi
  304. alias bridge='bridge -color=always'
  305. alias ip='ip -color=always'
  306. jenkins_log()
  307. {
  308. job="$1"
  309. curl --silent --user "admin:$(pass show magnolia/jenkins/admin)" \
  310. "https://jenkins.wugi.info/job/$job/lastBuild/consoleText"
  311. }
  312. jenkins_jobs_list()
  313. {
  314. curl --silent --user "admin:$(pass show magnolia/jenkins/admin)" \
  315. https://jenkins.wugi.info/api/json/ \
  316. | jq -r '.jobs[] | [.name, .color] | @tsv'
  317. }
  318. jenkins_active_jobs()
  319. {
  320. jenkins-jobs | grep --color=no red_anime
  321. }
  322. grub_list_entries()
  323. {
  324. awk -F\" '$1=="menuentry " {print i++ " : " $2}' /boot/grub/grub.cfg
  325. }
  326. # https://github.com/jarun/Buku/wiki/Third-party-integration
  327. fb()
  328. {
  329. # save newline separated string into an array
  330. mapfile -t website <<< "$(buku -p -f 4 | column -ts$'\t' | fzf --multi)"
  331. # open each website
  332. for i in "${website[@]}"; do
  333. index="$(echo "$i" | awk '{print $1}')"
  334. buku -p "$index"
  335. buku -o "$index"
  336. done
  337. }
  338. guix()
  339. {
  340. guix_modules="$HOME/.local/share/chezmoi/dotfiles/guixsd/modules"
  341. guix_extra_modules=(
  342. "${HOME}/src/cgit.duckdns.org/guix/guix-wigust/guix"
  343. "${HOME}/src/gitlab.com/nonguix/nonguix"
  344. "${HOME}/src/gitlab.com/wigust/prometheus-shepherd-exporter"
  345. )
  346. case "$1" in
  347. manifest)
  348. case "$2" in
  349. apply)
  350. command guix package \
  351. -L "$HOME/.local/share/chezmoi/dotfiles/guixsd/modules" \
  352. --fallback \
  353. --manifest="$HOME/.local/share/chezmoi/dotfiles/manifests/$HOSTNAME.scm" "${@:3}"
  354. ;;
  355. build)
  356. command guix environment \
  357. -L "$HOME/.local/share/chezmoi/dotfiles/guixsd/modules" \
  358. --fallback \
  359. --manifest="$HOME/.local/share/chezmoi/dotfiles/manifests/$HOSTNAME.scm" "${@:3}" \
  360. -- sh -c 'exit 0'
  361. ;;
  362. esac
  363. ;;
  364. home)
  365. case "$2" in
  366. build)
  367. (
  368. cd "${HOME}/.local/share/chezmoi" || exit 1
  369. command guix home build \
  370. -L "$HOME/.local/share/chezmoi/dotfiles/guixsd/modules" \
  371. --fallback \
  372. "$HOME/.local/share/chezmoi/dotfiles/guixsd/home/${HOSTNAME}.scm" "${@:3}"
  373. )
  374. ;;
  375. switch)
  376. (
  377. cd "${HOME}/.local/share/chezmoi"
  378. guix_command=()
  379. if [[ "${PRE_INST_ENV:-FALSE}" == TRUE ]]
  380. then
  381. guix_command+=("${HOME}/src/git.savannah.gnu.org/git/guix/pre-inst-env")
  382. else
  383. guix_command+=("command")
  384. fi
  385. guix_command+=(
  386. guix home reconfigure
  387. )
  388. for module in "${guix_extra_modules[@]}"
  389. do
  390. guix_command+=("--load-path=${module}")
  391. done
  392. guix_command+=(
  393. --load-path="$guix_modules"
  394. --fallback
  395. )
  396. if [[ -n $allow_downgrades ]]
  397. then
  398. guix_command+=("--allow-downgrades")
  399. fi
  400. if [[ -n $fallback ]]
  401. then
  402. guix_command+=("--fallback")
  403. fi
  404. guix_command+=(
  405. "$HOME/.local/share/chezmoi/dotfiles/guixsd/home/${HOSTNAME}.scm"
  406. )
  407. echo "${guix_command[@]}"
  408. )
  409. ;;
  410. esac
  411. ;;
  412. maintainer)
  413. mapfile -t output < <(git log --grep="gnu: Add" --format=%s --author=go.wigust@gmail.com)
  414. for line in "${output[@]}"
  415. do
  416. package="${line/gnu: Add /}"
  417. package="${package::-1}"
  418. case "$package" in
  419. *Revert*|*service*|premake4|perl-strictures-2|perl-role-tiny-2|emacs-cl-generic|emacs-seq|emacs-emms-player-simple-mpv|php-with-bcmath|shlomif-cmake-modules|go-github-com-muesli-reflow|go-github-com-emirpasic-gods-trees-binaryheap|nginx-socket-cloexec)
  420. printf ""
  421. ;;
  422. *go-github-com*)
  423. printf ""
  424. ;;
  425. *zabbix-agentd*)
  426. printf "zabbix-agentd\nzabbix-server\n"
  427. ;;
  428. *)
  429. echo "$package"
  430. ;;
  431. esac
  432. done
  433. ;;
  434. menu)
  435. {
  436. exec 3>&1
  437. mapfile -t options < <(
  438. dialog --separate-output --title "Guix" --checklist "Guix Options" 22 72 3 \
  439. -- --no-offload "--no-offload" off \
  440. --commit "--commit" off \
  441. --allow-downgrades "--allow-downgrades" off \
  442. --fallback "--fallback" off \
  443. --disable-authentication "--disable-authentication" off \
  444. 2>&1 2>&1 1>&3
  445. )
  446. declare -p options
  447. for option in "${options[@]}"
  448. do
  449. if [[ "$option" == "--commit" ]]
  450. then
  451. exec 3>&1
  452. options=("${options[@]/--commit}")
  453. commit=$(dialog --backtitle "Guix commit" --inputbox "Enter Guix's Git commit" 8 60 2>&1 1>&3)
  454. exec 3>&-
  455. fi
  456. if [[ "$option" == "--allow-downgrades" ]]
  457. then
  458. options=("${options[@]/--allow-downgrades}")
  459. allow_downgrades=true
  460. fi
  461. if [[ "$option" == "--fallback" ]]
  462. then
  463. options=("${options[@]/--fallback}")
  464. fallback=true
  465. fi
  466. if [[ "$option" == "--disable-authentication" ]]
  467. then
  468. options=("${options[@]/--disable-authentication}")
  469. disable_authentication=true
  470. fi
  471. done
  472. }
  473. {
  474. exec 3>&1
  475. mapfile -t selection < <(
  476. dialog --separate-output --title "Guix" --checklist "Guix Operations" 22 72 3 \
  477. "pull channels" "Test build and update channel file" "${STATE:-off}" \
  478. pull "Pull new changes" "${STATE:-on}" \
  479. "system build" "Build system" "${STATE:-on}" \
  480. "home build" "Build home" "${STATE:-on}" \
  481. "home switch" "Reconfigure home" "${STATE:-on}" \
  482. "manifest apply" "Apply package manifest" "${STATE:-on}" \
  483. "system switch" "Reconfigure system" "${STATE:-on}" \
  484. "distribute" "Pull on remote hosts" "${STATE:-off}" \
  485. "system deploy" "Deploy to remote hosts" "${STATE:-off}" \
  486. 2>&1 2>&1 1>&3
  487. )
  488. declare -p selection
  489. exec 3>&-
  490. }
  491. clear
  492. (
  493. set -ex
  494. for operation in "${selection[@]}"
  495. do
  496. eval "guix $operation $options"
  497. done
  498. )
  499. ;;
  500. pull)
  501. case "$2" in
  502. channels)
  503. (
  504. set -e
  505. cd "${HOME}/.local/share/chezmoi" || exit 1
  506. make dotfiles/channels-current.scm
  507. if [[ $(git diff dotfiles/channels-current.scm) != "" ]]
  508. then
  509. if git commit -m "channels-current: Update." dotfiles/channels-current.scm
  510. then
  511. :
  512. else
  513. exit 1
  514. fi
  515. fi
  516. )
  517. ;;
  518. *)
  519. guix_command=(
  520. "command"
  521. "guix" "pull"
  522. "--channels=${HOME}/.local/share/chezmoi/dotfiles/channels-current.scm"
  523. )
  524. if [[ -n $commit ]]
  525. then
  526. guix_command+=("--commit=$commit")
  527. fi
  528. if [[ -n $allow_downgrades ]]
  529. then
  530. guix_command+=("--allow-downgrades")
  531. fi
  532. if [[ -n $fallback ]]
  533. then
  534. guix_command+=("--fallback")
  535. fi
  536. if [[ -n $disable_authentication ]]
  537. then
  538. guix_command+=("--disable-authentication")
  539. fi
  540. (set -ex; "${guix_command[@]}" "${@:2}"; sudo --login "${guix_command[@]}" "${@:2}")
  541. esac
  542. ;;
  543. distribute)
  544. "$(guix build --file="$HOME/.local/share/chezmoi/dotfiles/ansible/guix.scm" "${@:2}")"
  545. ;;
  546. prebuild)
  547. guix_command=(
  548. echo
  549. sudo --login
  550. GUIX_PACKAGE_PATH="$guix_modules"
  551. "${HOME}/src/git.savannah.gnu.org/git/guix/pre-inst-env"
  552. guix
  553. system
  554. build
  555. --fallback
  556. --no-grafts
  557. )
  558. for module in "${guix_extra_modules[@]}"
  559. do
  560. guix_command+=("--load-path=${module}")
  561. done
  562. guix_command+=(
  563. "$HOME/.local/share/chezmoi/dotfiles/guixsd/guixsd.scm"
  564. )
  565. sudo --login "${guix_command[@]}" "${@:2}"
  566. ;;
  567. prereconfigure)
  568. guix_command=(
  569. "${HOME}/src/git.savannah.gnu.org/git/guix/pre-inst-env"
  570. "guix"
  571. )
  572. for module in "${guix_extra_modules[@]}"
  573. do
  574. guix_command+=("--load-path=${module}")
  575. done
  576. guix_command+=(
  577. system reconfigure
  578. --load-path="$guix_modules"
  579. --fallback "$HOME/.local/share/chezmoi/dotfiles/guixsd/guixsd.scm"
  580. --no-grafts
  581. "$HOME/.local/share/chezmoi/dotfiles/guixsd/guixsd.scm"
  582. )
  583. sudo --login "${guix_command[@]}" "${@:2}"
  584. ;;
  585. # repl)
  586. # GUILE_AUTO_COMPILE=0 command guix repl -L "$guix_modules" "${@:3}"
  587. # ;;
  588. remote)
  589. case "$2" in
  590. reboot)
  591. ansible-playbook <(
  592. cat <<'EOF'
  593. - hosts: guix_vm
  594. become: true
  595. tasks:
  596. - reboot: search_paths=/home/oleg/bin
  597. EOF
  598. )
  599. ;;
  600. esac
  601. ;;
  602. system)
  603. case "$2" in
  604. build)
  605. sudo --login GUILE_LOAD_PATH="${HOME}/.local/share/chezmoi/dotfiles/guixsd/modules:${GUILE_LOAD_PATH}" command guix system build \
  606. -L "$guix_modules" \
  607. "${@:3}" \
  608. "$HOME/.local/share/chezmoi/dotfiles/guixsd/$HOSTNAME.scm"
  609. ;;
  610. deploy)
  611. (
  612. set -ex
  613. guix deploy -L "$guix_modules" "${@:3}" \
  614. "$HOME/.local/share/chezmoi/dotfiles/guixsd/deploy.scm"
  615. )
  616. ;;
  617. reconfigure)
  618. sudo --login GUILE_LOAD_PATH="${HOME}/.local/share/chezmoi/dotfiles/guixsd/modules:${GUILE_LOAD_PATH}" command guix system reconfigure \
  619. -L "$guix_modules" \
  620. --no-bootloader "${@:3}" \
  621. "$HOME/.local/share/chezmoi/dotfiles/guixsd/$HOSTNAME.scm"
  622. ;;
  623. switch)
  624. guix_command=(
  625. sudo --login GUILE_LOAD_PATH="${HOME}/.local/share/chezmoi/dotfiles/guixsd/modules:${GUILE_LOAD_PATH}" command guix system reconfigure \
  626. -L "$guix_modules" \
  627. "${@:3}" \
  628. "$HOME/.local/share/chezmoi/dotfiles/guixsd/$HOSTNAME.scm"
  629. )
  630. if [[ -n $allow_downgrades ]]
  631. then
  632. guix_command+=("--allow-downgrades")
  633. fi
  634. "${guix_command[@]}"
  635. if [[ -n $fallback ]]
  636. then
  637. guix_command+=("--fallback")
  638. fi
  639. ;;
  640. *)
  641. command guix system "${@:2}"
  642. ;;
  643. esac
  644. ;;
  645. update)
  646. (
  647. set -e
  648. guix pull
  649. guix system build
  650. guix manifest
  651. guix system switch
  652. set +e
  653. )
  654. ;;
  655. master-staging)
  656. git -C "/home/oleg/src/git.savannah.gnu.org/git/guix" shortlog -n upstream/master..upstream/staging
  657. ;;
  658. qemu)
  659. # https://wiki.archlinux.org/index.php/QEMU#Creating_bridge_manually
  660. printf -v macaddr "52:54:%02x:%02x:%02x:%02x" \
  661. $(( RANDOM & 0xff)) $(( RANDOM & 0xff )) $(( RANDOM & 0xff)) $(( RANDOM & 0xff ))
  662. sudo command qemu-system-x86_64 -daemonize -enable-kvm \
  663. -smp cores=4,threads=1 -cpu host -m 4096 -vga virtio -full-screen \
  664. -net nic,model=virtio,macaddr="$macaddr" -net bridge,br=br0 \
  665. "$@"
  666. ;;
  667. clean)
  668. (
  669. set -x
  670. command guix package --delete-generations=1m
  671. command guix pull --delete-generations=1m
  672. command guix gc --list-failures | xargs guix gc --clear-failures
  673. sudo --login command guix system delete-generations 1m
  674. sudo --login command guix pull --delete-generations=1m
  675. )
  676. ;;
  677. profiles)
  678. echo "$(readlink --canonicalize $HOME/.guix-profile)" \
  679. "$(readlink --canonicalize /run/current-system)" \
  680. "$(readlink --canonicalize $HOME/.config/guix/current)"
  681. ;;
  682. test-vm)
  683. eval "$(sed 's,-nic user[^ ]* ,,' "$(./pre-inst-env guix system vm --no-offload "$2")" | tail -1 | sed 's/\sexec\s//') -m 4096 -smp 2 -nic user,model=virtio-net-pci,hostfwd=tcp::10022-:22"
  684. ;;
  685. updatedb)
  686. updatedb \
  687. --localpaths=/gnu/store \
  688. --findoptions='( -path /gnu/store/.links -o -name *.drv -o -name *.chroot ) -prune -o -type f -print' \
  689. --output="$HOME/.config/guix/dbfile"
  690. ;;
  691. locate)
  692. locate -d "$HOME/.config/guix/dbfile" "${@:2}"
  693. ;;
  694. xpanes)
  695. guix_machines=(
  696. vm1.wugi.info
  697. vm2.wugi.info
  698. vm3.wugi.info
  699. vm4.wugi.info
  700. vm5.wugi.info
  701. ws1.wugi.info
  702. localhost
  703. )
  704. xpanes -c 'ssh {}' "${guix_machines[@]}"
  705. ;;
  706. depends)
  707. for package in $(guix package --list-installed | cut --fields=1)
  708. do
  709. if guix graph "$package" | grep --quiet "$2"
  710. then
  711. echo "$package"
  712. fi
  713. done
  714. ;;
  715. *)
  716. command guix "$@"
  717. ;;
  718. esac
  719. }
  720. maintenance()
  721. {
  722. case "$1" in
  723. upgrade)
  724. (
  725. set -xe
  726. cd "${HOME}/.local/share/chezmoi" || exit 1
  727. make dotfiles/nix/flake.lock
  728. if make dotfiles/nix/flake.nix
  729. then
  730. :
  731. else
  732. git restore dotfiles/nix/flake.lock
  733. fi
  734. )
  735. ;;
  736. *)
  737. command nix "$@"
  738. ;;
  739. esac
  740. }
  741. listen_ports()
  742. {
  743. ss -tulpn | awk '{ print $5 }' | cut -d: -f 2 | sort -un | xargs echo
  744. }
  745. alias vnc-server-android="vncserver -AcceptSetDesktopSize=no -geometry 1280x720"
  746. # jenkins_log()
  747. # {
  748. # for project in $(curl -s -k "https://admin:$(pass show majordomo/public/jenkins.intr/admin)@jenkins.intr/api/json?pretty=true" | jq -r '.jobs[] | .name'); do
  749. # for job in $(curl -s -k "https://admin:$(pass show majordomo/public/jenkins.intr/admin)@jenkins.intr/job/$project/api/json" | jq -r '.jobs[] | .url'); do
  750. # echo "@ $job" |
  751. # curl -u "admin:$(pass show majordomo/public/jenkins.intr/admin)" -s -k "$job/job/master/lastBuild/consoleText"
  752. # done
  753. # done
  754. # }
  755. # https://markhneedham.com/blog/2015/11/14/jq-filtering-missing-keys/
  756. alias get-todos='scp work:/home/user/src/jord/doc/todo.org ~/src/todo.org '
  757. # nix-shell -E 'with import <nixpkgs> {}; callPackage ./default.nix {}' -A luaCrypto
  758. # nix-build -E 'with import <nixpkgs> {}; callPackage ./default.nix {}';
  759. # grep '^FAIL ' /tmp/6.txt | cut -d '/' -f 2- | cut -d ']' -f 1 | sed 's@^@ext/@'
  760. nix_untar_docker()
  761. {
  762. # argument example: /nix/store/vqi100nf7x7z82pr4lkagasmzl9zj0zp-docker-image-apache2-php56.tar.gz
  763. tar xv --wildcards '*/layer.tar' -f "$1" \
  764. | tee /tmp/tempstore \
  765. | xargs -n1 -I{} tar xvf {}
  766. }
  767. tmux_renumber_windows()
  768. {
  769. tmux movew -r
  770. }
  771. nix_user_uid()
  772. {
  773. nix-build -E 'with import <nixpkgs> {}; runCommand "foo" {} "id"'
  774. }
  775. nix_info()
  776. {
  777. nix-shell -p nix-info --run "nix-info -m"
  778. }
  779. guix_packages_json()
  780. {
  781. curl https://guix.gnu.org/packages.json
  782. }
  783. git_guix_pre_new_build()
  784. {
  785. number="$1"
  786. git log --oneline \
  787. | head -n "$number" \
  788. | grep Add \
  789. | awk '{ print $NF }' \
  790. | cut -d'.' -f 1 \
  791. | xargs ./pre-inst-env guix build --no-grafts
  792. }
  793. git_guix_pre_new_lint()
  794. {
  795. number="$1"
  796. git log --oneline \
  797. | head -n "$number" \
  798. | grep Add \
  799. | awk '{ print $NF }' \
  800. | cut -d'.' -f 1 \
  801. | xargs ./pre-inst-env guix lint
  802. }
  803. git_guix_pre_update()
  804. {
  805. number="$1"
  806. git log --oneline \
  807. | head -n "$number" \
  808. | grep Update \
  809. | awk '{ print $3 }' \
  810. | cut -d: -f 1 \
  811. | xargs ./pre-inst-env guix build --no-grafts
  812. }
  813. git_guix_home()
  814. {
  815. number="$1"
  816. git show "$number" | grep home | cut -d'"' -f 2
  817. }
  818. # archive_dir()
  819. # {
  820. # for dir in $@; do
  821. # mv -vi "$dir" "$HOME/archive/src/"
  822. # done
  823. # }
  824. terraform_init_with_nix()
  825. {
  826. terraform init -plugin-dir ~/.nix-profile/bin "$@"
  827. }
  828. terraform_init()
  829. {
  830. terraform_init_with_nix -plugin-dir ~/go/src/gitlab.intr/majordomo/terraform-provider-majordomo
  831. }
  832. alias tsw='tmuxifier s web'
  833. alias nn='notmuch new'
  834. alias wtr='curl -H "Accept-Language: ru" wttr.in/Санкт-Петербург'
  835. alias hylang='docker run --rm -it hylang'
  836. nix_build()
  837. {
  838. nix_build="$(which nix-build)"
  839. if [[ "$(readlink -f "$PWD")" =~ "majordomo" ]]
  840. then
  841. cmd="sshpass -Ppassphrase -p$(pass show majordomo/private/gitlab.intr/ssh/id_rsa_gitlab_intr) $nix_build --no-out-link"
  842. if grep --quiet buildLayeredImage default.nix
  843. then
  844. cmd="docker load --input $($cmd)"
  845. fi
  846. else
  847. cmd="$nix_build --no-out-link"
  848. fi
  849. case "$1" in
  850. 20.03)
  851. $cmd "$HOME/src/nixpkgs-master" "${@:2}"
  852. ;;
  853. 19.09)
  854. $cmd "$HOME/src/nixpkgs-master" "${@:2}"
  855. ;;
  856. master)
  857. $cmd "$HOME/src/nixpkgs-master" "${@:2}"
  858. ;;
  859. unstable)
  860. $cmd "$HOME/.nix-defexpr/channels/nixos-unstable" "${@:2}"
  861. ;;
  862. *)
  863. $cmd '<nixpkgs>' "$@"
  864. esac
  865. }
  866. nix_shell_python()
  867. {
  868. PYTHONPATH="" nix-shell -p "python35.withPackages(ps: with ps; [ $* ])"
  869. }
  870. nixos_interactive_test()
  871. {
  872. version="$1"
  873. nix-build build.nix \
  874. --cores 4 \
  875. -A "nixpkgsUnstable.php$version-test.driver" \
  876. --no-out-link --show-trace/bin/nixos-run-vms
  877. }
  878. terraform_refresh()
  879. {
  880. NIX_SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/Majordomo_LLC_Root_CA.crt" \
  881. SSL_CERT_DIR="$HOME/.guix-profile/etc/ssl/certs" \
  882. SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/ca-certificates.crt" \
  883. terraform refresh
  884. }
  885. terraform_plan()
  886. {
  887. NIX_SSL_CERT_FILE="$HOME/src/gitlab.intr/office/ssl-certificates/Majordomo_LLC_Root_CA.crt" \
  888. SSL_CERT_DIR="$HOME/.guix-profile/etc/ssl/certs" \
  889. SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/ca-certificates.crt" \
  890. terraform plan -out=plan "$@"
  891. }
  892. terraform_import()
  893. {
  894. NIX_SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/Majordomo_LLC_Root_CA.crt" \
  895. SSL_CERT_DIR="$HOME/.guix-profile/etc/ssl/certs" \
  896. SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/ca-certificates.crt" \
  897. terraform import "$@"
  898. }
  899. terraform_apply()
  900. {
  901. NIX_SSL_CERT_FILE="$HOME/src/gitlab.intr/office/ssl-certificates/Majordomo_LLC_Root_CA.crt" \
  902. SSL_CERT_DIR="$HOME/.guix-profile/etc/ssl/certs" \
  903. SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/ca-certificates.crt" \
  904. terraform apply "plan"
  905. }
  906. terraform_apply_no_plan()
  907. {
  908. NIX_SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/Majordomo_LLC_Root_CA.crt" \
  909. SSL_CERT_DIR="$HOME/.guix-profile/etc/ssl/certs" \
  910. SSL_CERT_FILE="$HOME/.guix-profile/etc/ssl/certs/ca-certificates.crt" \
  911. terraform apply
  912. }
  913. terraform_apply_gitlab()
  914. {
  915. grep resource gitlab/*.tf \
  916. | awk '{ gsub("\"",""); print $2, $3 }' \
  917. | sed 's/\s/./' \
  918. | sed 's/^/-target=/' \
  919. | xargs env NIX_SSL_CERT_FILE="/run/current-system/profile/etc/ssl/certs/Majordomo_LLC_Root_CA.crt" SSL_CERT_DIR="/run/current-system/profile/etc/ssl/certs" SSL_CERT_FILE="/run/current-system/profile/etc/ssl/certs/ca-certificates.crt" terraform apply plan
  920. }
  921. terraform_plan_gitlab()
  922. {
  923. grep resource gitlab/*.tf \
  924. | awk '{ gsub("\"",""); print $2, $3 }' \
  925. | sed 's/\s/./' \
  926. | sed 's/^/-target=/' \
  927. | xargs env NIX_SSL_CERT_FILE="/run/current-system/profile/etc/ssl/certs/Majordomo_LLC_Root_CA.crt" SSL_CERT_DIR="/run/current-system/profile/etc/ssl/certs" SSL_CERT_FILE="/run/current-system/profile/etc/ssl/certs/ca-certificates.crt" terraform plan -out=plan
  928. }
  929. terraformer_import_github()
  930. {
  931. terraformer import majordomo \
  932. --token "$GITHUB_TOKEN" \
  933. --organizations wugi-emacs \
  934. --resources=repositories
  935. }
  936. spb_log()
  937. {
  938. ssh spb -- bzcat "$1"
  939. }
  940. ssh_guix_build_log_file()
  941. {
  942. host="$1"
  943. file="$2"
  944. ssh "$host" -- guix build --log-file "$file" | xargs ssh "$host" -- bzcat
  945. }
  946. herd_reload()
  947. {
  948. herd reload root "$HOME/.config/shepherd/init.scm"
  949. }
  950. herd_kill_services_via_awk()
  951. {
  952. for service in $(awk '/define.*service/ { print $NF }' "$HOME/.config/shepherd/init.scm" | sed 's/-service//'); do
  953. pkill "$service"
  954. done
  955. }
  956. docker_strace()
  957. {
  958. container="$1"
  959. docker top "$container" | tail -n +2 | awk '{ print $2 }' | sed 's/^/-p/' | xargs sudo strace -o "$HOME/$container.strace" -f -s 4096
  960. }
  961. docker_strace_pids()
  962. {
  963. container="$1"
  964. docker top "$container" | tail -n +2 | awk '{ print $2 }' | sed 's/^/-p/';
  965. }
  966. vnc_server_zero()
  967. {
  968. sudo chmod 666 /var/run/slim-vt7.auth
  969. xauth merge /var/run/slim-vt7.auth
  970. while true; do
  971. guix shell tigervnc-server -- x0vncserver -PasswordFile "$HOME/.vnc/passwd" -display :0 -rfbport 5960
  972. sleep 5
  973. done
  974. }
  975. ansible_cmdb_my()
  976. {
  977. # https://itnext.io/create-a-host-inventory-in-a-minute-with-ansible-c7bf251166d9
  978. ansible -m setup --tree out/ all
  979. ansible-cmdb -t html_fancy_split -p local_js=1 out/
  980. }
  981. ansible_playbook_wrapper()
  982. {
  983. host="$1"
  984. ANSIBLE_HOST_KEY_CHECKING="False" ansible-playbook \
  985. --private-key="$HOME/.ssh/id_rsa" \
  986. --user=root \
  987. --inventory="$host", \
  988. --extra-vars=host="$host" \
  989. --extra-vars=ansible_python_interpreter=/usr/bin/python3 \
  990. "${@:2}"
  991. }
  992. bash_history_top()
  993. {
  994. # https://www.commandlinefu.com/commands/view/604/list-of-commands-you-use-most-often
  995. history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
  996. }
  997. fast_scan()
  998. {
  999. parallel -j200% -n1 -a textfile-with-hosts.txt nc -vz {} ::: 22
  1000. }
  1001. alias guix-docker-image-minimal='guix pack -f docker --symlink=/bin=bin bash'
  1002. alias find-yml="find -maxdepth 2 -name '*.yml' | grep -vF '.travis.yml' | grep -vF '.gitlab'"
  1003. alias docker-describe-image='dive'
  1004. alias inxi='inxi --full'
  1005. alias ansible-playbook-ping-all="ansible-playbook <(echo -e '---\n- hosts: all\n tasks:\n - ping:')"
  1006. alias ansible-playbook-cache-all="ansible-playbook <(echo -e '---\n- hosts:\n - all\n gather_facts: True\n')"
  1007. alias clock='while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &'
  1008. alias type-like-movie='echo "You can simulate on-screen typing just like in the movies" | pv -qL 10'
  1009. alias top-by-memory='ps aux | sort -nk +4 | tail'
  1010. alias smtpd='python -m smtpd -n -c DebuggingServer localhost:1025'
  1011. alias biggest='du -s * | sort -n | tail'
  1012. alias colors='for code in {0..255}; do echo -e "\\e[38;05;${code}m $code: Test"; done'
  1013. alias ps-tree='ps awwfux | less -S'
  1014. alias share='script -qf | tee >(nc -kl 5000) >(nc -kl 5001) >(nc -kl 5002)'
  1015. alias internet-programs='lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2'
  1016. md()
  1017. {
  1018. mkdir -p "$@" && cd "$@"
  1019. }
  1020. kernel_graph()
  1021. {
  1022. lsmod \
  1023. | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' \
  1024. | dot -Tpng \
  1025. | feh -
  1026. }
  1027. netstat_graph()
  1028. {
  1029. netstat -an \
  1030. | grep ESTABLISHED \
  1031. | awk '{print $5}' \
  1032. | awk -F: '{print $1}' \
  1033. | sort \
  1034. | uniq -c \
  1035. | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print "" }' \
  1036. | sort -k2 -n
  1037. }
  1038. random_password()
  1039. {
  1040. length="$1"
  1041. strings /dev/urandom | grep -o '[[:alnum:]]' | head -n "$length" | tr -d '\n'
  1042. }
  1043. # watch -n 1 mysqladmin --user=<user> --password=<password> processlist
  1044. alias alexa-top="curl -qsSl http://s3.amazonaws.com/alexa-static/top-1m.csv.zip 2>/dev/null | zcat | grep .de | head -1000 | awk -F, '{print }'"
  1045. dnsperf_my()
  1046. {
  1047. # https://muff.kiev.ua/content/dnsperf-testirovanie-proizvoditelnosti-dns-servera
  1048. sudo dnsperf -d ~/Downloads/dnsperf-example.txt -s 127.0.0.1 -l 60
  1049. }
  1050. tmuxifier_webs_user()
  1051. {
  1052. account="$(ihs web unix "$1")"
  1053. TMUXIFIER_USER="$(echo "$account" | recsel -P name)" \
  1054. TMUXIFIER_HOST="$(echo "$account" | recsel -P server_name)"s \
  1055. tmuxifier s ssh
  1056. }
  1057. tmuxifier_connect_host()
  1058. {
  1059. host="$1"
  1060. TMUXIFIER_USER=root TMUXIFIER_HOST="$host" tmuxifier w ssh-sudo
  1061. }
  1062. git_clean_up()
  1063. {
  1064. for dir in apache2-php52 apache2-php53 apache2-php54 apache2-php55 apache2-php56 apache2-php70 apache2-php71 apache2-php72 apache2-php73 ; do
  1065. cd $dir
  1066. for branch in $(git branch -r | grep -v master | sed 's|origin/||'); do
  1067. git push origin --delete "$branch"
  1068. done
  1069. cd -
  1070. done
  1071. }
  1072. tmux_ls()
  1073. {
  1074. command tmux ls | cut -d ':' -f 1 2>/dev/null
  1075. }
  1076. tmux()
  1077. {
  1078. case "$1" in
  1079. list)
  1080. session="$(tmux-ls | fzf)"
  1081. if [ "$session" == "" ]
  1082. then
  1083. return 1
  1084. else
  1085. command tmux at -t "$session"
  1086. fi
  1087. ;;
  1088. *)
  1089. command tmux "$@"
  1090. ;;
  1091. esac
  1092. }
  1093. docker_xorg()
  1094. {
  1095. xhost +local:
  1096. # MAYBE:
  1097. # --device /dev/video0 \
  1098. docker run -it \
  1099. -v /tmp/.X11-unix:/tmp/.X11-unix \
  1100. -e DISPLAY \
  1101. --device /dev/dri \
  1102. --device /dev/snd \
  1103. -v /etc/localtime:/etc/localtime:ro \
  1104. --device /dev/input \
  1105. "$@"
  1106. }
  1107. obs_docker_setup()
  1108. {
  1109. # https://github.com/mviereck/x11docker/wiki/Container-sound:-ALSA-or-Pulseaudio
  1110. "$(guix build pulseaudio)/bin/pactl" load-module module-native-protocol-unix socket=/tmp/pulseaudio.socket
  1111. cat > /tmp/pulseaudio.client.conf << EOF
  1112. default-server = unix:/tmp/pulseaudio.socket
  1113. # Prevent a server running in the container
  1114. autospawn = no
  1115. daemon-binary = /bin/true
  1116. # Prevent the use of shared memory
  1117. enable-shm = false
  1118. EOF
  1119. }
  1120. obs_docker()
  1121. {
  1122. docker-xorg \
  1123. --name obs \
  1124. --rm \
  1125. --env PULSE_SERVER=unix:/tmp/pulseaudio.socket \
  1126. --env PULSE_COOKIE=/tmp/pulseaudio.cookie \
  1127. --volume /tmp/pulseaudio.socket:/tmp/pulseaudio.socket \
  1128. --volume /tmp/pulseaudio.client.conf:/etc/pulse/client.conf \
  1129. --volume /home/oleg/obs:/home/obs \
  1130. --volume /home/oleg/.Xauthority:/home/oleg/.Xauthority \
  1131. --volume /srv/music/mp3:/home/obs/music \
  1132. obs
  1133. }
  1134. pacmd()
  1135. {
  1136. case "$1" in
  1137. ladspa)
  1138. command pacmd load-module module-ladspa-sink sink_name=compressor-stereo plugin=sc4_1882 label=sc4 control=1,1.5,401,-30,20,5,12
  1139. ;;
  1140. *)
  1141. command pacmd "$@"
  1142. ;;
  1143. esac
  1144. }
  1145. projectile_ls()
  1146. {
  1147. bash -c 'echo ${0:1:-1}' \
  1148. "$(printf "%b" "$(emacsclient -e "(mapconcat 'identity (mapcar #'expand-file-name (projectile-load-known-projects)) \"\n\")")")" \
  1149. | tr ' ' '\n'
  1150. }
  1151. projectile_command()
  1152. {
  1153. "$@" "$(projectile-ls | fzf)"
  1154. }
  1155. alias projectile-cd="projectile-command cd"
  1156. alias projectile-magit="projectile-command magit"
  1157. microseconds_to_seconds()
  1158. {
  1159. seconds="$1"
  1160. microseconds=$(echo "scale=2;${seconds}/1000000" | bc)
  1161. echo "$microseconds"
  1162. }
  1163. test_openvpn()
  1164. {
  1165. server="$1"
  1166. # https://serverfault.com/questions/262474/how-to-check-that-an-openvpn-server-is-listening-on-a-remote-port-without-using
  1167. echo -e "\x38\x01\x00\x00\x00\x00\x00\x00\x00" |
  1168. timeout 10 nc -u "$server" 1194 | cat -v
  1169. # Output example: @$M-^HM--LdM-t|M-^X^@^@^@^@^@@$M-^HM--LdM-t|M-^X^@^@^@^@^@@$M-^HM--LdM-t|M-^X...
  1170. }
  1171. # jq like for http
  1172. alias hq='pup'
  1173. guix_graph_chromium()
  1174. {
  1175. package="$1"
  1176. GUILE_LOAD_PATH=/home/oleg/src/git.savannah.gnu.org/git/guix:GUILE_LOAD_PATH guix graph -b d3js \
  1177. "$package" > /tmp/out.html && chromium --app=file:///tmp/out.html
  1178. }
  1179. docker_run_ansible()
  1180. {
  1181. docker run \
  1182. --network=host \
  1183. -v /home/oleg/src/dotfiles:/root/src/dotfiles \
  1184. -v /root/.ssh:/root/.ssh \
  1185. -v /home/oleg/.ansible-hosts:/etc/ansible/hosts \
  1186. -v /home/oleg/telnet.yml:/telnet.yml \
  1187. -v /home/oleg/ansible-out/ansible.cfg:/etc/ansible/ansible.cfg \
  1188. --rm -it quay.io/ansible/molecule:2.22 sh
  1189. }
  1190. alias bash-pure='env -i "$(command -v bash)" --login --noprofile --norc'
  1191. alias root-shedule="sudo herd schedule mcron 10"
  1192. alias tmux-reload="tmux source-file ~/.tmux.conf"
  1193. less_color()
  1194. {
  1195. LESSOPEN="| ~/.guix-profile/bin/src-hilite-lesspipe.sh %s"
  1196. export LESSOPEN
  1197. LESS=' -R '
  1198. export LESS
  1199. }
  1200. container()
  1201. {
  1202. case "$1" in
  1203. lint)
  1204. docker run --rm --interactive hadolint/hadolint:v1.17.2-8-g65736cb-debian < Dockerfile
  1205. ;;
  1206. ip)
  1207. docker ps --format='{{.ID}}' \
  1208. | xargs docker inspect \
  1209. | jq -r '.[] | [.NetworkSettings.Networks.bridge.IPAddress, .Config.Image] | @tsv'
  1210. ;;
  1211. intr-list)
  1212. mjru-docker-list-intr
  1213. ;;
  1214. intr-pull)
  1215. group="$1" # For example: “mail”.
  1216. for repo in $docker_list_intr; do
  1217. docker pull "docker-registry.intr/$repo"
  1218. done
  1219. ;;
  1220. intr-ps)
  1221. ANSIBLE_NOCOLOR=1 ansible swarm -m shell -a 'docker ps --no-trunc' --become --ask-become-pass
  1222. ;;
  1223. *)
  1224. docker "$@"
  1225. ;;
  1226. esac
  1227. }
  1228. if [ -f /run/current-system/profile/etc/bash_completion.d/docker ]; then
  1229. source /run/current-system/profile/etc/bash_completion.d/docker
  1230. complete -F _docker container
  1231. fi
  1232. ip_to_decimal()
  1233. {
  1234. ip="$1" # e.g. 127.0.0.1
  1235. perl -le "print unpack(\"N\", $ip)"
  1236. }
  1237. alias random-pass="perl -le 'print map { (a..z)[rand 26] } 1..8'"
  1238. urlescape ()
  1239. {
  1240. perl -MURI::Escape -lne 'print uri_escape($_)' <<< "$1"
  1241. }
  1242. urlunescape ()
  1243. {
  1244. perl -MURI::Escape -lne 'print uri_unescape($_)' <<< "$1"
  1245. }
  1246. build_farm()
  1247. {
  1248. emacsclient -e "(wi-build-farm \"$1\")"
  1249. }
  1250. find_touch_go()
  1251. {
  1252. find . -name '*.go' -exec touch {} +
  1253. }
  1254. function password()
  1255. {
  1256. if [ "$#" -ne 2 ]
  1257. then
  1258. (
  1259. cd "$HOME/.password-store" || exit
  1260. pass show "$(find . -not -path './.gitattributes' -not -path './.git/*' -type f | sed 's@\./@@' | sed 's@\.gpg@@' | fzf)" \
  1261. | xclip -i -sel p -f | xclip -i -sel c
  1262. )
  1263. else
  1264. pass "$@"
  1265. fi
  1266. }
  1267. if [ -f "$HOME/.guix-profile/etc/bash_completion.d/pass" ]; then
  1268. source "$HOME/.guix-profile/etc/bash_completion.d/pass"
  1269. complete -o filenames -F _pass password
  1270. fi
  1271. alias password='EDITOR=nano password'
  1272. genpass()
  1273. {
  1274. tr -dc 'a-zA-Z0-9_#@.-' < /dev/urandom | head -c "${1:-14}"
  1275. }
  1276. pass_list_all()
  1277. {
  1278. (
  1279. cd ~/.password-store || exit
  1280. for password in $(find . -not -path './.gitattributes' -not -path './.git/*' -type f | sed 's@\./@@' | sed 's@\.gpg@@'); do
  1281. pass show "$password" | tr -d '\n'
  1282. echo
  1283. done
  1284. )
  1285. }
  1286. gmail_mail()
  1287. {
  1288. # Source: https://www.commandlinefu.com/commands/view/3380/check-your-unread-gmail-from-the-command-line
  1289. curl -u "go.wigust:$(pass show myaccount.google.com/apppasswords/go.wigust)" \
  1290. --silent "https://mail.google.com/mail/feed/atom" \
  1291. | tr -d '\n' \
  1292. | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' \
  1293. | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p"
  1294. # Alternative variant:
  1295. # Checks your unread Gmail from the command line
  1296. # curl -u username --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print "\t" if /<name>/; print "$2\n" if /<(title|name)>(.*)<\/\1>/;
  1297. }
  1298. gmail_send()
  1299. {
  1300. rcpt="$1"
  1301. # Send email with curl and gmail
  1302. curl -n --ssl-reqd --mail-from "<go.wigust@gmail.com>" --mail-rcpt "$rcpt" --url smtps://smtp.gmail.com:465 -T file.txt
  1303. }
  1304. cmdfu()
  1305. {
  1306. curl -L "http://www.commandlinefu.com/commands/matching/$*/$(echo -n "$@" | openssl base64)/plaintext";
  1307. }
  1308. clfavs()
  1309. {
  1310. # backup all your commandlinefu.com favourites to a plaintext file
  1311. URL="http://www.commandlinefu.com"
  1312. wget -O - --save-cookies c --post-data "username=$1&password=$2&submit=Let+me+in" $URL/users/signin
  1313. for i in $(seq 0 25 "$3"); do
  1314. wget -O - --load-cookies c "$URL/commands/favourites/plaintext/$i" >> "$4"
  1315. done
  1316. rm -f c
  1317. }
  1318. gnuplot_bash_history()
  1319. {
  1320. HISTTIMEFORMAT='' history \
  1321. | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' \
  1322. | sort -rn \
  1323. | head > /tmp/cmds ; gnuplot -persist <<<'plot "/tmp/cmds" using 1:xticlabels(2) with boxes'
  1324. }
  1325. kill_xterm_on_display()
  1326. {
  1327. display="$1"
  1328. for pid in $(pidof xterm); do
  1329. if [[ $1 = $(tr '\000' '\n' < "/proc/$pid/environ" | grep "$display" | cut -d= -f 2) ]]; then
  1330. kill "$pid"
  1331. fi
  1332. done
  1333. }
  1334. docker_top_strace()
  1335. {
  1336. container="$1"
  1337. docker top "$container" \
  1338. | awk '{ print $2 }' \
  1339. | tail -n +2 \
  1340. | sed 's/^/-p/' \
  1341. | xargs strace -s 10000 -f -o /tmp/docker.strace
  1342. }
  1343. active_hms()
  1344. {
  1345. curl -s --user jenkins:"$(pass show majordomo/private/jenkins/jenkins)" nginx{1,2}-mr:8080/hms | jq -r .active | uniq
  1346. }
  1347. jenkins_build_project_branch()
  1348. {
  1349. dir="$1"
  1350. project="$2"
  1351. branch="$3"
  1352. url="$JENKINS_URL/job/$dir/job/$project/job/$branch"
  1353. curl -X POST \
  1354. -u "admin:$(pass show jenkins/admin-api-key)" \
  1355. "$url/build"
  1356. echo "$url/lastBuild/console"
  1357. }
  1358. # Create a new directory and enter it
  1359. mkd()
  1360. {
  1361. mkdir -p "$@"
  1362. cd "$@" || exit
  1363. }
  1364. # Make a temporary directory and enter it
  1365. tmpd()
  1366. {
  1367. local dir
  1368. if [ $# -eq 0 ]; then
  1369. dir=$(mktemp -d)
  1370. else
  1371. dir=$(mktemp -d -t "${1}.XXXXXXXXXX")
  1372. fi
  1373. cd "$dir" || exit
  1374. }
  1375. # Run `dig` and display the most useful info
  1376. digga()
  1377. {
  1378. dig +nocmd "$1" any +multiline +noall +answer
  1379. }
  1380. alias feh-bg="feh --borderless --image-bg black --auto-zoom --draw-filename"
  1381. lsp_mode()
  1382. {
  1383. docker run --tty --interactive --rm \
  1384. --volume "$PWD:/mnt/workspace" \
  1385. --volume /etc/localtime:/etc/localtime:ro \
  1386. --volume "$HOME/.gnupg:${HOME}/.gnupg" \
  1387. --volume /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket \
  1388. --volume /tmp/.X11-unix:/tmp/.X11-unix \
  1389. --volume /etc/machine-id:/etc/machine-id:ro \
  1390. --volume /home/oleg/src/gitlab.intr/hms:/src \
  1391. --volume "$PWD/emacs.d/:/home/lsp/.emacs.d" \
  1392. --user 1000:1000 \
  1393. --env DISPLAY="$DISPLAY" \
  1394. --network=host \
  1395. "${1:-yyoncho/lsp-emacs-docker}" emacs
  1396. }
  1397. git_all_history()
  1398. {
  1399. git log --oneline | awk '{ print $1 }' | xargs git show
  1400. }
  1401. git_grep_removed()
  1402. {
  1403. git log --full-diff -G '\*\*REMOVED\*\*'
  1404. }
  1405. git_prune()
  1406. {
  1407. git reflog expire --expire=now --all && git gc --prune=now --aggressive
  1408. }
  1409. bfg()
  1410. {
  1411. case "$1" in
  1412. pass)
  1413. command bfg --replace-text <(git-pass-secrets)
  1414. ;;
  1415. esac
  1416. }
  1417. atoi()
  1418. {
  1419. #Returns the integer representation of an IP arg, passed in ascii dotted-decimal notation (x.x.x.x)
  1420. IP=$1; IPNUM=0
  1421. for (( i=0 ; i<4 ; ++i )); do
  1422. ((IPNUM+=${IP%%.*}*$((256**$((3-i))))))
  1423. IP=${IP#*.}
  1424. done
  1425. echo $IPNUM
  1426. }
  1427. itoa()
  1428. {
  1429. #returns the dotted-decimal ascii form of an IP arg passed in integer format
  1430. echo -n $(($(($(($((${1}/256))/256))/256))%256)).
  1431. echo -n $(($(($((${1}/256))/256))%256)).
  1432. echo -n $(($((${1}/256))%256)).
  1433. echo $((${1}%256))
  1434. }
  1435. pactl_ladspa()
  1436. {
  1437. # TODO: LADSPA_PATH=/gnu/store/…-swh-plugins/lib/ladspa
  1438. # https://github.com/gotbletu/shownotes/blob/master/pulseaudio-dynamic-range-compression.md
  1439. pactl load-module module-ladspa-sink master=0 sink_name=compressor-stereo plugin=sc4_1882 label=sc4 control=1,1.5,401,-30,20,5,12
  1440. pactl load-module module-ladspa-sink master=1 sink_name=compressor-stereo plugin=sc4_1882 label=sc4 control=1,1.5,401,-30,20,5,12
  1441. }
  1442. docker_openresty()
  1443. {
  1444. docker run \
  1445. --entrypoint '' \
  1446. --interactive \
  1447. --name openresty \
  1448. --network=host \
  1449. --tty \
  1450. openresty/openresty:bionic bash
  1451. }
  1452. nix_python_with_pkgs()
  1453. {
  1454. nix-shell -p python "python.withPackages (python-packages: with python-packages; [ $* ])"
  1455. }
  1456. bpython()
  1457. {
  1458. nix-shell -p python3 "python3.withPackages (python-packages: with python-packages; [ bpython ])" --run bpython
  1459. }
  1460. http_prompt()
  1461. {
  1462. nix-shell -I "$HOME/.nix-defexpr/channels/nixos-unstable" \
  1463. -p http-prompt \
  1464. --run "http-prompt $@"
  1465. }
  1466. nix_python2_with_pkgs()
  1467. {
  1468. nix-shell -p python2 "python2.withPackages (python-packages: with python-packages; [ $* ])"
  1469. }
  1470. firefox()
  1471. {
  1472. case "$1" in
  1473. kill)
  1474. pgrep -fa firefox-wrapped | grep -v 'test' | cut -d ' ' -f 1 | xargs kill
  1475. ;;
  1476. sync)
  1477. rsync --archive --progress "$HOME/.mozilla/firefox/j56dvo43.default-1520714705340" vm1.wugi.info:.mozilla/firefox/
  1478. ;;
  1479. *)
  1480. command firefox "$@"
  1481. esac
  1482. }
  1483. firefox_esr_debian()
  1484. {
  1485. xhost +local:
  1486. docker run \
  1487. --tty \
  1488. --interactive \
  1489. --rm \
  1490. --name \
  1491. firefox-java \
  1492. --env \
  1493. DISPLAY="$DISPLAY" \
  1494. --volume \
  1495. /tmp/.X11-unix:/tmp/.X11-unix \
  1496. cmaohuang/firefox-java \
  1497. /usr/bin/firefox \
  1498. --new-instance
  1499. }
  1500. mitmproxy()
  1501. {
  1502. PYTHONPATH='' command mitmproxy --set ssl_insecure=true "$@"
  1503. }
  1504. alias ssh-clean='find ~/.ssh -type s -delete'
  1505. alias pass='EDITOR=nano pass'
  1506. alias ansible-vault='EDITOR=nano ansible-vault'
  1507. alias sw1-dh507='connect sw1-dh507'
  1508. alias sw1-mr11="connect sw1-mr11"
  1509. alias nix='EDITOR=less VISUAL="emacsclient -cn" nix'
  1510. alias dotfiles='EDITOR=nano dotfiles'
  1511. if [ -f "$HOME/.nix-profile/share/bash-completion/completions/chezmoi.bash" ]; then
  1512. source "$HOME/.nix-profile/share/bash-completion/completions/chezmoi.bash"
  1513. complete -o default -F __start_chezmoi dotfiles
  1514. fi
  1515. if [ -f "$HOME/.guix-profile/etc/bash_completion.d/restic" ]; then
  1516. source "$HOME/.guix-profile/etc/bash_completion.d/restic"
  1517. complete -o default -F __start_restic backup
  1518. fi
  1519. mycut()
  1520. {
  1521. command cut --characters="-$(tput cols)" "$@"
  1522. }
  1523. src_clean()
  1524. {
  1525. for directory in src archive/src; do
  1526. command src-clean --directory=$directory --author=go.wigust@gmail.com --ignore=src/dotfiles,src/rofi-themes,src/rfc,src/guix-videos,src/guile,src/fdroiddata,src/emacs-tramp-auto-auth,src/docker-nextcloud,archive/src/sendnotify,archive/src/php,archive/src/my-guix,archive/src/malware-scanner,archive/src/linux-panic,archive/src/linux,archive/src/guile-shepherd,archive/src/groovy,archive/src/gentoo,archive/src/freebsd,archive/src/cpython,archive/src/bash,archive/src/arch-packages,archive/src/arch-community
  1527. done
  1528. }
  1529. transmission_remote()
  1530. {
  1531. case "$1" in
  1532. clean)
  1533. command transmission-remote --list | awk '$2 == "100%" { system("transmission-remote --torrent " $1 " --remove") }'
  1534. ;;
  1535. *)
  1536. command transmission-remote "$@"
  1537. ;;
  1538. esac
  1539. }
  1540. rg()
  1541. {
  1542. case "$1" in
  1543. dist)
  1544. command rg "${@:2}" "$HOME"/{src/nixpkgs-master,src/guix,archive/src/{gentoo,freebsd,openbsd-ports,arch-{community,packages}}}
  1545. ;;
  1546. *)
  1547. command rg --no-heading "$@"
  1548. ;;
  1549. esac
  1550. }
  1551. if [ -f "$HOME/.bash.d/slack.bash" ]
  1552. then
  1553. # shellcheck source=.bash.d/mjru.bash
  1554. source "$HOME/.bash.d/slack.bash"
  1555. fi
  1556. if [ -f "$HOME/.bash.d/mjru.bash" ]
  1557. then
  1558. # shellcheck source=.bash.d/mjru.bash
  1559. source "$HOME/.bash.d/mjru.bash"
  1560. fi
  1561. chezmoi()
  1562. {
  1563. case "$1" in
  1564. add-all)
  1565. for file in $(command chezmoi diff | awk '/a\/home/ { print $NF }' | sed 's/^a//'); do
  1566. command chezmoi add "$file"
  1567. done
  1568. ;;
  1569. *)
  1570. command chezmoi "$@"
  1571. ;;
  1572. esac
  1573. }
  1574. if [ -f "$HOME/.bash_completion.d/mail" ]
  1575. then
  1576. source "$HOME/.bash_completion.d/mail"
  1577. fi
  1578. if [ -f "$HOME/.bash_completion.d/connect" ]
  1579. then
  1580. source "$HOME/.bash_completion.d/connect"
  1581. fi
  1582. gitlab()
  1583. {
  1584. GITLAB_API_PRIVATE_TOKEN="$(pass show majordomo/private/gitlab.intr/tokens/terraform)"
  1585. GITLAB_API_ENDPOINT="https://gitlab.intr/api/v4"
  1586. GITLAB_API_HTTPARTY_OPTIONS="{verify: false}"
  1587. case "$1" in
  1588. create_project)
  1589. group="$2"
  1590. name="$3"
  1591. command gitlab create_project "$name" "{visibility: public, namespace_id: $group}"
  1592. echo "https://gitlab.intr/$group/$name"
  1593. echo "git@gitlab.wugi.info:$group/$name.git"
  1594. ;;
  1595. *)
  1596. command gitlab "$@"
  1597. esac
  1598. }
  1599. youtube_dl_to_org()
  1600. {
  1601. jq --join-output '.entries[] | "- [ ] [[https://www.youtube.com/watch?v=", .id, "]", "[", .title, "]]\n"' "$@"
  1602. }
  1603. gita()
  1604. {
  1605. case "$1" in
  1606. emacs)
  1607. emacsclient -cn "$(command gita ls "$2")"
  1608. ;;
  1609. cd)
  1610. cd "$(command gita ls "$2")"
  1611. ;;
  1612. --help)
  1613. command gita --help "$@"
  1614. glow https://github.com/nosarthur/gita
  1615. ;;
  1616. week)
  1617. PAGER='' command gita super log --format="%ai %H %s" --date=relative --since="${2:-1} weeks ago" "${@:3}"
  1618. ;;
  1619. *)
  1620. command gita "$@"
  1621. ;;
  1622. esac
  1623. }
  1624. alias godaddy="GODADDY_DOMAIN_NAME=wugi.info godaddy"
  1625. wugi.info()
  1626. {
  1627. case "$1" in
  1628. a)
  1629. godaddy list a
  1630. ;;
  1631. cname)
  1632. godaddy list cname
  1633. ;;
  1634. ns)
  1635. godaddy list ns
  1636. ;;
  1637. esac
  1638. }
  1639. stumpwm_refcard()
  1640. {
  1641. (
  1642. cd "/home/oleg/src/git.savannah.gnu.org/git/guix-maintenance/doc/refcard" || exit 1
  1643. # convert -density 200 -rotate 90 "$(guix build -f build.scm)"/stumpwm-refcard.pdf - | zathura -
  1644. case "$1" in
  1645. wallpaper)
  1646. convert -trim -quality 100 -negate -density 130 -rotate 90 "$(guix build -f build.scm)"/stumpwm-refcard.pdf /tmp/out.png; feh --bg-center /tmp/out.png
  1647. ;;
  1648. *)
  1649. zathura "$(guix build -f build.scm)"/stumpwm-refcard.pdf
  1650. esac
  1651. )
  1652. }
  1653. ci()
  1654. {
  1655. "$BROWSER" "$CI"
  1656. }
  1657. elktail()
  1658. {
  1659. case "$1" in
  1660. --help|-h)
  1661. command elktail --help
  1662. echo "\
  1663. general commands
  1664. nginx show nginx logs
  1665. hms show log messages from hms index
  1666. trace trace OPERATION_IDENTITY in hms index
  1667. "
  1668. ;;
  1669. nginx)
  1670. command elktail -url 'http://es:9200' -i 'nginx-*' -n 10000 -l -f '%remote_addr %hostname %path %method %code %http_host'
  1671. ;;
  1672. hms)
  1673. # log_level:DEBUG
  1674. # log_level:ERROR
  1675. # stack:"$(curl --silent --user "jenkins:$(pass show majordomo/private/jenkins/jenkins)" -X GET http://nginx1.intr:8080/hms | jq --raw-output .active)"
  1676. command elktail \
  1677. -url 'http://es:9200' \
  1678. -i 'hms-*' \
  1679. -n 10000 \
  1680. -l \
  1681. -f '%@timestamp %log_level %stack %sleuth_trace %log_message' \
  1682. -a "$(printf "%s" "$(date --utc '+%FT%H:%M:00.000Z' --date '4 hours ago')")" \
  1683. "${@:3}" \
  1684. | grep --invert-match "ftp-user not found by" \
  1685. | grep --invert-match "org.apache.catalina.connector.RequestFacade" \
  1686. | grep --invert-match "Running the evict task with compensationTime" \
  1687. | grep --invert-match "Resolving eureka endpoints via configuration" \
  1688. | grep --invert-match "quotaUsed: " \
  1689. | grep --invert-match "unixAccounts quotaReport for host"
  1690. ;;
  1691. trace)
  1692. (
  1693. set -x
  1694. command elktail \
  1695. -url 'http://es:9200' \
  1696. -i 'hms-*' \
  1697. -n 10000 \
  1698. -l \
  1699. -f '%@timestamp %log_level %stack %sleuth_trace %log_message' \
  1700. -a "$(printf "%s" "$(date --utc '+%FT%H:%M:00.000Z' --date '5 hours ago')")" \
  1701. sleuth_trace:"$2"
  1702. )
  1703. ;;
  1704. *)
  1705. command elktail "$@"
  1706. ;;
  1707. esac
  1708. }
  1709. brute()
  1710. {
  1711. sudo grep 'Connection closed' /var/log/messages \
  1712. | grep --perl-regexp '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' --only-matching \
  1713. | grep --invert-match 127.0.0.1 \
  1714. | uniq \
  1715. | fzf \
  1716. | xargs sudo ipset add brute
  1717. }
  1718. docker()
  1719. {
  1720. case "$1" in
  1721. deploy)
  1722. command docker rm -f novnc
  1723. command docker rm -f monitoror
  1724. monitoror run
  1725. vnc client novnc
  1726. ;;
  1727. *)
  1728. command docker "$@"
  1729. esac
  1730. }
  1731. lsgpu()
  1732. {
  1733. printf "Memory: %s\n" "$(glxinfo | grep -i -o 'device|memory\|[0-9]\{1,12\} MB' | head -n 1)"
  1734. }
  1735. efi()
  1736. {
  1737. if [ -d /sys/firmware/efi ]
  1738. then
  1739. echo UEFI
  1740. else
  1741. echo BIOS
  1742. fi
  1743. }
  1744. mediatomb()
  1745. {
  1746. case "$1" in
  1747. start)
  1748. command mediatomb -e enp34s0 -f Videos/.mediatomb "${@:2}"
  1749. ;;
  1750. *)
  1751. command mediatomb "$@"
  1752. esac
  1753. }
  1754. if [[ -e $HOME/.guix-profile/bin/chezmoi ]]
  1755. then
  1756. eval "$(chezmoi completion bash)"
  1757. fi
  1758. SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
  1759. export SSH_AUTH_SOCK
  1760. # TODO: Fix imv alias.
  1761. #
  1762. # if [ -n $WAYLAND_DISPLAY ]
  1763. # then
  1764. # alias feh=imv
  1765. # fi
  1766. ssh_sudo()
  1767. {
  1768. sshpass -p"$1" \
  1769. ssh \
  1770. -o UserKnownHostsFile=/dev/null \
  1771. -o StrictHostKeyChecking=no \
  1772. -q \
  1773. -t \
  1774. "$3" \
  1775. -l "$2" \
  1776. -- "set +o history; sudo --stdin --validate --prompt='' <<< \"$1\"; exec -a sudo sudo -i"
  1777. }
  1778. ,s()
  1779. {
  1780. sshpass -p"$1" \
  1781. ssh \
  1782. -o UserKnownHostsFile=/dev/null \
  1783. -o StrictHostKeyChecking=no \
  1784. "${@:2}"
  1785. }
  1786. psql()
  1787. {
  1788. case "$1" in
  1789. *)
  1790. sudo -u postgres psql "$@"
  1791. ;;
  1792. esac
  1793. }
  1794. KUBECTL_WATCH_CGROUP="viddy"
  1795. export KUBECTL_WATCH_CGROUP
  1796. __lazy_kubectl()
  1797. {
  1798. local word_count=${#COMP_WORDS[*]}
  1799. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1800. case $COMP_CWORD in
  1801. *)
  1802. if [[ $(type -t __start_kubectl) != function ]]
  1803. then
  1804. if [[ -f /home/oleg/.guix-profile/bin/kubectl ]]
  1805. then
  1806. source <(/home/oleg/.guix-profile/bin/kubectl completion bash)
  1807. fi
  1808. fi
  1809. ;;
  1810. esac
  1811. }
  1812. complete -F __lazy_kubectl kubectl
  1813. __lazy_helm()
  1814. {
  1815. local word_count=${#COMP_WORDS[*]}
  1816. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1817. case $COMP_CWORD in
  1818. *)
  1819. if [[ $(type -t __start_helm) != function ]]
  1820. then
  1821. if [[ -f /home/oleg/.guix-profile/bin/helm ]]
  1822. then
  1823. source <(/home/oleg/.guix-profile/bin/helm completion bash)
  1824. fi
  1825. fi
  1826. ;;
  1827. esac
  1828. }
  1829. complete -F __lazy_helm helm
  1830. kubectl()
  1831. {
  1832. case "$1" in
  1833. master)
  1834. command kubectl get node --output=json \
  1835. | jq --raw-output '.items[] | select(.metadata.labels["node-role.kubernetes.io/master"] == "true") | .metadata.name'
  1836. ;;
  1837. cluster*)
  1838. KUBECONFIG="/home/oleg/.kube/config-mjru-${1}" \
  1839. kubectl \
  1840. exec \
  1841. --stdin=true \
  1842. --tty=true \
  1843. --namespace=kubernetes-tmuxifier \
  1844. tmuxifier-0 \
  1845. -- tmux at
  1846. ;;
  1847. drain)
  1848. if [[ -z "$2" ]]
  1849. then
  1850. cat <<'EOF'
  1851. drain example:
  1852. kubectl drain --ignore-daemonsets --delete-emptydir-data NODE
  1853. kubectl delete pods --all-namespaces --field-selector 'spec.nodeName=NODE,metadata.namespace!=default'
  1854. EOF
  1855. else
  1856. command kubectl drain "${@:2}"
  1857. fi
  1858. ;;
  1859. taint)
  1860. if [[ -z "$2" ]]
  1861. then
  1862. cat <<'EOF'
  1863. drain example:
  1864. kubectl taint node unschedulable=true:NoSchedule kube3
  1865. kubectl taint node unschedulable- kube3
  1866. EOF
  1867. else
  1868. command kubectl drain "${@:2}"
  1869. fi
  1870. ;;
  1871. noschedule)
  1872. case "$2" in
  1873. list)
  1874. command kubectl get nodes -o go-template='{{range $item := .items}}{{with $nodename := $item.metadata.name}}{{range $taint := $item.spec.taints}}{{if and (eq $taint.effect "NoSchedule")}}{{printf "%s\n" $nodename}}{{end}}{{end}}{{end}}{{end}}' \
  1875. | sort --version-sort
  1876. ;;
  1877. esac
  1878. ;;
  1879. prometheus)
  1880. command kubectl -n monitoring get secret prometheus-kube-prometheus-stack-prometheus -o json \
  1881. | jq -r '.data["prometheus.yaml.gz"]' \
  1882. | base64 -d \
  1883. | gunzip
  1884. ;;
  1885. reboot)
  1886. (
  1887. set -ex
  1888. kubectl drain "$2" --ignore-daemonsets --delete-emptydir-data
  1889. ansible "${2}.intr" -m reboot -a 'search_paths=/run/current-system/sw/bin'
  1890. kubectl uncordon "$2"
  1891. )
  1892. ;;
  1893. cilium)
  1894. command kubectl --namespace kube-system get pods --selector=k8s-app=cilium
  1895. ;;
  1896. tmuxifier)
  1897. case "$2" in
  1898. --help)
  1899. echo TMUXIFIER_KUBERNETES_NAMESPACE=cert-manager kubectl tmuxifier mjru-cluster2
  1900. ;;
  1901. mjru-cluster1-view)
  1902. flake="git+https://gitlab.intr/nixos/kubernetes#kubeconfig-view"
  1903. nix build --no-link "$flake"
  1904. TMUXIFIER_TMUX_OPTS="-L kubernetes-cluster1-view" \
  1905. KUBECONFIG="$(nix path-info "$flake")/.kube/config" \
  1906. tmuxifier s kubernetes
  1907. ;;
  1908. mjru-cluster1)
  1909. TMUXIFIER_TMUX_OPTS="-L kubernetes-cluster1" \
  1910. KUBECONFIG="${HOME}/.kube/config-mjru-cluster1" \
  1911. tmuxifier s kubernetes
  1912. ;;
  1913. mjru-cluster2)
  1914. TMUXIFIER_TMUX_OPTS="-L kubernetes-cluster2" \
  1915. KUBECONFIG="${HOME}/.kube/config-mjru-cluster2" \
  1916. tmuxifier s kubernetes
  1917. ;;
  1918. host1)
  1919. TMUXIFIER_TMUX_OPTS="-L config-host1-k3s" \
  1920. KUBECONFIG="${HOME}/.kube/config-host1-k3s" \
  1921. tmuxifier s kubernetes
  1922. ;;
  1923. home)
  1924. TMUXIFIER_TMUX_OPTS="-L kubernetes-home-k8s" \
  1925. KUBECONFIG="${HOME}/.kube/config-home-k8s" \
  1926. tmuxifier s kubernetes
  1927. ;;
  1928. *)
  1929. tmuxifier s kubernetes
  1930. ;;
  1931. esac
  1932. ;;
  1933. *)
  1934. command kubectl "$@"
  1935. ;;
  1936. esac
  1937. }
  1938. __lazy_virtctl()
  1939. {
  1940. local word_count=${#COMP_WORDS[*]}
  1941. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1942. case $COMP_CWORD in
  1943. *)
  1944. if [[ $(type -t __start_virtctl) != function ]]
  1945. then
  1946. if [[ -f /run/current-system/profile/bin/virtctl ]]
  1947. then
  1948. source <(/run/current-system/profile/bin/virtctl completion bash)
  1949. fi
  1950. fi
  1951. ;;
  1952. esac
  1953. }
  1954. __lazy_glab()
  1955. {
  1956. local word_count=${#COMP_WORDS[*]}
  1957. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1958. case $COMP_CWORD in
  1959. *)
  1960. if [[ $(type -t __start_glab) != function ]]
  1961. then
  1962. if [[ -f "${HOME}/.nix-profile/bin/glab" ]]
  1963. then
  1964. source <("${HOME}/.nix-profile/bin/glab" completion)
  1965. fi
  1966. fi
  1967. ;;
  1968. esac
  1969. }
  1970. complete -F __lazy_glab glab
  1971. glab()
  1972. {
  1973. case "$1" in
  1974. *)
  1975. command glab "${@:1}"
  1976. ;;
  1977. esac
  1978. }
  1979. __lazy_ipmi()
  1980. {
  1981. local word_count=${#COMP_WORDS[*]}
  1982. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1983. case $COMP_CWORD in
  1984. *)
  1985. if [[ $(type -t __start_ipmi) != function ]]
  1986. then
  1987. if [[ -f "$HOME/.nix-profile/share/bash-completion/completions/ipmi" ]]
  1988. then
  1989. source "$HOME/.nix-profile/share/bash-completion/completions/ipmi"
  1990. fi
  1991. fi
  1992. ;;
  1993. esac
  1994. }
  1995. complete -F __lazy_ipmi ipmi
  1996. ipmi()
  1997. {
  1998. case "$1" in
  1999. *)
  2000. IPMI_PASSWORD="$(pass show majordomo/public/ipmi/ADMIN)" command ipmi "${@:1}"
  2001. ;;
  2002. esac
  2003. }
  2004. __lazy_cscli()
  2005. {
  2006. local word_count=${#COMP_WORDS[*]}
  2007. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  2008. case $COMP_CWORD in
  2009. *)
  2010. if [[ $(type -t __start_cscli) != function ]]
  2011. then
  2012. if [[ -f /run/current-system/profile/bin/cscli ]]
  2013. then
  2014. source <(/run/current-system/profile/bin/cscli completion bash)
  2015. fi
  2016. fi
  2017. ;;
  2018. esac
  2019. }
  2020. complete -F __lazy_cscli cscli
  2021. cscli()
  2022. {
  2023. case "$1" in
  2024. *)
  2025. sudo cscli "${@:1}"
  2026. ;;
  2027. esac
  2028. }
  2029. herd()
  2030. {
  2031. case "$1" in
  2032. start)
  2033. case "$2" in
  2034. fzf)
  2035. service="$(sudo command herd status | awk '/ - / { print $NF }' | fzf)"
  2036. sudo command herd enable "$service"
  2037. sudo command herd start "$service"
  2038. ;;
  2039. *)
  2040. command herd enable "${@:2}"
  2041. command herd start "${@:2}"
  2042. ;;
  2043. esac
  2044. ;;
  2045. restart)
  2046. case "$2" in
  2047. mail)
  2048. for service in $(herd status | awk '/isync/ { print $NF }')
  2049. do
  2050. herd start "$service"
  2051. done
  2052. ;;
  2053. *)
  2054. command herd restart "${@:2}"
  2055. ;;
  2056. esac
  2057. ;;
  2058. *)
  2059. command herd "$@"
  2060. ;;
  2061. esac
  2062. }
  2063. opensearch()
  2064. {
  2065. case "$1" in
  2066. index)
  2067. viddy --differences --interval 10 "bash -c 'curl ${2:-opensearch.home}/_cat/indices?pretty | sort -k 3'"
  2068. ;;
  2069. esac
  2070. }
  2071. curl()
  2072. {
  2073. new_args=()
  2074. for var in "$@"
  2075. do
  2076. if [[ "$var" == *"https://api.majordomo.ru/"* ]]
  2077. then
  2078. json=true
  2079. new_args+=(
  2080. --header "Authorization: Bearer $(mjru-auth)"
  2081. --header 'Content-Type: application/json'
  2082. "$var"
  2083. )
  2084. elif [[ "$var" == *"https://api.hms.development.corp1.majordomo.ru/"* ]] || [[ "$var" == *"http://api.hms.development.corp1.majordomo.ru/"* ]]
  2085. then
  2086. json=true
  2087. new_args+=(
  2088. --header "Authorization: Bearer $(IHS_ENDPOINT=http://api.hms.development.corp1.majordomo.ru mjru-auth)"
  2089. --header 'Content-Type: application/json'
  2090. "$var"
  2091. )
  2092. elif [[ "$var" == *"https://api-dev.intr/"* ]]
  2093. then
  2094. json=true
  2095. new_args+=(
  2096. --header "Authorization: Bearer $(IHS_ENDPOINT=https://api-dev.intr mjru-auth)"
  2097. --header 'Content-Type: application/json'
  2098. "$var"
  2099. )
  2100. elif [[ "$var" == *"https://kubernetes.intr"* ]] || [[ "$var" == *"https://kubernetes-cluster2.intr"* ]]
  2101. then
  2102. new_args+=(
  2103. --cacert "${HOME}/src/gitlab.intr/office/ssl-certificates/kubernetes/ca.pem"
  2104. --cert "${HOME}/src/gitlab.intr/office/ssl-certificates/kubernetes/admin.pem"
  2105. --key "${HOME}/src/gitlab.intr/office/ssl-certificates/kubernetes/admin-key.pem"
  2106. "$var"
  2107. )
  2108. elif [[ "$var" == *"https://kubernetes.home:6443"* ]]
  2109. then
  2110. new_args+=(
  2111. --cacert /etc/kubernetes/pki/ca.pem
  2112. --cert /etc/kubernetes/pki/admin.pem
  2113. --key /etc/kubernetes/pki/admin-key.pem
  2114. "$var"
  2115. )
  2116. else
  2117. new_args+=($var)
  2118. fi
  2119. done
  2120. command curl "${new_args[@]}"
  2121. if [[ $json == true ]]
  2122. then
  2123. echo
  2124. fi
  2125. }
  2126. dig()
  2127. {
  2128. for var in "$@"
  2129. do
  2130. case "$var" in
  2131. *majordomo.ru|*.intr|172.16.*|*pwhost.ru|*fast-name.ru|*host-support.ru|*litename.ru|*vipname.su|*hoster24.ru)
  2132. name_servers=(
  2133. ns1-mr.intr
  2134. ns2-mr.intr
  2135. ns1-dh.intr
  2136. ns2-dh.intr
  2137. ns.majordomo.ru
  2138. ns2.majordomo.ru
  2139. ns3.majordomo.ru
  2140. ns4.majordomo.ru
  2141. ns1.corp1.majordomo.ru
  2142. ns2.corp2.majordomo.ru
  2143. ns1.svc1.majordomo.ru
  2144. ns2.svc2.majordomo.ru
  2145. ns1.corp2.majordomo.ru
  2146. ns2.corp2.majordomo.ru
  2147. ns1.svc2.majordomo.ru
  2148. ns2.svc2.majordomo.ru
  2149. 8.8.8.8
  2150. 8.8.4.4
  2151. )
  2152. (
  2153. set -x
  2154. command dig "$@"
  2155. )
  2156. for name_server in "${name_servers[@]}"
  2157. do
  2158. (
  2159. set -x
  2160. command dig "$@" "@${name_server}"
  2161. )
  2162. mapfile -t ip_addresses < <(command dig "$@" "@${name_server}" \
  2163. | jc --dig \
  2164. | jq --raw-output .[].answer[].data)
  2165. (
  2166. for ip_address in "${ip_addresses[@]}"
  2167. do
  2168. (
  2169. set -x
  2170. command dig -x "$ip_address" "@${name_server}"
  2171. )
  2172. done
  2173. ) | sort --version-sort
  2174. done
  2175. ;;
  2176. *)
  2177. command dig "$@"
  2178. ;;
  2179. esac
  2180. done
  2181. }
  2182. scream()
  2183. {
  2184. case "$1" in
  2185. *)
  2186. cat <<'EOF'
  2187. scream -i br154.154 -u -p 16400
  2188. EOF
  2189. command scream "$@"
  2190. ;;
  2191. esac
  2192. }
  2193. kresd()
  2194. {
  2195. case "$1" in
  2196. clear)
  2197. sudo socat - "UNIX-CONNECT:/var/cache/knot-resolver/control/$(pidof kresd)" <<< 'cache.clear()'
  2198. ;;
  2199. *)
  2200. command kresd "$@"
  2201. esac
  2202. }
  2203. sudo()
  2204. {
  2205. case "$1" in
  2206. herd)
  2207. case "$2" in
  2208. start)
  2209. case "$3" in
  2210. xorg-server)
  2211. if [[ -e /var/run/slim-vt7.auth ]]
  2212. then
  2213. echo "Removing '/var/run/slim-vt7.auth' file."
  2214. command sudo rm /var/run/slim-vt7.auth
  2215. fi
  2216. if [[ -e /var/run/slim-vt7.lock ]]
  2217. then
  2218. echo "Removing '/var/run/slim-vt7.lock' file."
  2219. command sudo rm /var/run/slim-vt7.lock
  2220. fi
  2221. command sudo herd enable xorg-server
  2222. command sudo herd start xorg-server
  2223. ;;
  2224. *)
  2225. command sudo herd enable "${@:3}"
  2226. command sudo herd start "${@:3}"
  2227. ;;
  2228. esac
  2229. ;;
  2230. *)
  2231. command sudo herd "${@:2}"
  2232. ;;
  2233. esac
  2234. ;;
  2235. *)
  2236. command sudo "$@"
  2237. ;;
  2238. esac
  2239. }
  2240. PASSWORD_STORE_ENABLE_EXTENSIONS="true"
  2241. export PASSWORD_STORE_ENABLE_EXTENSIONS
  2242. PATH="${HOME}/bin":"${HOME}/.local/bin":"$(/usr/bin/env --ignore-environment sh --norc --noprofile -c 'unset PATH; export HOME=/home/oleg; export USER=oleg; source /etc/profile; printf "$PATH"')":"${HOME}/go/bin":"${HOME}/.npm-global/bin":/opt/gradle/bin:"${HOME}/perl5/bin":"${HOME}/.nix-profile/lib/openjdk/bin":"${HOME}/.nix-profile/bin":"$PATH"
  2243. export PATH
  2244. if [[ -z $DISPLAY ]] && [[ $(tty) = /dev/tty1 ]]
  2245. then
  2246. exec sway
  2247. fi
  2248. pactl_configure()
  2249. {
  2250. echo pactl load-module module-ladspa-sink sink_name=compressor-stereo plugin=sc4_1882 label=sc4 control=1,1.5,401,-30,20,5,30
  2251. echo pactl load-module module-null-sink media.class=Audio/Sink sink_name=obs-microphone channel_map=front-left,front-right
  2252. echo pactl load-module module-ladspa-sink sink_name=compressor-stereo-family-17 plugin=sc4_1882 label=sc4 control=1,1.5,401,-30,20,5
  2253. }