Browse Source

[3599] kea-admin, mysql 1.0 to 2.0 upgrade added.

Tomek Mrugalski 10 years ago
parent
commit
2dda984636
2 changed files with 321 additions and 0 deletions
  1. 299 0
      src/bin/admin/kea-admin.in
  2. 22 0
      src/bin/admin/upgrade-scripts/mysql/1.0_to_2.0.sh

+ 299 - 0
src/bin/admin/kea-admin.in

@@ -0,0 +1,299 @@
+#!/bin/sh
+
+# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# This is kea-admin script that conducts administrative tasks on the Kea
+# installation. Currently supported operations are:
+#
+# - database version check
+# - database version upgrade
+
+
+# Get the location of the kea-admin scripts
+SCRIPTS_DIR_DEFAULT=@datarootdir@/@PACKAGE@/upgrade-scripts
+scripts_dir=${SCRIPTS_DIR_DEFAULT}
+
+# These are the default parameters. They will likely not work in any
+# specific deployment.
+db_user="keatest"
+db_password="keatest"
+db_name="keatest"
+
+usage() {
+    printf "kea-admin @PACKAGE_VERSION@\n"
+    printf "\n"
+    printf "This is a kea-admin script that conducts administrative tasks on\n"
+    printf "the Kea installation.\n"
+    printf "\n"
+    printf "Usage: $0 COMMAND BACKEND [parameters]\n"
+    printf "\n"
+    printf "COMMAND: Currently supported operations are:\n"
+    printf "\n"
+    printf " - init: Initalizes new database. Useful for first time installation.\n"
+    printf " - version: Checks version of the existing database scheme. Useful\n"
+    printf " -          for checking DB version when preparing for an upgrade.\n"
+    printf " - upgrade: Upgrades your database scheme\n"
+    printf "\n"
+    printf "BACKEND - one of the supported backends: memfile|mysql|pgsql\n"
+    printf "\n"
+    printf "PARAMETERS: Parameters are optional in general, but may be required\n"
+    printf "            for specific operation.\n"
+    printf " -u or --user name - specifies username when connecting to a database\n"
+    printf " -p or --password pass - specifies a password when connecting to a database\n"
+    printf " -n or --name database - specifies a database name to connect to\n"
+    printf " -d or --directory - path to upgrade scripts (default: ${SCRIPTS_DIR_DEFAULT})\n"
+}
+
+
+### Logging functions ###
+
+# Logs message at the error level.
+log_error() {
+    printf "ERROR/kea-admin: ${1}\n"
+}
+
+# Logs message at the warning level.
+log_warning() {
+    printf "WARNING/kea-admin: ${1}\n"
+}
+
+# Logs message at the info level.
+log_info() {
+    printf "INFO/kea-admin: ${1}\n"
+}
+
+### Convenience functions ###
+
+# Checks if the value is in the list. An example usage of this function
+# is to determine whether the kea-admin command belongs to the list of
+# supported commands.
+is_in_list() {
+    local member=${1}  # Value to be checked
+    local list="${2}"  # Comma separated list of items
+    _inlist=0          # Return value: 0 if not in list, 1 otherwise.
+    if [ -z ${member} ]; then
+        log_error "missing ${class}"
+    fi
+    # Iterate over all items on the list and compare with the member.
+    # If they match, return, otherwise log error and exit.
+    for item in ${list}
+    do
+        if [ ${item} = ${member} ]; then
+            _inlist=1
+            return
+        fi
+    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() {
+    log_error "NOT IMPLEMENTED"
+    exit 1
+}
+
+init_mysql() {
+    log_error "NOT IMPLEMENTED"
+    exit 1
+}
+
+init_pgsql() {
+    log_error "NOT IMPLEMENTED"
+    exit 1
+}
+
+### Functions that implement database version checking commands
+version_memfile() {
+    log_error "NOT IMPLEMENTED"
+    exit 1
+}
+
+version_mysql() {
+    MYSQL_VERSION=$(mysql_execute "SELECT version,minor FROM schema_version")
+    echo $MYSQL_VERSION
+}
+
+version_pgsql() {
+    log_error "NOT IMPLEMENTED"
+    exit 1
+}
+
+### Functions used for upgrade
+upgrade_memfile() {
+    log_error "NOT IMPLEMENTED"
+    exit 1
+}
+
+upgrade_mysql() {
+
+    printf "Version reported before upgrade: "
+    version_mysql
+
+    # Check if the scripts directory exists at all.
+    if [ ! -d ${scripts_dir}/mysql ]; then
+        log_error "Invalid scripts directory: ${scripts_dir}/mysql"
+        exit 1
+    fi
+
+    # Check if there are any files in it
+    num_files=`ls -1 ${scripts_dir}/mysql | wc -l &> /dev/null`
+    if [ $num_files -eq 0 ]; then
+        log_error "No scripts in ${scripts_dir}/mysql or the directory is not readable"
+        exit 1
+    fi
+
+    for script in ${scripts_dir}/mysql/*
+    do
+        echo "Processing $script file..."
+        sh ${script} --user=${db_user} --password=${db_password} ${db_name}
+    done
+
+    printf "Version reported after upgrade: "
+    version_mysql
+}
+
+upgrade_pgsql() {
+    log_error "NOT IMPLEMENTED"
+}
+
+
+### Script starts here ###
+
+command=${1}
+if [ -z ${command} ]; then
+    log_error "missing command"
+    usage
+    exit 1
+fi
+is_in_list "${command}" "init version upgrade"
+if [ ${_inlist} -eq 0 ]; then
+    log_error "invalid command: ${command}"
+    exit 1
+fi
+shift
+
+backend=${1}
+if [ -z ${backend} ]; then
+    log_error "missing backend"
+    usage
+    exit 1
+fi
+is_in_list "${backend}" "memfile mysql pgsql"
+if [ ${_inlist} -eq 0 ]; then
+    log_error "invalid backend: ${backend}"
+    exit 1
+fi
+shift
+
+# Ok, let's process parameters (if there are any)
+while [ ! -z "${1}" ]
+do
+    option=${1}
+    case ${option} in
+        # Specify database user
+        -u|--user)
+            shift
+            db_user=${1}
+            if [ -z ${db_user} ]; then
+                log_error "-u or --user requires a parameter"
+                usage
+                exit 1
+            fi
+            ;;
+        # Specify database password
+        -p|--password)
+            shift
+            db_pass=${1}
+            if [ -z ${db_pass} ]; then
+                log_error "-p or --password requires a parameter"
+                usage
+                exit 1
+            fi
+            ;;
+        # Specify database name
+        -n|--name)
+            shift
+            db_name=${1}
+            if [ -z ${db_name} ]; then
+                log_error "-n or --name requires a parameter"
+                usage
+                exit 1
+            fi
+            ;;
+        -d|--directory)
+            shift
+            scripts_dir=${1}
+            if [ -z ${scripts_dir} ]; then
+                log_error "-d or --directory requires a parameter"
+                usage
+                exit 1
+            fi
+            ;;
+        *)
+            log_error "invalid option: ${option}"
+            usage
+            exit 1
+    esac
+    shift
+done
+
+case ${command} in
+    # Initialize the database
+    init)
+        case ${backend} in
+            memfile)
+                init_memfile
+                ;;
+            mysql)
+                init_mysql
+                ;;
+            pgsql)
+                init_pgsql
+                ;;
+            esac
+        ;;
+    version)
+        case ${backend} in
+            memfile)
+                version_memfile
+                ;;
+            mysql)
+                version_mysql
+                ;;
+            pgsql)
+                version_pgsql
+                ;;
+            esac
+        ;;
+    upgrade)
+        case ${backend} in
+            memfile)
+                upgrade_memfile
+                ;;
+            mysql)
+                upgrade_mysql
+                ;;
+            pgsql)
+                upgrade_pgsql
+                ;;
+            esac
+        ;;
+esac
+
+exit 0

+ 22 - 0
src/bin/admin/upgrade-scripts/mysql/1.0_to_2.0.sh

@@ -0,0 +1,22 @@
+mysql "$@" <<EOF
+ALTER TABLE lease6
+    ADD COLUMN hwaddr varbinary(2),
+    ADD COLUMN hwtype smallint unsigned,
+    ADD COLUMN hwaddr_source int unsigned;
+
+CREATE TABLE lease6_hwaddr_source (
+    hwaddr_source INT PRIMARY KEY NOT NULL,
+    name VARCHAR(40)
+);
+
+-- See src/lib/dhcp/dhcp/pkt.h for detailed explanation
+INSERT INTO lease6_hwaddr_source VALUES (1, "HWADDR_SOURCE_RAW");
+INSERT INTO lease6_hwaddr_source VALUES (2, "HWADDR_SOURCE_IPV6_LINK_LOCAL");
+INSERT INTO lease6_hwaddr_source VALUES (4, "HWADDR_SOURCE_DUID");
+INSERT INTO lease6_hwaddr_source VALUES (8, "HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION");
+INSERT INTO lease6_hwaddr_source VALUES (16, "HWADDR_SOURCE_REMOTE_ID");
+INSERT INTO lease6_hwaddr_source VALUES (32, "HWADDR_SOURCE_SUBSCRIBER_ID");
+INSERT INTO lease6_hwaddr_source VALUES (64, "HWADDR_SOURCE_DOCSIS");
+
+UPDATE schema_version SET version="2", minor="0";
+EOF