superlint/.automation/test/ansible/ghe-initialize/templates/ghe-config-apply.sh

210 lines
6.9 KiB
Bash
Raw Normal View History

2020-06-23 12:02:45 -04:00
#!/usr/bin/env bash
2020-02-04 09:49:58 -05:00
################################################################################
# Script to run ghe-config-apply on the primary GHES instance
# and wait for any previous runs to complete
################################################################################
###########
# Globals #
###########
GHE_CONFIG_PID='/var/run/ghe-config.pid' # PID file when a config is running
GHE_APPLY_COMMAND='ghe-config-apply' # Command running when a config run
SLEEP_SECONDS=20 # Seconds to sleep before next check
PID_CHECK_LIMIT=15 # How many times to check the pid before moving on
PID_CHECK=0 # Count of times to check the pid
PROCESS_CHECK_LIMIT=15 # How many times to check the process before moving on
PROCESS_CHECK=0 # Count of times to check the process
################################################################################
########################### SUB ROUTINES BELOW #################################
################################################################################
################################################################################
#### Function CheckGHEPid ######################################################
CheckGHEPid()
{
##################################
# Check to prevent infinite loop #
##################################
2020-07-21 13:09:07 -04:00
if [ ${PID_CHECK} -gt ${PID_CHECK_LIMIT} ]; then
2020-02-04 09:49:58 -05:00
# Over the limit, move on
2020-07-21 13:09:07 -04:00
echo "We have checked the pid ${PID_CHECK} times, moving on..."
2020-02-04 09:49:58 -05:00
else
################################################
# Check to see if the PID is alive and running #
################################################
2020-07-21 13:09:07 -04:00
if [ ! -f "${GHE_CONFIG_PID}" ]; then
2020-02-04 09:49:58 -05:00
# File not found
2020-07-21 13:09:07 -04:00
echo "We're good to move forward, no .pid file found at:[${GHE_CONFIG_PID}]"
2020-02-04 09:49:58 -05:00
else
# Found the pid running, need to sleep
2020-07-21 13:09:07 -04:00
echo "Current PID found, sleeping ${SLEEP_SECONDS} seconds before next check..."
2020-02-04 09:49:58 -05:00
################
# Sleep it off #
################
2020-07-21 13:09:07 -04:00
SLEEP_CMD=$(sleep ${SLEEP_SECONDS} 2>&1)
2020-02-04 09:49:58 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2020-07-27 17:20:06 -04:00
error "Failed to sleep!${NC}"
error "[${SLEEP_CMD}]${NC}"
2020-02-04 09:49:58 -05:00
echo "Will try to call apply as last effort..."
####################################
# Call config apply as last effort #
####################################
RunConfigApply
else
#####################
# Increment counter #
#####################
((PID_CHECK++))
##################################
# Try to check for the pid again #
##################################
CheckGHEPid
fi
fi
fi
}
################################################################################
#### Function CheckGHEProcess ##################################################
CheckGHEProcess()
{
##################################
# Check to prevent infinite loop #
##################################
2020-07-21 13:09:07 -04:00
if [ ${PROCESS_CHECK} -gt ${PROCESS_CHECK_LIMIT} ]; then
2020-02-04 09:49:58 -05:00
# Over the limit, move on
2020-07-21 13:09:07 -04:00
echo "We have checked the process ${PROCESS_CHECK} times, moving on..."
2020-02-04 09:49:58 -05:00
else
####################################################
# Check to see if the process is alive and running #
####################################################
2020-07-21 13:09:07 -04:00
CHECK_PROCESS_CMD=$(pgrep -f "${GHE_APPLY_COMMAND}" 2>&1)
2020-02-04 09:49:58 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2020-02-04 09:49:58 -05:00
# No process running on the system
2020-07-21 13:09:07 -04:00
echo "Were good to move forward, no process like:[${GHE_APPLY_COMMAND}] running currently on the system"
2020-02-04 09:49:58 -05:00
else
# Found the process running, need to sleep
2020-07-21 13:09:07 -04:00
echo "Current process alive:[${CHECK_PROCESS_CMD}], sleeping ${SLEEP_SECONDS} seconds before next check..."
2020-02-04 09:49:58 -05:00
################
# Sleep it off #
################
2020-07-21 13:09:07 -04:00
SLEEP_CMD=$(sleep ${SLEEP_SECONDS} 2>&1)
2020-02-04 09:49:58 -05:00
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2020-07-27 17:20:06 -04:00
error "Failed to sleep!${NC}"
error "[${SLEEP_CMD}]${NC}"
2020-02-04 09:49:58 -05:00
echo "Will try to call apply as last effort..."
####################################
# Call config apply as last effort #
####################################
RunConfigApply
else
#####################
# Increment counter #
#####################
((PROCESS_CHECK++))
######################################
# Try to check for the process again #
######################################
CheckGHEProcess
fi
fi
fi
}
################################################################################
#### Function RunConfigApply ###################################################
RunConfigApply()
{
##########
# Header #
##########
2020-07-21 13:09:07 -04:00
echo "Running ${GHE_APPLY_COMMAND} to the server..."
2020-02-04 09:49:58 -05:00
##############################################
# Run the command to apply changes to server #
##############################################
APPLY_CMD=$(ghe-config-apply 2>&1)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
2020-07-21 13:09:07 -04:00
if [ ${ERROR_CODE} -ne 0 ]; then
2020-02-04 09:49:58 -05:00
# Errors
2020-07-27 17:20:06 -04:00
error "Failed to run config apply command!${NC}"
fatal "[${APPLY_CMD}]${NC}"
2020-02-04 09:49:58 -05:00
else
# Success
2020-07-27 17:20:06 -04:00
notice "Successfully ran ${F[C]}${GHE_APPLY_COMMAND}${NC}"
2020-02-04 09:49:58 -05:00
fi
}
################################################################################
################################## MAIN ########################################
################################################################################
######################
# Check for pid file #
######################
CheckGHEPid
#############################
# Check for running process #
#############################
CheckGHEProcess
####################
# Run config apply #
####################
RunConfigApply
2020-06-24 12:47:18 -04:00
###########################################
2020-06-23 17:48:01 -04:00
# We're going to run it again after a nap #
2020-06-24 12:47:18 -04:00
# to make sure there is no crazy actions #
###########################################
2020-02-04 09:49:58 -05:00
sleep 300s
######################
# Check for pid file #
######################
CheckGHEPid
#############################
# Check for running process #
#############################
CheckGHEProcess
####################
# Run config apply #
####################
RunConfigApply