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.

409 lines
13 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
  1. #!/bin/bash
  2. MIAOU_BASEDIR=$(readlink -f "$(dirname "$0")/..")
  3. # shellcheck source=/dev/null
  4. . "$MIAOU_BASEDIR/lib/functions.sh"
  5. readonly MIAOU_BASEDIR
  6. miaou_init
  7. EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  8. NEW_GROUP=lxd
  9. readonly NEW_GROUP EXPANDED_CONF
  10. on_exit() {
  11. if [[ "$SESSION_RELOAD_REQUIRED" == true ]]; then
  12. echo "======================================================"
  13. echo "Session Reload is required (due to new group <$NEW_GROUP>)"
  14. echo "======================================================"
  15. fi
  16. if [ -n "${1:-}" ]; then
  17. echo "Aborted by $1"
  18. elif [ "${status:-}" -ne 0 ]; then
  19. echo "Failure (status $status)"
  20. fi
  21. }
  22. function prepare_lxd {
  23. local PREFIX="lxd:prepare"
  24. # test group lxd assign to current user
  25. if ! groups | grep -q lxd; then
  26. echo "define lxd and assign to user <$USER>"
  27. sudo groupadd --force "$NEW_GROUP"
  28. sudo usermod --append --groups "$NEW_GROUP" "$(whoami)"
  29. exec sg "$NEW_GROUP" exec "$0 $TARGET SESSION_RELOAD_REQUIRED $@"
  30. # no further processing because exec has been called!
  31. else
  32. echo "user <$USER> already belongs to group <lxd>!"
  33. fi
  34. sudo /opt/miaou-bash/tools/idem_apt_install lxd btrfs-progs
  35. override_lxd_service
  36. # test lxdbr0
  37. if ! lxc network info lxdbr0 &>/dev/null; then
  38. echo "bridge <lxdbr0> down, so initialization will use default preseed..."
  39. empty_block_partition=''
  40. if lsblk -o NAME,FSTYPE,GROUP --noheadings -p | grep -E ^.─ | grep disk; then
  41. # FIXME: when nesting no DISK type partition found!
  42. empty_block_partition=$(lsblk -o NAME,FSTYPE,GROUP --noheadings -p | grep -E ^.─ | grep disk | awk '{if($3=="") print $1}' | cut -d'/' -f3)
  43. fi
  44. if [[ -n "$empty_block_partition" ]]; then
  45. echo "use empty block partition /dev/$empty_block_partition"
  46. cat <<EOF | sudo lxd init --preseed
  47. config: {}
  48. networks:
  49. - config:
  50. ipv4.address: auto
  51. ipv6.address: none
  52. description: ""
  53. name: lxdbr0
  54. type: ""
  55. project: default
  56. storage_pools:
  57. - config:
  58. source: /dev/$empty_block_partition
  59. description: ""
  60. name: default
  61. driver: btrfs
  62. profiles:
  63. - config: {}
  64. description: ""
  65. devices:
  66. eth0:
  67. name: eth0
  68. network: lxdbr0
  69. type: nic
  70. root:
  71. path: /
  72. pool: default
  73. type: disk
  74. name: default
  75. projects: []
  76. cluster: null
  77. EOF
  78. echo OK
  79. else
  80. echo "use dir partition"
  81. cat <<EOF | lxd init --preseed
  82. config: {}
  83. networks:
  84. - name: lxdbr0
  85. type: bridge
  86. config:
  87. ipv4.address: auto
  88. ipv6.address: none
  89. EOF
  90. fi
  91. else
  92. echo "bridge <lxdbr0> found implies it has been already initialized!"
  93. fi
  94. set_alias 'sameuser' "exec @ARG1@ -- su --whitelist-environment container_hostname - $(whoami)"
  95. set_alias 'login' 'exec @ARGS@ --mode interactive -- /bin/bash -c $@${user:-root} - exec su --whitelist-environment container_hostname - '
  96. set_alias 'll' 'list -c ns4mDN'
  97. # test environment container hostname
  98. local env_container_hostname
  99. env_container_hostname=$(lxc profile get default environment.container_hostname)
  100. if [[ -z "$env_container_hostname" ]]; then
  101. env_container_hostname=$(hostname -s)
  102. if env | grep -q container_hostname; then
  103. local previous_container_hostname=$(env | grep container_hostname | cut -d '=' -f2)
  104. env_container_hostname="$previous_container_hostname $env_container_hostname"
  105. fi
  106. echo -n "set environment container_hostname to <$env_container_hostname> ... "
  107. lxc profile set default environment.container_hostname "$env_container_hostname"
  108. PREFIX="" echoinfo OK
  109. else
  110. echo "environment container_hostname <$env_container_hostname> already defined!"
  111. fi
  112. if ! grep -q "root:$(id -u):1" /etc/subuid; then
  113. echo -n "subuid, subgid allowing <$(whoami)> ..."
  114. printf "root:$(id -u):1\n" | sudo tee -a /etc/subuid /etc/subgid
  115. PREFIX="" echoinfo DONE
  116. else
  117. echo "subuid, subgid allowing <$(whoami)> already done!"
  118. fi
  119. if [[ ! -d "$HOME/LXD/SHARED" ]]; then
  120. echo -n "$HOME/LXD/SHARED creating ... "
  121. mkdir "$HOME/LXD/SHARED" -p
  122. PREFIX="" echoinfo DONE
  123. else
  124. echo "folder <$HOME/LXD/SHARED> already created!"
  125. fi
  126. if [[ ! -d "$HOME/LXD/BACKUP" ]]; then
  127. echo -n "$HOME/LXD/BACKUP creating ... "
  128. mkdir "$HOME/LXD/BACKUP" -p
  129. PREFIX="" echoinfo DONE
  130. else
  131. echo "folder <$HOME/LXD/BACKUP> already created!"
  132. fi
  133. }
  134. function set_alias {
  135. local name="$1"
  136. local command="$2"
  137. if ! lxc alias list -f csv | grep -q "^$name,"; then
  138. echo -n "defining new lxc alias <$name> ..."
  139. lxc alias add "$name" "$command"
  140. PREFIX="" echoinfo OK
  141. else
  142. echo "lxc alias <$name> already defined!"
  143. fi
  144. }
  145. function miaou_evalfrombashrc() {
  146. local PREFIX="miaou:bashrc"
  147. output=$(
  148. /opt/miaou-bash/tools/append_or_replace \
  149. "^eval \"\\$\($MIAOU_BASEDIR/lib/install.sh shellenv\)\"$" \
  150. "eval \"\$($MIAOU_BASEDIR/lib/install.sh shellenv)\"" \
  151. "$HOME/.bashrc"
  152. )
  153. if [[ "$output" == "appended" ]]; then
  154. echo "new path <$MIAOU_BASEDIR> created!"
  155. SESSION_RELOAD_REQUIRED=true
  156. else
  157. echo "path <$MIAOU_BASEDIR> already loaded!"
  158. fi
  159. }
  160. function ask_target() {
  161. PS3='Choose miaou target purpose: '
  162. foods=("Dev" "Beta" "Prod")
  163. select ans in "${foods[@]}"; do
  164. builtin echo "${ans^^}"
  165. break
  166. done
  167. }
  168. function check_credential {
  169. local PREFIX="check:credential"
  170. check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.username' &&
  171. check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.shadow' &&
  172. check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.email' &&
  173. check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.password'
  174. }
  175. function check_target() {
  176. case "${TARGET^^}" in
  177. DEV) ;;
  178. BETA) ;;
  179. PROD) ;;
  180. *)
  181. if [[ -f /etc/miaou/defaults.yaml ]]; then
  182. # load already defined target in expanded conf
  183. TARGET=$(grep -Es "^target:" /etc/miaou/defaults.yaml | cut -d ' ' -f2)
  184. else
  185. TARGET=$(ask_target)
  186. fi
  187. ;;
  188. esac
  189. TARGET=${TARGET,,} # downcase
  190. return 0
  191. }
  192. function miaou_configfiles() {
  193. local PREFIX="miaou:config"
  194. if [[ ! -d /etc/miaou ]]; then
  195. echo -n "configuration initializing ..."
  196. sudo mkdir -p /etc/miaou
  197. sudo chown "$USER" /etc/miaou
  198. PREFIX="" echoinfo OK
  199. fi
  200. if [[ ! -f /etc/miaou/defaults.yaml ]]; then
  201. echo -n "building /etc/miaou/defaults.yaml for the first time..."
  202. shadow_passwd=$(sudo grep "$CURRENT_USER" /etc/shadow | cut -d ':' -f2)
  203. env current_user="$CURRENT_USER" shadow_passwd="$shadow_passwd" tera -e --env-key env --env-only -t "$MIAOU_BASEDIR/templates/etc/defaults.yaml.j2" -o /etc/miaou/defaults.yaml >/dev/null
  204. yq ".target=\"$TARGET\"" /etc/miaou/defaults.yaml -i
  205. PREFIX="" echoinfo OK
  206. fi
  207. if [[ ! -f /etc/miaou/miaou.yaml ]]; then
  208. echo -n "building /etc/miaou/miaou.yaml for the first time..."
  209. cp "$MIAOU_BASEDIR/templates/etc/miaou.yaml.j2" /etc/miaou/miaou.yaml
  210. PREFIX="" echoinfo OK
  211. fi
  212. PREVIOUS_TARGET=""
  213. echo "expanded configuration stored in <$MIAOU_CONFIGDIR>!"
  214. [[ -f "$EXPANDED_CONF" ]] && PREVIOUS_TARGET=$(grep -Es "^target:" "$EXPANDED_CONF" | cut -d ' ' -f2)
  215. if [[ "$PREVIOUS_TARGET" != "$TARGET" ]]; then
  216. if [[ -z "$PREVIOUS_TARGET" ]]; then
  217. echo "new target defined <$TARGET>"
  218. else
  219. echowarnn "TARGET has changed from <$PREVIOUS_TARGET> to <$TARGET>, do you agree?"
  220. if askConfirmation N; then
  221. echowarn "removing previous settings, please restart <miaou> to apply changes"
  222. rm "$MIAOU_CONFIGDIR" -rf
  223. else
  224. echoerr "TARGET not accepted, exit"
  225. exit 102
  226. fi
  227. fi
  228. yq ".target=\"$TARGET\"" /etc/miaou/defaults.yaml -i
  229. else
  230. echo "target <$TARGET> already defined!"
  231. fi
  232. }
  233. function opt_link() {
  234. if [[ $MIAOU_BASEDIR != '/opt/miaou' ]]; then
  235. if [[ -L '/opt/miaou' && -d '/opt/miaou' && $(readlink /opt/miaou) == "$MIAOU_BASEDIR" ]]; then
  236. echo "symbolic link /opt/miaou already set up!"
  237. else
  238. sudo rm -f /opt/miaou
  239. sudo ln -s "$MIAOU_BASEDIR" /opt/miaou
  240. echo "symbolic link /opt/miaou successfully defined!"
  241. fi
  242. else
  243. echo "real path /opt/miaou already set up!"
  244. fi
  245. }
  246. function miaou_resolver() {
  247. local PREFIX="miaou:resolver"
  248. bridge=$(ip addr show lxdbr0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
  249. gateway=$(ip route | grep default | cut -d' ' -f3)
  250. if command -v nmcli &>/dev/null; then
  251. if [[ ! -f /etc/NetworkManager/dispatcher.d/50-miaou-resolver ]]; then
  252. echo -n "use NetworkManager dispatcher to deal with LXD bridge automatically..."
  253. sudo cp "$MIAOU_BASEDIR/templates/network-manager/50-miaou-resolver" /etc/NetworkManager/dispatcher.d/
  254. sudo chmod +x /etc/NetworkManager/dispatcher.d/50-miaou-resolver
  255. ACTIVE_CONNECTION=$(nmcli -g NAME connection show --active | head -n1)
  256. sudo nmcli connection up "$ACTIVE_CONNECTION" &>/dev/null
  257. PREFIX="" echoinfo OK
  258. else
  259. echo "miaou-resolver in NetworkManager dispatcher already initialized!"
  260. fi
  261. else
  262. if ! grep -q "nameserver $bridge" /etc/resolv.conf; then
  263. echo "customize resolv.conf from scratch (SERVER)..."
  264. sudo tee /etc/resolv.conf &>/dev/null <<EOF
  265. nameserver $bridge
  266. nameserver $gateway
  267. EOF
  268. PREFIX="" echoinfo OK
  269. else
  270. echo "customize resolv.conf already already defined!"
  271. fi
  272. fi
  273. }
  274. function extra_dev_desktop {
  275. # detect if DEV
  276. # detect if DESKTOP
  277. :
  278. }
  279. function override_lxd_service {
  280. local PREFIX="lxd:override"
  281. if [[ ! -d /etc/systemd/system/lxd.service.d ]]; then
  282. echo -n "override lxd service..."
  283. sudo mkdir -p /etc/systemd/system/lxd.service.d
  284. cat <<EOF | sudo tee /etc/systemd/system/lxd.service.d/override.conf
  285. [Service]
  286. ExecStartPost=systemctl reload nftables.service
  287. Environment=LANGUAGE=en:en_US
  288. EOF
  289. sudo systemctl daemon-reload
  290. sudo systemctl restart lxd.service
  291. PREFIX="" echo "OK"
  292. else
  293. echo "lxd service already overridden!"
  294. fi
  295. }
  296. function ask_for_credential {
  297. local PREFIX="ask:credential"
  298. if ! check_credential 2>/dev/null; then
  299. echo "further details required, please replace any <TO BE DEFINED> by a proper value ...press any key to open editor"
  300. read -rn1
  301. editor /etc/miaou/defaults.yaml
  302. fi
  303. check_credential
  304. echo "successfully checked!"
  305. }
  306. function preload_bookworm_image {
  307. local PREFIX="preload:bookworm"
  308. if [[ $(lxc image list debian/12/cloud -f csv | wc -l) -lt 1 ]]; then
  309. echo -n "downloading images from publc remote, please hold on..."
  310. lxc image copy images:debian/12/cloud local: --copy-aliases --quiet
  311. PREFIX="" echoinfo OK
  312. else
  313. echo -n "refreshing images from publc remote..."
  314. lxc image refresh debian/12/cloud --quiet
  315. PREFIX="" echoinfo DONE
  316. fi
  317. }
  318. ### MAIN
  319. if [[ "${2:-}" == "SESSION_RELOAD_REQUIRED" ]]; then
  320. SESSION_RELOAD_REQUIRED=true
  321. shift
  322. else
  323. SESSION_RELOAD_REQUIRED=false
  324. fi
  325. if [[ "${1:-}" == "shellenv" ]]; then
  326. unset PREFIX
  327. echo "export MIAOU_BASEDIR=$MIAOU_BASEDIR"
  328. echo "export PATH=\"\$MIAOU_BASEDIR/scripts\":\$PATH"
  329. else
  330. . "$MIAOU_BASEDIR/lib/init.sh"
  331. trap 'status=$?; on_exit; exit $status' EXIT
  332. trap 'trap - HUP; on_exit SIGHUP; kill -HUP $$' HUP
  333. trap 'trap - INT; on_exit SIGINT; kill -INT $$' INT
  334. trap 'trap - TERM; on_exit SIGTERM; kill -TERM $$' TERM
  335. PREFIX="miaou"
  336. : $PREFIX
  337. TARGET=${1:-}
  338. CURRENT_USER=$(id -un)
  339. check_target
  340. sudo_required
  341. install_miaou_bash
  342. install_mandatory_commands
  343. prepare_toolbox
  344. add_toolbox_sudoers
  345. prepare_nftables
  346. prepare_lxd "$@"
  347. preload_bookworm_image
  348. miaou_resolver
  349. miaou_evalfrombashrc
  350. miaou_configfiles
  351. ask_for_credential
  352. opt_link
  353. extra_dev_desktop
  354. if [[ "$SESSION_RELOAD_REQUIRED" == false ]]; then
  355. echoinfo "successful installation"
  356. else
  357. echowarn "please reload your session, .bashrc needs to be reloaded!"
  358. fi
  359. fi