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.

98 lines
2.8 KiB

9 months ago
9 months ago
7 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 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. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_exp.conf" "$CONTAINER/etc/nginx/snippets/banner_exp.conf"
  67. echo "copying files to container <$CONTAINER> ... OK"
  68. else
  69. echo "no Nginx banner on PROD!"
  70. fi
  71. }
  72. # MAIN
  73. . "$MIAOU_BASEDIR/lib/init.sh"
  74. arg1_required "$@"
  75. readonly CONTAINER="$1"
  76. check || (
  77. install
  78. check
  79. )