dot_bashrc 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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 [ -z $IN_NIX_SHELL ]
  1755. then
  1756. if [ -e $HOME/.guix-profile/lib/bash/libguile-bash.so ]
  1757. then
  1758. enable -f "$HOME"/.guix-profile/lib/bash/libguile-bash.so scm
  1759. if [ -e "$HOME"/.bash.d/bash.scm ]
  1760. then
  1761. GUILE_AUTO_COMPILE=0 scm "$HOME"/.bash.d/bash.scm
  1762. fi
  1763. fi
  1764. fi
  1765. if [[ -e $HOME/.guix-profile/bin/chezmoi ]]
  1766. then
  1767. eval "$(chezmoi completion bash)"
  1768. fi
  1769. SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
  1770. export SSH_AUTH_SOCK
  1771. # TODO: Fix imv alias.
  1772. #
  1773. # if [ -n $WAYLAND_DISPLAY ]
  1774. # then
  1775. # alias feh=imv
  1776. # fi
  1777. ssh_sudo()
  1778. {
  1779. sshpass -p"$1" \
  1780. ssh \
  1781. -o UserKnownHostsFile=/dev/null \
  1782. -o StrictHostKeyChecking=no \
  1783. -q \
  1784. -t \
  1785. "$3" \
  1786. -l "$2" \
  1787. -- "set +o history; sudo --stdin --validate --prompt='' <<< \"$1\"; exec -a sudo sudo -i"
  1788. }
  1789. ,s()
  1790. {
  1791. sshpass -p"$1" \
  1792. ssh \
  1793. -o UserKnownHostsFile=/dev/null \
  1794. -o StrictHostKeyChecking=no \
  1795. "${@:2}"
  1796. }
  1797. psql()
  1798. {
  1799. case "$1" in
  1800. *)
  1801. sudo -u postgres psql "$@"
  1802. ;;
  1803. esac
  1804. }
  1805. KUBECTL_WATCH_CGROUP="viddy"
  1806. export KUBECTL_WATCH_CGROUP
  1807. __lazy_kubectl()
  1808. {
  1809. local word_count=${#COMP_WORDS[*]}
  1810. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1811. case $COMP_CWORD in
  1812. *)
  1813. if [[ $(type -t __start_kubectl) != function ]]
  1814. then
  1815. if [[ -f /home/oleg/.guix-profile/bin/kubectl ]]
  1816. then
  1817. source <(/home/oleg/.guix-profile/bin/kubectl completion bash)
  1818. fi
  1819. fi
  1820. ;;
  1821. esac
  1822. }
  1823. complete -F __lazy_kubectl kubectl
  1824. __lazy_helm()
  1825. {
  1826. local word_count=${#COMP_WORDS[*]}
  1827. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1828. case $COMP_CWORD in
  1829. *)
  1830. if [[ $(type -t __start_helm) != function ]]
  1831. then
  1832. if [[ -f /home/oleg/.guix-profile/bin/helm ]]
  1833. then
  1834. source <(/home/oleg/.guix-profile/bin/helm completion bash)
  1835. fi
  1836. fi
  1837. ;;
  1838. esac
  1839. }
  1840. complete -F __lazy_helm helm
  1841. kubectl()
  1842. {
  1843. case "$1" in
  1844. master)
  1845. command kubectl get node --output=json \
  1846. | jq --raw-output '.items[] | select(.metadata.labels["node-role.kubernetes.io/master"] == "true") | .metadata.name'
  1847. ;;
  1848. cluster*)
  1849. KUBECONFIG="/home/oleg/.kube/config-mjru-${1}" \
  1850. kubectl \
  1851. exec \
  1852. --stdin=true \
  1853. --tty=true \
  1854. --namespace=kubernetes-tmuxifier \
  1855. tmuxifier-0 \
  1856. -- tmux at
  1857. ;;
  1858. drain)
  1859. if [[ -z "$2" ]]
  1860. then
  1861. cat <<'EOF'
  1862. drain example:
  1863. kubectl drain --ignore-daemonsets --delete-emptydir-data NODE
  1864. kubectl delete pods --all-namespaces --field-selector 'spec.nodeName=NODE,metadata.namespace!=default'
  1865. EOF
  1866. else
  1867. command kubectl drain "${@:2}"
  1868. fi
  1869. ;;
  1870. taint)
  1871. if [[ -z "$2" ]]
  1872. then
  1873. cat <<'EOF'
  1874. drain example:
  1875. kubectl taint node unschedulable=true:NoSchedule kube3
  1876. kubectl taint node unschedulable- kube3
  1877. EOF
  1878. else
  1879. command kubectl drain "${@:2}"
  1880. fi
  1881. ;;
  1882. noschedule)
  1883. case "$2" in
  1884. list)
  1885. 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}}' \
  1886. | sort --version-sort
  1887. ;;
  1888. esac
  1889. ;;
  1890. prometheus)
  1891. command kubectl -n monitoring get secret prometheus-kube-prometheus-stack-prometheus -o json \
  1892. | jq -r '.data["prometheus.yaml.gz"]' \
  1893. | base64 -d \
  1894. | gunzip
  1895. ;;
  1896. reboot)
  1897. (
  1898. set -ex
  1899. kubectl drain "$2" --ignore-daemonsets --delete-emptydir-data
  1900. ansible "${2}.intr" -m reboot -a 'search_paths=/run/current-system/sw/bin'
  1901. kubectl uncordon "$2"
  1902. )
  1903. ;;
  1904. cilium)
  1905. command kubectl --namespace kube-system get pods --selector=k8s-app=cilium
  1906. ;;
  1907. tmuxifier)
  1908. case "$2" in
  1909. --help)
  1910. echo TMUXIFIER_KUBERNETES_NAMESPACE=cert-manager kubectl tmuxifier mjru-cluster2
  1911. ;;
  1912. mjru-cluster1-view)
  1913. flake="git+https://gitlab.intr/nixos/kubernetes#kubeconfig-view"
  1914. nix build --no-link "$flake"
  1915. TMUXIFIER_TMUX_OPTS="-L kubernetes-cluster1-view" \
  1916. KUBECONFIG="$(nix path-info "$flake")/.kube/config" \
  1917. tmuxifier s kubernetes
  1918. ;;
  1919. mjru-cluster1)
  1920. TMUXIFIER_TMUX_OPTS="-L kubernetes-cluster1" \
  1921. KUBECONFIG="${HOME}/.kube/config-mjru-cluster1" \
  1922. tmuxifier s kubernetes
  1923. ;;
  1924. mjru-cluster2)
  1925. TMUXIFIER_TMUX_OPTS="-L kubernetes-cluster2" \
  1926. KUBECONFIG="${HOME}/.kube/config-mjru-cluster2" \
  1927. tmuxifier s kubernetes
  1928. ;;
  1929. host1)
  1930. TMUXIFIER_TMUX_OPTS="-L config-host1-k3s" \
  1931. KUBECONFIG="${HOME}/.kube/config-host1-k3s" \
  1932. tmuxifier s kubernetes
  1933. ;;
  1934. home)
  1935. TMUXIFIER_TMUX_OPTS="-L kubernetes-home-k8s" \
  1936. KUBECONFIG="${HOME}/.kube/config-home-k8s" \
  1937. tmuxifier s kubernetes
  1938. ;;
  1939. *)
  1940. tmuxifier s kubernetes
  1941. ;;
  1942. esac
  1943. ;;
  1944. *)
  1945. command kubectl "$@"
  1946. ;;
  1947. esac
  1948. }
  1949. __lazy_virtctl()
  1950. {
  1951. local word_count=${#COMP_WORDS[*]}
  1952. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1953. case $COMP_CWORD in
  1954. *)
  1955. if [[ $(type -t __start_virtctl) != function ]]
  1956. then
  1957. if [[ -f /run/current-system/profile/bin/virtctl ]]
  1958. then
  1959. source <(/run/current-system/profile/bin/virtctl completion bash)
  1960. fi
  1961. fi
  1962. ;;
  1963. esac
  1964. }
  1965. __lazy_glab()
  1966. {
  1967. local word_count=${#COMP_WORDS[*]}
  1968. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1969. case $COMP_CWORD in
  1970. *)
  1971. if [[ $(type -t __start_glab) != function ]]
  1972. then
  1973. if [[ -f "${HOME}/.nix-profile/bin/glab" ]]
  1974. then
  1975. source <("${HOME}/.nix-profile/bin/glab" completion)
  1976. fi
  1977. fi
  1978. ;;
  1979. esac
  1980. }
  1981. complete -F __lazy_glab glab
  1982. glab()
  1983. {
  1984. case "$1" in
  1985. *)
  1986. command glab "${@:1}"
  1987. ;;
  1988. esac
  1989. }
  1990. __lazy_ipmi()
  1991. {
  1992. local word_count=${#COMP_WORDS[*]}
  1993. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  1994. case $COMP_CWORD in
  1995. *)
  1996. if [[ $(type -t __start_ipmi) != function ]]
  1997. then
  1998. if [[ -f "$HOME/.nix-profile/share/bash-completion/completions/ipmi" ]]
  1999. then
  2000. source "$HOME/.nix-profile/share/bash-completion/completions/ipmi"
  2001. fi
  2002. fi
  2003. ;;
  2004. esac
  2005. }
  2006. complete -F __lazy_ipmi ipmi
  2007. ipmi()
  2008. {
  2009. case "$1" in
  2010. *)
  2011. IPMI_PASSWORD="$(pass show majordomo/public/ipmi/ADMIN)" command ipmi "${@:1}"
  2012. ;;
  2013. esac
  2014. }
  2015. __lazy_cscli()
  2016. {
  2017. local word_count=${#COMP_WORDS[*]}
  2018. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  2019. case $COMP_CWORD in
  2020. *)
  2021. if [[ $(type -t __start_cscli) != function ]]
  2022. then
  2023. if [[ -f /run/current-system/profile/bin/cscli ]]
  2024. then
  2025. source <(/run/current-system/profile/bin/cscli completion bash)
  2026. fi
  2027. fi
  2028. ;;
  2029. esac
  2030. }
  2031. complete -F __lazy_cscli cscli
  2032. __lazy_flux()
  2033. {
  2034. local word_count=${#COMP_WORDS[*]}
  2035. local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
  2036. case $COMP_CWORD in
  2037. *)
  2038. if [[ $(type -t __start_flux) != function ]]
  2039. then
  2040. if [[ -f /home/oleg/.guix-profile/bin/flux ]]
  2041. then
  2042. source <(/home/oleg/.guix-profile/bin/flux completion bash)
  2043. fi
  2044. fi
  2045. ;;
  2046. esac
  2047. }
  2048. complete -F __lazy_flux flux
  2049. cscli()
  2050. {
  2051. case "$1" in
  2052. *)
  2053. sudo cscli "${@:1}"
  2054. ;;
  2055. esac
  2056. }
  2057. herd()
  2058. {
  2059. case "$1" in
  2060. start)
  2061. case "$2" in
  2062. fzf)
  2063. service="$(sudo command herd status | awk '/ - / { print $NF }' | fzf)"
  2064. sudo command herd enable "$service"
  2065. sudo command herd start "$service"
  2066. ;;
  2067. *)
  2068. command herd enable "${@:2}"
  2069. command herd start "${@:2}"
  2070. ;;
  2071. esac
  2072. ;;
  2073. restart)
  2074. case "$2" in
  2075. mail)
  2076. for service in $(herd status | awk '/isync/ { print $NF }')
  2077. do
  2078. herd start "$service"
  2079. done
  2080. ;;
  2081. *)
  2082. command herd restart "${@:2}"
  2083. ;;
  2084. esac
  2085. ;;
  2086. *)
  2087. command herd "$@"
  2088. ;;
  2089. esac
  2090. }
  2091. opensearch()
  2092. {
  2093. case "$1" in
  2094. index)
  2095. viddy --differences --interval 10 "bash -c 'curl ${2:-opensearch.home}/_cat/indices?pretty | sort -k 3'"
  2096. ;;
  2097. esac
  2098. }
  2099. curl()
  2100. {
  2101. new_args=()
  2102. for var in "$@"
  2103. do
  2104. if [[ "$var" == *"https://api.majordomo.ru/"* ]]
  2105. then
  2106. json=true
  2107. new_args+=(
  2108. --header "Authorization: Bearer $(mjru-auth)"
  2109. --header 'Content-Type: application/json'
  2110. "$var"
  2111. )
  2112. elif [[ "$var" == *"https://api.hms.development.corp1.majordomo.ru/"* ]] || [[ "$var" == *"http://api.hms.development.corp1.majordomo.ru/"* ]]
  2113. then
  2114. json=true
  2115. new_args+=(
  2116. --header "Authorization: Bearer $(IHS_ENDPOINT=http://api.hms.development.corp1.majordomo.ru mjru-auth)"
  2117. --header 'Content-Type: application/json'
  2118. "$var"
  2119. )
  2120. elif [[ "$var" == *"https://api-dev.intr/"* ]]
  2121. then
  2122. json=true
  2123. new_args+=(
  2124. --header "Authorization: Bearer $(IHS_ENDPOINT=https://api-dev.intr mjru-auth)"
  2125. --header 'Content-Type: application/json'
  2126. "$var"
  2127. )
  2128. elif [[ "$var" == *"https://kubernetes.intr"* ]] || [[ "$var" == *"https://kubernetes-cluster2.intr"* ]]
  2129. then
  2130. new_args+=(
  2131. --cacert "${HOME}/src/gitlab.intr/office/ssl-certificates/kubernetes/ca.pem"
  2132. --cert "${HOME}/src/gitlab.intr/office/ssl-certificates/kubernetes/admin.pem"
  2133. --key "${HOME}/src/gitlab.intr/office/ssl-certificates/kubernetes/admin-key.pem"
  2134. "$var"
  2135. )
  2136. elif [[ "$var" == *"https://kubernetes.home:6443"* ]]
  2137. then
  2138. new_args+=(
  2139. --cacert /etc/kubernetes/pki/ca.pem
  2140. --cert /etc/kubernetes/pki/admin.pem
  2141. --key /etc/kubernetes/pki/admin-key.pem
  2142. "$var"
  2143. )
  2144. else
  2145. new_args+=($var)
  2146. fi
  2147. done
  2148. command curl "${new_args[@]}"
  2149. if [[ $json == true ]]
  2150. then
  2151. echo
  2152. fi
  2153. }
  2154. dig()
  2155. {
  2156. for var in "$@"
  2157. do
  2158. case "$var" in
  2159. *majordomo.ru|*.intr|172.16.*|*pwhost.ru|*fast-name.ru|*host-support.ru|*litename.ru|*vipname.su|*hoster24.ru)
  2160. name_servers=(
  2161. ns1-mr.intr
  2162. ns2-mr.intr
  2163. ns1-dh.intr
  2164. ns2-dh.intr
  2165. ns.majordomo.ru
  2166. ns2.majordomo.ru
  2167. ns3.majordomo.ru
  2168. ns4.majordomo.ru
  2169. ns1.corp1.majordomo.ru
  2170. ns2.corp2.majordomo.ru
  2171. ns1.svc1.majordomo.ru
  2172. ns2.svc2.majordomo.ru
  2173. ns1.corp2.majordomo.ru
  2174. ns2.corp2.majordomo.ru
  2175. ns1.svc2.majordomo.ru
  2176. ns2.svc2.majordomo.ru
  2177. 8.8.8.8
  2178. 8.8.4.4
  2179. )
  2180. (
  2181. set -x
  2182. command dig "$@"
  2183. )
  2184. for name_server in "${name_servers[@]}"
  2185. do
  2186. (
  2187. set -x
  2188. command dig "$@" "@${name_server}"
  2189. )
  2190. mapfile -t ip_addresses < <(command dig "$@" "@${name_server}" \
  2191. | jc --dig \
  2192. | jq --raw-output .[].answer[].data)
  2193. (
  2194. for ip_address in "${ip_addresses[@]}"
  2195. do
  2196. (
  2197. set -x
  2198. command dig -x "$ip_address" "@${name_server}"
  2199. )
  2200. done
  2201. ) | sort --version-sort
  2202. done
  2203. ;;
  2204. *)
  2205. command dig "$@"
  2206. ;;
  2207. esac
  2208. done
  2209. }
  2210. scream()
  2211. {
  2212. case "$1" in
  2213. *)
  2214. cat <<'EOF'
  2215. scream -i br154.154 -u -p 16400
  2216. EOF
  2217. command scream "$@"
  2218. ;;
  2219. esac
  2220. }
  2221. kresd()
  2222. {
  2223. case "$1" in
  2224. clear)
  2225. sudo socat - "UNIX-CONNECT:/var/cache/knot-resolver/control/$(pidof kresd)" <<< 'cache.clear()'
  2226. ;;
  2227. *)
  2228. command kresd "$@"
  2229. esac
  2230. }
  2231. sudo()
  2232. {
  2233. case "$1" in
  2234. herd)
  2235. case "$2" in
  2236. start)
  2237. case "$3" in
  2238. xorg-server)
  2239. if [[ -e /var/run/slim-vt7.auth ]]
  2240. then
  2241. echo "Removing '/var/run/slim-vt7.auth' file."
  2242. command sudo rm /var/run/slim-vt7.auth
  2243. fi
  2244. if [[ -e /var/run/slim-vt7.lock ]]
  2245. then
  2246. echo "Removing '/var/run/slim-vt7.lock' file."
  2247. command sudo rm /var/run/slim-vt7.lock
  2248. fi
  2249. command sudo herd enable xorg-server
  2250. command sudo herd start xorg-server
  2251. ;;
  2252. *)
  2253. command sudo herd enable "${@:3}"
  2254. command sudo herd start "${@:3}"
  2255. ;;
  2256. esac
  2257. ;;
  2258. *)
  2259. command sudo herd "${@:2}"
  2260. ;;
  2261. esac
  2262. ;;
  2263. *)
  2264. command sudo "$@"
  2265. ;;
  2266. esac
  2267. }
  2268. PASSWORD_STORE_ENABLE_EXTENSIONS="true"
  2269. export PASSWORD_STORE_ENABLE_EXTENSIONS
  2270. 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"
  2271. export PATH
  2272. if [[ -z $DISPLAY ]] && [[ $(tty) = /dev/tty1 ]]
  2273. then
  2274. exec sway
  2275. fi
  2276. pactl_configure()
  2277. {
  2278. 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
  2279. echo pactl load-module module-null-sink media.class=Audio/Sink sink_name=obs-microphone channel_map=front-left,front-right
  2280. 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
  2281. }