Update install.sh

This commit is contained in:
2025-06-19 12:22:09 +00:00
parent ce018af136
commit 410a25e54c

View File

@@ -17,47 +17,47 @@ function banner() {
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}"
function system_status() {
echo -e "${YELLOW}...checking system status...${NC}"
echo -n "📦 Docker: "
if command -v docker &>/dev/null; then
echo -e "${GREEN}Installed${NC}"
else
echo -e "${RED}Missing${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}"
echo -n "🐳 Docker Compose: "
if docker compose version &>/dev/null; then
echo -e "${GREEN}Available (v2)${NC}"
else
echo -e "${RED}Missing or incompatible${NC}"
fi
}
function check_existing_container() {
echo -n "📁 Project folder '$CLONE_DIR': "
if [ -d "$CLONE_DIR" ]; then
echo -e "${YELLOW}Exists${NC}"
else
echo -e "${GREEN}Not present${NC}"
fi
echo -n "📦 Docker image '$CRON_NAME': "
if docker images --format '{{.Repository}}' | grep -q "$CRON_NAME"; then
echo -e "${YELLOW}Already built${NC}"
else
echo -e "${RED}Not built${NC}"
fi
echo -n "📦 Docker container '$CRON_NAME': "
if docker ps -a --format '{{.Names}}' | grep -q "^$CRON_NAME$"; then
echo -e "${RED}⚠️ A Docker container named '$CRON_NAME' already exists.${NC}"
read -rp "$(echo -e "${YELLOW}🔁 Do you want to stop and remove it (including its image)? [Y/n] ${NC}")" confirm
confirm="${confirm,,}"
if [[ "$confirm" =~ ^(y|yes| )?$ ]]; then
echo -e "${YELLOW}🛑 Removing existing container and image...${NC}"
docker rm -f "$CRON_NAME" >/dev/null 2>&1
docker rmi -f "$CRON_NAME" >/dev/null 2>&1
echo -e "${GREEN}✅ Removed container and image.${NC}"
else
echo -e "${RED}❌ Skipping removal. Existing instance may cause conflicts.${NC}"
fi
echo -e "${YELLOW}Exists${NC}"
else
echo -e "${GREEN}Not running${NC}"
fi
}
function menu() {
echo ""
echo -e "${YELLOW}1. Install bot (clone + build)"
echo -e "2. Check bot status + send test Telegram message"
echo -e "3. Enable .log file logging"
@@ -68,104 +68,12 @@ function menu() {
echo ""
}
function set_telegram_credentials() {
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}"
}
# Insert your previous functions here if needed (clone_and_build, etc.)
# Only showing header and status + menu for this snippet.
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}"
}
function check_bot_status() {
if [ ! -d "$CLONE_DIR" ]; then
echo -e "${RED}❌ Folder '$CLONE_DIR' not found. Please install the bot first.${NC}"
return
fi
cd "$CLONE_DIR" || exit 1
if ! docker images | grep -q "$CRON_NAME"; then
echo -e "${RED}❌ Docker image '$CRON_NAME' not found. Please build it first.${NC}"
return
fi
echo -e "${GREEN}✅ Docker image found.${NC}"
echo -e "${YELLOW}📨 Running the bot once to send a test Telegram message...${NC}"
docker compose run --rm "$CRON_NAME"
echo -e "${GREEN}✅ Done. If your Telegram is configured, you should receive a message now.${NC}"
}
# 🔍 Check for container/image immediately on script start
check_existing_container
# ⏳ Main Menu Loop
while true; do
clear
banner
menu
read -rp "$(echo -e "${YELLOW}👉 Choose [1-7]: ${NC}")" choice
case $choice in
1) clone_and_build ;;
2) check_bot_status ;;
3) enable_logging ;;
4) setup_cronjob ;;
5) show_cronjobs ;;
6) remove_cronjob ;;
7) 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
# Start script
clear
banner
system_status
menu
read -rp "$(echo -e "${YELLOW}👉 Choose [1-7]: ${NC}")" choice