31
This commit is contained in:
parent
a9f4b34299
commit
e38dc37670
@ -2,80 +2,97 @@
|
|||||||
|
|
||||||
URL="https://raw.githubusercontent.com/elico/cp-tls-inspection-application-update-cron/master/collect-clish-scripts.sh"
|
URL="https://raw.githubusercontent.com/elico/cp-tls-inspection-application-update-cron/master/collect-clish-scripts.sh"
|
||||||
|
|
||||||
|
PROCESS_UID="$1"
|
||||||
|
|
||||||
|
if [ -z "${PROCESS_UID}" ];then
|
||||||
|
echo "Missing proccess ID cli argurment"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
CURRENT_ETAG=""
|
CURRENT_ETAG=""
|
||||||
LOCAL_MD5=""
|
LOCAL_MD5=""
|
||||||
REMOTE_MD5=""
|
REMOTE_MD5=""
|
||||||
EXPECTED_MD5="dd0618772ee09cfe8c3cc7a0574d4a3f"
|
EXPECTED_MD5="dd0618772ee09cfe8c3cc7a0574d4a3f"
|
||||||
#AUTO_FETCH_URL="0"
|
#AUTO_FETCH_URL="0"
|
||||||
RUN_AS_A_FUNCTION="1"
|
RUN_AS_A_FUNCTION="1"
|
||||||
|
KILL_OLD_DAEMON="1"
|
||||||
|
|
||||||
|
KILL_RES=""
|
||||||
|
|
||||||
|
if [ "${KILL_OLD_DAEMON}" -eq "1" ];then
|
||||||
|
KILL_RES=$(ps aux|grep "/storage/collect-clish-scripts-daemon.sh" |grep -v grep|grep -v "${PROCESS_UID}"|awk '{print $2}'|xargs -n1 -I{} kill {})
|
||||||
|
fi
|
||||||
|
|
||||||
FILENAME="/storage/collect-clish-scripts.sh"
|
FILENAME="/storage/collect-clish-scripts.sh"
|
||||||
|
|
||||||
which curl_cli >/dev/null 2>&1 && CURL="curl_cli"
|
which curl_cli >/dev/null 2>&1 && CURL="curl_cli"
|
||||||
which curl >/dev/null 2>&1 && CURL="curl"
|
which curl >/dev/null 2>&1 && CURL="curl"
|
||||||
|
|
||||||
|
export CA_CERT_BUNDLE_PATH="/pfrm2.0/opt/fw1/bin/ca-bundle.crt"
|
||||||
|
export SSL_CERT_FILE="${CA_CERT_BUNDLE_PATH}"
|
||||||
|
alias curl_cli="curl_cli --cacert ${CA_CERT_BUNDLE_PATH}"
|
||||||
|
|
||||||
function collect() {
|
function collect() {
|
||||||
START_EXECUTION_TIME=$( date +"%Y_%m_%d_%H_%M_%SS" )
|
START_EXECUTION_TIME=$( date +"%Y_%m_%d_%H_%M_%SS" )
|
||||||
START_EXECUTION_DATE=$( date +"%Y_%m_%d" )
|
START_EXECUTION_DATE=$( date +"%Y_%m_%d" )
|
||||||
|
|
||||||
DEBUG="0"
|
|
||||||
DRY_RUN="0"
|
|
||||||
CLEANUP_AFTER="1"
|
|
||||||
LOCK_FILE="/tmp/clish-scripts-collector-cron-lockfile"
|
|
||||||
SCRIPTS_PATH="/storage/clish-scripts"
|
|
||||||
ARCHIVE_PATH="/storage/clish-scripts-execution-archive"
|
|
||||||
|
|
||||||
if [ -f "${LOCK_FILE}" ];then
|
|
||||||
echo "Lockfile exits, stopping update"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Creating LOCKFILE: \"${LOCK_FILE}\""
|
|
||||||
touch "${LOCK_FILE}"
|
|
||||||
|
|
||||||
if [ -f "debug" ];then
|
DEBUG="0"
|
||||||
DEBUG="1"
|
DRY_RUN="0"
|
||||||
fi
|
CLEANUP_AFTER="1"
|
||||||
|
LOCK_FILE="/tmp/clish-scripts-collector-cron-lockfile"
|
||||||
if [ -f "dry-run" ];then
|
SCRIPTS_PATH="/storage/clish-scripts"
|
||||||
DRY_RUN="1"
|
ARCHIVE_PATH="/storage/clish-scripts-execution-archive"
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "cleanup-after" ];then
|
|
||||||
CLEANUP_AFTER="1"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ! -d "${SCRIPTS_PATH}" ];then
|
if [ -f "${LOCK_FILE}" ];then
|
||||||
mkdir -v "${SCRIPTS_PATH}"
|
echo "Lockfile exits, stopping update"
|
||||||
if [ "$?" -gt "0" ];then
|
exit 0
|
||||||
echo "Error creating: \"${SCRIPTS_PATH}\""
|
fi
|
||||||
echo "Creating LOCKFILE: \"${LOCK_FILE}\""
|
|
||||||
rm -fv "${LOCK_FILE}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
FIND_PATH="${SCRIPTS_PATH}"
|
echo "Creating LOCKFILE: \"${LOCK_FILE}\""
|
||||||
CLISH_FILE_NAMES=$(find "${FIND_PATH}" -maxdepth 1 -type f -regex '.*\.clish$' -exec bash -c 'grep -r "^##clish" $1 1> /dev/null && echo $1' _ {} \;; true)
|
touch "${LOCK_FILE}"
|
||||||
|
|
||||||
if [ ! -z "${CLISH_FILE_NAMES}" ];then
|
if [ -f "debug" ];then
|
||||||
count=0
|
DEBUG="1"
|
||||||
while read -r file
|
fi
|
||||||
do
|
|
||||||
if [ ! -d "${ARCHIVE_PATH}/${START_EXECUTION_DATE}" ];then
|
if [ -f "dry-run" ];then
|
||||||
mkdir -p "${ARCHIVE_PATH}/${START_EXECUTION_DATE}"
|
DRY_RUN="1"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Starting to work on: \"${file}\" at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |tee -a "${ARCHIVE_PATH}/${START_EXECUTION_DATE}/execution.log"
|
if [ -f "cleanup-after" ];then
|
||||||
echo "Starting to work on: \"${file}\" at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |logger
|
CLEANUP_AFTER="1"
|
||||||
clish -f "${file}"
|
fi
|
||||||
mv -v "${file}" "${ARCHIVE_PATH}/${START_EXECUTION_DATE}/${count}.clish_${START_EXECUTION_TIME}"
|
|
||||||
echo "Finished working on: \"${file}\" , Exit Code: $? , at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |tee -a "${ARCHIVE_PATH}/${START_EXECUTION_DATE}/execution.log"
|
if [ ! -d "${SCRIPTS_PATH}" ];then
|
||||||
echo "Finished working on: \"${file}\" , Exit Code: $? , at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |logger
|
mkdir -v "${SCRIPTS_PATH}"
|
||||||
((count++))
|
if [ "$?" -gt "0" ];then
|
||||||
done <<< "${CLISH_FILE_NAMES}"
|
echo "Error creating: \"${SCRIPTS_PATH}\""
|
||||||
fi
|
echo "Creating LOCKFILE: \"${LOCK_FILE}\""
|
||||||
|
rm -fv "${LOCK_FILE}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
FIND_PATH="${SCRIPTS_PATH}"
|
||||||
|
CLISH_FILE_NAMES=$(find "${FIND_PATH}" -maxdepth 1 -type f -regex '.*\.clish$' -exec bash -c 'grep -r "^##clish" $1 1> /dev/null && echo $1' _ {} \;; true)
|
||||||
|
|
||||||
|
if [ ! -z "${CLISH_FILE_NAMES}" ];then
|
||||||
|
count=0
|
||||||
|
while read -r file
|
||||||
|
do
|
||||||
|
if [ ! -d "${ARCHIVE_PATH}/${START_EXECUTION_DATE}" ];then
|
||||||
|
mkdir -p "${ARCHIVE_PATH}/${START_EXECUTION_DATE}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Starting to work on: \"${file}\" at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |tee -a "${ARCHIVE_PATH}/${START_EXECUTION_DATE}/execution.log"
|
||||||
|
echo "Starting to work on: \"${file}\" at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |logger
|
||||||
|
clish -f "${file}"
|
||||||
|
mv -v "${file}" "${ARCHIVE_PATH}/${START_EXECUTION_DATE}/${count}.clish_${START_EXECUTION_TIME}"
|
||||||
|
echo "Finished working on: \"${file}\" , Exit Code: $? , at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |tee -a "${ARCHIVE_PATH}/${START_EXECUTION_DATE}/execution.log"
|
||||||
|
echo "Finished working on: \"${file}\" , Exit Code: $? , at: $( date +"%Y_%m_%d_%H_%M_%SS" ) , Execution count: ${count}" |logger
|
||||||
|
((count++))
|
||||||
|
done <<< "${CLISH_FILE_NAMES}"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Removing LOCKFILE: \"${LOCK_FILE}\""
|
echo "Removing LOCKFILE: \"${LOCK_FILE}\""
|
||||||
rm -fv "${LOCK_FILE}"
|
rm -fv "${LOCK_FILE}"
|
||||||
@ -83,43 +100,44 @@ EXIT_EXECUTION_TIME=$( date +"%Y_%m_%d_%H_%M_%SS" )
|
|||||||
EXIT_EXECUTION_DATE=$( date +"%Y_%m_%d" )
|
EXIT_EXECUTION_DATE=$( date +"%Y_%m_%d" )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TMP_REMOTE_IN_FILE=$( mktemp )
|
||||||
|
|
||||||
while true
|
while true
|
||||||
do
|
do
|
||||||
|
|
||||||
LOCAL_MD5=$( md5sum "${FILENAME}" |awk '{print $1}' )
|
LOCAL_MD5=$( md5sum "${FILENAME}" |awk '{print $1}' )
|
||||||
if [ ! -z "${EXPECTED_MD5}" ];then
|
if [ ! -z "${EXPECTED_MD5}" ];then
|
||||||
if [ "${LOCAL_MD5}" == "${EXPECTED_MD5}" ]; then
|
if [ "${LOCAL_MD5}" == "${EXPECTED_MD5}" ]; then
|
||||||
if [ "${RUN_AS_A_FUNCTION}" -eq "1" ];then
|
if [ "${RUN_AS_A_FUNCTION}" -eq "1" ];then
|
||||||
collect
|
collect
|
||||||
else
|
else
|
||||||
/bin/bash /storage/collect-clish-scripts.sh >/dev/null 2>&1
|
/bin/bash /storage/collect-clish-scripts.sh >/dev/null 2>&1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep 5
|
sleep 5
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
REMOTE_ETAG=$(${CURL} -k -s -I "${URL}" |grep "Etag" -i |head -1 |awk '{print $2}'|sed -e "s@\"@@")
|
REMOTE_ETAG=$(${CURL} -s -I "${URL}" |grep "Etag" -i |head -1 |awk '{print $2}'|sed -e "s@\"@@")
|
||||||
if [ "${CURRENT_ETAG}" != "${REMOTE_ETAG}" ];then
|
if [ "${CURRENT_ETAG}" != "${REMOTE_ETAG}" ];then
|
||||||
/usr/bin/wget -q "${URL}" -O "${FILENAME}.in"
|
${CURL} -s "${URL}" -o "${TMP_REMOTE_IN_FILE}"
|
||||||
REMOTE_MD5=$( md5sum "${FILENAME}.in" |awk '{print $1}' )
|
REMOTE_MD5=$( md5sum "${TMP_REMOTE_IN_FILE}" |awk '{print $1}' )
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${LOCAL_ETAG}" != "${REMOTE_ETAG}" ];then
|
|
||||||
CURRENT_ETAG="${REMOTE_ETAG}"
|
|
||||||
if [ "${REMOTE_MD5}" != "${LOCAL_MD5}" ]; then
|
|
||||||
mv "${FILENAME}.in" "${FILENAME}"
|
|
||||||
LOCAL_MD5="${REMOTE_MD5}"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
if [ "${RUN_AS_A_FUNCTION}" -eq "1" ];then
|
|
||||||
collect
|
|
||||||
else
|
|
||||||
/bin/bash /storage/collect-clish-scripts.sh >/dev/null 2>&1
|
|
||||||
fi
|
|
||||||
|
|
||||||
sleep 5
|
if [ "${LOCAL_ETAG}" != "${REMOTE_ETAG}" ];then
|
||||||
fi
|
CURRENT_ETAG="${REMOTE_ETAG}"
|
||||||
|
if [ "${REMOTE_MD5}" != "${LOCAL_MD5}" ]; then
|
||||||
|
mv "${TMP_REMOTE_IN_FILE}" "${FILENAME}"
|
||||||
|
LOCAL_MD5="${REMOTE_MD5}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [ "${RUN_AS_A_FUNCTION}" -eq "1" ];then
|
||||||
|
collect
|
||||||
|
else
|
||||||
|
/bin/bash /storage/collect-clish-scripts.sh >/dev/null 2>&1
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 5
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
@ -15,5 +15,5 @@ rm -f -v "${LOCK_FILE}"
|
|||||||
wget ${SCRIPT_URL} \\
|
wget ${SCRIPT_URL} \\
|
||||||
-O ${SCRIPT_PATH} && \\
|
-O ${SCRIPT_PATH} && \\
|
||||||
md5sum ${SCRIPT_PATH} | grep \"^${SCRIPT_MD5_SUM} \" >/dev/null && \\
|
md5sum ${SCRIPT_PATH} | grep \"^${SCRIPT_MD5_SUM} \" >/dev/null && \\
|
||||||
bash ${SCRIPT_PATH} >/dev/null 2>&1 &
|
bash ${SCRIPT_PATH} $(date|md5sum |awk '{print $1}')>/dev/null 2>&1 &
|
||||||
logger \"Exiting /pfrm2.0/etc/userScript\""
|
logger \"Exiting /pfrm2.0/etc/userScript\""
|
||||||
|
Loading…
Reference in New Issue
Block a user