first draft for migrating DB from v4 to v5 - nodegroups not handled properly yet
[plcapi.git] / migrations / migrate-v4-to-v5.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4 BASENAME=$(basename $COMMAND .sh)
5 DIRNAME=$(dirname $0)
6 # normalize
7 DIRNAME=$(cd ${DIRNAME}; /bin/pwd)
8
9 MIGRATION_SED=$DIRNAME/${BASENAME}.sed
10 MIGRATION_SQL=$DIRNAME/${BASENAME}.sql
11 # look in ..
12 SCHEMA_SQL=$(dirname $DIRNAME)/planetlab5.sql
13
14 DATE=$(date +%Y-%m-%d-%H-%M)
15 DATE_=$(date +%Y_%m_%d_%H_%M)
16 LOG=${DIRNAME}/${DATE}.log
17 DUMP=${DIRNAME}/${DATE}-pl4.sql
18 RESTORE=${DIRNAME}/${DATE}-pl5.sql
19 FAKE=${DIRNAME}/input-pl4.sql
20 VIEWS_SQL=$DIRNAME/${DATE}-views5.sql
21
22 # load config
23 . /etc/planetlab/plc_config
24
25 # return 0 (yes) or 1 (no) whether the database exists
26 function check_for_database () {
27     dbname=$1; shift
28     psql --user=$PLC_DB_USER --quiet -c "SELECT subversion from plc_db_version LIMIT 1" $dbname > /dev/null 2>&1 
29     return $?
30 }
31
32 # when 'service plc start' gets run, the planetlab5 DB gets created 
33 # so this script will drop the planetlab5 DB and re-create it from scratch 
34 # with the contents of the planetlab4 DB that is epxected to exist
35 function warning () {
36     echo "$COMMAND"
37     echo "This script is designed to ease the migration from myplc 4.2 to 5.0"
38     echo "You can run it before of after you install a 5.0 myplc"
39     echo "It will attempt to re-create the planetlab5 database from planetlab4"
40     echo "The planetlab5 database is renamed, not dropped, if it is found on the system"
41     echo -n "Are you sure you want to proceed y/[n] ? "
42     read answer
43     case $answer in
44         y|Y) echo See log in $LOG ;;
45         *) echo "Bye" ; exit 1 ;;
46     esac
47 }
48
49 function check () {
50     [ -f $MIGRATION_SED ] || { echo $MIGRATION_SED not found - exiting ; exit 1; }
51     [ -f $MIGRATION_SQL ] || { echo $MIGRATION_SQL not found - exiting ; exit 1; }
52     [ -f $SCHEMA_SQL ] || { echo $SCHEMA_SQL not found - exiting ; exit 1; }
53 }
54
55 function run () {
56     message=$1; shift
57
58     if [ -n "$DEBUG" ] ; then 
59         echo -n "Type enter to run next step XXX $message XXX ... "
60         read _answer_
61     fi
62
63     echo -n "$message "
64     echo "==================================================" >> $LOG
65     echo $message >> $LOG
66     echo "$@" >> $LOG
67     "$@" >> $LOG 2>&1
68     echo Done
69 }
70
71 function migrate () {
72     set -e
73     cd $DIRNAME
74
75     # check that planetlab4 exists
76     if check_for_database planetlab4 ; then
77         echo "OK : FOUND db planetlab4"
78     else
79         echo "ERROR : planetlab4 NOT FOUND - bye"
80         exit 1
81     fi
82
83     # check if planetlab5 exists
84     if check_for_database planetlab5 ; then
85         rename=planetlab5_${DATE_}
86         echo -n "There is an existing DB named planetlab5, drop or rename into $rename d/[r] ? "
87         read _answer_
88         case $_answer_ in
89             d|D)
90                 run "Dropping    planetlab5" psql --user=postgres template1 -c "DROP DATABASE planetlab5" || true
91                 ;;
92             *)
93                 if check_for_database $rename ; then
94                     echo "$rename already exists - exiting"
95                     exit 1
96                 else
97                     run "Renaming planetlab5 into $rename" \
98                         psql --user=postgres template1  -c "ALTER DATABASE planetlab5 RENAME TO $rename" 
99                 fi
100                 ;;
101         esac
102     fi
103
104     # again: now it should not exist
105     if check_for_database planetlab5 ; then
106         echo "ERROR : FOUND planetlab5 - should not happen - exiting"
107         exit 1
108     else
109         echo "OK, we're clear, let's go"
110     fi
111
112     # dump planetlab4
113     
114     if [ ! -f $FAKE ] ; then
115         run "Dumping     planetlab4 in $DUMP" pg_dump --user=$PLC_DB_USER -f $DUMP planetlab4 
116     else 
117         echo ''
118         echo 'xxxx     WARNING     WARNING     WARNING     WARNING     WARNING     xxx'
119         echo ''
120         echo Using fake input for tests $FAKE
121         echo ''
122         echo 'xxxx     WARNING     WARNING     WARNING     WARNING     WARNING     xxx'
123         echo ''
124         DUMP=$FAKE
125     fi
126
127     run "Copying     into $RESTORE" cp $DUMP $RESTORE
128     run "Renaming    identifiers in $RESTORE" sed -f $MIGRATION_SED -i $RESTORE
129
130     run "Creating    planetlab5 database" createdb --user=postgres --encoding=UNICODE --owner=$PLC_DB_USER planetlab5
131     run "Loading     language plpgsql" createlang -U postgres plpgsql planetlab5 || true
132     run "Populating  planetlab5 from $RESTORE" psql --user=postgres -f $RESTORE planetlab5 
133     run "Fine-tuning it with $MIGRATION_SQL" psql --user=$PLC_DB_USER -f $MIGRATION_SQL planetlab5
134     run "Extracting  views definitions from $SCHEMA_SQL" ./extract-views.py $SCHEMA_SQL $VIEWS_SQL
135     run "Inserting   views definitions in planetlab5" \
136         psql --user=$PLC_DB_USER -f $VIEWS_SQL planetlab5
137 }
138
139 function manage_link () {
140     dest=$1; shift
141     src=$1; shift
142     cd $DIRNAME
143     echo "Managing link $dest"
144     rm -f $dest
145     ln -s $src $dest
146 }
147
148 function links () {
149     # tmp 
150     result=${DIRNAME}/${DATE}-output.sql
151     run "Dumping result in $result" pg_dump --user=$PLC_DB_USER -f $result planetlab5
152
153     manage_link latest.log $LOG
154     manage_link latest-pl4.sql $DUMP
155     manage_link latest-pl5.sql $RESTORE
156     manage_link latest-views5.sql $VIEWS_SQL
157     manage_link latest-output.sql $result
158
159 }
160
161 function main () {
162     
163     check
164     warning
165     set -e
166     migrate
167     links
168
169 }
170
171 main "$@"