-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·112 lines (88 loc) · 3.48 KB
/
deploy.sh
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
112
#!/bin/bash
# Exit on error
set -e
# Set variables
APP_NAME="coins_alert_bot"
DESCRIPTION="My Coins Alert Telegram Bot"
PYTHON_SCRIPT="bot.py"
REQUIREMENTS_FILE="requirements.txt"
DOTENV_FILE=".env"
ENV_PATH="env"
# Function for error handling
handle_error() {
echo "Error: $1"
exit 1
}
# Check if running as root
[[ "$EUID" -ne 0 ]] && handle_error "Please run as root (use sudo)"
# Check if the OS is Linux
[[ "$(uname)" != "Linux" ]] && handle_error "This script must be run on Linux"
# Check if required files exist
[[ ! -f "$PYTHON_SCRIPT" ]] && handle_error "$PYTHON_SCRIPT not found in current directory"
[[ ! -f "$DOTENV_FILE" ]] && handle_error "$DOTENV_FILE not found in current directory"
[[ ! -f "$REQUIREMENTS_FILE" ]] && handle_error "$REQUIREMENTS_FILE not found in current directory"
echo "Installing required Python packages..."
apt-get update || handle_error "Failed to update package list"
# Install python3
if ! command -v python3 &> /dev/null; then
apt-get install -y python3 || handle_error "Failed to install Python3"
fi
# Install virtualenv
if ! command -v virtualenv &> /dev/null; then
apt-get install -y python3-virtualenv || handle_error "Failed to install virtualenv"
fi
# Create virtual environment
echo "Creating virtual environment..."
virtualenv "$ENV_PATH" || handle_error "Failed to create virtual environment"
# Install required packages
echo "Installing required packages..."
./"$ENV_PATH"/bin/pip install -r "$REQUIREMENTS_FILE" || handle_error "Failed to install requirements"
# Create systemd service
echo "Creating systemd service for $APP_NAME"
SERVICE_FILE="/lib/systemd/system/${APP_NAME}.service"
echo "[Unit]
Description=$DESCRIPTION
After=network.target
[Service]
Type=simple
WorkingDirectory=$(pwd)
ExecStart=$(pwd)/$ENV_PATH/bin/python $(pwd)/$PYTHON_SCRIPT
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target" > "$SERVICE_FILE" || handle_error "Failed to create service file"
# Start and enable service
echo "Starting and enabling service..."
systemctl daemon-reload || handle_error "Failed to reload systemd"
systemctl start "$APP_NAME" || handle_error "Failed to start service"
systemctl enable "$APP_NAME" || handle_error "Failed to enable service"
# Check service status
if systemctl is-active --quiet "$APP_NAME"; then
echo "Service '$APP_NAME' started successfully"
else
handle_error "Failed to start service '$APP_NAME'"
fi
# Restart service to ensure it's running with latest changes
systemctl restart "$APP_NAME" || handle_error "Failed to restart service"
echo "Deployment complete! Your bot is now running as a system service."
echo "Use 'systemctl status $APP_NAME' to check status"
echo "Use 'journalctl -u $APP_NAME' to view logs"
# Function to create swap space if needed
create_swap_space() {
echo "Creating 1GB swap space..."
# Check if swap already exists
if [ -f /swapfile ]; then
echo "Swap file already exists"
return
fi
# 1GB swap space create
fallocate -l 1G /swapfile || handle_error "Failed to create swap file"
chmod 600 /swapfile || handle_error "Failed to set swap file permissions"
mkswap /swapfile || handle_error "Failed to set up swap space"
swapon /swapfile || handle_error "Failed to enable swap space"
# Make permanent
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab || handle_error "Failed to update fstab"
echo "Swap space created and enabled successfully"
}
# Uncomment the following line if you need to create swap space
create_swap_space