gravity-sync/includes/gs-automate.sh
Michael Stanclift ff32465625
3.7.0 (#315)
* add path removal to clear_cron

* 3.6.4

* add local folder to path

* templates for systemd timers

* Wantedby

* user root

* Gonna be 3.7

* Remove legacy cron code from automate function

* Core for moving replication jobs into place

* Move stop before copy

* Add Gravity Sync messages

* Service file customization

* Line break

* 10 minutes to sudo

* if active else

* Do things quietly

* Custom exec path

* clear exec start

* warning about cron

* no longer exists
2022-03-29 16:11:00 -05:00

148 lines
3.8 KiB
Bash

# GRAVITY SYNC BY VMSTAN #####################
# gs-automate.sh #############################
# For documentation or downloading updates visit https://github.com/vmstan/gravity-sync
# This code is called from the main gravity-sync.sh file and should not execute directly!
## Automate Task
function task_automate {
TASKTYPE='AUTOMATE'
MESSAGE="${MESSAGE}: ${TASKTYPE}"
echo_good
CRON_EXIST='0'
CRON_CHECK=$(crontab -l | grep -q "${GS_FILENAME}" && echo '1' || echo '0')
if [ ${CRON_CHECK} == 1 ]
then
MESSAGE="${UI_AUTO_CRON_EXISTS}"
echo_warn
clear_cron
fi
MESSAGE="Customizing service file username"
sed -i "/User=unknown/c\User=$USER" ${LOCAL_FOLDR}/templates/gravity-sync.service
error_validate
MESSAGE="Customizing service file executable"
sed -i "/ExecStart=/c\ExecStart=${LOCAL_FOLDR}/${GS_FILENAME}" ${LOCAL_FOLDR}/templates/gravity-sync.service
error_validate
if systemctl is-active --quiet gravity-sync
then
MESSAGE="Stopping existing systemd service"
sudo systemctl stop gravity-sync
error_validate
fi
MESSAGE="Moving systemd timer into place"
sudo cp ${LOCAL_FOLDR}/templates/gravity-sync.timer ${DAEMON_PATH}
error_validate
MESSAGE="Moving systemd service into place"
sudo cp ${LOCAL_FOLDR}/templates/gravity-sync.service ${DAEMON_PATH}
error_validate
MESSAGE="Reloading systemd daemon"
sudo systemctl daemon-reload --quiet
error_validate
MESSAGE="Enabling Gravity Sync timer"
sudo systemctl enable gravity-sync.timer --quiet
error_validate
MESSAGE="Starting Gravity Sync service"
sudo systemctl start gravity-sync --quiet
error_validate
exit_withchange
}
function task_autocron {
TASKTYPE='AUTOCRON'
MESSAGE="${MESSAGE}: ${TASKTYPE}"
echo_good
MESSAGE="Crontab automation is deprecated and will be removed in a future release"
echo_warn
CRON_EXIST='0'
CRON_CHECK=$(crontab -l | grep -q "${GS_FILENAME}" && echo '1' || echo '0')
if [ ${CRON_CHECK} == 1 ]
then
MESSAGE="${UI_AUTO_CRON_EXISTS}"
echo_warn
CRON_EXIST='1'
fi
MESSAGE="${UI_AUTO_CRON_DISPLAY_FREQ}"
echo_info
if [[ $1 =~ ^[0-9][0-9]?$ ]]
then
INPUT_AUTO_FREQ=$1
else
MESSAGE="${UI_AUTO_CRON_SELECT_FREQ}"
echo_need
read INPUT_AUTO_FREQ
INPUT_AUTO_FREQ="${INPUT_AUTO_FREQ:-15}"
fi
if [ $INPUT_AUTO_FREQ == 5 ] || [ $INPUT_AUTO_FREQ == 10 ] || [ $INPUT_AUTO_FREQ == 15 ] || [ $INPUT_AUTO_FREQ == 30 ]
then
if [ $CRON_EXIST == 1 ]
then
clear_cron
fi
path_fix
MESSAGE="${UI_AUTO_CRON_SAVING}"
echo_stat
(crontab -l 2>/dev/null; echo "*/${INPUT_AUTO_FREQ} * * * * ${BASH_PATH} ${LOCAL_FOLDR}/${GS_FILENAME} smart > ${LOG_PATH}/${CRONJOB_LOG}") | crontab -
error_validate
elif [ $INPUT_AUTO_FREQ == 0 ]
then
if [ $CRON_EXIST == 1 ]
then
clear_cron
else
MESSAGE="${UI_AUTO_CRON_DISABLED}"
echo_warn
fi
else
MESSAGE="${UI_INVALID_SELECTION}"
echo_fail
exit_nochange
fi
exit_withchange
}
## Clear Existing Automation Settings
function clear_cron {
MESSAGE="${UI_AUTO_CRON_DISABLED}"
echo_stat
crontab -l > cronjob-old.tmp
sed "/${GS_FILENAME}/d" cronjob-old.tmp > cronjob-new.tmp
crontab cronjob-new.tmp 2>/dev/null
error_validate
rm cronjob-old.tmp
rm cronjob-new.tmp
}
## Cron Task
function task_cron {
TASKTYPE='CRON'
MESSAGE="${MESSAGE}: ${TASKTYPE}"
echo_good
show_crontab
}
function path_fix {
MESSAGE="Adding user path to Crontab"
echo_stat
(crontab -l 2>/dev/null; echo "PATH=$PATH") | crontab -
error_validate
}