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.

68 lines
2.5 KiB

10 months ago
  1. #!/bin/bash
  2. function check() {
  3. PREFIX="recipe:postgresql:check"
  4. container_running "$CONTAINER" || return 10
  5. echo "checking postgresql regarding access to the bridge subnet <$BRIDGE_SUBNET>..."
  6. lxc exec "$CONTAINER" -- bash <<EOF
  7. set -Eeuo pipefail
  8. systemctl is-active postgresql.service &>/dev/null
  9. ss -tlnp | grep postgres | grep -q 0.0.0.0:5432
  10. PG_VERSION=\$(pg_lsclusters -h | cut -d' ' -f1)
  11. grep -Eq "^host.*all.*all.*$BRIDGE_SUBNET.*md5" /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
  12. test -f /etc/default/autopostgresqlbackup
  13. EOF
  14. status="$?"
  15. [[ $status -eq 0 ]] && echo "container <$CONTAINER> approved!"
  16. return $status
  17. }
  18. function install() {
  19. PREFIX="recipe:postgresql:install"
  20. : "$PREFIX"
  21. echowarn "initializing postgresql regarding access to the bridge subnet <$BRIDGE_SUBNET>..."
  22. launch_container "$CONTAINER"
  23. lxc exec "$CONTAINER" -- bash <<EOF
  24. set -Eeuo pipefail
  25. apt update
  26. . /opt/debian-bash/lib/functions.sh
  27. /opt/debian-bash/tools/idem_apt_install postgresql
  28. echo -n "start postgresql now..."
  29. PG_VERSION=\$(pg_lsclusters -h | cut -d' ' -f1)
  30. pg_ctlcluster \$PG_VERSION main start
  31. echo "OK"
  32. function systemctl-exists() ([ \$(systemctl list-unit-files "\${1}*" | wc -l) -gt 3 ])
  33. systemctl-exists exim4.service && systemctl disable exim4.service
  34. /opt/debian-bash/tools/append_or_replace "^listen_addresses = .*$" "listen_addresses = '0.0.0.0'" /etc/postgresql/\$PG_VERSION/main/postgresql.conf
  35. /opt/debian-bash/tools/append_or_replace "^host.*all.*all.*$BRIDGE_SUBNET.*md5" "host\tall\t\tall\t\t$BRIDGE_SUBNET\t\tmd5" /etc/postgresql/\$PG_VERSION/main/pg_hba.conf
  36. systemctl restart postgresql.service
  37. EOF
  38. echo -n "copying <autopostgresqlbackup> files over container <$CONTAINER> ... "
  39. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/autopostgresqlbackup/script" "$CONTAINER/usr/sbin/autopostgresqlbackup"
  40. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/autopostgresqlbackup/cron.daily" "$CONTAINER/etc/cron.daily/autopostgresqlbackup"
  41. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/autopostgresqlbackup/default.conf" "$CONTAINER/etc/default/autopostgresqlbackup"
  42. PREFIX="" echo OK
  43. }
  44. # MAIN
  45. . "$MIAOU_BASEDIR/lib/init.sh"
  46. arg1_required "$@"
  47. CONTAINER="$1"
  48. BRIDGE_SUBNET=$(lxc network get lxdbr0 ipv4.address)
  49. readonly CONTAINER BRIDGE_SUBNET
  50. check || (
  51. install
  52. check
  53. )