4 BASENAME=$(basename $COMMAND .sh)
7 DIRNAME=$(cd ${DIRNAME}; /bin/pwd)
9 MIGRATION_SED=$DIRNAME/${BASENAME}.sed
10 MIGRATION_SQL=$DIRNAME/${BASENAME}.sql
12 UP=$(dirname $DIRNAME)
14 SCHEMA_SQL=$UPUP/planetlab5.sql
16 DATE=$(date +%Y-%m-%d-%H-%M)
17 DATE_=$(date +%Y_%m_%d_%H_%M)
18 LOG=${DIRNAME}/${DATE}.log
19 DUMP=${DIRNAME}/pl4.sql
20 RESTORE=${DIRNAME}/${DATE}-pl5.sql
21 FAKE=${DIRNAME}/input-pl4.sql
22 VIEWS_SQL=$DIRNAME/${DATE}-views5.sql
23 NODEGROUPS_DEF=$DIRNAME/site-nodegroups.def
24 NODEGROUPS_SQL=$DIRNAME/${DATE}-nodegroups.sql
26 PGM_VIEWS=$UP/extract-views.py
27 PGM_NODEGROUPS=$DIRNAME/parse-site-nodegroups.py
29 INTERACTIVE_MODE="true"
32 . /etc/planetlab/plc_config
37 if [ -n "$DEBUG" ] ; then
38 echo -n "Type enter to run next step XXX $message XXX ... "
43 echo "==================================================" >> $LOG
50 # return 0 (yes) or 1 (no) whether the database exists
51 function check_for_database () {
53 psql --user=$PLC_DB_USER --quiet -c "SELECT datname from pg_database where datname= '$dbname' LIMIT 1" $dbname > /dev/null 2>&1
57 # when 'service plc start' gets run, the planetlab5 DB gets created
58 # so this script will drop the planetlab5 DB and re-create it from scratch
59 # with the contents of the planetlab4 DB that is epxected to exist
60 function confirm_nodegroups () {
61 echo "========================================"
63 echo "This script is designed to ease the migration from myplc 4.2 to 4.3"
64 echo "It attempts to (re)create the planetlab5 database from planetlab4"
66 echo "You might wish to edit/review"
67 echo " $NODEGROUPS_DEF"
68 echo " to finetune your migration"
70 echo "Please refer to http://svn.planet-lab.org/wiki/Migration4to5"
71 echo "========================================"
72 echo -n "Are you sure you want to proceed y/[n] ? "
73 if [ "$INTERACTIVE_MODE" = "true" ] ; then
76 y|Y) echo See log in $LOG ;;
77 *) echo "Bye" ; exit 1 ;;
82 function check_env () {
83 [ -f $MIGRATION_SED ] || { echo $MIGRATION_SED not found - exiting ; exit 1; }
84 [ -f $MIGRATION_SQL ] || { echo $MIGRATION_SQL not found - exiting ; exit 1; }
85 [ -f $SCHEMA_SQL ] || { echo $SCHEMA_SQL not found - exiting ; exit 1; }
86 [ -f $NODEGROUPS_DEF ] || { echo $NODEGROUPS_DEF not found - exiting ; exit 1; }
89 # connect to the former myplc, performs a local dump of planetlab4 and creates is locally
90 function get_planetlab4 () {
93 if [ -f $FAKE ] ; then
95 echo 'xxxx WARNING WARNING WARNING WARNING WARNING xxx'
97 echo Using fake input for tests $FAKE
99 echo 'xxxx WARNING WARNING WARNING WARNING WARNING xxx'
102 elif [ -f $DUMP ] ; then
103 echo "Using planetlab4 from $DUMP"
106 echo -n "Enter the hostname for the former DB service : "
107 if [ "$INTERACTIVE_MODE" = "true" ] ; then
109 echo "Running pg_dump on $hostname.."
110 pg_dump --ignore-version --host=$hostname --user=$PLC_DB_USER planetlab4 -f ${DUMP}
112 pg_dump --ignore-version --user=$PLC_DB_USER planetlab4 -f ${DUMP}
118 function prepare_planetlab5 () {
120 # check if planetlab5 exists
121 if check_for_database planetlab5 ; then
122 rename=planetlab5_${DATE_}
123 echo -n "There is an existing DB named planetlab5, drop or rename into $rename d/[r] ? "
124 if [ "$INTERACTIVE_MODE" = "true" ] ; then
131 run "Dropping planetlab5" psql --user=postgres template1 -c "DROP DATABASE planetlab5" || true
134 if check_for_database $rename ; then
135 echo "$rename already exists - exiting"
138 run "Renaming planetlab5 into $rename" \
139 psql --user=postgres template1 -c "ALTER DATABASE planetlab5 RENAME TO $rename"
145 # again: now it should not exist
146 if check_for_database planetlab5 ; then
147 echo "ERROR : FOUND planetlab5 - should not happen - exiting"
154 function migrate () {
160 run "Copying into $RESTORE" cp $DUMP $RESTORE
161 run "Renaming identifiers in $RESTORE" sed -f $MIGRATION_SED -i $RESTORE
163 run "Creating planetlab5 database" createdb --user=postgres --encoding=UNICODE --owner=$PLC_DB_USER planetlab5
164 run "Loading language plpgsql" createlang -U postgres plpgsql planetlab5 || true
165 run "Populating planetlab5 from $RESTORE" psql --user=postgres -f $RESTORE planetlab5
166 run "Parsing $NODEGROUPS_DEF" $PGM_NODEGROUPS $NODEGROUPS_DEF $NODEGROUPS_SQL
167 run "Loading $NODEGROUPS_SQL" psql --user=$PLC_DB_USER -f $NODEGROUPS_SQL planetlab5
168 run "Fine-tuning it with $MIGRATION_SQL" psql --user=$PLC_DB_USER -f $MIGRATION_SQL planetlab5
169 run "Extracting views definitions from $SCHEMA_SQL" $PGM_VIEWS $SCHEMA_SQL $VIEWS_SQL
170 run "Inserting views definitions in planetlab5" \
171 psql --user=$PLC_DB_USER -f $VIEWS_SQL planetlab5
174 function manage_link () {
178 echo "Managing link $dest"
185 result=${DIRNAME}/${DATE}-output.sql
186 run "Dumping result in $result" pg_dump --user=$PLC_DB_USER -f $result planetlab5
188 manage_link latest.log $LOG
189 manage_link latest-pl4.sql $DUMP
190 manage_link latest-pl5.sql $RESTORE
191 manage_link latest-views5.sql $VIEWS_SQL
192 manage_link latest-output.sql $result
198 while getopts "b" opt ; do
200 b) INTERACTIVE_MODE='false' ;;
202 echo "migrate.sh [-b]"
203 echo " -b -- execute in batch mode without asking for user feedback"
211 echo "OK, we're clear, let's go"
217 echo "See logfile $LOG for detailed log"
218 echo "Checking for 'error' in the logfile"