Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New kea admin actions #66

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 181 additions & 20 deletions src/bin/admin/admin-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@
mysql_execute() {
QUERY=$1
shift
if [ $# -gt 1 ]; then
mysql -N -B "$@" -e "${QUERY}"
if [ $# -gt 0 ]; then
cmdline=$(mysql_compose_connect_cmd_line)
cmdline="$cmdline $* -e \"$QUERY\""
eval "mysql " $cmdline
retcode=$?
else
mysql -N -B --database="${db_name}" --user="${db_user}" --password="${db_password}" -e "${QUERY}"
retcode=$?
cmdline=$(mysql_compose_connect_cmd_line)
cmdline="$cmdline -e \"$QUERY\" $db_name"
eval "mysql " $cmdline
retcode="$?"
fi

return $retcode
}

mysql_execute_no_dbname() {
QUERY=$1
shift

cmdline=$(mysql_compose_connect_cmd_line)
cmdline="$cmdline -e \"$QUERY\""

eval "mysql " $cmdline
retcode="$?"

if [ $retcode -ne 0 ]; then
printf "mysql returned with exit status $retcode\n"
exit $retcode
fi

return $retcode
Expand All @@ -32,19 +54,21 @@ mysql_execute() {
mysql_execute_script() {
file=$1
shift
if [ $# -ge 1 ]; then
mysql -N -B "$@" < "${file}"
if [ $# -gt 0 ]; then
cmdline=$(mysql_compose_connect_cmd_line)
eval "mysql " $cmdline $* < ${file}
retcode=$?
else
mysql -N -B --database="${db_name}" --user="${db_user}" --password="${db_password}" < "${file}"
retcode=$?
cmdline=$(mysql_compose_connect_cmd_line)
eval "mysql "$cmdline $db_name < ${file}
retcode="$?"
fi

return $retcode
}

mysql_version() {
mysql_execute "SELECT CONCAT_WS('.', version, minor) FROM schema_version" "$@"
mysql_execute "SELECT CONCAT_WS('.', version, minor) FROM schema_version;" "$@"
return $?
}

Expand All @@ -62,16 +86,38 @@ pgsql_execute() {
QUERY=$1
shift
if [ $# -gt 0 ]; then
echo "${QUERY}" | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q "$@"
export PGPASSWORD=$db_password
cmdline=$(pgsql_compose_connect_cmd_line)
cmdline="$cmdline $*"
echo $QUERY | psql $cmdline
retcode=$?
else
export PGPASSWORD=$db_password
echo "${QUERY}" | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U "${db_user}" -d "${db_name}"
cmdline=$(pgsql_compose_connect_cmd_line)
cmdline="$cmdline -d $db_name"
echo $QUERY | psql $cmdline
retcode=$?
fi
return $retcode
}

pgsql_execute_no_dbname() {
QUERY=$1
shift
export PGPASSWORD=$db_password
cmdline=$(pgsql_compose_connect_cmd_line)
# Sometimes we don't need to connect to a specific database
# (e.g. when we want to create a new one)
# Postgresql client requires to connect all the time a database.
# The first database is always created by the initdb command when
# the data storage area is initialized is called postgres.
# So we will connect to the default database "postgres")
cmdline="$cmdline -d postgres"
echo $QUERY | psql $cmdline
retcode=$?
return $retcode
}

# Submits SQL in a given file to PostgreSQL
# There are two ways of calling this method.
# pgsql_execute SQL_FILE - This call is simpler, but requires db_user,
Expand All @@ -86,11 +132,16 @@ pgsql_execute_script() {
file=$1
shift
if [ $# -gt 0 ]; then
psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -f "${file}" "$@"
export PGPASSWORD=$db_password
cmdline=$(pgsql_compose_connect_cmd_line)
cmdline="$cmdline -d $db_name -f $file $*"
eval "psql "$cmdline
retcode=$?
else
export PGPASSWORD=$db_password
psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U "${db_user}" -d "${db_name}" -f "${file}"
cmdline=$(pgsql_compose_connect_cmd_line)
cmdline="$cmdline -d $db_name -f $file"
eval "psql "$cmdline
retcode=$?
fi
return $retcode
Expand All @@ -104,11 +155,15 @@ pgsql_version() {
cql_execute() {
query=$1
shift
if [ $# -gt 1 ]; then
cqlsh "$@" -e "$query"
if [ $# -gt 0 ]; then
cmdline=$(cql_compose_connect_cmd_line)
cmdline="$cmdline $* -e \"$query\""
eval "cqlsh " $cmdline
retcode=$?
else
cqlsh -u "${db_user}" -p "${db_password}" -k "${db_name}" -e "${query}"
cmdline=$(cql_compose_connect_cmd_line)
cmdline="$cmdline -k $db_name -e \"$query\""
eval "cqlsh " $cmdline
retcode=$?
fi

Expand All @@ -120,14 +175,35 @@ cql_execute() {
return $retcode
}

cql_execute_no_keyspace() {
query=$1
shift

cmdline=$(cql_compose_connect_cmd_line)
cmdline="$cmdline -e \"$query\""
eval "cqlsh " $cmdline
retcode=$?

if [ $retcode -ne 0 ]; then
printf "cqlsh returned with exit status $retcode\n"
exit $retcode
fi

return $retcode
}

cql_execute_script() {
file=$1
shift
if [ $# -gt 1 ]; then
cqlsh "$@" -e "$file"
if [ $# -gt 0 ]; then
cmdline=$(cql_compose_connect_cmd_line)
cmdline="$cmdline $* -f \"$file\""
eval "cqlsh " $cmdline
retcode=$?
else
cqlsh -u "${db_user}" -p "${db_password}" -k "${db_name}" -f "${file}"
cmdline=$(cql_compose_connect_cmd_line)
cmdline="$cmdline -k $db_name -f \"$file\""
eval "cqlsh " $cmdline
retcode=$?
fi

Expand All @@ -143,6 +219,91 @@ cql_version() {
version=$(cql_execute "SELECT version, minor FROM schema_version" "$@")
error=$?
version=$(echo "$version" | grep -A 1 "+" | grep -v "+" | tr -d ' ' | cut -d "|" -f 1-2 | tr "|" ".")
echo "$version"
echo $version
return $error
}

mysql_compose_connect_cmd_line() {
local line="-N -B"

if [ -n "$db_server_address" ]; then
line="$line --host=$db_server_address"
fi

if [ -n "$db_server_port" ]; then
line="$line --port=$db_server_port"
fi

if [ -n "$db_user" ]; then
line="$line --user=$db_user"
fi

if [ -n "$db_password" ]; then
line="$line --password=$db_password"
fi

if [ -n "$db_host" ]; then
line="$line --host=$db_host"
fi

echo $line
}

pgsql_compose_connect_cmd_line() {
local line="--set ON_ERROR_STOP=1 -A -t -q"

if [ -n "$db_server_address" ]
then
line="$line -h $db_server_address"
else
if [ -n "$db_host" ]; then
line="$line -h $db_host"
else
line="$line -h localhost"
fi
fi

if [ -n "$db_server_port" ]; then
line="$line -p $db_server_port"
fi

if [ -n "$db_user" ]; then
line="$line -U $db_user"
fi

if [ -n "$db_host" ]; then
line="$line -h $db_host"
fi

echo $line
}

cql_compose_connect_cmd_line() {
local line=""

if [ -n "$db_server_address" ]; then
line=$line" "$db_server_address
fi

if [ -n "$db_server_port" ]; then
line=$line" "$db_server_port
fi

if [ -n "$db_server_version" ]; then
line=$line" --cqlversion="$db_server_version
fi

if [ -n "$db_user" ]; then
line=$line" -u "$db_user
fi

if [ -n "$db_password" ]; then
line=$line" -p "$db_password
fi

if [ -n "$db_use_ssl" ]; then
line=$line" --ssl"
fi

echo $line
}
Loading