Created
November 26, 2021 06:36
-
-
Save xiaolei/52a54077441c39b573a06ba61e0a557b to your computer and use it in GitHub Desktop.
Shell script to manage rqlite process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "${BASH_VERSINFO:-0}" -lt 4 ] | |
then | |
echo "bash version: ${BASH_VERSION}" | |
echo "Current bash version doesn't support associative arrays, require bash version >=4" | |
exit 1 | |
fi | |
hostname=$(hostname) | |
command="$1" | |
service_name="rqlited" | |
auth_client_json_file="$HOME/rqlite/auth/auth-clients.json" | |
data_path="$HOME/rqlite/data" | |
http_cert="$HOME/rqlite/cert/rqlite.pem" | |
http_key="$HOME/rqlite/cert/rqlite.key" | |
node_cert="$HOME/rqlite/cert/rqlite.pem" | |
node_key="$HOME/rqlite/cert/rqlite.key" | |
user_name="rqlite" | |
# URL encoded | |
pwd="<replace with url encoded password>" | |
# rqlite nodes in the cluster. | |
# Should replace server1, server2, server3 with the actual server hostname. | |
declare -a cluster=("https://$user_name:$pwd@server1:4001" | |
"https://$user_name:$pwd@server2:4001" | |
"https://$user_name:$pwd@server3:4001") | |
join_urls="" | |
# Build join urls | |
build_join_urls() { | |
local self_node_url="https://$user_name:$pwd@$hostname:4001" | |
# Remove self node url from cluster node urls. | |
local cluster_exclude_self_node=( "${cluster[@]/$self_node_url}" ) | |
local temp=$(printf ",%s" "${cluster_exclude_self_node[@]}") | |
join_urls=${temp:1} | |
} | |
# Start rqlite if it's not running. | |
start() { | |
if [ -z "$1" ] | |
then | |
echo "process name is required." | |
return | |
fi | |
without_join_urls=$2 | |
local process_name=$1 | |
local running_process_count | |
running_process_count=$(ps -ef | grep "$process_name" | grep -v "grep" | wc -l) | |
if [ "$running_process_count" -ne 0 ] | |
then | |
echo "$process_name is running, skip it." | |
return | |
fi | |
if [ -z "$without_join_urls" ] | |
then | |
build_join_urls | |
echo "Start rqlite with join urls: $join_urls" | |
nohup ./rqlited -node-id "$hostname" -http-addr "$hostname:4001" -raft-addr "$hostname:4002" -auth "$auth_client_json_file" -http-cert "$http_cert" -http-key "$http_key" -node-encrypt -node-cert "$node_cert" -node-key "$node_key" -node-no-verify -join "$join_urls" "$data_path" &> rqlite.log & | |
else | |
# $without_join_urls value is not empty | |
# Start rqlite without join urls. | |
echo "Start rqlite without join urls, rqlite with run in standalone mode, it will not join to any cluster." | |
nohup ./rqlited -node-id "$hostname" -http-addr "$hostname:4001" -raft-addr "$hostname:4002" -auth "$auth_client_json_file" -http-cert "$http_cert" -http-key "$http_key" -node-encrypt -node-cert "$node_cert" -node-key "$node_key" -node-no-verify "$data_path" &> rqlite.log & | |
fi | |
local pid=$(pgrep -f "$process_name") | |
echo "$process_name started, pid: $pid" | |
} | |
# Stop rqlite. | |
stop() { | |
if [ -z "$1" ] | |
then | |
echo "process name is required." | |
return | |
fi | |
local process_name=$1 | |
local running_process_count | |
running_process_count=$(ps -ef | grep "$process_name" | grep -v "grep" | wc -l) | |
if [ "$running_process_count" -eq 0 ] | |
then | |
echo "$process_name is not running, skip it." | |
return | |
fi | |
local pid=$(pgrep -f "$process_name") | |
if [ -z "$pid" ] | |
then | |
echo "$process_name is not running." | |
return | |
else | |
echo "Prepare to stop pid: $pid, process name:" | |
ps -p "$pid" -o command h | |
kill -9 "$pid" | |
echo "Waiting pid: $pid to exit..." | |
tail --pid="$pid" -f /dev/null | |
echo "pid: $pid has been stopped successfully." | |
fi | |
} | |
restart() { | |
stop "$service_name" | |
start "$service_name" | |
} | |
start_standalone() { | |
start "$service_name" 1 | |
} | |
case "$command" in | |
start) | |
start "$service_name" | |
;; | |
start_standalone) | |
start_standalone | |
;; | |
restart) | |
restart "$service_name" | |
;; | |
stop) | |
stop "$service_name" | |
;; | |
*) | |
cat << EOF | |
./cnesb.sh {COMMAND} | |
Manage rqlite service. | |
Usage: | |
start Start rqlite service. | |
stop Stop rqlite service. | |
restart Restart rqlite service. | |
start_standalone Start with standalone mode, it will not join to any cluster. | |
EOF | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment