136 lines
4.1 KiB
Bash
136 lines
4.1 KiB
Bash
#!/bin/bash
|
||
|
||
REPO_URL="https://git.bitmaster.cc/BitMaster/tesla-bot.git"
|
||
CLONE_DIR="tesla-bot"
|
||
CRON_NAME="tesla-inventory-bot"
|
||
|
||
GREEN="\e[32m"
|
||
RED="\e[31m"
|
||
YELLOW="\e[33m"
|
||
NC="\e[0m"
|
||
|
||
function banner() {
|
||
echo -e "${GREEN}"
|
||
echo "=============================="
|
||
echo "🚗 Tesla Inventory Bot Setup"
|
||
echo "=============================="
|
||
echo -e "${NC}"
|
||
}
|
||
|
||
function check_and_install_docker() {
|
||
if ! command -v docker &> /dev/null; then
|
||
echo -e "${YELLOW}🐳 Docker not found. Installing Docker...${NC}"
|
||
apt update
|
||
apt install -y docker.io
|
||
systemctl enable --now docker
|
||
echo -e "${GREEN}✅ Docker installed.${NC}"
|
||
fi
|
||
|
||
if ! docker compose version &>/dev/null; then
|
||
echo -e "${YELLOW}➕ Installing Docker Compose plugin...${NC}"
|
||
if apt-cache show docker-compose-plugin &>/dev/null; then
|
||
apt install -y docker-compose-plugin
|
||
else
|
||
mkdir -p ~/.docker/cli-plugins
|
||
curl -SL https://github.com/docker/compose/releases/download/v2.27.1/docker-compose-linux-x86_64 \
|
||
-o ~/.docker/cli-plugins/docker-compose
|
||
chmod +x ~/.docker/cli-plugins/docker-compose
|
||
export PATH="$HOME/.docker/cli-plugins:$PATH"
|
||
fi
|
||
echo -e "${GREEN}✅ Docker Compose v2 installed.${NC}"
|
||
fi
|
||
}
|
||
|
||
function menu() {
|
||
echo -e "${YELLOW}1. Install bot (clone + build)"
|
||
echo -e "2. Enable .log file logging"
|
||
echo -e "3. Setup new cronjob (every X min)"
|
||
echo -e "4. Show current cronjobs"
|
||
echo -e "5. Remove existing cronjob"
|
||
echo -e "6. Exit${NC}"
|
||
echo ""
|
||
}
|
||
|
||
function set_telegram_credentials() {
|
||
# REMOVE THIS LINE:
|
||
# cd "$CLONE_DIR" || exit 1
|
||
|
||
echo -e "${YELLOW}📨 Enter your Telegram bot token:${NC}"
|
||
read -rp "🔑 TELEGRAM_TOKEN: " token
|
||
echo -e "${YELLOW}👥 Enter your Telegram chat ID:${NC}"
|
||
read -rp "🆔 TELEGRAM_CHAT_ID: " chat_id
|
||
sed -i "s|^TELEGRAM_TOKEN = .*|TELEGRAM_TOKEN = \"$token\"|" bot.py
|
||
sed -i "s|^CHAT_ID = .*|CHAT_ID = \"$chat_id\"|" bot.py
|
||
echo -e "${GREEN}✅ Credentials updated in bot.py${NC}"
|
||
}
|
||
|
||
|
||
function clone_and_build() {
|
||
if [ -d "$CLONE_DIR" ]; then
|
||
echo -e "${RED}⚠️ Folder '$CLONE_DIR' already exists.${NC}"
|
||
read -rp "$(echo -e "${YELLOW}🔁 Do you want me to remove it? Press Enter to continue, or Ctrl+C to cancel...${NC}")"
|
||
rm -rf "$CLONE_DIR"
|
||
echo -e "${GREEN}✅ Removed old '$CLONE_DIR' folder.${NC}"
|
||
fi
|
||
|
||
echo -e "${GREEN}📥 Cloning repo...${NC}"
|
||
git clone "$REPO_URL" "$CLONE_DIR"
|
||
cd "$CLONE_DIR" || { echo -e "${RED}❌ Failed to enter directory $CLONE_DIR.${NC}"; exit 1; }
|
||
|
||
check_and_install_docker
|
||
|
||
echo -e "${GREEN}🔧 Building Docker image...${NC}"
|
||
docker compose build
|
||
|
||
set_telegram_credentials
|
||
}
|
||
|
||
|
||
function enable_logging() {
|
||
cd "$CLONE_DIR" || exit 1
|
||
sed -i '1s|^|exec > >(tee -a /app/bot.log) 2>&1\n|' bot.py
|
||
echo -e "${GREEN}📝 Logging enabled.${NC}"
|
||
}
|
||
|
||
function setup_cronjob() {
|
||
cd "$CLONE_DIR" || exit 1
|
||
echo -e "${YELLOW}📆 Every how many minutes should the bot run? (1-60)${NC}"
|
||
read -rp "🕓 Interval: " interval
|
||
if ! [[ "$interval" =~ ^[0-9]+$ ]] || [ "$interval" -lt 1 ] || [ "$interval" -gt 60 ]; then
|
||
echo -e "${RED}❌ Invalid input. Enter a number between 1 and 60.${NC}"
|
||
return
|
||
fi
|
||
cron_expr="*/$interval * * * *"
|
||
CMD="cd $(pwd) && docker compose run --rm $CRON_NAME >> ./data/cron.log 2>&1"
|
||
JOB="$cron_expr $CMD"
|
||
crontab -l 2>/dev/null | grep -v "$CRON_NAME" | crontab -
|
||
(crontab -l 2>/dev/null; echo "$JOB") | crontab -
|
||
echo -e "${GREEN}✅ Cronjob installed to run every $interval minutes.${NC}"
|
||
}
|
||
|
||
function show_cronjobs() {
|
||
crontab -l | grep "$CRON_NAME" || echo -e "${RED}❌ No cronjobs found.${NC}"
|
||
}
|
||
|
||
function remove_cronjob() {
|
||
crontab -l 2>/dev/null | grep -v "$CRON_NAME" | crontab -
|
||
echo -e "${GREEN}🗑️ Cronjob removed.${NC}"
|
||
}
|
||
|
||
while true; do
|
||
clear
|
||
banner
|
||
menu
|
||
read -rp "$(echo -e "${YELLOW}👉 Choose [1-6]: ${NC}")" choice
|
||
case $choice in
|
||
1) clone_and_build ;;
|
||
2) enable_logging ;;
|
||
3) setup_cronjob ;;
|
||
4) show_cronjobs ;;
|
||
5) remove_cronjob ;;
|
||
6) echo -e "${RED}❌ Exiting...${NC}"; exit 0 ;;
|
||
*) echo -e "${RED}❌ Invalid option.${NC}" ;;
|
||
esac
|
||
read -rp "$(echo -e "${GREEN}🔁 Press Enter to continue...${NC}")"
|
||
done
|