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.

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