* tentative merge of onelab myplc
[myplc.git] / plc.d / db
1 #!/bin/bash
2 #
3 # priority: 800
4 #
5 # Bootstrap the database
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: db 316 2007-04-24 17:25:09Z thierry $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Export so that we do not have to specify -p to psql invocations
21 export PGPORT=$PLC_DB_PORT
22
23 # Updates the database by applying all migration scripts in
24 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
25 # current subversion. At least one of the migration scripts with the
26 # same N must update plc_db_version.subversion.
27 function migrate_db()
28 {
29     subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
30                  "SELECT subversion FROM plc_db_version LIMIT 1" \
31                  $PLC_DB_NAME 2>/dev/null || echo 0)
32     shopt -s nullglob
33     for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
34         script=$(basename $file)
35         index=${script%-up*}
36         extension=${script##*.}
37         if [ $index -gt $subversion ] ; then
38             if [ "$extension" = "sql" ] ; then
39                 dialog " - $script (dbdumped)"
40                 dump_planetlab_db "before-$script"
41                 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
42             elif [ -x $file ] ; then
43                 dialog " - $script (dbdumped)"
44                 dump_planetlab_db "before-$script"
45                 $file
46             else
47                 dialog "\nWarning: migration $file not executable"
48             fi
49             check
50         fi
51     done
52 }
53
54 # Dumps the database - optional argument to specify filename suffix
55 function dump_planetlab_db()
56 {
57     if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
58     dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M-%S${suffix}.sql")
59     pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump
60     check
61 }
62
63 function dump_drupal_db()
64 {
65     dump=/var/lib/pgsql/backups/$(date +"drupal.%Y-%m-%d-%H-%M-%S.sql")
66     pg_dump -U $PLC_DB_USER drupal > $dump
67     check
68 }
69
70 # Clean up old backups
71 function clean_dumps()
72 {
73     find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
74     check
75 }
76
77 if [ "$PLC_DB_ENABLED" != "1" ] ; then
78     exit 0
79 fi
80
81 case "$1" in
82     start)
83         MESSAGE=$"Bootstrapping the database"
84         dialog "$MESSAGE"
85
86         # Apply schema updates
87         migrate_db
88
89         # Update the maintenance account username. This can't be
90         # done through the api-config script since it uses the
91         # maintenance account to access the API. The maintenance
92         # account should be person_id 1 since it is created by the
93         # DB schema itself.
94         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
95
96         # Update the Drupal site_name variable
97         psql -U $PLC_DB_USER drupal <<EOF
98 DELETE FROM variable WHERE name = 'site_name';
99 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
100 EOF
101
102         # Bootstrap the DB
103         db-config
104         check
105
106         result "$MESSAGE"
107         ;;
108
109     migrate)
110         MESSAGE=$"Migrating the database"
111         dialog "$MESSAGE"
112
113         migrate_db
114         result "$MESSAGE"
115         ;;
116
117     dump)
118         MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
119         dialog "$MESSAGE"
120
121         dump_planetlab_db
122         dump_drupal_db
123         result "$MESSAGE"
124         ;;
125
126     clean-dump)
127         MESSAGE=$"Cleaning old database dumps"
128         dialog "$MESSAGE"
129
130         clean_dumps
131         result "$MESSAGE"
132         ;;
133
134     *)
135         echo "Usage: $0 [start|migrate|dump|clean-dump]"
136         exit 1
137         ;;
138 esac
139
140 exit $ERRORS