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.

166 lines
4.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. PGHOST="$docker_ip"
  29. PGUSER="postgres"
  30. export PGHOST PGUSER
  31. }
  32. ## Must setup a direct connection
  33. _set_up_connection() {
  34. if [ -e "$DB_PASSFILE" ]; then
  35. POSTGRES_ROOT_PASSWORD=$(cat "$DB_PASSFILE" | cut -f 5 -d :)
  36. else
  37. POSTGRES_ROOT_PASSWORD="$(gen_password)"
  38. fi
  39. ##
  40. ## Setting up access from host
  41. ##
  42. debug docker exec -i "$container_id" psql -U postgres -qAt
  43. docker exec -i "$container_id" psql -U postgres -qAt \
  44. < <(echo "ALTER USER postgres WITH ENCRYPTED password '$POSTGRES_ROOT_PASSWORD'") || {
  45. die "direct PSQL injection failed."
  46. }
  47. sed -ri 's%^host all all 0\.0\.0\.0/0 trust$%host all all 0.0.0.0/0 md5%g' \
  48. "$SERVICE_DATASTORE/var/lib/postgresql/data/pg_hba.conf" || return 1
  49. docker restart "$container_id" || return 1
  50. ## XXXvlab: this won't help support multiple project running on the
  51. ## same host
  52. cat <<EOF > "$DB_PASSFILE"
  53. *:*:*:postgres:$POSTGRES_ROOT_PASSWORD
  54. EOF
  55. chmod 600 "$DB_PASSFILE" || return 1
  56. }
  57. ddb () { dcmd psql -qAt "$@"; }
  58. ##
  59. ## Entrypoints
  60. ##
  61. db_drop () {
  62. local dbname="$1"
  63. dcmd dropdb --if-exists "$1"
  64. }
  65. db_create () {
  66. local dbname="$1"
  67. dcmd createdb "$dbname" || return 1
  68. info "Database '$dbname' created."
  69. if [ "$POSTGIS" ]; then
  70. ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
  71. dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
  72. info "Installed postgis extensions on database '$dbname'."
  73. fi
  74. if [ "$UNACCENT" ]; then
  75. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS unaccent;") || return 1
  76. info "Installed unaccent extension on database '$dbname'."
  77. fi
  78. }
  79. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  80. db_has_database() {
  81. local dbname="$1"
  82. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  83. debug "Database $dbname exists."
  84. return 0
  85. else
  86. debug "Database $dbname exists."
  87. return 1
  88. fi
  89. }
  90. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  91. db_has_user() {
  92. local user="$1" users
  93. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  94. err "Failed to get user list"
  95. return 1
  96. }
  97. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  98. }
  99. db_create_user() {
  100. local user="$1" password="$2"
  101. ## Using this instead of pipes is important so that trap works
  102. ddb < <(echo "CREATE USER $user WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  103. }
  104. db_change_password() {
  105. local user="$1" password="$2"
  106. ## Using this instead of pipes is important so that trap works
  107. ddb < <(echo "ALTER USER $user WITH PASSWORD '$password';")
  108. }
  109. db_grant_rights () {
  110. local dbname="$1" user="$2"
  111. PGM chown "$user" "$dbname"
  112. }
  113. PGM() {
  114. local src="$1" dst="$2"
  115. require psql || apt-get install -y postgresql-client </dev/null
  116. require pgm || {
  117. (
  118. cd /opt/apps
  119. git clone https://github.com/0k/pgm.git
  120. ln -sf /opt/apps/pgm/bin/* /usr/local/bin/
  121. apt-get install -y --force-yes pv buffer < /dev/null
  122. # cd /opt/apps/0k-docker
  123. # git checkout master
  124. )
  125. }
  126. ensure_db_docker_running </dev/null || return 1
  127. debug pgm "$@"
  128. pgm "$@"
  129. }