Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Created March 17, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perfecto25/19cdda06d79d229b4370d35e834ce993 to your computer and use it in GitHub Desktop.
Save perfecto25/19cdda06d79d229b4370d35e834ce993 to your computer and use it in GitHub Desktop.
Consul startup script (bootstrap server)
#!/bin/bash
# chkconfig: 2345 80 05
# description: CONSUL startup script
application='consul'
run_as_user='root'
port=8300
curr_user=$(whoami)
ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | grep 192)
datacenter='mrx'
echo "IP=$ip"
# stderr
echo_err() {
echo "[ERROR] $@" 2>&1
exit 1
}
check_user() {
## check if logged in as run_as_user
if [[ ! $1 == $2 ]]
then
echo_err "Please login as ${2} to continue.."
fi
}
## check if run_as_user exists
user_exists=$(getent passwd $run_as_user)
if [ ! "${user_exists}" ];
then
echo_err "User ${run_as_user} does not exist..exiting";
fi
get_PID() {
PID=$(ps -ef | grep 'consul agent' | grep -v 'grep' | awk '{print $2}')
}
start() {
check_user $curr_user $run_as_user
echo "Starting ${application}"
consul agent -server -bootstrap -bind=$ip -client=$ip -ui -data-dir=/opt/consul -config-dir=/etc/consul -node=$(hostname) -datacenter=$datacenter &
#consul agent -server -bootstrap -bind=127.0.0.1 -data-dir /tmp/consul &
# check exit status
get_PID
if [ "${PID}" ]
then
echo "${application} started on port ${port}"
return 0
else
echo_err "Could not start ${application}"
fi
}
stop() {
check_user $curr_user $run_as_user
echo "Shutting down $application: "
get_PID
if [ "${PID}" ]
then
echo $(kill ${PID})
if [ $? -eq 0 ]
then
echo "${application} is shut down."
return 0
else
echo_err "could not stop ${application}"
fi
fi
}
restart() {
check_user $curr_user $run_as_user
echo "restarting ${application}"
stop
start
echo "done."
}
status(){
get_PID
if [ "${PID}" ]
then
echo "${application} is running."
return 0
else
echo "${application} is NOT running."
return 0
fi
}
case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment