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.

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