Commit f60a344a authored by Francis Dupont's avatar Francis Dupont

[#1196] Checkpoint: todo tests

parent 4f2457a9
..
Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
Copyright (C) 2019-2020 Internet Systems Consortium, Inc. ("ISC")
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
......@@ -43,13 +43,16 @@ Arguments
**db-upgrade**
Conducts a database schema upgrade. This is useful when upgrading Kea.
**db-dump**
**lease-dump**
Dumps the contents of the lease database (for MySQL, PostgreSQL,
or CQL backends) to a CSV (comma-separated values) text file.
The first line of the file contains the column names. This is meant
to be used as a diagnostic tool, so it provides a portable,
human-readable form of the lease data.
**lease-recount**
Recounts leases for MySQL or PostgreSQL database.
``backend``
Specifies the backend type. Currently allowed backends are: memfile,
mysql, and pgsql.
......
#!/bin/sh
# Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
# Copyright (C) 2014-2020 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
......@@ -150,3 +150,31 @@ cql_version() {
echo "$version"
return $error
}
recount4_query() {
# recount IPv4 leases from scratch
query="
START TRANSACTION;
DELETE FROM lease4_stat;
INSERT INTO lease4_stat (subnet_id, state, leases)
SELECT subnet_id, state, COUNT(*)
FROM lease4 WHERE state = 0 OR state = 1
GROUP BY subnet_id, state;
COMMIT;
"
return $query
}
recount6_query() {
# recount IPv6 leases from scratch
query="
START TRANSACTION;
DELETE FROM lease6_stat;
INSERT INTO lease6_stat (subnet_id, lease_type, state, leases)
SELECT subnet_id, lease_type, state, COUNT(*)
FROM lease6 WHERE state = 0 OR state = 1
GROUP BY subnet_id, lease_type, state;
COMMIT;
"
return $query
}
#!/bin/sh
# Copyright (C) 2014-2019 Internet Systems Consortium, Inc. ("ISC")
# Copyright (C) 2014-2020 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
......@@ -13,6 +13,7 @@
# - database version check
# - database version upgrade
# - lease database dump
# - lease database recount
# Get the location of the kea-admin scripts
......@@ -59,6 +60,7 @@ usage() {
printf " - for checking databaseB version when preparing for an upgrade.\n"
printf " - db-upgrade: Upgrades your database scheme\n"
printf " - lease-dump: Dump current leases to a CSV file\n"
printf " - lease-recount: Recount leases.
printf "\n"
printf "BACKEND - one of the supported backends: memfile|mysql|pgsql|cql\n"
printf "\n"
......@@ -622,6 +624,51 @@ cql_dump() {
exit 0
}
### Functions used for recount
mysql_recount() {
printf "Recount leases from database\n"
QUERY4=recount4_query()
RESULT=$(mysql_execute "$QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
log_error "mysql failed to recount IPv4 leases, mysql status = $ERRCODE"
exit 1
fi
QUERY6=recount6_query()
RESULT=$(mysql_execute "$QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
log_error "mysql failed to recount IPv6 leases, mysql status = $ERRCODE"
exit 1
fi
}
pgsql_recount() {
printf "Recount leases from database\n"
QUERY4=recount4_query()
RESULT=$(pgsql_execute "$QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
log_error "pgsql failed to recount IPv4 leases, pgsql status = $ERRCODE"
exit 1
fi
QUERY6=recount6_query()
RESULT=$(pgsql_execute "$QUERY")
ERRCODE=$?
if [ $ERRCODE -ne 0 ]
then
log_error "pgsql failed to recount IPv6 leases, pgsql status = $ERRCODE"
exit 1
fi
}
### Script starts here ###
# First, find what the command is
......@@ -638,7 +685,7 @@ if test "${command}" = "-v" || test "${command}" = "--version" ; then
exit 0
fi
is_in_list "${command}" "db-init db-version db-upgrade lease-dump"
is_in_list "${command}" "db-init db-version db-upgrade lease-dump lease-recount"
if [ ${_inlist} -eq 0 ]; then
log_error "invalid command: ${command}"
usage
......@@ -817,6 +864,22 @@ case ${command} in
;;
esac
;;
lease-recount)
case ${backend} in
memfile)
log_info "memfile does not count leases"
;;
mysql)
mysql_recount
;;
pgsql)
pgsql_recount
;;
cql)
log_info "cql does not count leases"
;;
esac
;;
esac
exit 0
......@@ -3,7 +3,7 @@ kea-admin
Kea
kea-admin
Shell script for managing Kea databases
2014-2018
2014-2020
Internet Systems Consortium, Inc. ("ISC")
kea-admin
command
......@@ -54,6 +54,9 @@ OPTIONS
meant to be used as a diagnostic tool, so it provides a portable,
human-readable form of the lease data.
lease-recount
Recounts leases for MySQL or PostgreSQL database.
``backend``
Specifies backend type. Currently allowed backends are: memfile,
mysql and pgsql.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment