-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_install.sh
More file actions
325 lines (265 loc) · 10.5 KB
/
auto_install.sh
File metadata and controls
325 lines (265 loc) · 10.5 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
#
# Non-Interactive Auto Installer for OV-Node (White-Label System)
# This script bypasses all interactive prompts and auto-configures the node
#
set -e
# Color codes
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
BLUE="\033[0;34m"
NC="\033[0m"
# Configuration variables (will be injected by SSH installer)
NODE_SERVICE_PORT="${NODE_SERVICE_PORT:-9090}"
NODE_API_KEY="${NODE_API_KEY}"
R2_ACCESS_KEY_ID="${R2_ACCESS_KEY_ID}"
R2_SECRET_ACCESS_KEY="${R2_SECRET_ACCESS_KEY}"
R2_BUCKET_NAME="${R2_BUCKET_NAME}"
R2_ACCOUNT_ID="${R2_ACCOUNT_ID}"
R2_PUBLIC_BASE_URL="${R2_PUBLIC_BASE_URL}"
R2_DOWNLOAD_TOKEN="${R2_DOWNLOAD_TOKEN:-8638b5a1-77df-4d24-8253-58977fa508a4}"
OPENVPN_PORT="${OPENVPN_PORT:-1194}"
OPENVPN_PROTOCOL="${OPENVPN_PROTOCOL:-udp}"
# Installation directories
APP_NAME="ov-node"
INSTALL_DIR="/opt/$APP_NAME"
REPO_URL="https://github.com/TinyActive/OpenVpn-Panel-Node"
PYTHON="/usr/bin/python3"
VENV_DIR="$INSTALL_DIR/venv"
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Validation
validate_config() {
log_info "Validating configuration..."
if [ -z "$NODE_API_KEY" ]; then
log_error "NODE_API_KEY is required"
exit 1
fi
if [ -z "$R2_ACCESS_KEY_ID" ] || [ -z "$R2_SECRET_ACCESS_KEY" ] || [ -z "$R2_BUCKET_NAME" ]; then
log_error "R2 configuration is incomplete (ACCESS_KEY_ID, SECRET_ACCESS_KEY, BUCKET_NAME required)"
exit 1
fi
if [ -z "$R2_ACCOUNT_ID" ]; then
log_error "R2_ACCOUNT_ID is required"
exit 1
fi
if [ -z "$R2_PUBLIC_BASE_URL" ]; then
log_error "R2_PUBLIC_BASE_URL is required"
exit 1
fi
log_success "Configuration validated"
}
# Install system dependencies
install_dependencies() {
log_info "Installing system dependencies..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -y
apt-get install -y python3 python3-pip python3-venv wget curl git iptables openssl ca-certificates gnupg
log_success "System dependencies installed"
}
# Auto-configure OpenVPN (non-interactive)
install_openvpn() {
log_info "Installing OpenVPN in non-interactive mode..."
# Check if OpenVPN is already installed
if [ -f "/etc/openvpn/server/server.conf" ]; then
log_warning "OpenVPN is already installed, skipping installation"
return 0
fi
# Get the primary IP address
if [ -z "$SERVER_IP" ]; then
SERVER_IP=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | head -1)
fi
if [ -z "$SERVER_IP" ]; then
log_error "Could not detect server IP address"
exit 1
fi
log_info "Server IP: $SERVER_IP"
log_info "OpenVPN Port: $OPENVPN_PORT"
log_info "OpenVPN Protocol: $OPENVPN_PROTOCOL"
# Copy our custom openvpn-install.sh from repo
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ -f "$SCRIPT_DIR/openvpn-install.sh" ]; then
log_info "Using openvpn-install.sh from repository..."
cp "$SCRIPT_DIR/openvpn-install.sh" /root/openvpn-install.sh
chmod +x /root/openvpn-install.sh
elif [ ! -f "/root/openvpn-install.sh" ]; then
log_info "Downloading OpenVPN installer..."
wget https://git.io/vpn -O /root/openvpn-install.sh
chmod +x /root/openvpn-install.sh
fi
# Use printf to pipe all inputs to the installer script
# This bypasses all interactive prompts
log_info "Running OpenVPN installer with automated responses..."
# Prepare automated responses:
# 1. IP selection (just press enter for first IP)
# 2. Port (will be auto-selected in the script)
# 3. Protocol (will be auto-selected in the script)
# 4. DNS (will be auto-selected in the script)
# 5. Client name (will be auto-selected in the script)
# The openvpn-install.sh we have already auto-selects everything,
# but it might still show prompts. We just send newlines.
export DEBIAN_FRONTEND=noninteractive
export APPROVE_INSTALL=y
export APPROVE_IP=$SERVER_IP
export MENU_OPTION=1
# Pipe empty responses (just press enter repeatedly)
printf '\n\n\n\n\n\n\n\n\n\n' | bash /root/openvpn-install.sh > /tmp/openvpn_install.log 2>&1
local exit_code=$?
# Check if OpenVPN was installed successfully
if [ -f "/etc/openvpn/server/server.conf" ]; then
log_success "OpenVPN installed successfully"
# Ensure OpenVPN service is running
systemctl enable openvpn-server@server || true
systemctl start openvpn-server@server || true
sleep 2
if systemctl is-active --quiet openvpn-server@server; then
log_success "OpenVPN service is running"
else
log_warning "OpenVPN installed but service not running, will retry..."
systemctl restart openvpn-server@server || true
fi
return 0
else
log_error "OpenVPN installation failed (exit code: $exit_code)"
log_error "Installation log:"
cat /tmp/openvpn_install.log
exit 1
fi
}
# Clone repository
clone_repository() {
log_info "Setting up OV-Node repository..."
if [ -d "$INSTALL_DIR" ]; then
log_warning "Directory exists, removing..."
rm -rf "$INSTALL_DIR"
fi
git clone "$REPO_URL" "$INSTALL_DIR"
cd "$INSTALL_DIR"
log_success "Repository cloned to $INSTALL_DIR"
}
# Setup Python virtual environment
setup_virtualenv() {
log_info "Creating Python virtual environment..."
if [ ! -d "$VENV_DIR" ]; then
$PYTHON -m venv "$VENV_DIR"
fi
log_info "Installing Python dependencies..."
"$VENV_DIR/bin/pip" install --upgrade pip
if [ -f "$INSTALL_DIR/requirements.txt" ]; then
"$VENV_DIR/bin/pip" install -r "$INSTALL_DIR/requirements.txt"
else
log_warning "requirements.txt not found, installing basic dependencies..."
"$VENV_DIR/bin/pip" install fastapi uvicorn psutil pydantic_settings python-dotenv colorama pexpect requests
fi
log_success "Virtual environment configured"
}
# Configure .env file
configure_env() {
log_info "Configuring environment variables..."
ENV_FILE="$INSTALL_DIR/.env"
# Create .env from .env.example
if [ -f "$INSTALL_DIR/.env.example" ]; then
cp "$INSTALL_DIR/.env.example" "$ENV_FILE"
else
log_error ".env.example not found"
exit 1
fi
# Replace all configuration values
sed -i "s/^SERVICE_PORT = .*/SERVICE_PORT = $NODE_SERVICE_PORT/" "$ENV_FILE"
sed -i "s/^API_KEY = .*/API_KEY = $NODE_API_KEY/" "$ENV_FILE"
sed -i "s/^R2_ACCESS_KEY_ID = .*/R2_ACCESS_KEY_ID = $R2_ACCESS_KEY_ID/" "$ENV_FILE"
sed -i "s/^R2_SECRET_ACCESS_KEY = .*/R2_SECRET_ACCESS_KEY = $R2_SECRET_ACCESS_KEY/" "$ENV_FILE"
sed -i "s/^R2_BUCKET_NAME = .*/R2_BUCKET_NAME = $R2_BUCKET_NAME/" "$ENV_FILE"
sed -i "s/^R2_ACCOUNT_ID = .*/R2_ACCOUNT_ID = $R2_ACCOUNT_ID/" "$ENV_FILE"
sed -i "s|^R2_PUBLIC_BASE_URL = .*|R2_PUBLIC_BASE_URL = $R2_PUBLIC_BASE_URL|" "$ENV_FILE"
sed -i "s/^R2_DOWNLOAD_TOKEN = .*/R2_DOWNLOAD_TOKEN = $R2_DOWNLOAD_TOKEN/" "$ENV_FILE"
log_success "Environment configuration complete"
}
# Create systemd service
create_systemd_service() {
log_info "Creating systemd service..."
cat > /etc/systemd/system/ov-node.service << EOF
[Unit]
Description=OV-Node App (White-Label)
After=network.target openvpn-server@server.service
[Service]
Type=simple
User=root
WorkingDirectory=$INSTALL_DIR/core
ExecStart=$VENV_DIR/bin/python app.py
Restart=always
RestartSec=5
Environment="PATH=$VENV_DIR/bin:/usr/local/bin:/usr/bin:/bin"
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ov-node
log_success "Systemd service created"
}
# Start service
start_service() {
log_info "Starting OV-Node service..."
systemctl start ov-node
sleep 3
if systemctl is-active --quiet ov-node; then
log_success "OV-Node service is running"
else
log_error "OV-Node service failed to start"
systemctl status ov-node --no-pager
exit 1
fi
}
# Display summary
display_summary() {
echo ""
echo -e "${GREEN}═══════════════════════════════════════════════════════${NC}"
echo -e "${GREEN} OV-Node Installation Completed Successfully!${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════════${NC}"
echo ""
echo -e "${BLUE}Configuration Summary:${NC}"
echo -e " • Node Address: ${GREEN}$SERVER_IP${NC}"
echo -e " • Node Port: ${GREEN}$NODE_SERVICE_PORT${NC}"
echo -e " • OpenVPN Port: ${GREEN}$OPENVPN_PORT${NC}"
echo -e " • OpenVPN Protocol: ${GREEN}$OPENVPN_PROTOCOL${NC}"
echo -e " • R2 Bucket: ${GREEN}$R2_BUCKET_NAME${NC}"
echo -e " • Service Status: ${GREEN}$(systemctl is-active ov-node)${NC}"
echo ""
echo -e "${YELLOW}Next Steps:${NC}"
echo -e " 1. Check service: ${BLUE}systemctl status ov-node${NC}"
echo -e " 2. View logs: ${BLUE}journalctl -u ov-node -f${NC}"
echo -e " 3. The node will be automatically synced with the panel"
echo ""
echo -e "${GREEN}═══════════════════════════════════════════════════════${NC}"
}
# Main installation flow
main() {
echo -e "${BLUE}═══════════════════════════════════════════════════════${NC}"
echo -e "${BLUE} OV-Node Auto Installer (Non-Interactive)${NC}"
echo -e "${BLUE} White-Label System - TinyActive${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════${NC}"
echo ""
validate_config
install_dependencies
install_openvpn
clone_repository
setup_virtualenv
configure_env
create_systemd_service
start_service
display_summary
exit 0
}
# Run main installation
main