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.

676 lines
20 KiB

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