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.

454 lines
13 KiB

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