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.

95 lines
2.7 KiB

10 months ago
10 months ago
8 months ago
10 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. }
  12. function check_reverseproxy() {
  13. lxc exec "$CONTAINER" -- bash <<EOF
  14. set -Eeuo pipefail
  15. dpkg -l nginx | grep -q ^ii
  16. systemctl is-active --quiet nginx
  17. nginx -tq
  18. EOF
  19. }
  20. function check_certbot() {
  21. lxc exec "$CONTAINER" -- bash <<EOF
  22. set -Eeuo pipefail
  23. dpkg -l certbot | grep -q ^ii
  24. dpkg -l python3-certbot-nginx | grep -q ^ii
  25. EOF
  26. }
  27. function check_banner() {
  28. if [[ $TARGET != "prod" ]]; then
  29. lxc exec "$CONTAINER" -- bash <<EOF
  30. set -Eeuo pipefail
  31. test -f /etc/nginx/snippets/banner_$TARGET.conf
  32. EOF
  33. fi
  34. }
  35. function install() {
  36. PREFIX="recipe:dmz:install"
  37. : $PREFIX
  38. echowarn "about to deploy new container <$CONTAINER> ..."
  39. if ! container_exists "$CONTAINER"; then
  40. echowarn "about to create new container <$CONTAINER> ..."
  41. lxc-miaou-create "$CONTAINER"
  42. echo OK
  43. fi
  44. if ! container_running "$CONTAINER"; then
  45. echowarn "about to start asleep container <$CONTAINER> ..."
  46. lxc start "$CONTAINER"
  47. echo OK
  48. fi
  49. credential_email=$(load_yaml_from_expanded credential.email)
  50. lxc exec "$CONTAINER" -- bash <<EOF
  51. set -Eeuo pipefail
  52. apt-get update && apt-get dist-upgrade -y
  53. apt-get install -y nginx ssl-cert libnginx-mod-http-subs-filter certbot python3-certbot-nginx
  54. echo "registering with your default credential email <$credential_email>"
  55. certbot register --agree-tos --email $credential_email --no-eff-email || echo "already resgistered!"
  56. rm /etc/nginx/sites-{enabled,available}/default -f
  57. systemctl enable nginx
  58. nginx -tq || rm /etc/nginx/sites-enabled/hosts
  59. systemctl start nginx
  60. EOF
  61. if [[ "$TARGET" != "prod" ]]; then
  62. echo "copying Nginx banner to container <$CONTAINER> ... "
  63. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_$TARGET.conf" "$CONTAINER/etc/nginx/snippets/banner_$TARGET.conf"
  64. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_exp.conf" "$CONTAINER/etc/nginx/snippets/banner_exp.conf"
  65. echo "copying files to container <$CONTAINER> ... OK"
  66. else
  67. echo "no Nginx banner on PROD!"
  68. fi
  69. }
  70. # MAIN
  71. . "$MIAOU_BASEDIR/lib/init.sh"
  72. arg1_required "$@"
  73. readonly CONTAINER="$1"
  74. check || (
  75. install
  76. check
  77. )