features
This commit is contained in:
parent
2d89fbdfd8
commit
165db4f8e1
1 changed files with 32 additions and 127 deletions
|
@ -54,10 +54,12 @@ gitUserSetup() {
|
|||
}
|
||||
|
||||
gitDirSetup() {
|
||||
cd /opt/docker || echo_error "could not cd /opt/docker"
|
||||
|
||||
if ping -w2 10.255.255.1; then
|
||||
local location="hl"
|
||||
elif ping -w2 10.69.69.2; then
|
||||
location="hc"
|
||||
location="vps"
|
||||
fi
|
||||
|
||||
git init .
|
||||
|
@ -70,138 +72,37 @@ gitDirSetup() {
|
|||
}
|
||||
|
||||
set_cronjob() {
|
||||
# Color setup using tput
|
||||
local RED=$(tput setaf 1)
|
||||
local GREEN=$(tput setaf 2)
|
||||
local YELLOW=$(tput setaf 3)
|
||||
local BLUE=$(tput setaf 4)
|
||||
local BOLD=$(tput bold)
|
||||
local RESET=$(tput sgr0)
|
||||
|
||||
# Validate user is not root
|
||||
if [[ $EUID -eq 0 ]]; then
|
||||
echo "${RED}Error: Do not run this as root! Use your regular user.${RESET}" >&2
|
||||
return 1
|
||||
fi
|
||||
# Configuration
|
||||
CRON_COMMAND="/opt/scripts/bash/setup/hlpush.sh"
|
||||
CRON_SCHEDULE="0 3,15 * * *"
|
||||
CRON_LOG="./cronjobs.log"
|
||||
CRON_ENTRY="$CRON_SCHEDULE $CRON_COMMAND >> $CRON_LOG 2>&1"
|
||||
|
||||
# Initialize variables
|
||||
local SCHEDULE=""
|
||||
local SCRIPT_PATH=""
|
||||
local LOGGING=""
|
||||
local COMMENT=""
|
||||
local PRESET=""
|
||||
# Check for existing entry
|
||||
EXISTING_ENTRIES=$(crontab -l 2>/dev/null)
|
||||
|
||||
echo "${BOLD}${BLUE}Cronjob Setup Wizard${RESET}"
|
||||
# Add entry if not exists
|
||||
if ! echo "$EXISTING_ENTRIES" | grep -qF "$CRON_ENTRY"; then
|
||||
# Create temporary cron file
|
||||
TMPFILE=$(mktemp)
|
||||
|
||||
# 1. Select preset or custom
|
||||
PS3="$(echo -e "${BOLD}Choose schedule type [1-4]: ${RESET}")"
|
||||
echo "${BOLD}Available presets:${RESET}"
|
||||
select PRESET in "Daily" "Weekly" "Monthly" "Custom"; do
|
||||
case $REPLY in
|
||||
1)
|
||||
SCHEDULE="0 0 * * *"
|
||||
break
|
||||
;;
|
||||
2)
|
||||
SCHEDULE="0 0 * * 0"
|
||||
break
|
||||
;;
|
||||
3)
|
||||
SCHEDULE="0 0 1 * *"
|
||||
break
|
||||
;;
|
||||
4) break ;;
|
||||
*) echo "${RED}Invalid selection!${RESET}" ;;
|
||||
esac
|
||||
done
|
||||
# Preserve existing entries
|
||||
[ -n "$EXISTING_ENTRIES" ] && echo "$EXISTING_ENTRIES" >"$TMPFILE"
|
||||
|
||||
# 2. Custom schedule setup
|
||||
if [[ "$PRESET" == "Custom" ]]; then
|
||||
echo "${YELLOW}Enter schedule components (use '*' for any)${RESET}"
|
||||
read -p "Minute (0-59): " MIN
|
||||
read -p "Hour (0-23): " HOUR
|
||||
read -p "Day of month (1-31): " DOM
|
||||
read -p "Month (1-12): " MONTH
|
||||
read -p "Day of week (0-6, 0=Sunday): " DOW
|
||||
SCHEDULE="$MIN $HOUR $DOM $MONTH $DOW"
|
||||
fi
|
||||
# Add header and new entry
|
||||
echo -e "\n# Daily 3AM/3PM job added $(date)" >>"$TMPFILE"
|
||||
echo "$CRON_ENTRY" >>"$TMPFILE"
|
||||
|
||||
# Validate schedule format
|
||||
local cron_regex='^([0-9*/, -]+[[:space:]]){4}[0-9*/, -]+$'
|
||||
if ! [[ "$SCHEDULE" =~ $cron_regex ]]; then
|
||||
echo "${RED}Invalid cron schedule format!${RESET}"
|
||||
return 1
|
||||
fi
|
||||
# Install new cron file
|
||||
crontab "$TMPFILE"
|
||||
rm -f "$TMPFILE"
|
||||
|
||||
# 3. Script path selection
|
||||
while true; do
|
||||
read -e -p "$(echo -e "${BOLD}Path to script: ${RESET}")" SCRIPT_PATH
|
||||
if [[ ! -f "$SCRIPT_PATH" ]]; then
|
||||
echo "${RED}Script does not exist!${RESET}"
|
||||
continue
|
||||
fi
|
||||
if [[ ! -x "$SCRIPT_PATH" ]]; then
|
||||
read -p "$(echo -e "${YELLOW}Script is not executable. Make it executable? [y/N]: ${RESET}")" -n 1 -r
|
||||
echo
|
||||
[[ $REPLY =~ ^[Yy]$ ]] && chmod +x "$SCRIPT_PATH"
|
||||
fi
|
||||
break
|
||||
done
|
||||
|
||||
# 4. Logging options
|
||||
local LOG_DIR="$HOME/logs"
|
||||
mkdir -p "$LOG_DIR"
|
||||
PS3="$(echo -e "${BOLD}Select logging option [1-4]: ${RESET}")"
|
||||
echo "${BOLD}Logging options:${RESET}"
|
||||
select LOGGING in "Full logging" "Errors only" "No logging" "Custom path"; do
|
||||
case $REPLY in
|
||||
1)
|
||||
LOGGING=">> $LOG_DIR/${SCRIPT_PATH##*/}.log 2>&1"
|
||||
break
|
||||
;;
|
||||
2)
|
||||
LOGGING="2>> $LOG_DIR/${SCRIPT_PATH##*/}.error.log"
|
||||
break
|
||||
;;
|
||||
3)
|
||||
LOGGING="> /dev/null 2>&1"
|
||||
break
|
||||
;;
|
||||
4)
|
||||
read -e -p "Enter full log path: " LOGGING_PATH
|
||||
LOGGING=">> $LOGGING_PATH 2>&1"
|
||||
break
|
||||
;;
|
||||
*) echo "${RED}Invalid selection!${RESET}" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# 5. Add comment
|
||||
read -p "$(echo -e "${BOLD}Optional comment: ${RESET}")" COMMENT
|
||||
[[ -n "$COMMENT" ]] && COMMENT="# $COMMENT"
|
||||
|
||||
# 6. Confirm and install
|
||||
echo "${YELLOW}${BOLD}New cronjob:${RESET}"
|
||||
echo -e "${BLUE}Schedule: ${SCHEDULE}\nCommand: ${SCRIPT_PATH} ${LOGGING}\n${COMMENT}${RESET}"
|
||||
|
||||
read -p "$(echo -e "${BOLD}Add this cronjob? [y/N]: ${RESET}")" -n 1 -r
|
||||
echo
|
||||
[[ $REPLY =~ ^[Yy]$ ]] || return 0
|
||||
|
||||
# Add to crontab
|
||||
(
|
||||
crontab -l 2>/dev/null
|
||||
echo -e "\n${COMMENT}\n${SCHEDULE} ${SCRIPT_PATH} ${LOGGING}"
|
||||
) | crontab -
|
||||
|
||||
# Verify
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo "${GREEN}${BOLD}Cronjob added successfully!${RESET}"
|
||||
echo "${YELLOW}Current cronjobs:${RESET}"
|
||||
crontab -l
|
||||
echo_info "Success: Cronjob installed"
|
||||
echo_note "Verify with: crontab -l"
|
||||
else
|
||||
echo "${RED}Failed to add cronjob!${RESET}"
|
||||
return 1
|
||||
echo_warning "Notice: Cronjob already exists"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -211,7 +112,11 @@ if ! command_exists git; then
|
|||
fi
|
||||
|
||||
gitUserSetup
|
||||
set_cronjob
|
||||
|
||||
[[ -d /opt/docker/ ]] &&
|
||||
if [[ -d /opt/docker/ ]]; then
|
||||
gitDirSetup
|
||||
|
||||
[[ -d /opt/scripts/bash ]] &&
|
||||
set_cronjob
|
||||
|
||||
fi
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue