-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
111 lines (92 loc) · 2.92 KB
/
Copy pathdeploy.sh
File metadata and controls
111 lines (92 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# Deployment script for Telegram Reminder Bot
# Run as: ./deploy.sh
set -e
echo "🚀 Deploying Telegram Reminder Bot..."
# Configuration
APP_DIR="/opt/reminder-bot"
APP_USER="reminder-bot"
SERVICE_NAME="reminder-bot"
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo "❌ This script must be run as root (use sudo)"
exit 1
fi
# Install Python and required packages
echo "📦 Installing system dependencies..."
apt update
apt install -y python3 python3-pip python3-venv git
# Create application user
if ! id "$APP_USER" &>/dev/null; then
echo "👤 Creating application user: $APP_USER"
useradd --system --shell /bin/false --home-dir "$APP_DIR" --create-home "$APP_USER"
fi
# Create application directory
echo "📁 Setting up application directory: $APP_DIR"
mkdir -p "$APP_DIR"
# Copy application files (assuming script is run from repo root)
echo "📋 Copying application files..."
cp -r . "$APP_DIR/"
chown -R "$APP_USER:$APP_USER" "$APP_DIR"
# Setup Python virtual environment
echo "🐍 Setting up Python virtual environment..."
sudo -u "$APP_USER" python3 -m venv "$APP_DIR/venv"
sudo -u "$APP_USER" "$APP_DIR/venv/bin/pip" install -r "$APP_DIR/requirements.txt"
# Create environment file if it doesn't exist
if [ ! -f "$APP_DIR/.env" ]; then
echo "⚙️ Creating environment configuration..."
cat > "$APP_DIR/.env" << EOF
# Telegram Bot Token - REQUIRED
# Get this from @BotFather on Telegram
TELEGRAM_BOT_TOKEN=your_bot_token_here
# Optional: Log level (DEBUG, INFO, WARNING, ERROR)
LOG_LEVEL=INFO
EOF
chown "$APP_USER:$APP_USER" "$APP_DIR/.env"
chmod 600 "$APP_DIR/.env"
echo "⚠️ Please edit $APP_DIR/.env and set your TELEGRAM_BOT_TOKEN"
fi
# Create systemd service file
echo "🔧 Creating systemd service..."
cat > "/etc/systemd/system/$SERVICE_NAME.service" << EOF
[Unit]
Description=Telegram Reminder Bot
After=network.target
Wants=network.target
[Service]
Type=simple
User=$APP_USER
Group=$APP_USER
WorkingDirectory=$APP_DIR
Environment=PATH=$APP_DIR/venv/bin
EnvironmentFile=$APP_DIR/.env
ExecStart=$APP_DIR/venv/bin/python $APP_DIR/viberemind.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$APP_DIR
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and enable service
echo "🔄 Configuring systemd service..."
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
# Create log directory
mkdir -p /var/log/reminder-bot
chown "$APP_USER:$APP_USER" /var/log/reminder-bot
echo "✅ Deployment complete!"
echo ""
echo "Next steps:"
echo "1. Edit $APP_DIR/.env and set your TELEGRAM_BOT_TOKEN"
echo "2. Start the service: sudo systemctl start $SERVICE_NAME"
echo "3. Check status: sudo systemctl status $SERVICE_NAME"
echo "4. View logs: sudo journalctl -u $SERVICE_NAME -f"
echo ""
echo "The bot will automatically start on system boot."