Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Created March 16, 2017 16:15
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/27fb152613f0fc63ae1d5401e5f15597 to your computer and use it in GitHub Desktop.
Save perfecto25/27fb152613f0fc63ae1d5401e5f15597 to your computer and use it in GitHub Desktop.
Hashicorp Vault startup script for Centos/RHEL 6 / Fedora (/etc/init.d/vault)
#!/bin/bash
# chkconfig: 2345 80 05
# description: VAULT startup script
application="vault"
run_as_user="root"
port=8200
curr_user=$(whoami)
# 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 'vault server' | grep -v 'grep' | awk '{print $2}')
}
start() {
check_user $curr_user $run_as_user
echo "Starting ${application}"
vault server -config=/etc/vault/vault.conf &
# 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