Created
September 30, 2017 19:59
-
-
Save captbunzo/fc9b3b005adcd13dc583239f12365c25 to your computer and use it in GitHub Desktop.
RocketMap hive start/stop/status/attach script
This file contains hidden or 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 | |
# | |
# rockethive.sh -- RocketMap hive start/stop/status/attach script | |
# | |
# CHANGELOG | |
# | |
# Date Author Description | |
# --------------------------------------------------------------------------------------------------------- | |
# 2017-09-01 captbunzo Initial version (again) | |
# 2017-09-12 captbunzo Modified to support multiple user region setup | |
# 2017-09-28 captbunzo Modified to be a general purpose start/stop/status/attach script | |
# --------------------------------------------------------------------------------------------------------- | |
# | |
# Get some colors ready | |
#if [[ -z $RED ]]; then | |
# if [[ -t 1 ]]; then | |
# RED=$( tput setaf 1 ) ; BLUE=$( tput setaf 4 ) ; WHITE=$( tput setaf 7 ) | |
# GREEN=$( tput setaf 2 ) ; MAGENTA=$( tput setaf 5 ) ; PLAIN=$( tput sgr0 ) ; REVCOLOR=$( tput rev ) | |
# YELLOW=$( tput setaf 3 ) ; CYAN=$( tput setaf 6 ) ; BOLD=$( tput bold ) ; UNDERLINE=$( tput smul ) | |
# else | |
# RED='' ; BLUE='' ; WHITE='' | |
# GREEN='' ; MAGENTA='' ; PLAIN='' ; REVCOLOR='' | |
# YELLOW='' ; CYAN='' ; BOLD='' ; UNDERLINE='' | |
# fi | |
#fi | |
# Get the full path of the script | |
SCRIPT_DIR=$( echo $( cd $(dirname $0) && pwd -P) ) | |
SCRIPT=$SCRIPT_DIR/$(basename $0) | |
SCRIPT_CONFIG_DIR=$SCRIPT_DIR/config | |
REGION=$( echo $USER | cut -f2 -d- ) | |
COMMAND= | |
while [[ $# -gt 0 ]]; do | |
nextarg="$1" | |
case $nextarg in | |
start | stop | status | restart | attach | list ) COMMAND=$nextarg ;; | |
* ) HIVE=$nextarg ;; | |
esac | |
shift | |
done | |
# Please let us kill screen soon | |
SESSION=hive-$HIVE | |
# Check if we are running the all command | |
case "${HIVE}" in | |
-a | --a | -all | --all ) | |
if [[ $COMMAND = attach ]]; then | |
echo "ERROR - Cannot attach to all hives!" | |
exit 1 | |
fi | |
hives=$( ls $HOME/config/RocketMap/args.*.txt | rev | cut -f1 -d/ | rev | cut -f2 -d. ) | |
if [[ $COMMAND != list ]]; then | |
echo | |
echo "${UNDERLINE}Running ${COMMAND} for all hives${PLAIN}" | |
echo | |
fi | |
for HIVE in $hives; do | |
if [[ $COMMAND != status && $COMMAND != list ]]; then | |
echo $HIVE | |
fi | |
$SCRIPT $COMMAND $HIVE | |
done | |
if [[ $COMMAND != list ]]; then | |
echo | |
fi | |
exit 0 | |
;; | |
esac | |
# If we are stopping, statusing or attaching, let us please just do it | |
case $COMMAND in | |
list ) | |
echo $HIVE | |
exit 0 | |
;; | |
stop ) | |
screen -X -S hive-$HIVE quit | |
exit 0 | |
;; | |
status ) | |
screen -ls hive-$HIVE > /dev/null 2>&1 | |
rc=$? | |
if [[ $rc -eq 0 ]] | |
then printf "%-24s ${GREEN}%s${PLAIN}\n" $HIVE "active (running)" ; exit $rc | |
else printf "%-24s ${RED}%s${PLAIN}\n" $HIVE "inactive (dead)" ; exit $rc | |
fi | |
;; | |
restart ) | |
$SCRIPT stop $HIVE | |
$SCRIPT start $HIVE | |
exit 0 | |
;; | |
attach ) | |
screen -rd hive-$HIVE | |
exit 0 | |
;; | |
esac | |
if [[ $COMMAND != start ]]; then | |
echo "Invalid command: $COMMAND" | |
exit 1 | |
fi | |
################################################################################ | |
# Attempt to respawn in a screen session if we are not in one | |
################################################################################ | |
if [[ $TERM != screen ]]; then | |
# Check if a screen session is already running for this session | |
screen -ls $SESSION > /dev/null 2>&1 | |
if [[ $? -eq 0 ]]; then | |
echo "ERROR - Screen session for $SESSION found, but we are not inside..." | |
echo "ABORTING!!!" | |
exit | |
fi | |
# Otherwise, respawn in a screen session | |
screen -d -m -L -S $SESSION $SCRIPT $COMMAND $HIVE | |
exit | |
fi | |
################################################################################ | |
# If we have gotten this far, we are already in a screen session | |
# Make sure this screen session is the right one! | |
################################################################################ | |
CURRENT_SESSION=$( echo $STY | cut -f2 -d. ) | |
if [[ $CURRENT_SESSION != $SESSION ]]; then | |
echo "ERROR - We are in a screen session named $CURRENT_SESSION, expecting $SESSION" | |
echo "ABORTING!!!" | |
exit | |
fi | |
################################################################################ | |
# Fantastic, we are in the right screen session. | |
# Let's actually run the stuff now, this is the real script! | |
################################################################################ | |
ACCOUNTS_DIR=/home/teamunity/config/Accounts/$REGION | |
ROCKETMAP_CONFIG_DIR=/home/teamunity/config/RocketMap | |
HIVE_CONFIG_DIR=$HOME/config/RocketMap | |
ROCKETMAP_DIR=/home/teamunity/current/RocketMap | |
ROCKETMAP=runserver.py | |
# Define a few variables and paths | |
HIVE_LOG_DIR=$HOME/log/RocketMap/$HIVE | |
HIVE_RUN_DIR=$HOME/run | |
HIVE_TMP_DIR=$HIVE_RUN_DIR/tmp/$HIVE | |
# Process management variables | |
STOP_FILE=$HIVE_RUN_DIR/$SESSION.stop | |
PID_FILE=$HIVE_RUN_DIR/$SESSION.pid | |
SLEEP_SECS=5 | |
echo $$ > $PID_FILE # Save the PID | |
rm -f $STOP_FILE # Remove any previous stop files | |
mkdir -m 2755 -p $HIVE_LOG_DIR # Create the hive log directory | |
mkdir -m 2755 -p $HIVE_TMP_DIR # Create the hive temporary directory | |
cd $HIVE_TMP_DIR # Change to the hive temporary path | |
# Start the process inside a loop | |
result=0 | |
while [[ $result = 0 ]]; do | |
# Create some space | |
echo | |
echo | |
echo | |
# Figure out the hour | |
typeset -i hour=10#$(date +%H) | |
if [[ $hour -ge 0 && $hour -lt 6 ]]; then accounts_hour="0000" | |
elif [[ $hour -ge 6 && $hour -lt 12 ]]; then accounts_hour="0600" | |
elif [[ $hour -ge 12 && $hour -lt 18 ]]; then accounts_hour="1200" | |
elif [[ $hour -ge 18 && $hour -lt 24 ]]; then accounts_hour="1800" | |
fi | |
# Setup the hive arguments | |
CONFIG_ARGS="--config $ROCKETMAP_CONFIG_DIR/config.ini" | |
ROCKETMAP_ARGS=$( cat $ROCKETMAP_CONFIG_DIR/args.rocketmap-no-server.txt ) | |
HIVE_ARGS=$( cat $HIVE_CONFIG_DIR/args.$HIVE.txt ) | |
HIVE_HOOKS=$( cat $HIVE_CONFIG_DIR/hooks.$HIVE.txt ) | |
NAME_ARGS="--status-name $REGION-$HIVE" | |
ACCOUNTS_ARGS="--accountcsv $ACCOUNTS_DIR/accounts.$HIVE.$accounts_hour.csv" | |
LOG_ARGS="--log-path $HIVE_LOG_DIR" | |
# Setup the webhook argument | |
set -x | |
. $ROCKETMAP_CONFIG_DIR/webhooks.sh | |
HOOK_ARGS= | |
for hook in $HIVE_HOOKS; do | |
case $hook in | |
greatswamp ) HOOK_ARGS="${HOOK_ARGS} --webhook ${WEBHOOK_GREATSWAMP}" ;; | |
boonton ) HOOK_ARGS="${HOOK_ARGS} --webhook ${WEBHOOK_BOONTON}" ;; | |
ramapo ) HOOK_ARGS="${HOOK_ARGS} --webhook ${WEBHOOK_RAMAPO}" ;; | |
route22 ) HOOK_ARGS="${HOOK_ARGS} --webhook ${WEBHOOK_ROUTE22}" ;; | |
esac | |
done | |
set +x | |
# I has a Rocketmap. | |
echo $ROCKETMAP_DIR/$ROCKETMAP $CONFIG_ARGS $ROCKETMAP_ARGS $HIVE_ARGS $NAME_ARGS $ACCOUNTS_ARGS $LOG_ARGS $HOOK_ARGS | |
$ROCKETMAP_DIR/$ROCKETMAP $CONFIG_ARGS $ROCKETMAP_ARGS $HIVE_ARGS $NAME_ARGS $ACCOUNTS_ARGS $LOG_ARGS $HOOK_ARGS | |
if [[ -e $STOP_FILE ]]; then | |
# Noooo they be stealing my hive! | |
echo "stop file found - ending the loop" | |
rm -f $STOP_FILE | |
result=1 | |
else | |
# Beetins will continue til server returns! | |
echo "sleeping for $SLEEP_SECS seconds - a ctrl-c now should end the loop" | |
sleep $SLEEP_SECS | |
result=$? | |
fi | |
done | |
rm -f $STOP_FILE $PID_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment