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.

124 lines
3.7 KiB

9 months ago
8 months ago
9 months ago
  1. #!/bin/bash
  2. readonly EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  3. TARGET=$(yq '.target' "$EXPANDED_CONF")
  4. readonly TARGET
  5. function check() {
  6. container_exists "$CONTAINER" || return 1
  7. container_running "$CONTAINER" || return 2
  8. check_reverseproxy || return 4
  9. check_banner || return 5
  10. check_certbot || return 6
  11. PREFIX="recipe:dmz:check" echo "container <$CONTAINER> approved successfully!"
  12. return 0
  13. }
  14. function check_reverseproxy() {
  15. lxc exec "$CONTAINER" -- bash <<EOF
  16. set -Eeuo pipefail
  17. dpkg -l nginx | grep -q ^ii
  18. systemctl is-active --quiet nginx
  19. nginx -tq
  20. EOF
  21. }
  22. function check_certbot() {
  23. lxc exec "$CONTAINER" -- bash <<EOF
  24. set -Eeuo pipefail
  25. dpkg -l certbot | grep -q ^ii
  26. dpkg -l python3-certbot-nginx | grep -q ^ii
  27. EOF
  28. }
  29. function check_banner() {
  30. if [[ $TARGET != "prod" ]]; then
  31. lxc exec "$CONTAINER" -- bash <<EOF
  32. set -Eeuo pipefail
  33. test -f /etc/nginx/snippets/banner_$TARGET.conf
  34. EOF
  35. fi
  36. }
  37. function install() {
  38. PREFIX="recipe:dmz:install"
  39. : $PREFIX
  40. echowarn "about to deploy new container <$CONTAINER> ..."
  41. if ! container_exists "$CONTAINER"; then
  42. echowarn "about to create new container <$CONTAINER> ..."
  43. lxc-miaou-create "$CONTAINER"
  44. echo OK
  45. fi
  46. if ! container_running "$CONTAINER"; then
  47. echowarn "about to start asleep container <$CONTAINER> ..."
  48. lxc start "$CONTAINER"
  49. echo OK
  50. fi
  51. credential_email=$(load_yaml_from_expanded credential.email)
  52. lxc exec "$CONTAINER" -- bash <<EOF
  53. set -Eeuo pipefail
  54. apt-get update && apt-get dist-upgrade -y
  55. apt-get install -y nftables nginx ssl-cert libnginx-mod-http-subs-filter certbot python3-certbot-nginx
  56. echo "registering with your default credential email <$credential_email>"
  57. certbot register --agree-tos --email $credential_email --no-eff-email || echo "already resgistered!"
  58. rm /etc/nginx/sites-{enabled,available}/default -f
  59. systemctl enable nginx
  60. nginx -tq || rm /etc/nginx/sites-enabled/hosts
  61. systemctl start nginx
  62. EOF
  63. if [[ "$TARGET" != "prod" ]]; then
  64. echo "copying Nginx banner to container <$CONTAINER> ... "
  65. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_$TARGET.conf" "$CONTAINER/etc/nginx/snippets/banner_$TARGET.conf"
  66. echo "copying files over container <$CONTAINER> ... OK"
  67. else
  68. echo "no Nginx banner on PROD!"
  69. fi
  70. echo "populate nftables entries into yaml"
  71. local wan_interface dmz_ip
  72. wan_interface=$(ip route show default | cut -d ' ' -f5)
  73. dmz_ip=$(host "$CONTAINER.lxd" | cut -d ' ' -f4)
  74. yq ".nftables.wan_interface=\"$wan_interface\"" "$EXPANDED_CONF" -i
  75. yq ".nftables.dmz_ip=\"$dmz_ip\"" "$EXPANDED_CONF" -i
  76. local nftables_reloading=false
  77. if [[ "$TARGET" != "dev" ]]; then
  78. mkdir -p "$MIAOU_CONFIGDIR/nftables.rules.d"
  79. echo "nat http/s port to dmz"
  80. tera -t "$MIAOU_BASEDIR/templates/nftables/nat.table.j2" "$EXPANDED_CONF" -o "$MIAOU_CONFIGDIR/nftables.rules.d/nat.table" &>/dev/null
  81. sudo cp "$MIAOU_CONFIGDIR/nftables.rules.d/nat.table" /etc/nftables.rules.d/nat.table
  82. nftables_reloading=true
  83. else
  84. if [[ -f /etc/nftables.rules.d/nat.table ]]; then
  85. sudo_required "remove previous nat.table"
  86. sudo rm -f /etc/nftables.rules.d/nat.table
  87. nftables_reloading=true
  88. fi
  89. fi
  90. if [[ "$nftables_reloading" == true ]]; then
  91. sudo_required "reload nftables"
  92. sudo systemctl reload nftables.service
  93. fi
  94. }
  95. # MAIN
  96. . "$MIAOU_BASEDIR/lib/init.sh"
  97. arg1_required "$@"
  98. readonly CONTAINER="$1"
  99. check || (
  100. install
  101. check
  102. )