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.

499 lines
15 KiB

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