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
30 . /etc/planetlab/plc_config
35 if [ -n "$DEBUG" ] ; then
36 echo -n "Type enter to run next step XXX $message XXX ... "
41 echo "==================================================" >> $LOG
48 # return 0 (yes) or 1 (no) whether the database exists
49 function check_for_database () {
51 psql --user=$PLC_DB_USER --quiet -c "SELECT datname from pg_database where datname= '$dbname' LIMIT 1" $dbname > /dev/null 2>&1
55 # when 'service plc start' gets run, the planetlab5 DB gets created
56 # so this script will drop the planetlab5 DB and re-create it from scratch
57 # with the contents of the planetlab4 DB that is epxected to exist
58 function confirm_nodegroups () {
59 echo "========================================"
61 echo "This script is designed to ease the migration from myplc 4.2 to 4.3"
62 echo "It attempts to (re)create the planetlab5 database from planetlab4"
64 echo "You might wish to edit/review"
65 echo " $NODEGROUPS_DEF"
66 echo " to finetune your migration"
68 echo "Please refer to http://svn.planet-lab.org/wiki/Migration4to5"
69 echo "========================================"
70 echo -n "Are you sure you want to proceed y/[n] ? "
73 y|Y) echo See log in $LOG ;;
74 *) echo "Bye" ; exit 1 ;;
78 function check_env () {
79 [ -f $MIGRATION_SED ] || { echo $MIGRATION_SED not found - exiting ; exit 1; }
80 [ -f $MIGRATION_SQL ] || { echo $MIGRATION_SQL not found - exiting ; exit 1; }
81 [ -f $SCHEMA_SQL ] || { echo $SCHEMA_SQL not found - exiting ; exit 1; }
82 [ -f $NODEGROUPS_DEF ] || { echo $NODEGROUPS_DEF not found - exiting ; exit 1; }
85 # connect to the former myplc, performs a local dump of planetlab4 and creates is locally
86 function get_planetlab4 () {
89 if [ -f $FAKE ] ; then
91 echo 'xxxx WARNING WARNING WARNING WARNING WARNING xxx'
93 echo Using fake input for tests $FAKE
95 echo 'xxxx WARNING WARNING WARNING WARNING WARNING xxx'
98 elif [ -f $DUMP ] ; then
99 echo "Using planetlab4 from $DUMP"
102 echo -n "Enter the hostname for the former DB service : "
104 echo "Running pg_dump on $hostname.."
105 pg_dump --ignore-version --host=$hostname --user=$PLC_DB_USER planetlab4 -f ${DUMP}
110 function prepare_planetlab5 () {
112 # check if planetlab5 exists
113 if check_for_database planetlab5 ; then
114 rename=planetlab5_${DATE_}
115 echo -n "There is an existing DB named planetlab5, drop or rename into $rename d/[r] ? "
119 run "Dropping planetlab5" psql --user=postgres template1 -c "DROP DATABASE planetlab5" || true
122 if check_for_database $rename ; then
123 echo "$rename already exists - exiting"
126 run "Renaming planetlab5 into $rename" \
127 psql --user=postgres template1 -c "ALTER DATABASE planetlab5 RENAME TO $rename"
133 # again: now it should not exist
134 if check_for_database planetlab5 ; then
135 echo "ERROR : FOUND planetlab5 - should not happen - exiting"
142 function migrate () {
148 run "Copying into $RESTORE" cp $DUMP $RESTORE
149 run "Renaming identifiers in $RESTORE" sed -f $MIGRATION_SED -i $RESTORE
151 run "Creating planetlab5 database" createdb --user=postgres --encoding=UNICODE --owner=$PLC_DB_USER planetlab5
152 run "Loading language plpgsql" createlang -U postgres plpgsql planetlab5 || true
153 run "Populating planetlab5 from $RESTORE" psql --user=postgres -f $RESTORE planetlab5
154 run "Parsing $NODEGROUPS_DEF" $PGM_NODEGROUPS $NODEGROUPS_DEF $NODEGROUPS_SQL
155 run "Loading $NODEGROUPS_SQL" psql --user=$PLC_DB_USER -f $NODEGROUPS_SQL planetlab5
156 run "Fine-tuning it with $MIGRATION_SQL" psql --user=$PLC_DB_USER -f $MIGRATION_SQL planetlab5
157 run "Extracting views definitions from $SCHEMA_SQL" $PGM_VIEWS $SCHEMA_SQL $VIEWS_SQL
158 run "Inserting views definitions in planetlab5" \
159 psql --user=$PLC_DB_USER -f $VIEWS_SQL planetlab5
162 function manage_link () {
166 echo "Managing link $dest"
173 result=${DIRNAME}/${DATE}-output.sql
174 run "Dumping result in $result" pg_dump --user=$PLC_DB_USER -f $result planetlab5
176 manage_link latest.log $LOG
177 manage_link latest-pl4.sql $DUMP
178 manage_link latest-pl5.sql $RESTORE
179 manage_link latest-views5.sql $VIEWS_SQL
180 manage_link latest-output.sql $result
188 echo "OK, we're clear, let's go"
194 echo "See logfile $LOG for detailed log"
195 echo "Checking for 'error' in the logfile"