fork 0k-charms
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.

150 lines
4.1 KiB

  1. # -*- mode: shell-script -*-
  2. include pretty
  3. export DB_NAME="$SERVICE_NAME" ## general type of database (ie: postgres/mysql...)
  4. export DB_DATADIR=/var/lib/postgresql/data
  5. export DATA_DIR=$SERVICE_DATASTORE$DB_DATADIR
  6. export LOCAL_DB_PASSFILE="$DATA_DIR/pgpass"
  7. export CLIENT_DB_PASSFILE="/root/.pgpass"
  8. export PG_HBA="$DATA_DIR/pg_hba.conf"
  9. is_db_locked() {
  10. local host_db_volume
  11. if [ "${HOST_DATASTORE+x}" ]; then
  12. host_db_volume="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  13. else
  14. host_db_volume="$DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  15. fi
  16. is_volume_used "$host_db_volume"
  17. }
  18. _set_server_db_params() {
  19. server_docker_opts+=()
  20. }
  21. _set_db_params() {
  22. local docker_ip="$1" docker_network="$2"
  23. if [ "${HOST_DATASTORE+x}" ]; then
  24. export HOST_DB_PASSFILE="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR/pgpass"
  25. else
  26. export HOST_DB_PASSFILE="$CLIENT_DB_PASSFILE"
  27. fi
  28. [ -f "$CLIENT_DB_PASSFILE" ] || touch "$CLIENT_DB_PASSFILE"
  29. db_docker_opts+=("--network" "$docker_network"
  30. "-e" PGHOST="$docker_ip"
  31. "-e" PGUSER=postgres
  32. "-e" prefix_pg_local_command=' ')
  33. db_cmd_opts+=()
  34. check_command="SELECT 1;"
  35. }
  36. ddb () { dcmd psql -qAt "$@"; }
  37. ##
  38. ## Entrypoints
  39. ##
  40. db_drop () {
  41. local dbname="$1"
  42. dcmd dropdb --if-exists "$1"
  43. }
  44. db_create () {
  45. local dbname="$1"
  46. shift
  47. dcmd createdb "$dbname" "$@" || return 1
  48. info "Database '$dbname' created."
  49. }
  50. db_install_extensions() {
  51. local dbname="$1"
  52. shift
  53. while [ "$#" != 0 ]; do
  54. case "$1" in
  55. postgis)
  56. if out=$(ddb -d "$dbname" < <(echo "SELECT extname
  57. FROM pg_extension
  58. WHERE extname = 'postgis';")); then
  59. if [ "$out" ]; then
  60. info "PostGIS already installed."
  61. shift
  62. continue
  63. fi
  64. fi
  65. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS postgis;
  66. CREATE EXTENSION IF NOT EXISTS postgis_topology;") || return 1
  67. dcmd /bin/bash -c "psql -d '$dbname' -v ON_ERROR_STOP=1 -f /usr/local/share/postgresql/contrib/postgis-*/legacy.sql" || return 1
  68. info "Installed postgis extensions on database '$dbname'."
  69. ;;
  70. *)
  71. if ! [[ "$1" =~ ^[0-9a-zA-Z_.-]+$ ]]; then
  72. err "Invalid extension name: $1"
  73. return 1
  74. fi
  75. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS \"$1\";") || return 1
  76. info "Installed $1 extension on database '$dbname'."
  77. ;;
  78. esac
  79. shift
  80. done
  81. }
  82. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  83. db_has_database() {
  84. local dbname="$1"
  85. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  86. debug "Database $dbname exists."
  87. return 0
  88. else
  89. debug "Database $dbname exists."
  90. return 1
  91. fi
  92. }
  93. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  94. db_has_user() {
  95. local user="$1" users
  96. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  97. err "Failed to get user list"
  98. return 1
  99. }
  100. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  101. }
  102. db_create_user() {
  103. local user="$1" password="$2"
  104. ## Using this instead of pipes is important so that trap works
  105. ddb < <(echo "CREATE USER \"$user\" WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  106. }
  107. db_change_password() {
  108. local user="$1" password="$2"
  109. ## Using this instead of pipes is important so that trap works
  110. ddb < <(echo "ALTER USER \"$user\" WITH PASSWORD '$password';")
  111. }
  112. PGM() {
  113. ensure_db_docker_running </dev/null || return 1
  114. echo "${db_docker_opts[@]}"
  115. dcmd pgm "$@"
  116. }
  117. db_grant_rights () {
  118. local dbname="$1" user="$2"
  119. PGM chown -v "$user" "$dbname"
  120. }