2017-06-22 14:39:07 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
INFRA_REFERENCE={{ dataminer_infra_reference }}
|
|
|
|
ADD_ALGORITHM_DIR={{ smartgears_user_home }}/algorithmInstaller
|
|
|
|
ADD_ALGORITHM_PATH={{ smartgears_user_home }}/algorithmInstaller/addAlgorithm.sh
|
2017-07-25 18:37:59 +02:00
|
|
|
ALGORITHMS_FILE={{ dataminer_wps_algorithms_dest }}/${INFRA_REFERENCE}/algorithms
|
2017-06-22 14:39:07 +02:00
|
|
|
OUT_DIR=$( mktemp -d -t algorithms-updater.XXXXXXXXXX )
|
|
|
|
ALGORITHMS_TEMP_SCRIPT=$OUT_DIR/add_algorithms
|
2017-09-04 19:37:03 +02:00
|
|
|
LOG_DIR={{ smartgears_user_home }}/wps_algorithms_install_log
|
2017-06-22 14:39:07 +02:00
|
|
|
LOG_FILE=${LOG_DIR}/algorithms_updater.log
|
2017-07-19 18:26:35 +02:00
|
|
|
ALGORITHMS_INSTALLED_FILE=${LOG_DIR}/already_installed_algorithms.txt
|
2017-06-22 14:39:07 +02:00
|
|
|
LOCK_FILE=${LOG_DIR}/.algorithms_updater.lock
|
2017-07-25 18:37:59 +02:00
|
|
|
ALGO_DIR={{ dataminer_wps_algorithms_dest }}
|
2017-06-22 14:39:07 +02:00
|
|
|
RUNNING_JOB=
|
|
|
|
RUNNING_JOB_RETVAL=
|
|
|
|
|
|
|
|
trap "logger 'algorithms-updater: trap intercepted, exiting.' ; cleanup 1" SIGHUP SIGINT SIGTERM
|
|
|
|
|
|
|
|
function cleanup() {
|
|
|
|
rm -fr $OUT_DIR
|
|
|
|
rm -f $LOCK_FILE
|
2017-07-26 17:17:06 +02:00
|
|
|
logger 'algorithms-updater: Exiting'
|
2017-06-22 14:39:07 +02:00
|
|
|
exit $1
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_log_dir() {
|
|
|
|
if [ ! -d $LOG_DIR ] ; then
|
|
|
|
mkdir -p $LOG_DIR
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_lock_file() {
|
|
|
|
# Create the lock file
|
|
|
|
if [ -f $LOCK_FILE ] ; then
|
|
|
|
set +o pipefail
|
|
|
|
set +e
|
2017-07-25 15:59:47 +02:00
|
|
|
RUNNING_JOB=$( ps auwwx | grep `cat $LOCK_FILE` | grep -v grep )
|
2017-06-22 14:39:07 +02:00
|
|
|
RUNNING_JOB_RETVAL=$?
|
|
|
|
if [ $RUNNING_JOB_RETVAL -eq 0 ] ; then
|
|
|
|
logger 'algorithms-updater: another job still running, exiting.'
|
|
|
|
rm -fr $OUT_DIR
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
rm -f $LOCK_FILE
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
logger 'algorithms-updater: no other jobs running, proceeding.'
|
|
|
|
fi
|
|
|
|
set -o pipefail
|
|
|
|
set -e
|
|
|
|
echo "$$" > $LOCK_FILE
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_svn_repo() {
|
|
|
|
logger 'algorithms-updater: update the SVN repo'
|
|
|
|
cd $ALGO_DIR
|
|
|
|
svn update > $LOG_FILE 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
function algorithms_updater() {
|
|
|
|
logger 'algorithms-updater: scan the algorithms list and build the algorithms script. Reference infra is {{ dataminer_infra_reference }}'
|
2017-07-19 18:26:35 +02:00
|
|
|
echo "#!/bin/bash" > $ALGORITHMS_TEMP_SCRIPT.head
|
|
|
|
echo "cd $ADD_ALGORITHM_DIR" >> $ALGORITHMS_TEMP_SCRIPT.head
|
|
|
|
echo "" >> $ALGORITHMS_TEMP_SCRIPT.head
|
2017-09-04 19:37:03 +02:00
|
|
|
awk -F \| '{ print $2 "|" $6 "|" $8 }' $ALGORITHMS_FILE > $ALGORITHMS_TEMP_SCRIPT.algorithmslist
|
2017-07-19 18:26:35 +02:00
|
|
|
sed -i -e 's/<notextile>//g' $ALGORITHMS_TEMP_SCRIPT.algorithmslist
|
|
|
|
sed -i -e 's/<\/notextile>//g' $ALGORITHMS_TEMP_SCRIPT.algorithmslist
|
2017-07-25 17:32:31 +02:00
|
|
|
sed -i -e 's/\ N\ /\ Y\ /g' $ALGORITHMS_TEMP_SCRIPT.algorithmslist
|
2017-07-19 18:26:35 +02:00
|
|
|
if [ ! -f $ALGORITHMS_INSTALLED_FILE ] ; then
|
|
|
|
touch $ALGORITHMS_INSTALLED_FILE
|
|
|
|
fi
|
|
|
|
set +o pipefail
|
|
|
|
set +e
|
|
|
|
while read algo ; do
|
|
|
|
ALGO_PRESENT=
|
|
|
|
ALGO_NAME=$( echo "$algo" | awk -F \| '{ print $1 }' )
|
|
|
|
ALGO_BODY=$( echo "$algo" | awk -F \| '{ print $2 }' )
|
2017-09-04 19:37:03 +02:00
|
|
|
ALGO_DATE=$( echo "$algo" | awk -F \| '{ print $3 }' )
|
|
|
|
ALGO_TIMESTAMP=$( date +%s -d "$ALGO_DATE" )
|
2017-07-25 18:29:54 +02:00
|
|
|
ALGO_LINE=$( egrep ^"$ALGO_NAME" $ALGORITHMS_INSTALLED_FILE )
|
2017-07-19 18:26:35 +02:00
|
|
|
ALGO_PRESENT=$?
|
|
|
|
if [ $ALGO_PRESENT -ne 0 ] ; then
|
|
|
|
echo "logger 'algorithms-updater: running the add command of algorithm $ALGO_NAME'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "logger 'algorithms-updater: the add command string is $ALGO_BODY'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "RETVAL=" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo '-------------------------------------------'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo 'Adding algorithm $ALGO_NAME'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo 'With command $ALGO_BODY'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "$ALGO_BODY" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo 'RETVAL=$?' >> $ALGORITHMS_TEMP_SCRIPT.body_
|
2017-07-25 17:51:26 +02:00
|
|
|
echo "echo 'Done.'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
2017-07-19 18:26:35 +02:00
|
|
|
echo 'if [ $RETVAL -ne 0 ] ; then' >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo " logger 'algorithms-updater: the adding of algorithm $ALGO_NAME failed'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "else" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
2017-09-04 19:37:03 +02:00
|
|
|
echo " echo '$ALGO_NAME | $ALGO_DATE' >> $ALGORITHMS_INSTALLED_FILE" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
2017-07-19 18:26:35 +02:00
|
|
|
echo " logger 'algorithms-updater: the adding of algorithm $ALGO_NAME succeeded'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "fi" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
2017-09-04 19:37:03 +02:00
|
|
|
else
|
|
|
|
ALGO_LOCAL_TIMESTAMP=0
|
|
|
|
ALGO_LOCAL_DATE=$( egrep ^"$ALGO_NAME" $ALGORITHMS_INSTALLED_FILE | awk -F \| '{ print $2 }' | grep -v \' )
|
|
|
|
ALGO_LOCAL_TIMESTAMP=$( date +%s -d "$ALGO_LOCAL_DATE" )
|
|
|
|
if [ $ALGO_LOCAL_TIMESTAMP -ne $ALGO_TIMESTAMP ] ; then
|
|
|
|
echo "logger 'algorithms-updater: running the add command of algorithm $ALGO_NAME'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "logger 'algorithms-updater: the add command string is $ALGO_BODY'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "RETVAL=" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo '-------------------------------------------'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo 'Adding algorithm $ALGO_NAME'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo 'With command $ALGO_BODY'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "$ALGO_BODY" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo 'RETVAL=$?' >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo 'Done.'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "echo ''" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo 'if [ $RETVAL -ne 0 ] ; then' >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo " logger 'algorithms-updater: the adding of algorithm $ALGO_NAME failed'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "else" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo " echo 'grep -v $ALGO_NAME $ALGORITHMS_INSTALLED_FILE > ${ALGORITHMS_INSTALLED_FILE}_ " >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo " echo 'mv ${ALGORITHMS_INSTALLED_FILE}_ $ALGORITHMS_INSTALLED_FILE" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo " echo '$ALGO_NAME | $ALGO_DATE' >> $ALGORITHMS_INSTALLED_FILE" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo " logger 'algorithms-updater: the adding of algorithm $ALGO_NAME succeeded'" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
echo "fi" >> $ALGORITHMS_TEMP_SCRIPT.body_
|
|
|
|
fi
|
2017-07-19 18:26:35 +02:00
|
|
|
fi
|
|
|
|
done < ${ALGORITHMS_TEMP_SCRIPT}.algorithmslist
|
|
|
|
set -o pipefail
|
|
|
|
set -e
|
2017-07-26 17:17:06 +02:00
|
|
|
if [ -f $ALGORITHMS_TEMP_SCRIPT.body_ ] ; then
|
|
|
|
mv $ALGORITHMS_TEMP_SCRIPT.body_ $ALGORITHMS_TEMP_SCRIPT.body
|
|
|
|
cat $ALGORITHMS_TEMP_SCRIPT.head $ALGORITHMS_TEMP_SCRIPT.body > $ALGORITHMS_TEMP_SCRIPT
|
|
|
|
chmod 755 $ALGORITHMS_TEMP_SCRIPT
|
|
|
|
if [ -x $ADD_ALGORITHM_PATH ] ; then
|
|
|
|
logger 'algorithms-updater: add the algorithms configurations. Reference infra is {{ dataminer_infra_reference }}'
|
|
|
|
$ALGORITHMS_TEMP_SCRIPT >> $LOG_FILE 2>&1
|
|
|
|
else
|
|
|
|
logger 'algorithms-updater: {{ smartgears_user_home }}/algorithmInstaller/addAlgorithm.sh is not an executable, aborting'
|
|
|
|
cleanup 1
|
|
|
|
fi
|
2017-06-22 14:39:07 +02:00
|
|
|
else
|
2017-07-26 17:17:06 +02:00
|
|
|
cleanup 0
|
2017-06-22 14:39:07 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Main
|
|
|
|
#
|
|
|
|
create_log_dir
|
|
|
|
check_lock_file
|
|
|
|
update_svn_repo
|
|
|
|
algorithms_updater
|
|
|
|
cleanup 0
|