From: tierno Date: Thu, 27 Apr 2017 16:49:37 +0000 (+0200) Subject: changes at migrate_vim_db to use database version as a parameter and not use openvim... X-Git-Tag: v2.0.1~21 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F18%2F1718%2F6;p=osm%2Fopenvim.git changes at migrate_vim_db to use database version as a parameter and not use openvim version Change-Id: I3d39d04020e551b127526fca24671176a932c806 Signed-off-by: tierno --- diff --git a/database_utils/dump_db.sh b/database_utils/dump_db.sh index 1a7a4b6..affb2be 100755 --- a/database_utils/dump_db.sh +++ b/database_utils/dump_db.sh @@ -22,7 +22,7 @@ ## LICENSE_HEAD='/** -* Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. +* Copyright 2017 Telefónica Investigación y Desarrollo, S.A.U. * This file is part of openvim * All Rights Reserved. * @@ -54,7 +54,7 @@ DBNAME="vim_db" MYSQL=$(which mysql) AWK=$(which awk) GREP=$(which grep) -DIRNAME=`dirname $0` +DIRNAME=`dirname $(readlink -f $0)` function usage(){ echo -e "Usage: $0 OPTIONS" diff --git a/database_utils/init_vim_db.sh b/database_utils/init_vim_db.sh index 1edaa68..248f821 100755 --- a/database_utils/init_vim_db.sh +++ b/database_utils/init_vim_db.sh @@ -23,31 +23,34 @@ DBUSER="vim" DBPASS="" -DBHOST="localhost" +DEFAULT_DBPASS="vimpw" +DBHOST="" DBPORT="3306" DBNAME="vim_db" - +QUIET_MODE="" + # Detect paths MYSQL=$(which mysql) AWK=$(which awk) GREP=$(which grep) -DIRNAME=`dirname $0` +DIRNAME=`dirname $(readlink -f $0)` function usage(){ - echo -e "Usage: $0 OPTIONS [{openvim_version}]" - echo -e " Inits openvim database; deletes previous one and loads from ${DBNAME}_structure.sql" + echo -e "Usage: $0 OPTIONS [version]" + echo -e " Inits openvim database; deletes previous one and loads from ${DBNAME}_structure.sql"\ echo -e " and data from host_ranking.sql, nets.sql, of_ports_pci_correspondece*.sql" - echo -e " If openvim_version is not provided it tries to get from openvimd.py using relative path" + "If [version] is not provided, it is upgraded to the last version" echo -e " OPTIONS" echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails" - echo -e " -p PASS database password. 'No password' or 'vimpw' by default. Prompts if DB access fails" + echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting" echo -e " -P PORT database port. '$DBPORT' by default" - echo -e " -h HOST database host. '$DBHOST' by default" + echo -e " -h HOST database host. 'localhost' by default" echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails" + echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database" echo -e " --help shows this help" } -while getopts ":u:p:P:h:d:-:" o; do +while getopts ":u:p:P:h:d:q-:" o; do case "${o}" in u) DBUSER="$OPTARG" @@ -64,82 +67,88 @@ while getopts ":u:p:P:h:d:-:" o; do h) DBHOST="$OPTARG" ;; + q) + export QUIET_MODE="-q" + ;; -) [ "${OPTARG}" == "help" ] && usage && exit 0 - echo "Invalid option: --$OPTARG" >&2 && usage >&2 + [ "${OPTARG}" == "quiet" ] && export QUIET_MODE="-q" && continue + echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2 exit 1 ;; \?) - echo "Invalid option: -$OPTARG" >&2 && usage >&2 + echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2 exit 1 ;; :) - echo "Option -$OPTARG requires an argument." >&2 && usage >&2 + echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2 exit 1 ;; *) usage >&2 - exit -1 + exit 1 ;; esac done shift $((OPTIND-1)) -#Creating temporary file +DB_VERSION=$1 + +if [ -n "$DB_VERSION" ] ; then + # check it is a number and an allowed one + [ "$DB_VERSION" -eq "$DB_VERSION" ] 2>/dev/null || + ! echo "parameter 'version' requires a integer value" >&2 || exit 1 +fi + +# Creating temporary file TEMPFILE="$(mktemp -q --tmpdir "initmanodb.XXXXXX")" trap 'rm -f "$TEMPFILE"' EXIT chmod 0600 "$TEMPFILE" - -#if password is missing, before prompting for it try without password and with "manopw" -DBHOST_="-h$DBHOST" -DBPORT_="-P$DBPORT" DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE" -if [ -z "${DBPASS}" ] -then - password_ok="" - echo -e "[client]\nuser='${DBUSER}'\npassword=vimpw" > "$TEMPFILE" - mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS="vimpw" - echo -e "[client]\nuser='${DBUSER}'\npassword=''" > "$TEMPFILE" - mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS="" -fi -echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE" +echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" -#check and ask for database user password -while ! mysql "$DEF_EXTRA_FILE_PARAM" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 +# Check and ask for database user password +FIRST_TRY="yes" +while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null` do - [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)" - [ -z "$logintry" ] && echo -e "\nProvide database name and credentials" - read -e -p "mysql database name($DBNAME): " KK - [ -n "$KK" ] && DBNAME="$KK" - read -e -p "mysql user($DBUSER): " KK - [ -n "$KK" ] && DBUSER="$KK" - read -e -s -p "mysql password: " DBPASS - echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE" - logintry="yes" - echo + # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials + [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" && + echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" && + continue + echo "$DB_ERROR" + [[ -n "$QUIET_MODE" ]] && echo -e "Invalid database credentials!!!" >&2 && exit 1 + echo -e "Provide database name and credentials (Ctrl+c to abort):" + read -e -p " mysql database name($DBNAME): " KK + [ -n "$KK" ] && DBNAME="$KK" + read -e -p " mysql user($DBUSER): " KK + [ -n "$KK" ] && DBUSER="$KK" + read -e -s -p " mysql password: " DBPASS + echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" + FIRST_TRY="" + echo done -DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME" -DBUSER_="-u$DBUSER" -DBPASS_="-p$DBPASS" +DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBNAME" +DBUSER_="" && [ -n "$DBUSER" ] && DBUSER_="-u$DBUSER" +DBPASS_="" && [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS" +DBHOST_="" && [ -n "$DBHOST" ] && DBHOST_="-h$DBHOST" +DBPORT_="-P$DBPORT" echo " loading ${DIRNAME}/vim_db_structure.sql" -sed -e "s/vim_db/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ - -echo " migrage database version" -#${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $1 -DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]})) -echo "${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $1" -${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $1 +sed -e "s/{{vim_db}}/$DBNAME/" ${DIRNAME}/vim_db_structure.sql | mysql $DEF_EXTRA_FILE_PARAM echo " loading ${DIRNAME}/host_ranking.sql" -mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/host_ranking.sql +mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/host_ranking.sql echo " loading ${DIRNAME}/of_ports_pci_correspondence.sql" -mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql +mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/of_ports_pci_correspondence.sql #mysql -h $HOST -P $PORT -u $MUSER -p$MPASS $MDB < ${DIRNAME}/of_ports_pci_correspondence_centos.sql echo " loading ${DIRNAME}/nets.sql" -mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME < ${DIRNAME}/nets.sql +mysql $DEF_EXTRA_FILE_PARAM $DBNAME < ${DIRNAME}/nets.sql + +echo " migrage database version" +# echo "${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION" +${DIRNAME}/migrate_vim_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME $QUIET_MODE $DB_VERSION diff --git a/database_utils/migrate_vim_db.sh b/database_utils/migrate_vim_db.sh index a2c116c..a5ff836 100755 --- a/database_utils/migrate_vim_db.sh +++ b/database_utils/migrate_vim_db.sh @@ -27,31 +27,34 @@ DBUSER="vim" DBPASS="" -DBHOST="localhost" +DEFAULT_DBPASS="vimpw" +DBHOST="" DBPORT="3306" DBNAME="vim_db" - +QUIET_MODE="" +#TODO update it with the last database version +LAST_DB_VERSION=17 + # Detect paths MYSQL=$(which mysql) AWK=$(which awk) GREP=$(which grep) -DIRNAME=`dirname $0` -HERE=$(realpath $(dirname $0)) function usage(){ - echo -e "Usage: $0 OPTIONS [{openvim_version}]" - echo -e " Upgrades/Downgrades openvim database preserving the content" - echo -e " if openvim_version is not provided it tries to get from openvimd.py using relative path" + echo -e "Usage: $0 OPTIONS [version]" + echo -e " Upgrades/Downgrades openvim database preserving the content."\ + "If [version] is not provided, it is upgraded to the last version" echo -e " OPTIONS" echo -e " -u USER database user. '$DBUSER' by default. Prompts if DB access fails" - echo -e " -p PASS database password. 'No password' or 'vimpw' by default. Prompts if DB access fails" + echo -e " -p PASS database password. If missing it tries without and '$DEFAULT_DBPASS' password before prompting" echo -e " -P PORT database port. '$DBPORT' by default" - echo -e " -h HOST database host. '$DBHOST' by default" + echo -e " -h HOST database host. 'localhost' by default" echo -e " -d NAME database name. '$DBNAME' by default. Prompts if DB access fails" + echo -e " -q --quiet: Do not prompt for credentials and exit if cannot access to database" echo -e " --help shows this help" } -while getopts ":u:p:P:h:d:-:" o; do +while getopts ":u:p:P:h:d:q-:" o; do case "${o}" in u) DBUSER="$OPTARG" @@ -68,88 +71,79 @@ while getopts ":u:p:P:h:d:-:" o; do h) DBHOST="$OPTARG" ;; + q) + export QUIET_MODE=yes + ;; -) [ "${OPTARG}" == "help" ] && usage && exit 0 - echo "Invalid option: --$OPTARG" >&2 && usage >&2 + [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && continue + echo "Invalid option: '--$OPTARG'. Type --help for more information" >&2 exit 1 - ;; + ;; \?) - echo "Invalid option: -$OPTARG" >&2 && usage >&2 + echo "Invalid option: '-$OPTARG'. Type --help for more information" >&2 exit 1 ;; :) - echo "Option -$OPTARG requires an argument." >&2 && usage >&2 + echo "Option '-$OPTARG' requires an argument. Type --help for more information" >&2 exit 1 ;; *) usage >&2 - exit -1 + exit 1 ;; esac done shift $((OPTIND-1)) - -#GET OPENVIM VERSION -OPENVIM_VER="$1" -if [ -z "$OPENVIM_VER" ] -then - OVIM_PATH=$(dirname $HERE) - OPENVIM_VER=`python ${OVIM_PATH}/ovim.py -v 2> /dev/null` - OPENVIM_VER=${OPENVIM_VER%%-r*} - OPENVIM_VER=${OPENVIM_VER##*version } - echo " Detected openvim version $OPENVIM_VER" +DB_VERSION=$1 + +if [ -n "$DB_VERSION" ] ; then + # check it is a number and an allowed one + [ "$DB_VERSION" -eq "$DB_VERSION" ] 2>/dev/null || + ! echo "parameter 'version' requires a integer value" >&2 || exit 1 + if [ "$DB_VERSION" -lt 0 ] || [ "$DB_VERSION" -gt "$LAST_DB_VERSION" ] ; then + echo "parameter 'version' requires a valid database version between '0' and '$LAST_DB_VERSION'"\ + "If you need an upper version, get a newer version of this script '$0'" >&2 + exit 1 + fi +else + DB_VERSION="$LAST_DB_VERSION" fi -VERSION_1=`echo $OPENVIM_VER | cut -f 1 -d"."` -VERSION_2=`echo $OPENVIM_VER | cut -f 2 -d"."` -VERSION_3=`echo $OPENVIM_VER | cut -f 3 -d"."` -if ! [ "$VERSION_1" -ge 0 -a "$VERSION_2" -ge 0 -a "$VERSION_3" -ge 0 ] 2>/dev/null -then - [ -n "$1" ] && echo "Invalid openvim version '$1', expected 'X.X.X'" >&2 - [ -z "$1" ] && echo "Can not get openvim version" >&2 - exit -1 -fi -OPENVIM_VER_NUM=`printf "%d%03d%03d" ${VERSION_1} ${VERSION_2} ${VERSION_3}` -#Creating temporary file -TEMPFILE="$(mktemp -q --tmpdir "migratemanodb.XXXXXX")" +# Creating temporary file +TEMPFILE="$(mktemp -q --tmpdir "migratevimdb.XXXXXX")" trap 'rm -f "$TEMPFILE"' EXIT chmod 0600 "$TEMPFILE" - -#if password is missing, before prompting for it try without password and with "manopw" -DBHOST_="-h$DBHOST" -DBPORT_="-P$DBPORT" DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE" -if [ -z "${DBPASS}" ] -then - password_ok="" - echo -e "[client]\nuser='${DBUSER}'\npassword='vimpw'" > "$TEMPFILE" - mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS="vimpw" - echo -e "[client]\nuser='${DBUSER}'\npassword=''" > "$TEMPFILE" - mysql --defaults-extra-file="$TEMPFILE" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 && DBPASS="" -fi -echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE" +echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" -#check and ask for database user password -while ! mysql "$DEF_EXTRA_FILE_PARAM" $DBHOST_ $DBPORT_ $DBNAME -e "quit" >/dev/null 2>&1 +# Check and ask for database user password +FIRST_TRY="yes" +while ! DB_ERROR=`mysql "$DEF_EXTRA_FILE_PARAM" $DBNAME -e "quit" 2>&1 >/dev/null` do - [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)" - [ -z "$logintry" ] && echo -e "\nProvide database name and credentials" - read -e -p "mysql database name($DBNAME): " KK - [ -n "$KK" ] && DBNAME="$KK" - read -e -p "mysql user($DBUSER): " KK - [ -n "$KK" ] && DBUSER="$KK" - read -e -s -p "mysql password: " DBPASS - echo -e "[client]\nuser='${DBUSER}'\npassword='${DBPASS}'" > "$TEMPFILE" - logintry="yes" - echo + # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials + [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" && + echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" && + continue + echo "$DB_ERROR" + [[ -n "$QUIET_MODE" ]] && echo -e "Invalid database credentials!!!" >&2 && exit 1 + echo -e "Provide database name and credentials (Ctrl+c to abort):" + read -e -p " mysql database name($DBNAME): " KK + [ -n "$KK" ] && DBNAME="$KK" + read -e -p " mysql user($DBUSER): " KK + [ -n "$KK" ] && DBUSER="$KK" + read -e -s -p " mysql password: " DBPASS + echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" + FIRST_TRY="" + echo done -DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBHOST_ $DBPORT_ $DBNAME" +DBCMD="mysql $DEF_EXTRA_FILE_PARAM $DBNAME" #echo DBCMD $DBCMD #GET DATABASE VERSION -#check that the database seems a openvim database +# check that the database seems a openvim database if ! echo -e "show create table instances;\nshow create table numas" | $DBCMD >/dev/null 2>&1 then echo " database $DBNAME does not seem to be an openvim database" >&2 @@ -160,37 +154,41 @@ if ! echo 'show create table schema_version;' | $DBCMD >/dev/null 2>&1 then DATABASE_VER="0.0" DATABASE_VER_NUM=0 -else - DATABASE_VER_NUM=`echo "select max(version_int) from schema_version;" | $DBCMD | tail -n+2` +else + DATABASE_VER_NUM=`echo "select max(version_int) from schema_version;" | $DBCMD | tail -n+2` DATABASE_VER=`echo "select version from schema_version where version_int='$DATABASE_VER_NUM';" | $DBCMD | tail -n+2` - [ "$DATABASE_VER_NUM" -lt 0 -o "$DATABASE_VER_NUM" -gt 100 ] && echo " Error can not get database version ($DATABASE_VER?)" >&2 && exit -1 + [ "$DATABASE_VER_NUM" -lt 0 -o "$DATABASE_VER_NUM" -gt 100 ] && + echo " Error can not get database version ($DATABASE_VER?)" >&2 && exit -1 #echo "_${DATABASE_VER_NUM}_${DATABASE_VER}" fi +[ "$DATABASE_VER_NUM" -gt "$LAST_DB_VERSION" ] && + echo "Database has been upgraded with a newer version of this script. Use this version to downgrade" >&2 && + exit 1 #GET DATABASE TARGET VERSION -DATABASE_TARGET_VER_NUM=0 -[ $OPENVIM_VER_NUM -gt 1091 ] && DATABASE_TARGET_VER_NUM=1 #>0.1.91 => 1 -[ $OPENVIM_VER_NUM -ge 2003 ] && DATABASE_TARGET_VER_NUM=2 #0.2.03 => 2 -[ $OPENVIM_VER_NUM -ge 2005 ] && DATABASE_TARGET_VER_NUM=3 #0.2.5 => 3 -[ $OPENVIM_VER_NUM -ge 3001 ] && DATABASE_TARGET_VER_NUM=4 #0.3.1 => 4 -[ $OPENVIM_VER_NUM -ge 4001 ] && DATABASE_TARGET_VER_NUM=5 #0.4.1 => 5 -[ $OPENVIM_VER_NUM -ge 4002 ] && DATABASE_TARGET_VER_NUM=6 #0.4.2 => 6 -[ $OPENVIM_VER_NUM -ge 4005 ] && DATABASE_TARGET_VER_NUM=7 #0.4.5 => 7 -[ $OPENVIM_VER_NUM -ge 4010 ] && DATABASE_TARGET_VER_NUM=8 #0.4.10 => 8 -[ $OPENVIM_VER_NUM -ge 5001 ] && DATABASE_TARGET_VER_NUM=9 #0.5.1 => 9 -[ $OPENVIM_VER_NUM -ge 5002 ] && DATABASE_TARGET_VER_NUM=10 #0.5.2 => 10 -[ $OPENVIM_VER_NUM -ge 5004 ] && DATABASE_TARGET_VER_NUM=11 #0.5.4 => 11 -[ $OPENVIM_VER_NUM -ge 5005 ] && DATABASE_TARGET_VER_NUM=12 #0.5.5 => 12 -[ $OPENVIM_VER_NUM -ge 5006 ] && DATABASE_TARGET_VER_NUM=13 #0.5.6 => 13 -[ $OPENVIM_VER_NUM -ge 5007 ] && DATABASE_TARGET_VER_NUM=14 #0.5.7 => 14 -[ $OPENVIM_VER_NUM -ge 5008 ] && DATABASE_TARGET_VER_NUM=15 #0.5.8 => 15 -[ $OPENVIM_VER_NUM -ge 5009 ] && DATABASE_TARGET_VER_NUM=16 #0.5.9 => 16 -[ $OPENVIM_VER_NUM -ge 5010 ] && DATABASE_TARGET_VER_NUM=17 #0.5.10 => 17 +#DB_VERSION=0 +#[ $OPENVIM_VER_NUM -gt 1091 ] && DATABASE_TARGET_VER_NUM=1 #>0.1.91 => 1 +#[ $OPENVIM_VER_NUM -ge 2003 ] && DATABASE_TARGET_VER_NUM=2 #0.2.03 => 2 +#[ $OPENVIM_VER_NUM -ge 2005 ] && DATABASE_TARGET_VER_NUM=3 #0.2.5 => 3 +#[ $OPENVIM_VER_NUM -ge 3001 ] && DATABASE_TARGET_VER_NUM=4 #0.3.1 => 4 +#[ $OPENVIM_VER_NUM -ge 4001 ] && DATABASE_TARGET_VER_NUM=5 #0.4.1 => 5 +#[ $OPENVIM_VER_NUM -ge 4002 ] && DATABASE_TARGET_VER_NUM=6 #0.4.2 => 6 +#[ $OPENVIM_VER_NUM -ge 4005 ] && DATABASE_TARGET_VER_NUM=7 #0.4.5 => 7 +#[ $OPENVIM_VER_NUM -ge 4010 ] && DATABASE_TARGET_VER_NUM=8 #0.4.10 => 8 +#[ $OPENVIM_VER_NUM -ge 5001 ] && DATABASE_TARGET_VER_NUM=9 #0.5.1 => 9 +#[ $OPENVIM_VER_NUM -ge 5002 ] && DATABASE_TARGET_VER_NUM=10 #0.5.2 => 10 +#[ $OPENVIM_VER_NUM -ge 5004 ] && DATABASE_TARGET_VER_NUM=11 #0.5.4 => 11 +#[ $OPENVIM_VER_NUM -ge 5005 ] && DATABASE_TARGET_VER_NUM=12 #0.5.5 => 12 +#[ $OPENVIM_VER_NUM -ge 5006 ] && DATABASE_TARGET_VER_NUM=13 #0.5.6 => 13 +#[ $OPENVIM_VER_NUM -ge 5007 ] && DATABASE_TARGET_VER_NUM=14 #0.5.7 => 14 +#[ $OPENVIM_VER_NUM -ge 5008 ] && DATABASE_TARGET_VER_NUM=15 #0.5.8 => 15 +#[ $OPENVIM_VER_NUM -ge 5009 ] && DATABASE_TARGET_VER_NUM=16 #0.5.9 => 16 +#[ $OPENVIM_VER_NUM -ge 5010 ] && DATABASE_TARGET_VER_NUM=17 #0.5.10 => 17 #TODO ... put next versions here function upgrade_to_1(){ - echo " upgrade database from version 0.0 to version 0.1" + # echo " upgrade database from version 0.0 to version 0.1" echo " CREATE TABLE \`schema_version\`" echo "CREATE TABLE \`schema_version\` ( \`version_int\` INT NOT NULL COMMENT 'version as a number. Must not contain gaps', @@ -210,14 +208,14 @@ function upgrade_to_1(){ ADD COLUMN \`last_error\` VARCHAR(200) NULL AFTER \`status\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_1(){ - echo " downgrade database from version 0.1 to version 0.0" + # echo " downgrade database from version 0.1 to version 0.0" echo " ALTER TABLE \`nets\` DROP COLUMN \`last_error\`" echo "ALTER TABLE \`nets\` DROP COLUMN \`last_error\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo " DROP TABLE \`schema_version\`" echo "DROP TABLE \`schema_version\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_2(){ - echo " upgrade database from version 0.1 to version 0.2" + # echo " upgrade database from version 0.1 to version 0.2" echo " ALTER TABLE \`of_ports_pci_correspondence\` \`resources_port\` \`ports\` ADD COLUMN \`switch_dpid\`" for table in of_ports_pci_correspondence resources_port ports do @@ -255,7 +253,7 @@ function upgrade_to_2(){ VALUES (2, '0.2', '0.2.03', 'update Procedure UpdateSwitchPort', '2015-05-06');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_3(){ - echo " upgrade database from version 0.2 to version 0.3" + # echo " upgrade database from version 0.2 to version 0.3" echo " change size of source_name at table resources_port" echo "ALTER TABLE resources_port CHANGE COLUMN source_name source_name VARCHAR(24) NULL DEFAULT NULL AFTER port_id;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo " CREATE PROCEDURE GetAllAvailablePorts" @@ -293,7 +291,7 @@ function upgrade_to_3(){ } function upgrade_to_4(){ - echo " upgrade database from version 0.3 to version 0.4" + # echo " upgrade database from version 0.3 to version 0.4" echo " remove unique VLAN index at 'resources_port', 'ports'" echo "ALTER TABLE resources_port DROP INDEX vlan_switch_port;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE ports DROP INDEX vlan_switch_port;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -306,7 +304,7 @@ function upgrade_to_4(){ function upgrade_to_X(){ #TODO, this change of foreign key does not work - echo " upgrade database from version 0.X to version 0.X" + # echo " upgrade database from version 0.X to version 0.X" echo "ALTER TABLE instances DROP FOREIGN KEY FK_instances_flavors, DROP INDEX FK_instances_flavors, DROP FOREIGN KEY FK_instances_images, DROP INDEX FK_instances_flavors,;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE instances @@ -315,7 +313,7 @@ function upgrade_to_X(){ } function downgrade_from_2(){ - echo " downgrade database from version 0.2 to version 0.1" + # echo " downgrade database from version 0.2 to version 0.1" echo " UPDATE procedure UpdateSwitchPort" echo "DROP PROCEDURE IF EXISTS UpdateSwitchPort; delimiter // @@ -346,7 +344,7 @@ function downgrade_from_2(){ echo "DELETE FROM \`schema_version\` WHERE \`version_int\` = '2';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_3(){ - echo " downgrade database from version 0.3 to version 0.2" + # echo " downgrade database from version 0.3 to version 0.2" echo " change back size of source_name at table resources_port" echo "ALTER TABLE resources_port CHANGE COLUMN source_name source_name VARCHAR(20) NULL DEFAULT NULL AFTER port_id;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo " DROP PROCEDURE GetAllAvailablePorts" @@ -354,7 +352,7 @@ function downgrade_from_3(){ echo "DELETE FROM schema_version WHERE version_int = '3';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_4(){ - echo " downgrade database from version 0.4 to version 0.3" + # echo " downgrade database from version 0.4 to version 0.3" echo " adding back unique index VLAN at 'resources_port','ports'" echo "ALTER TABLE resources_port ADD COLUMN vlan SMALLINT(5) UNSIGNED NULL DEFAULT NULL AFTER Mbps_used;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "UPDATE resources_port SET vlan= 99+id-root_id WHERE id != root_id;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -368,20 +366,20 @@ function downgrade_from_4(){ function upgrade_to_5(){ - echo " upgrade database from version 0.4 to version 0.5" + # echo " upgrade database from version 0.4 to version 0.5" echo " add 'ip_address' to ports'" echo "ALTER TABLE ports ADD COLUMN ip_address VARCHAR(64) NULL DEFAULT NULL AFTER mac;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (5, '0.5', '0.4.1', 'Add ip_address to ports', '2015-09-04');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_5(){ - echo " downgrade database from version 0.5 to version 0.4" + # echo " downgrade database from version 0.5 to version 0.4" echo " removing 'ip_address' from 'ports'" echo "ALTER TABLE ports DROP COLUMN ip_address;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '5';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_6(){ - echo " upgrade database from version 0.5 to version 0.6" + # echo " upgrade database from version 0.5 to version 0.6" echo " Change enalarge name, description to 255 at all database" for table in flavors images instances tenants do @@ -407,7 +405,7 @@ function upgrade_to_6(){ echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (6, '0.6', '0.4.2', 'Enlarging name at database', '2016-02-01');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_6(){ - echo " downgrade database from version 0.6 to version 0.5" + # echo " downgrade database from version 0.6 to version 0.5" echo " Change back name,description to shorter length at all database" for table in flavors images instances tenants do @@ -434,7 +432,7 @@ function downgrade_from_6(){ } function upgrade_to_7(){ - echo " upgrade database from version 0.6 to version 0.7" + # echo " upgrade database from version 0.6 to version 0.7" echo " add 'bind_net','bind_type','cidr','enable_dhcp' to 'nets'" echo "ALTER TABLE nets ADD COLUMN cidr VARCHAR(64) NULL DEFAULT NULL AFTER bind, ADD COLUMN enable_dhcp ENUM('true','false') NOT NULL DEFAULT 'false' after cidr, ADD COLUMN dhcp_first_ip VARCHAR(64) NULL DEFAULT NULL AFTER enable_dhcp, ADD COLUMN dhcp_last_ip VARCHAR(64) NULL DEFAULT NULL AFTER dhcp_first_ip;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE nets CHANGE COLUMN bind provider VARCHAR(36) NULL DEFAULT NULL AFTER vlan;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -442,7 +440,7 @@ function upgrade_to_7(){ echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (7, '0.7', '0.4.5', 'Add bind_net to net table', '2016-02-12');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_7(){ - echo " downgrade database from version 0.7 to version 0.6" + # echo " downgrade database from version 0.7 to version 0.6" echo " removing 'bind_net','bind_type','cidr','enable_dhcp' from 'nets'" echo "ALTER TABLE nets CHANGE COLUMN provider bind NULL DEFAULT NULL AFTER vlan;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE nets DROP COLUMN cidr, DROP COLUMN enable_dhcp, DROP COLUMN bind_net, DROP COLUMN bind_type, DROP COLUMN dhcp_first_ip, DROP COLUMN dhcp_last_ip;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -450,27 +448,27 @@ function downgrade_from_7(){ } function upgrade_to_8(){ - echo " upgrade database from version 0.7 to version 0.8" + # echo " upgrade database from version 0.7 to version 0.8" echo " add column 'checksum' to 'images'" echo "ALTER TABLE images ADD COLUMN checksum VARCHAR(32) NULL AFTER name;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (8, '0.8', '0.4.10', 'add column checksum to images', '2016-09-30');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_8(){ - echo " downgrade database from version 0.8 to version 0.7" + # echo " downgrade database from version 0.8 to version 0.7" echo " remove column 'checksum' from 'images'" echo "ALTER TABLE images DROP COLUMN checksum;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '8';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_9(){ - echo " upgrade database from version 0.8 to version 0.9" + # echo " upgrade database from version 0.8 to version 0.9" echo " change length of columns 'path' and 'name' to 255 in table 'images', and change length of column 'name' to 255 in table 'flavors'" echo "ALTER TABLE images CHANGE COLUMN path path VARCHAR(255) NOT NULL AFTER uuid, CHANGE COLUMN name name VARCHAR(255) NOT NULL AFTER path;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE flavors CHANGE COLUMN name name VARCHAR(255) NOT NULL AFTER uuid;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (9, '0.9', '0.5.1', 'increase length of columns path and name to 255 in table images, and change length of column name to 255 in table flavors', '2017-01-10');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_9(){ - echo " downgrade database from version 0.9 to version 0.8" + # echo " downgrade database from version 0.9 to version 0.8" echo " change length of columns 'path' and 'name' to 100 and 64 in table 'images'" echo "ALTER TABLE images CHANGE COLUMN path path VARCHAR(100) NOT NULL AFTER uuid, CHANGE COLUMN name name VARCHAR(64) NOT NULL AFTER path;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE flavors CHANGE COLUMN name name VARCHAR(64) NOT NULL AFTER uuid;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -478,32 +476,32 @@ function downgrade_from_9(){ } function upgrade_to_10(){ - echo " upgrade database from version 0.9 to version 0.10" + # echo " upgrade database from version 0.9 to version 0.10" echo " change types at 'ports'" echo "ALTER TABLE ports CHANGE COLUMN type type ENUM('instance:bridge','instance:data','external','instance:ovs','controller:ovs') NOT NULL DEFAULT 'instance:bridge' AFTER status;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (10, '0.10', '0.5.2', 'change ports type, adding instance:ovs', '2017-02-01');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_10(){ - echo " downgrade database from version 0.10 to version 0.9" + # echo " downgrade database from version 0.10 to version 0.9" echo " change back types at 'ports'" echo "ALTER TABLE ports CHANGE COLUMN type type ENUM('instance:bridge','instance:data','external') NOT NULL DEFAULT 'instance:bridge' AFTER status;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '10';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_11(){ - echo " upgrade database from version 0.10 to version 0.11" + # echo " upgrade database from version 0.10 to version 0.11" echo " Add gateway_ip colum to 'nets'" echo "ALTER TABLE nets ADD COLUMN gateway_ip VARCHAR(64) NULL DEFAULT NULL AFTER dhcp_last_ip;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (11, '0.11', '0.5.4', 'Add gateway_ip colum to nets', '2017-02-13');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_11(){ - echo " downgrade database from version 0.11 to version 0.10" + # echo " downgrade database from version 0.11 to version 0.10" echo " Delete gateway_ip colum from 'nets'" echo "ALTER TABLE nets DROP COLUMN gateway_ip;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '11';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_12(){ - echo " upgrade database from version 0.11 to version 0.12" + # echo " upgrade database from version 0.11 to version 0.12" echo " Create of_controller table " echo "CREATE TABLE ofcs ( uuid VARCHAR(36) NOT NULL, @@ -525,7 +523,7 @@ ENGINE=InnoDB;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_12(){ - echo " downgrade database from version 0.12 to version 0.11" + # echo " downgrade database from version 0.12 to version 0.11" echo " Delete ofcs table" echo "DROP TABLE ofcs;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE uuids CHANGE COLUMN used_at used_at ENUM('flavors', 'hosts', 'images', 'instances', 'nets', 'ports', 'tenants') NULL DEFAULT NULL COMMENT 'Table that uses this UUID' ;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -533,7 +531,7 @@ function downgrade_from_12(){ } function upgrade_to_13(){ - echo " upgrade database from version 0.12 to version 0.13" + # echo " upgrade database from version 0.12 to version 0.13" echo " Create of_port_mapings table " echo "CREATE TABLE of_port_mappings ( uuid VARCHAR(36) NOT NULL, @@ -555,14 +553,14 @@ function upgrade_to_13(){ } function downgrade_from_13(){ - echo " downgrade database from version 0.13 to version 0.12" + # echo " downgrade database from version 0.13 to version 0.12" echo " Delete of_port_mappings table" echo "DROP TABLE of_port_mappings;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '13';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_14(){ - echo " upgrade database from version 0.13 to version 0.14" + # echo " upgrade database from version 0.13 to version 0.14" echo " Add switch_mac, ofc_id colum to 'ports' and 'resources_port'" echo "ALTER TABLE ports ADD COLUMN switch_mac VARCHAR(18) NULL DEFAULT NULL AFTER switch_port, @@ -576,7 +574,7 @@ function upgrade_to_14(){ } function downgrade_from_14(){ - echo " downgrade database from version 0.14 to version 0.13" + # echo " downgrade database from version 0.14 to version 0.13" echo " Delete switch_mac, ofc_id colum to 'ports'" echo "ALTER TABLE ports DROP COLUMN switch_mac, @@ -590,7 +588,7 @@ function downgrade_from_14(){ } function upgrade_to_15(){ - echo " upgrade database from version 0.14 to version 0.15" + # echo " upgrade database from version 0.14 to version 0.15" echo " Add ofc_id colum to 'of_flows'" echo "ALTER TABLE of_flows ADD COLUMN ofc_id VARCHAR(36) NULL DEFAULT NULL AFTER net_id, @@ -599,7 +597,7 @@ function upgrade_to_15(){ } function downgrade_from_15(){ - echo " downgrade database from version 0.15 to version 0.14" + # echo " downgrade database from version 0.15 to version 0.14" echo " Delete ofc_id to 'of_flows'" echo "ALTER TABLE of_flows DROP COLUMN ofc_id, @@ -609,7 +607,7 @@ function downgrade_from_15(){ function upgrade_to_16(){ - echo " upgrade database from version 0.15 to version 0.16" + # echo " upgrade database from version 0.15 to version 0.16" echo " Add last_error and status colum to 'ofcs'" echo "ALTER TABLE ofcs ADD COLUMN last_error VARCHAR(255) NULL DEFAULT NULL AFTER password, @@ -618,14 +616,14 @@ function upgrade_to_16(){ } function downgrade_from_16(){ - echo " downgrade database from version 0.16 to version 0.15" + # echo " downgrade database from version 0.16 to version 0.15" echo " Delete last_error and status colum to 'ofcs'" echo "ALTER TABLE ofcs DROP COLUMN last_error, DROP COLUMN status; " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '16';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function upgrade_to_17(){ - echo " upgrade database from version 0.16 to version 0.17" + # echo " upgrade database from version 0.16 to version 0.17" echo " Add pci to the unique indexes switch_dpid_switch_port switch_dpid_switch_mac at of_port_mappings" echo "ALTER TABLE of_port_mappings DROP INDEX switch_dpid_switch_port, "\ "ADD UNIQUE INDEX switch_dpid_switch_port (switch_dpid, switch_port, pci);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -637,7 +635,7 @@ function upgrade_to_17(){ } function downgrade_from_17(){ - echo " downgrade database from version 0.17 to version 0.16" + # echo " downgrade database from version 0.17 to version 0.16" echo " Delete pci fromthe unique indexes switch_dpid_switch_port switch_dpid_switch_mac at of_port_mappings" echo "ALTER TABLE of_port_mappings DROP INDEX switch_dpid_switch_port, "\ "ADD UNIQUE INDEX switch_dpid_switch_port (switch_dpid, switch_port);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 @@ -650,13 +648,13 @@ function downgrade_from_17(){ #TODO ... put funtions here -echo "db version = "${DATABASE_VER_NUM} -[ $DATABASE_TARGET_VER_NUM -eq $DATABASE_VER_NUM ] && echo " current database version $DATABASE_VER is ok" +# echo "db version = "${DATABASE_VER_NUM} +[ $DB_VERSION -eq $DATABASE_VER_NUM ] && echo " current database version '$DATABASE_VER_NUM' is ok" #UPGRADE DATABASE step by step -while [ $DATABASE_TARGET_VER_NUM -gt $DATABASE_VER_NUM ] +while [ $DB_VERSION -gt $DATABASE_VER_NUM ] do + echo " upgrade database from version '$DATABASE_VER_NUM' to '$((DATABASE_VER_NUM+1))'" DATABASE_VER_NUM=$((DATABASE_VER_NUM+1)) - upgrade_to_${DATABASE_VER_NUM} #FILE_="${DIRNAME}/upgrade_to_${DATABASE_VER_NUM}.sh" #[ ! -x "$FILE_" ] && echo "Error, can not find script '$FILE_' to upgrade" >&2 && exit -1 @@ -664,8 +662,9 @@ do done #DOWNGRADE DATABASE step by step -while [ $DATABASE_TARGET_VER_NUM -lt $DATABASE_VER_NUM ] +while [ $DB_VERSION -lt $DATABASE_VER_NUM ] do + echo " downgrade database from version '$DATABASE_VER_NUM' to '$((DATABASE_VER_NUM-1))'" #FILE_="${DIRNAME}/downgrade_from_${DATABASE_VER_NUM}.sh" #[ ! -x "$FILE_" ] && echo "Error, can not find script '$FILE_' to downgrade" >&2 && exit -1 #$FILE_ || exit -1 # if fail return diff --git a/database_utils/vim_db_structure.sql b/database_utils/vim_db_structure.sql index fd4aeed..fdeaf45 100644 --- a/database_utils/vim_db_structure.sql +++ b/database_utils/vim_db_structure.sql @@ -1,5 +1,5 @@ /** -* Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. +* Copyright 2017 Telefónica Investigación y Desarrollo, S.A.U. * This file is part of openvim * All Rights Reserved. * @@ -19,11 +19,11 @@ * contact with: nfvlabs@tid.es **/ --- MySQL dump 10.13 Distrib 5.5.43, for debian-linux-gnu (x86_64) +-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64) -- --- Host: localhost Database: vim_db +-- Host: localhost Database: {{vim_db}} -- ------------------------------------------------------ --- Server version 5.5.43-0ubuntu0.14.04.1 +-- Server version 5.7.17-0ubuntu0.16.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -37,14 +37,14 @@ /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- --- Current Database: `vim_db` +-- Current Database: `{{vim_db}}` -- -/*!40000 DROP DATABASE IF EXISTS `vim_db`*/; +/*!40000 DROP DATABASE IF EXISTS `{{vim_db}}`*/; -CREATE DATABASE /*!32312 IF NOT EXISTS*/ `vim_db` /*!40100 DEFAULT CHARACTER SET utf8 */; +CREATE DATABASE /*!32312 IF NOT EXISTS*/ `{{vim_db}}` /*!40100 DEFAULT CHARACTER SET utf8 */; -USE `vim_db`; +USE `{{vim_db}}`; -- -- Table structure for table `flavors` @@ -55,7 +55,7 @@ DROP TABLE IF EXISTS `flavors`; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `flavors` ( `uuid` varchar(36) NOT NULL, - `name` varchar(64) NOT NULL, + `name` varchar(255) NOT NULL, `description` varchar(255) DEFAULT NULL, `disk` smallint(5) unsigned DEFAULT NULL, `ram` smallint(5) unsigned DEFAULT NULL, @@ -121,8 +121,9 @@ DROP TABLE IF EXISTS `images`; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `images` ( `uuid` varchar(36) NOT NULL, - `path` varchar(100) NOT NULL, - `name` varchar(64) NOT NULL, + `path` varchar(255) NOT NULL, + `name` varchar(255) NOT NULL, + `checksum` varchar(32) DEFAULT NULL, `description` varchar(255) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `modified_at` timestamp NULL DEFAULT NULL, @@ -155,7 +156,7 @@ CREATE TABLE `instance_devices` ( KEY `FK_instance_devices_images` (`image_id`), CONSTRAINT `FK_instance_devices_images` FOREIGN KEY (`image_id`) REFERENCES `tenants_images` (`image_id`), CONSTRAINT `FK_instance_devices_instances` FOREIGN KEY (`instance_id`) REFERENCES `instances` (`uuid`) ON DELETE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -208,7 +209,7 @@ CREATE TABLE `logs` ( `level` enum('panic','error','info','debug','verbose') NOT NULL, `description` varchar(200) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -235,6 +236,7 @@ CREATE TABLE `nets` ( `enable_dhcp` enum('true','false') NOT NULL DEFAULT 'false', `dhcp_first_ip` varchar(64) DEFAULT NULL, `dhcp_last_ip` varchar(64) DEFAULT NULL, + `gateway_ip` varchar(64) DEFAULT NULL, PRIMARY KEY (`uuid`), UNIQUE KEY `type_vlan` (`type`,`vlan`), UNIQUE KEY `physical` (`provider`), @@ -261,7 +263,7 @@ CREATE TABLE `numas` ( PRIMARY KEY (`id`), KEY `FK_numas_hosts` (`host_id`), CONSTRAINT `FK_numas_hosts` FOREIGN KEY (`host_id`) REFERENCES `hosts` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -275,6 +277,7 @@ CREATE TABLE `of_flows` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, `net_id` varchar(36) DEFAULT NULL, + `ofc_id` varchar(36) DEFAULT NULL, `priority` int(10) unsigned DEFAULT NULL, `vlan_id` smallint(5) unsigned DEFAULT NULL, `ingress_port` varchar(10) DEFAULT NULL, @@ -284,8 +287,34 @@ CREATE TABLE `of_flows` ( PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), KEY `FK_of_flows_nets` (`net_id`), - CONSTRAINT `FK_of_flows_nets` FOREIGN KEY (`net_id`) REFERENCES `nets` (`uuid`) ON DELETE SET NULL ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; + KEY `FK_of_flows_ofcs` (`ofc_id`), + CONSTRAINT `FK_of_flows_nets` FOREIGN KEY (`net_id`) REFERENCES `nets` (`uuid`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `FK_of_flows_ofcs` FOREIGN KEY (`ofc_id`) REFERENCES `ofcs` (`uuid`) ON DELETE SET NULL ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `of_port_mappings` +-- + +DROP TABLE IF EXISTS `of_port_mappings`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `of_port_mappings` ( + `uuid` varchar(36) NOT NULL, + `ofc_id` varchar(36) DEFAULT NULL, + `region` varchar(64) DEFAULT NULL, + `compute_node` varchar(64) DEFAULT NULL, + `pci` varchar(50) DEFAULT NULL, + `switch_dpid` varchar(64) DEFAULT NULL, + `switch_port` varchar(64) DEFAULT NULL, + `switch_mac` char(18) DEFAULT NULL, + UNIQUE KEY `region_compute_node_pci` (`region`,`compute_node`,`pci`), + UNIQUE KEY `switch_dpid_switch_port` (`switch_dpid`,`switch_port`,`pci`), + UNIQUE KEY `switch_dpid_switch_mac` (`switch_dpid`,`switch_mac`,`pci`), + KEY `FK_of_port_mappings_ofcs` (`ofc_id`), + CONSTRAINT `FK_of_port_mappings_ofcs` FOREIGN KEY (`ofc_id`) REFERENCES `ofcs` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -302,7 +331,31 @@ CREATE TABLE `of_ports_pci_correspondence` ( `switch_port` varchar(64) DEFAULT NULL, `switch_dpid` varchar(64) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `ofcs` +-- + +DROP TABLE IF EXISTS `ofcs`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `ofcs` ( + `uuid` varchar(36) NOT NULL, + `name` varchar(255) NOT NULL, + `dpid` varchar(64) NOT NULL, + `ip` varchar(64) NOT NULL, + `port` int(5) NOT NULL, + `type` varchar(64) NOT NULL, + `version` varchar(12) DEFAULT NULL, + `user` varchar(64) DEFAULT NULL, + `password` varchar(64) DEFAULT NULL, + `last_error` varchar(255) DEFAULT NULL, + `status` enum('ACTIVE','INACTIVE','ERROR') DEFAULT 'ACTIVE', + `nets_with_same_vlan` enum('true','false') NOT NULL DEFAULT 'false', + PRIMARY KEY (`uuid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -322,10 +375,12 @@ CREATE TABLE `ports` ( `Mbps` mediumint(8) unsigned DEFAULT NULL COMMENT 'In Mbits/s', `admin_state_up` enum('true','false') NOT NULL DEFAULT 'true', `status` enum('ACTIVE','DOWN','BUILD','ERROR') NOT NULL DEFAULT 'ACTIVE', - `type` enum('instance:bridge','instance:data','external') NOT NULL DEFAULT 'instance:bridge', + `type` enum('instance:bridge','instance:data','external','instance:ovs','controller:ovs') NOT NULL DEFAULT 'instance:bridge', `vlan` smallint(5) DEFAULT NULL COMMENT 'vlan of this SRIOV, or external port', `switch_port` varchar(64) DEFAULT NULL, + `switch_mac` varchar(18) DEFAULT NULL, `switch_dpid` varchar(64) DEFAULT NULL, + `ofc_id` varchar(36) DEFAULT NULL, `mac` char(18) DEFAULT NULL COMMENT 'mac address format XX:XX:XX:XX:XX:XX', `ip_address` varchar(64) DEFAULT NULL, `model` varchar(12) DEFAULT NULL COMMENT 'driver model for bridge ifaces; PF,VF,VFnotShared for data ifaces', @@ -334,7 +389,9 @@ CREATE TABLE `ports` ( KEY `FK_instance_ifaces_instances` (`instance_id`), KEY `FK_instance_ifaces_nets` (`net_id`), KEY `FK_ports_tenants` (`tenant_id`), + KEY `FK_port_ofc_id` (`ofc_id`), CONSTRAINT `FK_instance_ifaces_nets` FOREIGN KEY (`net_id`) REFERENCES `nets` (`uuid`), + CONSTRAINT `FK_port_ofc_id` FOREIGN KEY (`ofc_id`) REFERENCES `ofcs` (`uuid`), CONSTRAINT `FK_ports_instances` FOREIGN KEY (`instance_id`) REFERENCES `instances` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_ports_tenants` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Bridge interfaces used by instances'; @@ -361,7 +418,7 @@ CREATE TABLE `resources_core` ( KEY `FK_resources_core_numas` (`numa_id`), CONSTRAINT `FK_resources_core_instances` FOREIGN KEY (`instance_id`) REFERENCES `instances` (`uuid`), CONSTRAINT `FK_resources_core_numas` FOREIGN KEY (`numa_id`) REFERENCES `numas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contain an entry by thread (two entries per core) of all available cores. Threy will be free if instance_id is NULL'; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contain an entry by thread (two entries per core) of all available cores. Threy will be free if instance_id is NULL'; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -381,7 +438,7 @@ CREATE TABLE `resources_mem` ( KEY `FK_resources_mem_numas` (`numa_id`), CONSTRAINT `FK_resources_mem_instances` FOREIGN KEY (`instance_id`) REFERENCES `instances` (`uuid`) ON DELETE CASCADE, CONSTRAINT `FK_resources_mem_numas` FOREIGN KEY (`numa_id`) REFERENCES `numas` (`id`) ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Include the hugepages memory used by one instance (VM) in one host NUMA.'; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Include the hugepages memory used by one instance (VM) in one host NUMA.'; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -403,17 +460,21 @@ CREATE TABLE `resources_port` ( `status` enum('ok','error','notused') NOT NULL DEFAULT 'ok', `Mbps_used` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Speed bandwidth used when asigned', `switch_port` varchar(64) DEFAULT NULL, + `switch_mac` varchar(18) DEFAULT NULL, `switch_dpid` varchar(64) DEFAULT NULL, + `ofc_id` varchar(36) DEFAULT NULL, `mac` char(18) DEFAULT NULL COMMENT 'mac address format XX:XX:XX:XX:XX:XX', PRIMARY KEY (`id`), UNIQUE KEY `mac` (`mac`), UNIQUE KEY `port_id` (`port_id`), KEY `FK_resources_port_numas` (`numa_id`), KEY `FK_resources_port_instances` (`instance_id`), + KEY `FK_resource_ofc_id` (`ofc_id`), + CONSTRAINT `FK_resource_ofc_id` FOREIGN KEY (`ofc_id`) REFERENCES `ofcs` (`uuid`), CONSTRAINT `FK_resources_port_instances` FOREIGN KEY (`instance_id`) REFERENCES `instances` (`uuid`), CONSTRAINT `FK_resources_port_numas` FOREIGN KEY (`numa_id`) REFERENCES `numas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_resources_port_ports` FOREIGN KEY (`port_id`) REFERENCES `ports` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contain NIC ports SRIOV and availabes, and current use. Every port contain several entries, one per port (root_id=NULL) and all posible SRIOV (root_id=id of port)'; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contain NIC ports SRIOV and availabes, and current use. Every port contain several entries, one per port (root_id=NULL) and all posible SRIOV (root_id=id of port)'; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -467,7 +528,7 @@ CREATE TABLE `tenants_flavors` ( KEY `FK__flavors` (`flavor_id`), CONSTRAINT `FK__flavors` FOREIGN KEY (`flavor_id`) REFERENCES `flavors` (`uuid`), CONSTRAINT `FK__tenants` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -486,7 +547,7 @@ CREATE TABLE `tenants_images` ( KEY `FK_tenants_images_images` (`image_id`), CONSTRAINT `FK_tenants_images_images` FOREIGN KEY (`image_id`) REFERENCES `images` (`uuid`), CONSTRAINT `FK_tenants_images_tenants` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- @@ -500,13 +561,13 @@ CREATE TABLE `uuids` ( `uuid` varchar(36) NOT NULL, `root_uuid` varchar(36) DEFAULT NULL COMMENT 'Some related UUIDs can be grouped by this field, so that they can be deleted at once', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `used_at` enum('flavors','hosts','images','instances','nets','ports','tenants') DEFAULT NULL COMMENT 'Table that uses this UUID', + `used_at` varchar(64) DEFAULT NULL COMMENT 'Table that uses this UUID', PRIMARY KEY (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Used to avoid UUID repetitions'; /*!40101 SET character_set_client = @saved_cs_client */; -- --- Dumping routines for database 'vim_db' +-- Dumping routines for database '{{vim_db}}' -- /*!50003 DROP PROCEDURE IF EXISTS `GetAllAvailablePorts` */; /*!50003 SET @saved_cs_client = @@character_set_client */ ; @@ -917,17 +978,17 @@ DELIMITER ; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2016-05-13 12:52:19 +-- Dump completed on 2017-04-28 14:12:17 --- MySQL dump 10.13 Distrib 5.5.43, for debian-linux-gnu (x86_64) +-- MySQL dump 10.13 Distrib 5.7.17, for Linux (x86_64) -- --- Host: localhost Database: vim_db +-- Host: localhost Database: {{vim_db}} -- ------------------------------------------------------ --- Server version 5.5.43-0ubuntu0.14.04.1 +-- Server version 5.7.17-0ubuntu0.16.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; @@ -946,7 +1007,7 @@ DELIMITER ; LOCK TABLES `schema_version` WRITE; /*!40000 ALTER TABLE `schema_version` DISABLE KEYS */; -INSERT INTO `schema_version` VALUES (1,'0.1','0.2.00','insert schema_version; alter nets with last_error column','2015-05-05'),(2,'0.2','0.2.03','update Procedure UpdateSwitchPort','2015-05-06'),(3,'0.3','0.2.5','New Procedure GetAllAvailablePorts','2015-07-09'),(4,'0.4','0.3.1','Remove unique index VLAN at resources_port','2015-09-04'),(5,'0.5','0.4.1','Add ip_address to ports','2015-09-04'),(6,'0.6','0.4.2','Enlarging name at database','2016-02-01'),(7,'0.7','0.4.4','Add bind_net to net table','2016-02-12'); +INSERT INTO `schema_version` VALUES (1,'0.1','0.2.00','insert schema_version; alter nets with last_error column','2015-05-05'),(2,'0.2','0.2.03','update Procedure UpdateSwitchPort','2015-05-06'),(3,'0.3','0.2.5','New Procedure GetAllAvailablePorts','2015-07-09'),(4,'0.4','0.3.1','Remove unique index VLAN at resources_port','2015-09-04'),(5,'0.5','0.4.1','Add ip_address to ports','2015-09-04'),(6,'0.6','0.4.2','Enlarging name at database','2016-02-01'),(7,'0.7','0.4.4','Add bind_net to net table','2016-02-12'),(8,'0.8','0.4.10','add column checksum to images','2016-09-30'),(9,'0.9','0.5.1','increase length of columns path and name to 255 in table images, and change length of column name to 255 in table flavors','2017-01-10'),(10,'0.10','0.5.2','change ports type, adding instance:ovs','2017-02-01'),(11,'0.11','0.5.4','Add gateway_ip colum to nets','2017-02-13'),(12,'0.12','0.5.5','Add of_controller table','2017-02-17'),(13,'0.13','0.5.6','Add of_port_mapings table','2017-03-09'),(14,'0.14','0.5.7','Add switch_mac, ofc_id colum to ports and resources_port tables','2017-03-09'),(15,'0.15','0.5.8','Add ofc_id colum to of_flows','2017-03-15'),(16,'0.16','0.5.9','Add last_error and status colum to ofcs','2017-03-17'),(17,'0.17','0.5.10','Add pci to unique index dpid port/mac at of_port_mappings','2017-04-05'); /*!40000 ALTER TABLE `schema_version` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; @@ -959,4 +1020,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2016-05-13 12:52:19 +-- Dump completed on 2017-04-28 14:12:17 diff --git a/osm_openvim/ovim.py b/osm_openvim/ovim.py index a0c380d..e7a8b49 100755 --- a/osm_openvim/ovim.py +++ b/osm_openvim/ovim.py @@ -31,6 +31,7 @@ import threading import vim_db import logging # import imp +import os.path import argparse from netaddr import IPNetwork from jsonschema import validate as js_v, exceptions as js_e @@ -41,9 +42,9 @@ import openflow_conn __author__ = "Alfonso Tierno, Leonardo Mirabal" __date__ = "$06-Feb-2017 12:07:15$" -__version__ = "0.5.11-r527" -version_date = "Apr 2017" -database_version = "0.17" #expected database schema version +__version__ = "0.5.12-r528" +version_date = "May 2017" +database_version = 17 #needed database schema version HTTP_Bad_Request = 400 HTTP_Unauthorized = 401 @@ -171,12 +172,20 @@ class ovim(): # if self.running_info: # return #TODO service can be checked and rebuild broken threads r = self.db.get_db_version() + db_path = __file__ + db_path = db_path[:db_path.rfind("/")] + if os.path.exists(db_path + "/database_utils/migrate_vim_db.sh"): + db_path += "/database_utils" + else: + db_path += "/../database_utils" + if r[0] < 0: - raise ovimException("DATABASE is not a VIM one or it is a '0.0' version. Try to upgrade to version '{}' with "\ - "'./database_utils/migrate_vim_db.sh'".format(database_version) ) - elif r[1] != database_version: - raise ovimException("DATABASE wrong version '{}'. Try to upgrade/downgrade to version '{}' with "\ - "'./database_utils/migrate_vim_db.sh'".format(r[1], database_version) ) + raise ovimException("DATABASE is not valid. If you think it is corrupted, you can init it with" + " '{db_path}/init_vim_db.sh' script".format(db_path=db_path)) + elif r[0] != database_version: + raise ovimException("DATABASE wrong version '{current}'. Try to upgrade/downgrade to version '{target}'" + " with '{db_path}/migrate_vim_db.sh {target}'".format( + current=r[0], target=database_version, db_path=db_path)) self.logger.critical("Starting ovim server version: '{} {}' database version '{}'".format( self.get_version(), self.get_version_date(), self.get_database_version())) # create database connection for openflow threads