Browse Source

[3599] Assortment of changes:

 - admin-util.sh scripts added.
 - upgrade_1.0_to_2.0.sh updated with version check.
 - kea-admin.in updated.
Tomek Mrugalski 10 years ago
parent
commit
4ff9b4c1ec

+ 27 - 0
src/bin/admin/admin-utils.sh

@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# There are two ways of calling this method.
+# mysql_execute SQL_QUERY - This call is simpler, but requires db_user,
+#     db_password and db_name variables to be bet.
+# mysql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters
+#     may be specified. They are passed directly to mysql. This one is
+#     more convenient to use if the script didn't parse db_user db_password
+#     and db_name.
+mysql_execute() {
+    if [ $# -gt 1 ]; then
+        QUERY=$1
+        shift
+        _RESULT=`echo $QUERY | mysql -N -B $@ | sed "s/\t/./g"`
+    else
+        _RESULT=`mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name | sed "s/\t/./g"`
+    fi
+}
+
+mysql_version() {
+    mysql_execute "SELECT version,minor FROM schema_version" "$@"
+}
+
+mysql_version_print() {
+    mysql_version "$@"
+    printf "%s" $_RESULT
+}

+ 26 - 31
src/bin/admin/kea-admin.in

@@ -31,6 +31,8 @@ db_user="keatest"
 db_password="keatest"
 db_name="keatest"
 
+. ./admin-utils.sh
+
 usage() {
     printf "kea-admin @PACKAGE_VERSION@\n"
     printf "\n"
@@ -97,53 +99,45 @@ is_in_list() {
     done
 }
 
-mysql_execute() {
-    RESULT=`mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name | sed "s/\t/./g"`
-    printf ${RESULT}
-}
 
 ### Functions that implement database initialization commands
-init_memfile() {
+memfile_init() {
     log_error "NOT IMPLEMENTED"
     exit 1
 }
 
-init_mysql() {
+mysql_init() {
     log_error "NOT IMPLEMENTED"
     exit 1
 }
 
-init_pgsql() {
+pgsql_init() {
     log_error "NOT IMPLEMENTED"
     exit 1
 }
 
 ### Functions that implement database version checking commands
-version_memfile() {
+memfile_version() {
     log_error "NOT IMPLEMENTED"
     exit 1
 }
 
-version_mysql() {
-    MYSQL_VERSION=$(mysql_execute "SELECT version,minor FROM schema_version")
-    echo $MYSQL_VERSION
-}
-
-version_pgsql() {
+pgsql_version() {
     log_error "NOT IMPLEMENTED"
     exit 1
 }
 
 ### Functions used for upgrade
-upgrade_memfile() {
+memfile_upgrade() {
     log_error "NOT IMPLEMENTED"
     exit 1
 }
 
-upgrade_mysql() {
+mysql_upgrade() {
 
     printf "Version reported before upgrade: "
-    version_mysql
+    mysql_version_print
+    printf "\n"
 
     # Check if the scripts directory exists at all.
     if [ ! -d ${scripts_dir}/mysql ]; then
@@ -152,23 +146,24 @@ upgrade_mysql() {
     fi
 
     # Check if there are any files in it
-    num_files=`ls -1 ${scripts_dir}/mysql | wc -l &> /dev/null`
+    num_files=`ls -1 ${scripts_dir}/mysql/upgrade*.sh | wc -l &> /dev/null`
     if [ $num_files -eq 0 ]; then
-        log_error "No scripts in ${scripts_dir}/mysql or the directory is not readable"
+        log_error "No scripts in ${scripts_dir}/mysql or the directory is not readable or does not have any upgrade* scripts."
         exit 1
     fi
 
-    for script in ${scripts_dir}/mysql/*
+    for script in ${scripts_dir}/mysql/upgrade*.sh
     do
         echo "Processing $script file..."
         sh ${script} --user=${db_user} --password=${db_password} ${db_name}
     done
 
     printf "Version reported after upgrade: "
-    version_mysql
+    mysql_version_print
+    printf "\n"
 }
 
-upgrade_pgsql() {
+pgsql_upgrade() {
     log_error "NOT IMPLEMENTED"
 }
 
@@ -258,39 +253,39 @@ case ${command} in
     init)
         case ${backend} in
             memfile)
-                init_memfile
+                memfile_init
                 ;;
             mysql)
-                init_mysql
+                mysql_init
                 ;;
             pgsql)
-                init_pgsql
+                pgsql_init
                 ;;
             esac
         ;;
     version)
         case ${backend} in
             memfile)
-                version_memfile
+                memfile_version
                 ;;
             mysql)
-                version_mysql
+                mysql_version
                 ;;
             pgsql)
-                version_pgsql
+                pgsql_version
                 ;;
             esac
         ;;
     upgrade)
         case ${backend} in
             memfile)
-                upgrade_memfile
+                memfile_upgrade
                 ;;
             mysql)
-                upgrade_mysql
+                mysql_upgrade
                 ;;
             pgsql)
-                upgrade_pgsql
+                pgsql_upgrade
                 ;;
             esac
         ;;

+ 17 - 0
src/bin/admin/scripts/mysql/upgrade_1.0_to_2.0.sh

@@ -1,3 +1,16 @@
+#!/bin/sh
+
+# Import common library.
+. /home/thomson/devel/kea/src/bin/admin/admin-utils.sh
+
+mysql_version "$@"
+VERSION=$_RESULT
+
+if [ "$VERSION" != "1.0" ]; then
+    printf "This script upgrades 1.0 to 2.0. Reported version is $VERSION. Skipping upgrade.\n"
+    exit 0
+fi
+
 mysql "$@" <<EOF
 ALTER TABLE lease6
     ADD COLUMN hwaddr varbinary(2),
@@ -20,3 +33,7 @@ INSERT INTO lease6_hwaddr_source VALUES (64, "HWADDR_SOURCE_DOCSIS");
 
 UPDATE schema_version SET version="2", minor="0";
 EOF
+
+RESULT=$?
+
+exit $?