f196a00d40c28d9304b8d452155586feea0d1624
[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,v 1.5 2007/01/30 11:29:36 thierry Exp $
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 subversion column in plc_db_version by applying
24 ### all migrations scripts in /usr/share/plc_api/migrations/nnn-up-*
25 ### between the current_subversion+1 and the last one
26 ### migration indices must be contiguous
27 function migrate_db () {
28
29     ### check for (or create) the subversion column in plc_db_version
30     psql -U $PLC_DB_USER -c 'select (subversion) from plc_db_version' $PLC_DB_NAME > /dev/null
31     [ "$?" = 0 ] || psql -U $PLC_DB_USER -c 'ALTER TABLE plc_db_version ADD subversion integer DEFAULT 0' $PLC_DB_NAME
32
33     # get current subversion - OUCH, THIS IS UGLY
34     current_subversion=$(psql -U $PLC_DB_USER -c 'select (subversion) from plc_db_version' $PLC_DB_NAME | \
35                          grep '^  *[0-9][0-9]*$')
36     current_subversion=$(($current_subversion))
37     upgrade_index=$current_subversion
38                          
39     while true; do
40         upgrade_index=$(printf "%03d" $(($upgrade_index+1)))
41         upgrade_scripts=$(ls -1 /usr/share/plc_api/migrations/${upgrade_index}-up-* 2> /dev/null)
42         if [ -z "$upgrade_scripts" ] ; then
43             echo "DB current migration is $current_subversion"
44             psql -U $PLC_DB_USER -c "UPDATE plc_db_version SET subversion=$current_subversion" $PLC_DB_NAME
45             break
46         else
47             for script in $upgrade_scripts; do
48                 # is this an sql script
49                 if [ $(basename "$script") != $(basename $script .sql) ] ; then
50                     echo "Applying SQL migration script $script"
51                     psql -U $PLC_DB_USER -f "$script" $PLC_DB_NAME
52                 elif [ -x "$script" ] ; then
53                     echo "Running executable migration script $script"
54                     "$script"
55                 else
56                     echo "WARNING - script $script ignored"
57                 fi                  
58             done
59             current_subversion=$(($upgrade_index))
60         fi
61     done
62 }
63
64 #### dumps the database for future restoration
65 # uses /root/dumps since /root is mounted on /data/root
66 function dump_db () {
67
68     dump=/root/dumps/$(date +'%Y-%m-%d-%H-%M.sql')
69     mkdir -p $(dirname $dump)
70     pg_dump --user=$PLC_DB_USER $PLC_DB_NAME > $dump
71     
72 }
73
74 function clean_dumps () {
75     find /root/dumps -name '[0-9]*.sql' -atime +15 | xargs rm
76 }
77
78 ##########
79 if [ "$PLC_DB_ENABLED" != "1" ] ; then
80   exit 0
81 fi
82
83 case "$1" in
84     start)
85
86         MESSAGE=$"Bootstrapping the database"
87         dialog "$MESSAGE"
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         # Handle migrations
107         migrate_db
108
109         result "$MESSAGE"
110         ;;
111
112     migrate)
113
114         MESSAGE=$"Migrating the database"
115         dialog "$MESSAGE"
116
117         migrate_db
118         result "$MESSAGE"
119         ;;
120
121     dump)
122
123         MESSAGE=$"Dumping the database"
124         dialog "$MESSAGE"
125
126         dump_db
127         result "$MESSAGE"
128         ;;
129
130     clean-dump)
131
132         MESSAGE=$"Cleaning old database dumps"
133         dialog "$MESSAGE"
134
135         clean_dumps
136         result "$MESSAGE"
137         ;;
138
139     *)
140         echo "Usage: $0 [start|migrate|dump|clean-dump]"
141         exit 1
142         ;;
143
144       
145 esac
146
147 exit $ERRORS