Commit cf22f8d2 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[master] Merge branch 'trac3599' (kea-admin)

parents 23fca8ea 4daec4cd
......@@ -1393,6 +1393,15 @@ AC_CONFIG_FILES([compatcheck/Makefile
src/bin/perfdhcp/Makefile
src/bin/perfdhcp/tests/Makefile
src/bin/perfdhcp/tests/testdata/Makefile
src/bin/admin/Makefile
src/bin/admin/kea-admin
src/bin/admin/scripts/Makefile
src/bin/admin/tests/Makefile
src/bin/admin/tests/memfile_tests.sh
src/bin/admin/tests/mysql_tests.sh
src/bin/admin/scripts/mysql/Makefile
src/bin/admin/scripts/mysql/upgrade_1.0_to_2.0.sh
src/bin/admin/scripts/pgsql/Makefile
src/hooks/Makefile
src/hooks/dhcp/Makefile
src/hooks/dhcp/user_chk/Makefile
......@@ -1465,6 +1474,7 @@ AC_CONFIG_FILES([compatcheck/Makefile
chmod +x src/bin/dhcp6/tests/dhcp6_process_tests.sh
chmod +x src/bin/keactrl/keactrl
chmod +x src/bin/keactrl/tests/keactrl_tests.sh
chmod +x src/bin/admin/kea-admin
chmod +x src/lib/dns/gen-rdatacode.py
chmod +x src/lib/log/tests/console_test.sh
chmod +x src/lib/log/tests/destination_test.sh
......
# The following build order must be maintained.
SUBDIRS = dhcp4 dhcp6 d2 perfdhcp
SUBDIRS = dhcp4 dhcp6 d2 perfdhcp admin
if CONFIG_BACKEND_JSON
SUBDIRS += keactrl
......
/kea-admin
/kea-admin.8
SUBDIRS = . scripts tests
# Install kea-admin in sbin.
sbin_SCRIPTS = kea-admin
man_MANS = kea-admin.8
DISTCLEANFILES = $(man_MANS)
EXTRA_DIST = $(man_MANS) kea-admin.xml admin-utils.sh
if GENERATE_DOCS
kea-admin.8: kea-admin.xml
@XSLTPROC@ --novalid --xinclude --nonet -o $@ http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $(builddir)/kea-admin.xml
else
$(man_MANS):
@echo Man generation disabled. Creating dummy $@. Configure with --enable-generate-docs to enable it.
@echo Man generation disabled. Remove this file, configure with --enable-generate-docs, and rebuild Kea > $@
endif
adminscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts
adminscripts_DATA = admin-utils.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 an utility script that is being included by other scripts.
# 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.
#
# @todo: Catch mysql return code. I tried to use PIPESTATUS[X], but it doesn't
# seem to work (or at least I don't know how to use it).
mysql_execute() {
if [ $# -gt 1 ]; then
QUERY=$1
shift
_RESULT=`echo $QUERY | mysql -N -B $@`
else
_RESULT=$(mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name)
fi
}
mysql_version() {
mysql_execute "SELECT CONCAT(version,\".\",minor) FROM schema_version" "$@"
}
mysql_version_print() {
mysql_version "$@"
printf "%s" $_RESULT
}
#!/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:
#
# - lease database init
# - lease database version check
# - lease database version upgrade
# Get the location of the kea-admin scripts
prefix=@prefix@
SCRIPTS_DIR_DEFAULT=@datarootdir@/@PACKAGE@/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"
# Include utilities. Use installed version if available and
# use build version if it isn't.
if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
. @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
else
. @abs_top_builddir@/src/bin/admin/admin-utils.sh
fi
# Prints out usage version.
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 " - lease-init: Initalizes new lease database. Useful for first time installation.\n"
printf " - lease-version: Checks version of the existing lease database scheme. Useful\n"
printf " - for checking lease DB version when preparing for an upgrade.\n"
printf " - lease-upgrade: Upgrades your lease 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.
# Takes one parameter that is printed as is.
log_error() {
printf "ERROR/kea-admin: ${1}\n"
}
# Logs message at the warning level.
# Takes one parameter that is printed as is.
log_warning() {
printf "WARNING/kea-admin: ${1}\n"
}
# Logs message at the info level.
# Takes one parameter that is printed as is.
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
}
### Functions that implement database initialization commands
memfile_init() {
# @todo Implement this as part of #3601
log_error "NOT IMPLEMENTED"
exit 1
}
# Initializes a new, empty MySQL database.
# It essentially calls scripts/mysql/dhcpdb_create.mysql script, with
# some extra sanity checks. It will refuse to use it if there are any
# existing tables. It's better safe than sorry.
mysql_init() {
printf "Checking if there is a database initialized already. Please ignore errors.\n"
# Let's try to count the number of tables. Anything above 0 means that there
# is some database in place. If there is anything, we abort. Note that
# mysql may spit out connection or access errors to stderr, we ignore those.
# We should not hide them as they may give hints to user what is wrong with
# his setup.
#
mysql_execute "SHOW TABLES;"
COUNT=`echo $_RESULT | wc -w`
if [ $COUNT -gt 0 ]; then
# Let't start with a new line. mysql could have printed something out.
printf "\n"
log_error "Expected empty database $db_name, but there are $COUNT tables: \n$_RESULT. Aborting."
exit 1
fi
printf "Initializing database using script %s\n" $scripts_dir/mysql/dhcpdb_create.mysql
mysql -B --user=$db_user --password=$db_password $db_name < $scripts_dir/mysql/dhcpdb_create.mysql
ERRCODE=$?
printf "mysql returned status code $ERRCODE\n"
if [ "$ERRCODE" -eq 0 ]; then
printf "Lease DB version reported after initialization: "
mysql_version_print
printf "\n"
fi
exit $ERRCODE
}
pgsql_init() {
log_error "NOT IMPLEMENTED"
exit 1
}
### Functions that implement database version checking commands
memfile_version() {
# @todo Implement this as part of #3601
log_error "NOT IMPLEMENTED"
exit 1
}
pgsql_version() {
log_error "NOT IMPLEMENTED"
exit 1
}
### Functions used for upgrade
memfile_upgrade() {
# @todo Implement this as part of #3601
log_error "NOT IMPLEMENTED"
exit 1
}
# Upgrades existing MySQL database installation. The idea is that
# it will go over all upgrade scripts from (prefix)/share/kea/scripts/mysql
# and run them one by one. They will be named properly, so they will
# be run in order.
#
# This function prints version before and after upgrade.
mysql_upgrade() {
printf "Lease DB version reported before upgrade: "
mysql_version_print
printf "\n"
# 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=$(find ${scripts_dir}/mysql/upgrade*.sh -type f | wc -l)
if [ $num_files -eq 0 ]; then
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/upgrade*.sh
do
echo "Processing $script file..."
sh ${script} --user=${db_user} --password=${db_password} ${db_name}
done
printf "Lease DB version reported after upgrade: "
mysql_version_print
printf "\n"
}
pgsql_upgrade() {
log_error "NOT IMPLEMENTED"
}
### Script starts here ###
# First, find what the command is
command=${1}
if [ -z ${command} ]; then
log_error "missing command"
usage
exit 1
fi
is_in_list "${command}" "lease-init lease-version lease-upgrade"
if [ ${_inlist} -eq 0 ]; then
log_error "invalid command: ${command}"
exit 1
fi
shift
# Second, check what's the backend
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
lease-init)
case ${backend} in
memfile)
memfile_init
;;
mysql)
mysql_init
;;
pgsql)
pgsql_init
;;
esac
;;
lease-version)
case ${backend} in
memfile)
memfile_version
;;
mysql)
mysql_version_print
printf "\n"
;;
pgsql)
pgsql_version
;;
esac
;;
lease-upgrade)
case ${backend} in
memfile)
memfile_upgrade
;;
mysql)
mysql_upgrade
;;
pgsql)
pgsql_upgrade
;;
esac
;;
esac
exit 0
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
[<!ENTITY mdash "&#8212;">]>
<!--
- 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.
-->
<refentry>
<refentryinfo>
<date>Nov 5, 2014</date>
</refentryinfo>
<refmeta>
<refentrytitle>kea-admin</refentrytitle>
<manvolnum>8</manvolnum>
<refmiscinfo>Kea</refmiscinfo>
</refmeta>
<refnamediv>
<refname>kea-admin</refname>
<refpurpose>Shell script for managing Kea databases</refpurpose>
</refnamediv>
<docinfo>
<copyright>
<year>2014</year>
<holder>Internet Systems Consortium, Inc. ("ISC")</holder>
</copyright>
</docinfo>
<refsynopsisdiv>
<cmdsynopsis>
<command>kea-admin</command>
<arg>command</arg>
<arg>backend</arg>
<arg><option>-u <replaceable class="parameter">database username</replaceable></option></arg>
<arg><option>-p <replaceable class="parameter">database password</replaceable></option></arg>
<arg><option>-n <replaceable class="parameter">database name</replaceable></option></arg>
<arg><option>-d <replaceable class="parameter">scripts directory</replaceable></option></arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
<title>DESCRIPTION</title>
<para>
kea-admin is a shell script which offers database maintenance. In particular,
it features database initialization, database version checking and
database schema upgrade.
</para>
</refsect1>
<refsect1>
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term><option><replaceable class="parameter">command</replaceable></option></term>
<listitem>
<para>
Command to be issued to the servers. It can be one of the following:
</para>
<variablelist>
<varlistentry>
<term>lease-init</term>
<listitem>
<para>Initializes a new lease database. Useful during first Kea
installation. The database is initialized to the latest version
supported by the version of the software.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>lease-version</term>
<listitem>
<para>Reports lease database version. This is not necessarily
equal to Kea version as each backend has its own versioning
scheme.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>lease-upgrade</term>
<listitem>
<para>Conducts lease database upgrade. This is useful when
migrating between old and new Kea versions.</para>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
<varlistentry>
<term><option><replaceable class="parameter">backend</replaceable></option></term>
<listitem>
<para>
Specifies backend type. Currently allowed backends are: memfile, mysql and
pgsql.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-u|--user <replaceable class="parameter">username</replaceable></option></term>
<listitem>
<para>
Specify the username when connecting to a database. If not specified,
the default value of keatest is used.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-p|--password <replaceable class="parameter">password</replaceable></option></term>
<listitem>
<para>
Specifies the password when connecting to a database. If not specified,
the default value of keatest is used.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-n|--name <replaceable class="parameter">database-name</replaceable></option></term>
<listitem>
<para>
Specifies the database name to connect to. If not specified,
the default value of keatest is used.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-d|--directory <replaceable class="parameter">script-directory</replaceable></option></term>
<listitem>
<para>
Used for override scripts directory. That script is used during upgrades,
database initialization and possibly other operations. If not specified,
the default value of (prefix)/share/kea/scripts/ is used.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>SEE ALSO</title>
<para>
<citerefentry>
<refentrytitle>keactrl</refentrytitle>
<manvolnum>8</manvolnum>
</citerefentry>
<citerefentry>
<refentrytitle>kea-dhcp4</refentrytitle>
<manvolnum>8</manvolnum>
</citerefentry>
<citerefentry>
<refentrytitle>kea-dhcp6