split plc.d/ and db-config.d between myplc and plcapi modules as a first step
[plcapi.git] / plc.d / db
1 #!/bin/bash
2 # $Id$
3 # $URL$
4 #
5 # priority: 900
6 #
7 # Bootstrap the database
8 #
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2006 The Trustees of Princeton University
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 function checkpoint_planetlab_db()
55 {
56     dumpfile=$1
57     pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile
58     check
59 }
60
61 function restore_planetlab_db()
62 {
63     dumpfile=$1
64     if [ -n "$dumpfile" ] ; then 
65         [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile
66         check
67     fi
68 }
69
70 # use a single date of this script invocation for the dump_*_db functions.
71 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
72
73 # Dumps the database - optional argument to specify filename suffix
74 function dump_planetlab_db()
75 {
76     if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
77     dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
78     checkpoint_planetlab_db $dumpfile
79 }
80
81 function restore_drupal_db()
82 {
83     dumpfile=$1
84     if [ -n "$dumpfile" ] ; then 
85         [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
86         check
87     fi
88 }
89
90 function checkpoint_drupal_db()
91 {
92     dumpfile=$1
93     pg_dump -U $PLC_DB_USER drupal > $dumpfile
94     check
95 }
96
97 function dump_drupal_db()
98 {
99     dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
100     checkpoint_drupal_db $dumpfile
101     check
102 }
103
104 # Clean up old backups
105 function clean_dumps()
106 {
107     find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
108     check
109 }
110
111 [ $PLC_DB_ENABLED -ne 1 ] && exit 0
112 case "$1" in
113     start)
114         MESSAGE=$"Bootstrapping the database"
115         dialog "$MESSAGE"
116
117         # Apply schema updates
118         migrate_db
119
120         # Update the maintenance account username. This can't be
121         # done through the api-config script since it uses the
122         # maintenance account to access the API. The maintenance
123         # account should be person_id 1 since it is created by the
124         # DB schema itself.
125         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
126
127         # Update the Drupal site_name variable
128         # also turn off drupal native user registration
129         psql -U $PLC_DB_USER drupal <<EOF
130 DELETE FROM variable WHERE name = 'site_name';
131 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
132 DELETE FROM variable WHERE name = 'user_register';
133 INSERT INTO variable (name, value) VALUES ('user_register', 's:1:"0";');
134 DELETE FROM cache;
135 EOF
136
137         # Bootstrap the DB
138         db-config
139         check
140
141         result "$MESSAGE"
142         ;;
143
144     migrate)
145         MESSAGE=$"Migrating the database"
146         dialog "$MESSAGE"
147
148         migrate_db
149         result "$MESSAGE"
150         ;;
151
152     dump)
153         MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
154         dialog "$MESSAGE"
155
156         dump_planetlab_db
157         dump_drupal_db
158         result "$MESSAGE"
159         ;;
160
161     checkpoint)
162         MESSAGE=$"Checkpointing the databases"
163         checkpoint_planetlab_db $2
164         checkpoint_drupal_db $3
165         ;;
166
167     restore)
168         MESSAGE=$"Restoring the databases from checkpoint files"
169         restore_planetlab_db $2
170         restore_drupal_db $3
171         ;;
172
173     clean-dump)
174         MESSAGE=$"Cleaning old database dumps"
175         dialog "$MESSAGE"
176
177         clean_dumps
178         result "$MESSAGE"
179         ;;
180
181     stop)
182         MESSAGE="Ignoring request to stop myplc databases"
183         dialog "$MESSAGE"
184         result ""
185         ;;
186
187     *)
188         echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]"
189         exit 1
190         ;;
191 esac
192
193 exit $ERRORS