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.

66 lines
2.3 KiB

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