Skip to content

Instantly share code, notes, and snippets.

@JCotton1123
Created October 30, 2014 23:04
Show Gist options
  • Save JCotton1123/448e9d0e04f0fa089b82 to your computer and use it in GitHub Desktop.
Save JCotton1123/448e9d0e04f0fa089b82 to your computer and use it in GitHub Desktop.
vertica
#!/bin/bash
CMD=$(basename $0)
STATE=$1
NAME=$2
HOSTS=$3
LICENSE_PATH=$4
PASSWORD=$5
DATA_PATH=$6
RESTART_POLICY=$7
ADMIN_TOOLS="/opt/vertica/bin/adminTools -t"
function main() {
# Some parameter validation
valid_state_supplied=1
for valid_state in present absent started stopped; do
if [ "$STATE" == "$valid_state" ]; then
valid_state_supplied=0
break
fi
done
[ $valid_state_supplied -eq 0 ] || fail "Invalid state supplied"
[ -z "$NAME" ] && fail "A database name is required"
[ -z "$HOSTS" ] && fail "At least one host must be supplied"
current_state=$(get_db_state)
[ $? -ne 0 ] && fail "Unable to determine database state"
if [[ $STATE == "absent" && $current_state != "absent" ]]; then
remove_db
return $?
fi
if [ "$current_state" == "absent" ]; then
create_db
[ $? -ne 0 ] && exit 1
else
desired_hosts=$(echo $HOSTS | tr "," " ")
member_hosts=$(get_db_info "hosts")
add_hosts=$(find_host_additions "$member_hosts" "$desired_hosts")
remove_hosts=$(find_host_removals "$member_hosts" "$desired_hosts")
if [ ! -z "$add_hosts" ]; then
add_hosts_to_db "$add_hosts"
[ $? -ne 0 ] && exit 1
fi
if [ ! -z "$remove_hosts" ]; then
remove_hosts_from_db "$remove_hosts"
[ $? -ne 0 ] && exit 1
fi
fi
# Refresh the state since either of the above
# actions could've altered it
current_state=$(get_db_state)
if [[ $STATE == "started" && $current_state == "stopped" ]]; then
start_or_stop_db "start"
exit $?
fi
if [[ $STATE == "stopped" && $current_state == "started" ]]; then
start_or_stop_db "stop"
exit $?
fi
}
function create_db() {
cmd="${ADMIN_TOOLS} create_db "
params=""
params+="--database '${NAME}' "
params+="--hosts ${HOSTS} "
[ ! -z "$PASSWORD" ] && params+="--password '${PASSWORD}' "
[ ! -z "$DATA_PATH" ] && params+="--data_path '${DATA_PATH}' "
[ ! -z "$RESTART_POLICY" ] && params+="--policy ${RESTART_POLICY} "
[ ! -z "$LICENSE_PATH" ] && params+="--license '${LICENSE_PATH}'"
echo "$cmd $params"
return $?
}
function remove_db() {
cmd="${ADMIN_TOOLS} drop_db "
params=""
params+="--database '${NAME}'"
echo "$cmd $params"
return $?
}
function get_db_state() {
# Does the database exist?
hosts=$(get_db_info "hosts")
db_exists=$?
# Is it started?
db_started=1
if [ $db_exists ]; then
db_states=$($ADMIN_TOOLS db_status -s UP)
[ $? -ne 0 ] && return 1
for db in $db_states; do
if [ "$NAME" == "$db" ]; then
db_started=0
break
fi
done
fi
if [ $db_exists ]; then
[ $db_started ] && echo "started"
[ $db_started ] || echo "stopped"
else
echo "absent"
fi
return 0
}
function start_or_stop_db() {
start_or_stop="$1"
subcmd=""
if [ "$start_or_stop" == "start" ]; then
subcmd="start_db"
elif [ "$start_or_stop" == "stop" ]; then
subcmd="stop_db"
else
echo "Unexpected state value"
return 1
fi
cmd="${ADMIN_TOOLS} ${subcmd} "
params=""
params+="--noprompts "
params+="--database '${NAME}' "
[ ! -z "$PASSWORD" ] && params+="--password '${PASSWORD}' "
echo "$cmd $params"
return $?
}
function get_db_info() {
property="$1"
db_info=$($ADMIN_TOOLS list_db --database $NAME)
[ $? -ne 0 ] && return 2
if [ "$property" == "hosts" ]; then
hosts=$(echo "$db_info" | grep "Hosts" | cut -d" " -f2)
echo "$hosts" | tr "," " "
return 0
else
echo "Unexpected property"
return 1
fi
return 0
}
function add_hosts_to_db() {
hosts="$1"
cmd="$ADMIN_TOOLS db_add_node "
params="--noprompts "
params+="--database $NAME "
params+="--hosts $hosts "
[ ! -z "$password" ] && params+="--password '${PASSWORD}'"
echo "$cmd $params"
return $?
}
function remove_hosts_from_db() {
hosts="$1"
for host in $hosts; do
cmd="$ADMIN_TOOLS db_remove_node "
params="--noprompts "
params+="--database $NAME "
params+="--hosts $hosts "
[ ! -z "$password" ] && params+="--password '${PASSWORD}'"
echo "$cmd $params"
[ $? -ne 0 ] && return 1
done
return 0
}
function find_host_additions() {
member_hosts="$1"
desired_hosts="$2"
add_hosts=""
for desired_host in $desired_hosts; do
for member_host in $member_hosts; do
[ "$desired_host" == "$member_host" ] && continue 2
done
if [ -z "$add_hosts" ]; then
add_hosts=$desired_host
else
add_hosts="${add_hosts},$desired_host"
fi
done
echo $add_hosts
[ ! -z "$add_hosts" ]
return $?
}
function find_host_removals() {
member_hosts="$1"
desired_hosts="$2"
remove_hosts=""
for member_host in $member_hosts; do
for desired_host in $desired_hosts; do
[ "$desired_host" == "$member_host" ] && continue 2
done
if [ -z "$remove_hosts" ]; then
remove_hosts=$member_host
else
remove_hosts="${add_hosts},$member_host"
fi
done
echo $remove_hosts
[ ! -z "$remove_hosts" ]
return $?
}
function fail() {
echo "$1"
exit 1
}
function usage() {
echo "Create a database or add hosts to it if it already exists"
echo "usage: $cmd state name hosts [license_path] [password] [data_path] [restart_policy]"
echo " state - present|absent|started|stopped"
echo " name - name of the database"
echo " hosts - comma delim'd list of hosts"
echo " license_path - (optional) path to license file"
echo " password - (optional) database password"
echo " data_path - (optional) database storage location"
echo " restart_policy - (optional) database restart policy"
exit 0
}
[[ "$1" == "-h" || "$1" == "--help" ]] && usage
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment