[3733] | 1 | #!/bin/sh |
---|
| 2 | # |
---|
| 3 | # Build the DAViCal database |
---|
| 4 | # |
---|
| 5 | |
---|
| 6 | DBNAME="${1:-davical}" |
---|
| 7 | ADMINPW="${2}" |
---|
| 8 | |
---|
| 9 | DBADIR="`dirname \"$0\"`" |
---|
| 10 | |
---|
| 11 | INSTALL_NOTE_FN="`mktemp -t tmp.XXXXXXXXXX`" |
---|
| 12 | |
---|
| 13 | testawldir() { |
---|
| 14 | [ -f "${1}/dba/awl-tables.sql" ] |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | # |
---|
| 18 | # Attempt to locate the AWL directory |
---|
| 19 | AWLDIR="${DBADIR}/../../awl" |
---|
| 20 | if ! 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 |
---|
| 29 | fi |
---|
| 30 | |
---|
| 31 | export AWL_DBAUSER=davical_dba |
---|
| 32 | export AWL_APPUSER=davical_app |
---|
| 33 | |
---|
| 34 | # Get the major version for PostgreSQL |
---|
| 35 | export DBVERSION="`psql -qXAt -c "SELECT version();" template1 | cut -f2 -d' ' | cut -f1-2 -d'.'`" |
---|
| 36 | |
---|
| 37 | install_note() { |
---|
| 38 | cat >>"${INSTALL_NOTE_FN}" |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | db_users() { |
---|
| 42 | psql -qXAt -c "SELECT usename FROM pg_user;" template1 |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | create_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 | |
---|
| 52 | EONOTE |
---|
| 53 | fi |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | create_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 | |
---|
| 62 | try_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 |
---|
| 67 | export PGOPTIONS='--client-min-messages=warning' |
---|
| 68 | |
---|
| 69 | create_db_user "${AWL_DBAUSER}" |
---|
| 70 | create_db_user "${AWL_APPUSER}" |
---|
| 71 | |
---|
| 72 | # FIXME: Need to check that the database was actually created. |
---|
| 73 | if ! createdb --encoding UTF8 --template template0 --owner "${AWL_DBAUSER}" "${DBNAME}" ; then |
---|
| 74 | echo "Unable to create database" |
---|
| 75 | exit 1 |
---|
| 76 | fi |
---|
| 77 | |
---|
| 78 | # |
---|
| 79 | # Try a few alternatives for a database user or give up... |
---|
| 80 | if try_db_user "${AWL_DBAUSER}" ; then |
---|
| 81 | export DBA="-U ${AWL_DBAUSER}" |
---|
| 82 | else |
---|
| 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 * * * * |
---|
| 94 | I cannot find a usable database user to construct the DAViCal database with, but |
---|
| 95 | may have successfully created the davical_app and davical_dba users (I tried :-). |
---|
| 96 | |
---|
| 97 | You should edit your pg_hba.conf file to give permissions to the davical_app and |
---|
| 98 | davical_dba users to access the database and run this script again. If you still |
---|
| 99 | continue to see this message then you will need to make sure you run the script |
---|
| 100 | as a user with full permissions to access the local PostgreSQL database. |
---|
| 101 | |
---|
| 102 | If your PostgreSQL database is non-standard then you will need to set the PGHOST, |
---|
| 103 | PGPORT and/or PGCLUSTER environment variables before running this script again. |
---|
| 104 | |
---|
| 105 | See: http://wiki.davical.org/w/Install_Errors/No_Database_Rights |
---|
| 106 | |
---|
| 107 | EOFAILURE |
---|
| 108 | exit 1 |
---|
| 109 | fi |
---|
| 110 | fi |
---|
| 111 | fi |
---|
| 112 | fi |
---|
| 113 | |
---|
| 114 | create_plpgsql_language |
---|
| 115 | |
---|
| 116 | # |
---|
| 117 | # Load the AWL base tables and schema management tables |
---|
| 118 | psql -qXAt ${DBA} -f "${AWLDIR}/dba/awl-tables.sql" "${DBNAME}" 2>&1 |
---|
| 119 | psql -qXAt ${DBA} -f "${AWLDIR}/dba/schema-management.sql" "${DBNAME}" 2>&1 |
---|
| 120 | |
---|
| 121 | # |
---|
| 122 | # Load the DAViCal tables |
---|
| 123 | psql -qXAt ${DBA} -f "${DBADIR}/davical.sql" "${DBNAME}" 2>&1 |
---|
| 124 | |
---|
| 125 | # |
---|
| 126 | # Set permissions for the application DB user on the database |
---|
| 127 | if ! ${DBADIR}/update-davical-database --dbname "${DBNAME}" --appuser "${AWL_APPUSER}" --nopatch --owner "${AWL_DBAUSER}" ; then |
---|
| 128 | cat <<EOFAILURE |
---|
| 129 | * * * * ERROR * * * * |
---|
| 130 | The database administration utility failed. This may be due to database |
---|
| 131 | permissions for the davical_dba user, or because the Perl DBD::Pg or YAML |
---|
| 132 | libraries are not available. |
---|
| 133 | |
---|
| 134 | Check that your pg_hba.conf allows the davical_dba user to connect to the |
---|
| 135 | database (and make sure you've reloaded PostgreSQL since changing that). |
---|
| 136 | |
---|
| 137 | Also see: http://wiki.davical.org/w/Install_Errors/No_Perl_YAML |
---|
| 138 | |
---|
| 139 | EOFAILURE |
---|
| 140 | exit 1 |
---|
| 141 | fi |
---|
| 142 | # |
---|
| 143 | # Load the required base data |
---|
| 144 | psql -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 |
---|
| 148 | if [ "${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 \"\\\\\'\" '^='`" |
---|
| 153 | fi |
---|
| 154 | |
---|
| 155 | if [ "$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`" |
---|
| 160 | fi |
---|
| 161 | |
---|
| 162 | if [ "$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" |
---|
| 166 | fi |
---|
| 167 | |
---|
| 168 | psql -qX -c "UPDATE usr SET password = '**${ADMINPW}' WHERE user_no = 1;" "${DBNAME}" |
---|
| 169 | |
---|
| 170 | echo "NOTE" |
---|
| 171 | echo "====" |
---|
| 172 | cat "${INSTALL_NOTE_FN}" |
---|
| 173 | rm "${INSTALL_NOTE_FN}" |
---|
| 174 | |
---|
| 175 | cat <<FRIENDLY |
---|
| 176 | * The password for the 'admin' user has been set to '${ADMINPW}'" |
---|
| 177 | |
---|
| 178 | Thanks for trying DAViCal! Check in /usr/share/doc/davical/examples/ for |
---|
| 179 | some configuration examples. For help, visit #davical on irc.oftc.net. |
---|
| 180 | |
---|
| 181 | FRIENDLY |
---|