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.

120 lines
3.5 KiB

  1. # -*- mode: shell-script -*-
  2. include pretty
  3. export DB_NAME="postgres"
  4. export DB_DATADIR=/var/lib/postgresql/data
  5. export DB_PASSFILE=/root/.pgpass
  6. is_db_locked() {
  7. require lsof || apt-get install -y lsof </dev/null || {
  8. err "Couldn't install command 'lsof'."
  9. return 1
  10. }
  11. # "$lsof" +D "$host_db_working_dir" || true
  12. ## We need:
  13. ## 1- to protect against set -e
  14. ## 2- to detect if errorlevel 1 because lsof failed for good reason: permissions error, directory not found
  15. ## 3- to detect if errorlevel 1 because no process found
  16. [ -d "$host_db_working_dir" ] || {
  17. err "Directory '$host_db_working_dir' not found. Might be normal at this stage."
  18. return 1
  19. }
  20. ## We choose to ignore (2), and use stdout emptyness to ensure (3)
  21. open_files=$(lsof +D "$host_db_working_dir") || true
  22. [ "$open_files" ]
  23. }
  24. _set_db_params() {
  25. local docker_ip="$1" docker_network="$2"
  26. export db_docker_opts="--network $docker_network -e PGHOST=$docker_ip -e PGUSER=postgres"
  27. export db_cmd_opts=
  28. }
  29. ddb () { dcmd psql -qAt "$@"; }
  30. ##
  31. ## Entrypoints
  32. ##
  33. db_drop () {
  34. local dbname="$1"
  35. dcmd dropdb --if-exists "$1"
  36. }
  37. db_create () {
  38. local dbname="$1"
  39. dcmd createdb "$dbname" || return 1
  40. info "Database '$dbname' created."
  41. if [ "$POSTGIS" ]; then
  42. ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
  43. dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
  44. info "Installed postgis extensions on database '$dbname'."
  45. fi
  46. if [ "$UNACCENT" ]; then
  47. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS unaccent;") || return 1
  48. info "Installed unaccent extension on database '$dbname'."
  49. fi
  50. }
  51. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  52. db_has_database() {
  53. local dbname="$1"
  54. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  55. debug "Database $dbname exists."
  56. return 0
  57. else
  58. debug "Database $dbname exists."
  59. return 1
  60. fi
  61. }
  62. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  63. db_has_user() {
  64. local user="$1" users
  65. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  66. err "Failed to get user list"
  67. return 1
  68. }
  69. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  70. }
  71. db_create_user() {
  72. local user="$1" password="$2"
  73. ## Using this instead of pipes is important so that trap works
  74. ddb < <(echo "CREATE USER $user WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  75. }
  76. db_change_password() {
  77. local user="$1" password="$2"
  78. ## Using this instead of pipes is important so that trap works
  79. ddb < <(echo "ALTER USER $user WITH PASSWORD '$password';")
  80. }
  81. db_grant_rights () {
  82. local dbname="$1" user="$2"
  83. require psql || apt-get install -y postgresql-client </dev/null
  84. require pgm || {
  85. (
  86. cd /opt/apps
  87. git clone https://github.com/0k/pgm.git
  88. ln -sf /opt/apps/pgm/bin/* /usr/local/bin/
  89. apt-get install -y --force-yes pv buffer < /dev/null
  90. # cd /opt/apps/0k-docker
  91. # git checkout master
  92. )
  93. }
  94. debug PGHOST="$DOCKER_IP" PGUSER=postgres pgm chown "$user" "$dbname"
  95. PGHOST="$DOCKER_IP" PGUSER=postgres prefix_pg_local_command=" " pgm chown "$user" "$dbname"
  96. }