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.

707 lines
22 KiB

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