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.

683 lines
20 KiB

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