admin-utils.sh 4.34 KB
Newer Older
1
# Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC")
2
#
3 4 5
# 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
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 7

# This is an utility script that is being included by other scripts.
8 9 10

# There are two ways of calling this method.
# mysql_execute SQL_QUERY - This call is simpler, but requires db_user,
11
#     db_password and db_name variables to be set.
12 13 14 15
# 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.
16
#
17
# It returns the mysql command exit status to the caller as $?
18
mysql_execute() {
19 20
    QUERY=$1
    shift
21
    if [ $# -gt 1 ]; then
22
        mysql -N -B "$@" -e "${QUERY}"
23
        retcode=$?
24
    else
25
        mysql -N -B --host="${db_host}" --database="${db_name}" --user="${db_user}" --password="${db_password}" -e "${QUERY}"
Andrei Pavel's avatar
Andrei Pavel committed
26 27 28 29 30 31 32 33 34 35 36 37 38
        retcode=$?
    fi

    return $retcode
}

mysql_execute_script() {
    file=$1
    shift
    if [ $# -ge 1 ]; then
        mysql -N -B "$@" < "${file}"
        retcode=$?
    else
39
        mysql -N -B --host="${db_host}" --database="${db_name}" --user="${db_user}" --password="${db_password}" < "${file}"
Andrei Pavel's avatar
Andrei Pavel committed
40
        retcode=$?
41
    fi
42

43
    return $retcode
44 45 46
}

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

51 52 53 54 55
# Submits given SQL text to PostgreSQL
# There are two ways of calling this method.
# pgsql_execute SQL_QUERY - This call is simpler, but requires db_user,
#     db_password and db_name variables to be set.
# pgsql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters
56
#     may be specified. They are passed directly to pgsql. This one is
57 58 59
#     more convenient to use if the script didn't parse db_user db_password
#     and db_name.
#
60
# It returns the pgsql command exit status to the caller as $?
61 62 63 64
pgsql_execute() {
    QUERY=$1
    shift
    if [ $# -gt 0 ]; then
Andrei Pavel's avatar
Andrei Pavel committed
65
        echo "${QUERY}" | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q "$@"
66 67 68
        retcode=$?
    else
        export PGPASSWORD=$db_password
69
        echo "${QUERY}" | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U "${db_user}" -d "${db_name}"
70 71 72 73 74
        retcode=$?
    fi
    return $retcode
}

75 76 77 78 79
# 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,
#     db_password and db_name variables to be set.
# pgsql_execute SQL_FILE PARAM1 PARAM2 .. PARAMN - Additional parameters
80
#     may be specified. They are passed directly to pgsql. This one is
81 82 83
#     more convenient to use if the script didn't parse db_user db_password
#     and db_name.
#
84
# It returns the pgsql command exit status to the caller as $?
85 86 87 88
pgsql_execute_script() {
    file=$1
    shift
    if [ $# -gt 0 ]; then
89
        psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -f "${file}" "$@"
90 91 92
        retcode=$?
    else
        export PGPASSWORD=$db_password
93
        psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -U "${db_user}" -d "${db_name}" -f "${file}"
94 95 96 97 98 99 100 101 102
        retcode=$?
    fi
    return $retcode
}

pgsql_version() {
    pgsql_execute "SELECT version || '.' || minor FROM schema_version" "$@"
    return $?
}
103

Tomek Mrugalski's avatar
Tomek Mrugalski committed
104
cql_execute() {
105
    query=$1
106 107
    shift
    if [ $# -gt 1 ]; then
108
        cqlsh "$@" -e "$query"
109 110
        retcode=$?
    else
111
        cqlsh -u "${db_user}" -p "${db_password}" -k "${db_name}" -e "${query}"
112 113 114 115
        retcode=$?
    fi

    if [ $retcode -ne 0 ]; then
116
        printf "cqlsh returned with exit status %s\n" "${retcode}"
117
        exit $retcode
118 119 120 121 122
    fi

    return $retcode
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
123 124 125 126
cql_execute_script() {
    file=$1
    shift
    if [ $# -gt 1 ]; then
127
        cqlsh "$@" -e "$file"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
128 129
        retcode=$?
    else
130
        cqlsh -u "${db_user}" -p "${db_password}" -k "${db_name}" -f "${file}"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
131 132 133 134
        retcode=$?
    fi

    if [ $retcode -ne 0 ]; then
135
        printf "cqlsh returned with exit status %s\n" "${retcode}"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
136 137 138 139 140 141
        exit $retcode
    fi

    return $retcode
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
142
cql_version() {
143
    version=$(cql_execute "SELECT version, minor FROM schema_version" "$@")
144 145 146 147
    error=$?
    version=$(echo "$version" | grep -A 1 "+" | grep -v "+" | tr -d ' ' | cut -d "|" -f 1-2 | tr "|" ".")
    echo "$version"
    return $error
148
}