source: contrib/davical/dba/create-database.sh @ 3733

Revision 3733, 5.2 KB checked in by gabriel.malheiros, 13 years ago (diff)

Ticket #1541 - <Davical customizado para o Expresso.Utiliza Caldav e CardDav?>

Line 
1#!/bin/sh
2#
3# Build the DAViCal database
4#
5
6DBNAME="${1:-davical}"
7ADMINPW="${2}"
8
9DBADIR="`dirname \"$0\"`"
10
11INSTALL_NOTE_FN="`mktemp -t tmp.XXXXXXXXXX`"
12
13testawldir() {
14  [ -f "${1}/dba/awl-tables.sql" ]
15}
16
17#
18# Attempt to locate the AWL directory
19AWLDIR="${DBADIR}/../../awl"
20if ! testawldir "${AWLDIR}"; then
21  AWLDIR="/usr/share/awl"
22  if ! testawldir "${AWLDIR}"; then
23    AWLDIR="/usr/local/share/awl"
24    if ! testawldir "${AWLDIR}"; then
25      echo "Unable to find AWL libraries"
26      exit 1
27    fi
28  fi
29fi
30
31export AWL_DBAUSER=davical_dba
32export AWL_APPUSER=davical_app
33
34# Get the major version for PostgreSQL
35export DBVERSION="`psql -qXAt -c "SELECT version();" template1 | cut -f2 -d' ' | cut -f1-2 -d'.'`"
36
37install_note() {
38  cat >>"${INSTALL_NOTE_FN}"
39}
40
41db_users() {
42  psql -qXAt -c "SELECT usename FROM pg_user;" template1
43}
44
45create_db_user() {
46  if ! db_users | grep "^${1}$" >/dev/null ; then
47    psql -qXAt -c "CREATE USER ${1} NOCREATEDB NOCREATEROLE;" template1
48    cat <<EONOTE | install_note
49*  You will need to edit the PostgreSQL pg_hba.conf to allow the
50   '${1}' database user access to the 'davical' database.
51
52EONOTE
53  fi
54}
55
56create_plpgsql_language() {
57  if ! psql ${DBA} -qXAt -c "SELECT lanname FROM pg_language;" "${DBNAME}" | grep "^plpgsql$" >/dev/null; then
58    createlang plpgsql "${DBNAME}"
59  fi
60}
61
62try_db_user() {
63  [ "XtestX`psql -U "${1}" -qXAt -c \"SELECT usename FROM pg_user;\" \"${DBNAME}\" 2>/dev/null`" != "XtestX" ]
64}
65
66# Hide all the annoying NOTICE... messages
67export PGOPTIONS='--client-min-messages=warning'
68
69create_db_user "${AWL_DBAUSER}"
70create_db_user "${AWL_APPUSER}"
71
72# FIXME: Need to check that the database was actually created.
73if ! createdb --encoding UTF8 --template template0 --owner "${AWL_DBAUSER}" "${DBNAME}" ; then
74  echo "Unable to create database"
75  exit 1
76fi
77
78#
79# Try a few alternatives for a database user or give up...
80if try_db_user "${AWL_DBAUSER}" ; then
81  export DBA="-U ${AWL_DBAUSER}"
82else
83  if try_db_user "postgres" ; then
84    export DBA="-U postgres"
85  else
86    if try_db_user "${USER}" ; then
87      export DBA=""
88    else
89      if try_db_user "${PGUSER}" ; then
90        export DBA=""
91      else
92        cat <<EOFAILURE
93* * * * ERROR * * * *
94I cannot find a usable database user to construct the DAViCal database with, but
95may have successfully created the davical_app and davical_dba users (I tried :-).
96
97You should edit your pg_hba.conf file to give permissions to the davical_app and
98davical_dba users to access the database and run this script again.  If you still
99continue to see this message then you will need to make sure you run the script
100as a user with full permissions to access the local PostgreSQL database.
101
102If your PostgreSQL database is non-standard then you will need to set the PGHOST,
103PGPORT and/or PGCLUSTER environment variables before running this script again.
104
105See:  http://wiki.davical.org/w/Install_Errors/No_Database_Rights
106
107EOFAILURE
108        exit 1
109      fi
110    fi
111  fi
112fi
113
114create_plpgsql_language
115
116#
117# Load the AWL base tables and schema management tables
118psql -qXAt ${DBA} -f "${AWLDIR}/dba/awl-tables.sql" "${DBNAME}" 2>&1
119psql -qXAt ${DBA} -f "${AWLDIR}/dba/schema-management.sql" "${DBNAME}" 2>&1
120
121#
122# Load the DAViCal tables
123psql -qXAt ${DBA} -f "${DBADIR}/davical.sql" "${DBNAME}" 2>&1
124
125#
126# Set permissions for the application DB user on the database
127if ! ${DBADIR}/update-davical-database --dbname "${DBNAME}" --appuser "${AWL_APPUSER}" --nopatch --owner "${AWL_DBAUSER}" ; then
128        cat <<EOFAILURE
129* * * * ERROR * * * *
130The database administration utility failed.  This may be due to database
131permissions for the davical_dba user, or because the Perl DBD::Pg or YAML
132libraries are not available.
133
134Check that your pg_hba.conf allows the davical_dba user to connect to the
135database (and make sure you've reloaded PostgreSQL since changing that).
136
137Also see:  http://wiki.davical.org/w/Install_Errors/No_Perl_YAML
138
139EOFAILURE
140  exit 1
141fi
142#
143# Load the required base data
144psql -qXAt ${DBA} -f "${DBADIR}/base-data.sql" "${DBNAME}" | egrep -v '^10'
145
146#
147# We can override the admin password generation for regression testing predictability
148if [ "${ADMINPW}" = "" ] ; then
149  #
150  # Generate a random administrative password.  If pwgen is available we'll use that,
151  # otherwise try and hack something up using a few standard utilities
152  ADMINPW="`pwgen -Bcny 2>/dev/null | tr \"\\\\\'\" '^='`"
153fi
154
155if [ "$ADMINPW" = "" ] ; then
156  # OK.  They didn't supply one, and pwgen didn't work, so we hack something
157  # together from /dev/random ...
158  export LC_ALL=C
159  ADMINPW="`dd if=/dev/urandom bs=512 count=1 2>/dev/null | tr -c -d 'a-km-zA-HJ-NP-Y0-9' | cut -c2-9`"
160fi
161
162if [ "$ADMINPW" = "" ] ; then
163  # Right.  We're getting desperate now.  We'll have to use a default password
164  # and hope that they change it to something more sensible.
165  ADMINPW="please change this password"
166fi
167
168psql -qX -c "UPDATE usr SET password = '**${ADMINPW}' WHERE user_no = 1;" "${DBNAME}"
169
170echo "NOTE"
171echo "===="
172cat "${INSTALL_NOTE_FN}"
173rm "${INSTALL_NOTE_FN}"
174
175cat <<FRIENDLY
176*  The password for the 'admin' user has been set to '${ADMINPW}'"
177
178Thanks for trying DAViCal!  Check in /usr/share/doc/davical/examples/ for
179some configuration examples.  For help, visit #davical on irc.oftc.net.
180
181FRIENDLY
Note: See TracBrowser for help on using the repository browser.