-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·103 lines (84 loc) · 2.82 KB
/
deploy.sh
File metadata and controls
executable file
·103 lines (84 loc) · 2.82 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
#!/bin/bash
# Automated deployment script for SplitBot
# Usage: ./deploy.sh <server_ip> [ssh_user]
set -e
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
if [ -z "$1" ]; then
echo -e "${RED}Error: Server IP required${NC}"
echo "Usage: ./deploy.sh <server_ip> [ssh_user]"
exit 1
fi
SERVER_IP=$1
SSH_USER=${2:-$USER}
BOT_NAME="split-bot"
# Prompt for SSH key if needed
read -p "Enter SSH key path [default: ~/.ssh/id_rsa]: " SSH_KEY
SSH_KEY=${SSH_KEY:-"$HOME/.ssh/id_rsa"}
SSH_OPTS="-i $SSH_KEY"
echo -e "${GREEN}Deploying $BOT_NAME to $SSH_USER@$SERVER_IP...${NC}"
# 1. Create deployment package
echo -e "${GREEN}[1/4] Creating deployment package...${NC}"
DEPLOY_DIR=$(mktemp -d)
tar -czf "$DEPLOY_DIR/bundle.tar.gz" \
--exclude='venv' \
--exclude='__pycache__' \
--exclude='.git' \
--exclude='.env' \
.
# 2. Upload to server
echo -e "${GREEN}[2/4] Uploading to server...${NC}"
scp $SSH_OPTS "$DEPLOY_DIR/bundle.tar.gz" "$SSH_USER@$SERVER_IP:/tmp/"
# Upload .env separately for security (or assuming it's already there)
# scp $SSH_OPTS .env "$SSH_USER@$SERVER_IP:/tmp/.env.prod"
# 3. Remote Execution
echo -e "${GREEN}[3/4] Configuring remote server...${NC}"
ssh $SSH_OPTS "$SSH_USER@$SERVER_IP" "bash -s" << 'ENDSSH'
set -e
BOT_NAME="split-bot"
INSTALL_DIR="$HOME/$BOT_NAME"
# Clean/Create Dir
# Stop service if running, ignore errors if it doesn't exist
sudo systemctl stop split-bot 2>/dev/null || true
mkdir -p "$INSTALL_DIR"
# Extract
tar -xzf /tmp/bundle.tar.gz -C "$INSTALL_DIR"
rm /tmp/bundle.tar.gz
# Setup Venv
cd "$INSTALL_DIR"
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
source venv/bin/activate
pip install -r requirements.txt
# Systemd Service
# Note: Using 'cat' to create the service file.
# Ensure variables like $USER and $INSTALL_DIR are expanded correctly by the *local* shell before sending,
# OR escape them if they need to be dynamic on the server.
# Here we want the *remote* user and *remote* install dir, but we set them at the top of this script block.
cat > split-bot.service << EOF
[Unit]
Description=SplitBot Telegram Service
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$INSTALL_DIR
# Ensure .env exists in INSTALL_DIR or set vars here
EnvironmentFile=$INSTALL_DIR/.env
ExecStart=$INSTALL_DIR/venv/bin/python $INSTALL_DIR/bot.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo mv split-bot.service /etc/systemd/system/split-bot.service
sudo systemctl daemon-reload
sudo systemctl enable split-bot
sudo systemctl start split-bot
ENDSSH
echo -e "${GREEN}[4/4] Deployment Complete!${NC}"
ssh $SSH_OPTS "$SSH_USER@$SERVER_IP" "sudo systemctl status split-bot --no-pager"