provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

716 lines
22 KiB

9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
3 months ago
9 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
2 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. #!/bin/bash
  2. RED='\e[0;41m\e[1;37m'
  3. GREEN='\033[0;32m'
  4. YELLOW='\033[0;33m'
  5. PURPLE='\033[0;35m'
  6. DARK='\e[100m'
  7. NC='\033[0m' # No Color
  8. # BOLD='\033[1m'
  9. # DIM='\e[2m\e[0;90m'
  10. TO_BE_DEFINED="TO BE DEFINED"
  11. FDN_DOMAINNAME=fdn.fr
  12. FDN_NAMESERVER="ns0.$FDN_DOMAINNAME"
  13. FDN_RESOLVER=80.67.169.12
  14. : $FDN_DOMAINNAME $FDN_NAMESERVER $FDN_RESOLVER
  15. function echo() {
  16. [[ -n ${PREFIX:-} ]] && printf "${DARK}%25.25s${NC} " "${PREFIX}"
  17. builtin echo "$@"
  18. }
  19. function check_normal_user() {
  20. [[ $(id -u) -lt 1000 ]] && echoerr "normal user (>1000) expected, please connect as a normal user then call again!" && exit 100
  21. return 0
  22. }
  23. function sudo_required() {
  24. check_normal_user
  25. command -v sudo &>/dev/null &&
  26. id -G | grep -q sudo && echoerr "command <sudo> not found, please install as so: \`apt install -y sudo\`" && exit 1
  27. if ! sudo -n true &>/dev/null; then
  28. if [[ -n "${1:-}" ]]; then
  29. echowarnn "[sudo] requiring authorized access for: [ $1 ]"
  30. else
  31. echowarnn "[sudo] requiring authorized access for further processing"
  32. fi
  33. fi
  34. sudo -vp ' : '
  35. }
  36. # idempotent cargo install <package1 package2 ...>
  37. function idem_cargo_install() {
  38. for i in "$@"; do
  39. if [ ! -f ~/.cargo/bin/"$i" ]; then
  40. cargo install "$i"
  41. fi
  42. done
  43. }
  44. # display error in red
  45. function echoerr() {
  46. echo -e "${RED}$*${NC}" >&2
  47. }
  48. function echoerrn() {
  49. echo -en "${RED}$*${NC}" >&2
  50. }
  51. # display warn in yellow
  52. function echowarn() {
  53. echo -e "${YELLOW}$*${NC}" >&2
  54. }
  55. function echowarnn() {
  56. echo -en "${YELLOW}$*${NC}" >&2
  57. }
  58. # display error in green
  59. function echoinfo() {
  60. echo -e "${GREEN}$*${NC}" >&2
  61. }
  62. function echoinfon() {
  63. echo -en "${GREEN}$*${NC}" >&2
  64. }
  65. # test whether <ip> is a valid ipv4 address?
  66. function valid_ipv4() {
  67. local ip="$1"
  68. if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  69. IFS='.' read -ra ADDR <<<"$ip"
  70. [[ ${ADDR[0]} -le 255 && ${ADDR[1]} -le 255 && ${ADDR[2]} -le 255 && ${ADDR[3]} -le 255 ]]
  71. return $?
  72. fi
  73. return 1
  74. }
  75. function enable_trace() {
  76. trap 'trap_error $? ${LINENO:-0} ${BASH_LINENO:-0} ${BASH_COMMAND:-empty} $(printf "::%s" ${FUNCNAME[@]})' ERR
  77. }
  78. function disable_trace() {
  79. trap - ERR
  80. }
  81. function disable_all_signals {
  82. trap - ERR
  83. trap - HUP
  84. trap - INT
  85. trap - TERM
  86. }
  87. function prepare_nftables() {
  88. local PREFIX="miaou:nftables"
  89. if ! [[ -f /etc/nftables.rules.d/firewall.table ]]; then
  90. echo "installing nftables ..."
  91. sudo apt install -y nftables
  92. sudo cp -f "$MIAOU_BASEDIR/templates/hardened/nftables.conf" /etc/
  93. sudo mkdir -p /etc/nftables.rules.d
  94. sudo systemctl enable nftables
  95. sudo systemctl start nftables
  96. else
  97. echo "nftables already installed!"
  98. fi
  99. current_target="${TARGET:-not_defined_yet}"
  100. desktop=false
  101. samba=false
  102. transmission_daemon=false
  103. if [[ $current_target == not_defined_yet ]]; then
  104. echo -n "generating new firewall table first time... "
  105. else
  106. if [[ $current_target == 'dev' ]]; then
  107. if exist_command 'xprop'; then
  108. desktop=true
  109. fi
  110. if exist_command 'smbstatus'; then
  111. samba=true
  112. fi
  113. if exist_command 'transmission-daemon'; then
  114. transmission_daemon=true
  115. fi
  116. echo -n "generating new firewall table according to target=<${current_target}>, desktop=<$desktop>, samba=<$samba>, transmission_daemon=<$transmission_daemon> ..."
  117. else
  118. echo -n "generating new firewall table according to target=<${current_target}>..."
  119. fi
  120. fi
  121. sudo env target="$current_target" desktop="$desktop" samba="$samba" transmission_daemon="$transmission_daemon" tera -e --env-only --env-key env -t "$MIAOU_BASEDIR/templates/nftables/firewall.table.j2" -o /etc/nftables.rules.d/firewall.table &>/dev/null
  122. sudo systemctl reload nftables
  123. PREFIX="" echoinfo "DONE"
  124. }
  125. function miaou_init() {
  126. # shellcheck source=/dev/null
  127. [[ -f /opt/miaou-bash/lib/functions.sh ]] && source /opt/miaou-bash/lib/functions.sh
  128. # shellcheck source=/dev/null
  129. . "$MIAOU_BASEDIR/lib/functions.sh"
  130. export MIAOU_CONFIGDIR="$HOME/.config/miaou"
  131. set -Eeuo pipefail
  132. enable_trace
  133. trap 'ctrl_c $? ${LINENO:-0} ${BASH_LINENO:-0} ${BASH_COMMAND:-empty} $(printf "::%s" ${FUNCNAME[@]})' INT
  134. }
  135. function ctrl_c() {
  136. PREFIX="miaou:trap" echoerr "Ctrl + C happened, exiting!!! $*"
  137. exit 125
  138. }
  139. # extract source code error triggered on trap error <error_code> <error_line>
  140. function trap_error() {
  141. ERRORS_COUNT=0
  142. if [[ -f "$MIAOU_CONFIGDIR"/error_count ]]; then
  143. ERRORS_COUNT=$(cat "$MIAOU_CONFIGDIR"/error_count)
  144. else
  145. mkdir -p "$MIAOU_CONFIGDIR"
  146. printf 0 >"$MIAOU_CONFIGDIR"/error_count
  147. fi
  148. ERRORS_COUNT=$((ERRORS_COUNT + 1))
  149. printf '%s' $ERRORS_COUNT >"$MIAOU_CONFIGDIR"/error_count
  150. local PREFIX=""
  151. # local file="${0:-}"
  152. local err=$1 # error status
  153. local line=$2 # LINENO
  154. # local linecallfunc=${3:-}
  155. local command="${4:-}"
  156. local funcstack="${5:-}"
  157. local caller
  158. caller=$(caller | cut -d' ' -f2)
  159. # echo >&2
  160. # if [ "$funcstack" != "::" ]; then
  161. # echo -e "${RED}ERROR <$err>, due to command <$command> at line $line from <$caller>, stack=${funcstack}${NC}" >&2
  162. # else
  163. # echo >&2 "ERROR DETECTED"
  164. # fi
  165. # echo
  166. # echo -e "${PURPLE}$caller:$line ${NC}EXIT ${RED}<$err>${NC}" >&2
  167. # echo -e "${PURPLE}------------------------------------------ ${NC}" >&2
  168. if [[ $ERRORS_COUNT == 1 ]]; then
  169. echo
  170. echo -e "${RED}ERROR <$err>, due to command <$command $funcstack>${NC}" >&2
  171. fi
  172. echo -e "${PURPLE}$ERRORS_COUNT: $caller:$line ${RED}$command $funcstack${NC}" >&2
  173. # echo -e "${PURPLE}----------------------------- ${PURPLE}EXIT CODE ${PURPLE}--------------${PURPLE} $err ${NC}" >&2
  174. # if [[ $line -gt 2 ]]; then
  175. # sed "$((line - 2))q;d" "$caller" >&2
  176. # sed "$((line - 1))q;d" "$caller" >&2
  177. # fi
  178. # echo -ne "${BOLD}" >&2
  179. # sed "${line}q;d" "$caller" >&2
  180. # echo -e "${PURPLE}------------------------------------------ ${NC}" >&2
  181. }
  182. # exist_command(cmd1, ...)
  183. # test all commands exist, else fail
  184. function exist_command() {
  185. for i in "$@"; do
  186. command -v "$i" >/dev/null || return 50
  187. done
  188. }
  189. # test whether container <ct> is up and running?
  190. function container_running() {
  191. arg1_required "$@"
  192. container_exists "$1" && lxc list "$1" -c ns -f csv | head -n1 | grep -q "$1,RUNNING"
  193. lxc exec "$1" -- bash <<EOF
  194. set -Eeuo pipefail
  195. if [[ ! -f /root/cloud-status.json ]]; then
  196. cloud-init status --wait >/dev/null
  197. fi
  198. EOF
  199. }
  200. # test arg1 required
  201. function arg1_required() {
  202. [[ -z "${1:-}" ]] && echoerr "ERROR: arg#1 expected!" && return 125
  203. return 0
  204. }
  205. # test arg2 required
  206. function arg2_required() {
  207. [[ -z "${2:-}" ]] && echoerr "ERROR: arg#2 expected!" && return 125
  208. return 0
  209. }
  210. # test arg3 required
  211. function arg3_required() {
  212. [[ -z "${3:-}" ]] && echoerr "ERROR: arg#3 expected!" && return 125 || return 0
  213. }
  214. # test arg4 required
  215. function arg4_required() {
  216. [[ -z "${4:-}" ]] && echoerr "ERROR: arg#4 expected!" && return 125 || return 0
  217. }
  218. # test whether container <ct> exists yet?
  219. function container_exists() {
  220. arg1_required "$@"
  221. lxc list "$1" -c n -f csv | grep -q "^$1\$"
  222. }
  223. # 3 args expected: <commmand> <delay in s, example: 0.2> <max_attempts>
  224. function wait_for_command {
  225. arg3_required "$@"
  226. command=$1
  227. delay=$2
  228. max_attempt=$3
  229. attempt=0
  230. while ! eval "$command"; do
  231. attempt=$((attempt + 1))
  232. if [[ $attempt -gt $max_attempt ]]; then
  233. echoerr "command <$command> failed after a delay of $(bc <<<"$max_attempt * $delay")s and $max_attempt attempts"
  234. return 1
  235. else
  236. sleep "$delay"
  237. fi
  238. done
  239. echo SUCCESS
  240. }
  241. function wait_for_container_full_initialization {
  242. arg1_required "$@"
  243. wait_for_command "lxc exec $1 -- test -f /root/cloud-status.json" 0.2 40
  244. }
  245. # build debian image with prebuild miaou-bash and various useful settings
  246. # ARG1=release [bullseye, buster]
  247. function build_miaou_image() {
  248. local RELEASE="$1"
  249. local IMAGE_LABEL="$RELEASE-miaou"
  250. local PREFIX="miaou:image"
  251. local DEB_REPOSITORY
  252. DEB_REPOSITORY=$(grep ^deb /etc/apt/sources.list | head -n1 | cut -d ' ' -f2 | cut -d '/' -f3)
  253. if ! lxc image -cl list -f csv | grep -q "$IMAGE_LABEL"; then
  254. echo "building lxc image <$IMAGE_LABEL> ... "
  255. echo "image will reuse same local repository <$DEB_REPOSITORY>"
  256. creation_date=$(date +%s)
  257. sudo /opt/miaou-bash/tools/idem_apt_install debootstrap
  258. cat <<EOF1 | sudo bash
  259. set -euo pipefail
  260. rm -rf /tmp/$IMAGE_LABEL{,-image}
  261. mkdir -p /tmp/$IMAGE_LABEL{,-image}
  262. debootstrap $RELEASE /tmp/$IMAGE_LABEL http://$DEB_REPOSITORY/debian
  263. echo
  264. echo "DEBOOTSTRAP ... OK"
  265. echo
  266. cat <<EOF2 | chroot /tmp/$IMAGE_LABEL
  267. set -euo pipefail
  268. echo "image prepare source.list from $DEB_REPOSITORY"
  269. if [[ "$RELEASE" == "buster" ]]; then
  270. cat <<EOF3 >/etc/apt/sources.list
  271. deb http://$DEB_REPOSITORY/debian $RELEASE main contrib
  272. deb http://$DEB_REPOSITORY/debian $RELEASE-updates main contrib
  273. deb http://$DEB_REPOSITORY/debian-security/ $RELEASE/updates main contrib
  274. EOF3
  275. else
  276. cat <<EOF3 >/etc/apt/sources.list
  277. deb http://$DEB_REPOSITORY/debian $RELEASE main contrib
  278. deb http://$DEB_REPOSITORY/debian $RELEASE-updates main contrib
  279. deb http://$DEB_REPOSITORY/debian-security/ $RELEASE-security main contrib
  280. EOF3
  281. fi
  282. echo APT UPDATE
  283. apt update && apt dist-upgrade -y
  284. apt install -y curl wget file git sudo bash-completion
  285. curl https://git.artcode.re/miaou/miaou-bash/raw/branch/main/install.sh | sudo bash -s -- --host
  286. # TODO: remove line below
  287. # ln -sf /usr/share/zoneinfo/Indian/Reunion /etc/localtime
  288. cat <<EOF3 >/etc/network/interfaces
  289. # This file describes the network interfaces available on your system
  290. # and how to activate them. For more information, see interfaces(5).
  291. # The loopback network interface
  292. auto lo
  293. iface lo inet loopback
  294. auto eth0
  295. iface eth0 inet dhcp
  296. source /etc/network/interfaces.d/*
  297. EOF3
  298. echo "deboostrap ready!"
  299. EOF2
  300. cd /tmp/$IMAGE_LABEL-image
  301. tar -czf rootfs.tar.gz -C /tmp/$IMAGE_LABEL .
  302. cat <<EOF2 >metadata.yaml
  303. architecture: "x86_64"
  304. creation_date: $creation_date
  305. properties:
  306. architecture: "x86_64"
  307. description: "Debian $RELEASE for miaou instances"
  308. os: "debian"
  309. release: "$RELEASE"
  310. EOF2
  311. tar -czf metadata.tar.gz metadata.yaml
  312. EOF1
  313. lxc image import "/tmp/$IMAGE_LABEL-image/metadata.tar.gz" "/tmp/$IMAGE_LABEL-image/rootfs.tar.gz" --alias "$IMAGE_LABEL"
  314. echo "image <$IMAGE_LABEL> successfully built!"
  315. echo DONE
  316. else
  317. echo "image <$IMAGE_LABEL> already built!"
  318. fi
  319. }
  320. # convert array to string according to IFS arg1
  321. # example: join "," "${MY_ARRAY[@]}" => one,two,three
  322. function join() {
  323. local IFS="$1"
  324. shift
  325. builtin echo "$*"
  326. }
  327. # execute remote scripting over one LXC container <CONTAINER> [COMMANDS, ...]
  328. # may use one command like: `lxc_exec ct1 uname -a`
  329. # or pipe like so: `
  330. # cat <<EOF | lxc_exec ct1
  331. # ls -l
  332. # uname -a
  333. # echo [\$0] [\$1] [\$2] # toto titi tata
  334. # EOF
  335. # `
  336. function lxc_exec() {
  337. arg1_required "$@"
  338. container="$1"
  339. shift
  340. declare -a ARGUMENTS
  341. ARGUMENTS=(toto titi tata) # might be overriden with interesting stuff!
  342. if ((${#} == 0)); then
  343. multiline=""
  344. while read -r line; do
  345. if [[ ! "$line" =~ ^\# ]] && [[ ! "$line" =~ ^[[:space:]]*$ ]]; then
  346. if [[ "$line" =~ .*\;$ ]] || [[ "$line" =~ do$ ]] || [[ "$line" =~ then$ ]] || [[ "$line" =~ else$ ]]; then
  347. multiline+="${line} " # append space in case of ending with either '; do then else'
  348. else
  349. multiline+="${line};" # append ; for multiple commands
  350. fi
  351. fi
  352. done
  353. # echo "DEBUG: multiline = [$multiline]"
  354. # echo DEBUG: lxc exec "$container" -- bash -lc "$multiline" "${ARGUMENTS[@]}"
  355. lxc exec "$container" -- bash -lc "$multiline" "${ARGUMENTS[@]}"
  356. else
  357. lxc exec "$container" -- bash -lc "$*" "${ARGUMENTS[@]}"
  358. fi
  359. }
  360. # check container exist and running
  361. function check_container() {
  362. arg1_required "$@"
  363. local CT="$1"
  364. container_exists "$CT"
  365. container_running "$CT"
  366. }
  367. function launch_container() {
  368. arg1_required "$@"
  369. local ct="$1"
  370. if ! container_exists "$ct"; then
  371. echo "container <$ct> about to be created ..."
  372. local extra_release="${2:-}"
  373. if [[ -n "$extra_release" ]] && ! lxc image info "${extra_release}-miaou" >/dev/null; then
  374. echoerrn "unknown extra_release <${extra_release}-miaou>!\nHINT : please add it into /etc/miaou/defaults.yaml, then re-install miaou!"
  375. exit 128
  376. fi
  377. if [[ -n "$extra_release" ]]; then
  378. echoerrn "FIXME: lxc-miaou-create -o release=bookworm should be implemented ...."
  379. lxc-miaou-create "$ct" "$extra_release"
  380. else
  381. lxc-miaou-create "$ct"
  382. fi
  383. echo "DONE"
  384. fi
  385. if ! container_running "$ct"; then
  386. echowarn "container <$ct> seems to be asleep, starting ..."
  387. lxc start "$ct"
  388. echowarn DONE
  389. fi
  390. }
  391. function load_yaml_from_expanded {
  392. arg1_required "$@"
  393. yaml_key="$1"
  394. yaml_file="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  395. yaml_value=$(yq ".$yaml_key" "$yaml_file")
  396. if [[ -n "$yaml_value" ]] && [[ "$yaml_value" != "null" ]] && [[ "$yaml_value" != "$TO_BE_DEFINED" ]]; then
  397. PREFIX="" echo "$yaml_value"
  398. else
  399. echoerr "undefined value for key: <$yaml_key> from file: <$yaml_file>"
  400. return 98
  401. fi
  402. }
  403. function check_yaml_defined_value {
  404. yaml_file="$1"
  405. yaml_key="$2"
  406. yaml_value=$(yq ".$yaml_key" "$yaml_file")
  407. if [[ -n "$yaml_value" ]] && [[ "$yaml_value" != "null" ]] && [[ "$yaml_value" != "$TO_BE_DEFINED" ]]; then
  408. return 0
  409. else
  410. echoerr "undefined value for key: <$yaml_key> from file: <$yaml_file>"
  411. return 99
  412. fi
  413. }
  414. # halt unless current user is root
  415. function root_required() {
  416. [[ $(id -u) == 0 ]] || (echoerr "root required" && return 1)
  417. }
  418. # arg#1: environment variable
  419. # read from environment or ask entry before exporting new variable
  420. function env_or_ask {
  421. if [[ -n ${1+x} ]]; then
  422. if printenv "$1" >/dev/null; then
  423. echo "value defined as $(printenv "$1")"
  424. else
  425. printf "Please define %20s: " "$1"
  426. read -r
  427. export "$1=\"$REPLY\"" >/dev/null
  428. fi
  429. else
  430. echoerr "env_or_ask requires one argument: <VARIABLE_NAME>" && exit 5
  431. fi
  432. }
  433. # grab and install related project
  434. function install_miaou_bash() {
  435. local PREFIX="miaou-bash:install"
  436. if [[ ! -d /opt/miaou-bash ]]; then
  437. echo "installing curl wget commands ..."
  438. sudo apt install -y curl wget
  439. echo "installing miaou-bash..."
  440. curl https://git.artcode.re/miaou/miaou-bash/raw/branch/main/install.sh | sudo bash -s
  441. echo "OK"
  442. else
  443. echo "addon <miaou-bash> already installed!"
  444. fi
  445. # shellcheck source=/dev/null
  446. source /etc/bash.bashrc
  447. sudo /opt/miaou-bash/tools/idem_apt_install bash-completion
  448. }
  449. function add_toolbox_sudoers {
  450. local PREFIX="toolbox:sudoers"
  451. echo -n "creating sudoers file to allow sudo as command from /TOOLBOX... "
  452. sudo mkdir -p /etc/sudoers.d
  453. if [[ ! -f /etc/sudoers.d/add_TOOLBOX_to_PATH ]]; then
  454. sudo tee /etc/sudoers.d/add_TOOLBOX_to_PATH &>/dev/null <<EOF
  455. Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/TOOLBOX"
  456. EOF
  457. PREFIX="" echo "updated!"
  458. else
  459. PREFIX="" echo "already done!"
  460. fi
  461. }
  462. function prepare_toolbox() {
  463. local PREFIX="toolbox:prepare"
  464. sudo mkdir -p /TOOLBOX
  465. if ! command -v cargo &>/dev/null; then
  466. echo -n "installing <cargo> ... "
  467. curl -sSf https://sh.rustup.rs | sh -s -- -y
  468. # shellcheck source=/dev/null
  469. source "$HOME/.cargo/env"
  470. /opt/miaou-bash/tools/append_or_replace "^PATH=\$PATH:\$HOME/\\.cargo/bin" "PATH=\$PATH:\$HOME/.cargo/bin" ~/.bashrc
  471. PREFIX="" echo "OK"
  472. else
  473. echo "command <cargo> already installed!"
  474. fi
  475. echo -n "installing <fd> ... "
  476. if [ ! -f "/TOOLBOX/fd" ]; then
  477. idem_cargo_install fd-find
  478. sudo cp "$HOME"/.cargo/bin/fd /TOOLBOX/fd
  479. PREFIX="" echo "successfully installed!"
  480. else
  481. PREFIX="" echo "already done!"
  482. fi
  483. echo -n "installing <viu> ... "
  484. if [ ! -f "/TOOLBOX/viu" ]; then
  485. idem_cargo_install viu
  486. sudo cp "$HOME"/.cargo/bin/viu /TOOLBOX/
  487. PREFIX="" echo "successfully installed!"
  488. else
  489. PREFIX="" echo "already done!"
  490. fi
  491. echo -n "installing <rg> alias <ripgrep> ... "
  492. if [ ! -f "/TOOLBOX/rg" ]; then
  493. sudo /opt/miaou-bash/tools/idem_apt_install ripgrep
  494. sudo ln /usr/bin/rg /TOOLBOX/
  495. PREFIX="" echo "successfully installed"
  496. else
  497. PREFIX="" echo "already done!"
  498. fi
  499. echo -n "installing <ag> alias <silversearcher-ag> ... "
  500. if [ ! -f "/TOOLBOX/ag" ]; then
  501. sudo /opt/miaou-bash/tools/idem_apt_install silversearcher-ag
  502. sudo ln /usr/bin/ag /TOOLBOX/
  503. PREFIX="" echo "successfully installed"
  504. else
  505. PREFIX="" echo "already done!"
  506. fi
  507. echo -n "installing <bandwhich> ... "
  508. if [ ! -f "/TOOLBOX/bandwhich" ]; then
  509. idem_cargo_install bandwhich
  510. sudo cp "$HOME"/.cargo/bin/bandwhich /TOOLBOX/bandwhich
  511. PREFIX="" echo "successfully installed"
  512. else
  513. PREFIX="" echo "already done!"
  514. fi
  515. echo -n "installing <btm> alias <bottom> ... "
  516. if [ ! -f "/TOOLBOX/btm" ]; then
  517. VERSION=$(wget_semver github ClementTsang/bottom)
  518. cd /tmp
  519. wget "https://github.com/ClementTsang/bottom/releases/download/$VERSION/bottom_x86_64-unknown-linux-musl.tar.gz"
  520. tar -xzvf bottom_x86_64-unknown-linux-musl.tar.gz
  521. sudo cp btm /usr/local/bin/
  522. sudo ln /usr/local/bin/btm /TOOLBOX/
  523. PREFIX="" echo "successfully installed"
  524. else
  525. PREFIX="" echo "already done!"
  526. fi
  527. echo -n "installing <micro> ... "
  528. if [ ! -f "/TOOLBOX/micro" ]; then
  529. cd /tmp || (echoerr "/tmp wrong permission" && exit 101)
  530. curl -q https://getmic.ro | GETMICRO_REGISTER=n sh
  531. sudo mv micro /TOOLBOX/micro
  532. sudo chown root:root /TOOLBOX/micro
  533. PREFIX="" echo "successfully installed"
  534. else
  535. PREFIX="" echo "already done!"
  536. fi
  537. echo -n "installing <ncdu> ... "
  538. if [ ! -f "/TOOLBOX/ncdu" ]; then
  539. sudo /opt/miaou-bash/tools/idem_apt_install ncdu
  540. sudo cp /usr/bin/ncdu /TOOLBOX/ncdu
  541. PREFIX="" echo "successfully installed"
  542. else
  543. PREFIX="" echo "already done!"
  544. fi
  545. echo -n "installing <unzip> ... "
  546. if [ ! -f "/TOOLBOX/unzip" ]; then
  547. sudo /opt/miaou-bash/tools/idem_apt_install unzip
  548. sudo cp /usr/bin/unzip /TOOLBOX/unzip
  549. PREFIX="" echo "successfully installed"
  550. else
  551. PREFIX="" echo "already done!"
  552. fi
  553. echo -n "installing <tree> ... "
  554. if [ ! -f "/TOOLBOX/tree" ]; then
  555. sudo /opt/miaou-bash/tools/idem_apt_install tree
  556. sudo cp /bin/tree /TOOLBOX/tree
  557. PREFIX="" echo "successfully installed"
  558. else
  559. PREFIX="" echo "already done!"
  560. fi
  561. echo -n "installing <duf> ... "
  562. if [ ! -f "/TOOLBOX/duf" ]; then
  563. VERSION=$(/opt/miaou-bash/tools/wget_semver github muesli/duf)
  564. VERSION_WITHOUT_V=${VERSION#v}
  565. wget -O /tmp/duf.deb "https://github.com/muesli/duf/releases/download/${VERSION}/duf_${VERSION_WITHOUT_V}_linux_amd64.deb"
  566. sudo dpkg -i /tmp/duf.deb
  567. sudo cp /bin/duf /TOOLBOX/duf
  568. PREFIX="" echo "successfully installed"
  569. else
  570. PREFIX="" echo "already done!"
  571. fi
  572. echo -n "installing <curl> ... "
  573. if [ ! -f "/TOOLBOX/curl" ]; then
  574. sudo wget -O /TOOLBOX/curl "https://github.com/moparisthebest/static-curl/releases/latest/download/curl-amd64"
  575. sudo chmod +x /TOOLBOX/curl
  576. PREFIX="" echo "successfully installed"
  577. else
  578. PREFIX="" echo "already done!"
  579. fi
  580. echo -n "installing <wget> ... "
  581. if [ ! -f "/TOOLBOX/wget" ]; then
  582. sudo ln -f /usr/bin/wget /TOOLBOX/wget
  583. PREFIX="" echo "successfully installed"
  584. else
  585. PREFIX="" echo "already done!"
  586. fi
  587. }
  588. # install_mandatory_commands
  589. function install_mandatory_commands() {
  590. local PREFIX="mandatory:commands"
  591. echo "installing various mandatory commands"
  592. sudo /opt/miaou-bash/tools/idem_apt_install dnsutils build-essential curl mariadb-client postgresql-client
  593. if ! exist_command tera; then
  594. echo "installing <tera> ..."
  595. local version=v0.2.4
  596. wget -q "https://github.com/chevdor/tera-cli/releases/download/${version}/tera-cli_linux_amd64.deb" -O /tmp/tera-cli_linux_amd64.deb
  597. sudo dpkg -i /tmp/tera-cli_linux_amd64.deb
  598. else
  599. echo "command <tera> already installed!"
  600. fi
  601. if ! exist_command yq; then
  602. local version binary
  603. version='v4.35.2'
  604. binary='yq_linux_amd64'
  605. sudo sh -c "wget https://github.com/mikefarah/yq/releases/download/${version}/${binary}.tar.gz -O - |\
  606. tar -xz ./${binary} && sudo mv ${binary} /usr/bin/yq"
  607. else
  608. echo "command <yq> already installed!"
  609. fi
  610. }
  611. # flatten array, aka remove duplicated elements in array
  612. # return: `mapfile -t OUTPUT_ARRAY < <(sort_array "${INPUT_ARRAY[@]}")`
  613. function flatten_array {
  614. declare -a array=("$@")
  615. IFS=" " read -r -a array <<<"$(tr ' ' '\n' <<<"${array[@]}" | sort -u | tr '\n' ' ')"
  616. printf '%s\n' "${array[@]}"
  617. }