forked from Zebratic/localping
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·352 lines (299 loc) · 10.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·352 lines (299 loc) · 10.4 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
set -e
echo "🚀 LocalPing Installation Script"
echo "=================================="
# Check if running with sudo
if [[ $EUID -ne 0 ]]; then
echo "❌ This script must be run with sudo"
exit 1
fi
# Parse arguments
REPO_URL="${1:-https://github.com/zebratic/localping.git}"
BRANCH="${2:-main}"
INSTALL_DIR="${3:-/opt/localping}"
SKIP_UPDATE="${4:-false}"
echo "📋 Installation Configuration:"
echo " Repository: $REPO_URL"
echo " Branch: $BRANCH"
echo " Install Path: $INSTALL_DIR"
echo ""
# Detect if already installed
IS_UPDATE=false
if [ -d "$INSTALL_DIR" ]; then
if [ -f "$INSTALL_DIR/.env" ] && [ -f "$INSTALL_DIR/package.json" ]; then
echo "✓ Existing LocalPing installation detected at $INSTALL_DIR"
echo ""
echo "What will be updated:"
echo " • Source code (latest version from $BRANCH branch)"
echo " • Dependencies (npm packages)"
echo " • Systemd service configuration"
echo ""
echo "What will be preserved:"
echo " • Database (data/localping.db)"
echo " • Configuration (.env file)"
echo " • Admin credentials"
echo ""
# Only ask for confirmation if running in interactive terminal and not skipping confirmation
if [ "$SKIP_UPDATE" != "true" ] && [ -t 0 ]; then
read -p "Continue with update? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "❌ Update cancelled."
exit 0
fi
fi
IS_UPDATE=true
else
echo "⚠️ Directory exists at $INSTALL_DIR but is not a valid LocalPing installation"
echo "❌ Please remove the directory or use a different path"
exit 1
fi
fi
echo "📦 Installing system dependencies..."
apt-get update -qq 2>/dev/null || apt-get update
# Install minimal dependencies
apt-get install -y -qq \
curl \
git \
build-essential \
python3 \
libnotify-bin \
2>/dev/null || apt-get install -y \
curl \
git \
build-essential \
python3 \
libnotify-bin
# Install Node.js if not present
if ! command -v node &> /dev/null; then
echo "📦 Installing Node.js v20..."
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - 2>/dev/null
apt-get install -y -qq nodejs 2>/dev/null || apt-get install -y nodejs
fi
# Install git if not present (shouldn't happen but just in case)
if ! command -v git &> /dev/null; then
echo "📦 Installing git..."
apt-get install -y -qq git 2>/dev/null || apt-get install -y git
fi
echo "✅ System dependencies installed"
# Clone or update repository
if [ "$IS_UPDATE" = false ]; then
echo "📥 Cloning LocalPing repository..."
mkdir -p "$(dirname "$INSTALL_DIR")"
git clone --branch "$BRANCH" "$REPO_URL" "$INSTALL_DIR"
else
echo "🔄 Updating LocalPing repository..."
cd "$INSTALL_DIR"
git fetch origin
git checkout "$BRANCH"
git pull origin "$BRANCH"
fi
cd "$INSTALL_DIR"
# Validate required files exist
if [ ! -f ".env.example" ]; then
echo "❌ .env.example not found at $INSTALL_DIR"
echo "The repository may be corrupted. Please try again."
exit 1
fi
echo "📥 Installing npm dependencies..."
npm install --silent 2>/dev/null || npm install
echo "📁 Creating data directory..."
mkdir -p "$INSTALL_DIR/data"
echo "🗄️ Setting up PostgreSQL..."
# Install PostgreSQL if not present
if ! command -v psql &> /dev/null; then
echo "📦 Installing PostgreSQL..."
apt-get install -y -qq postgresql postgresql-contrib 2>/dev/null || apt-get install -y postgresql postgresql-contrib
systemctl enable postgresql
systemctl start postgresql
echo "✅ PostgreSQL installed"
else
echo "✓ PostgreSQL already installed"
# Ensure PostgreSQL is running
systemctl start postgresql 2>/dev/null || true
fi
# Wait for PostgreSQL to be ready
sleep 2
# Generate secure database password
DB_PASSWORD=$(openssl rand -base64 24 | tr -d "=+/" | cut -c1-24)
DB_NAME="localping"
DB_USER="localping"
# Create database and user if they don't exist
echo "🔧 Configuring PostgreSQL database..."
sudo -u postgres psql -c "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" | grep -q 1 || sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';"
sudo -u postgres psql -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" | grep -q 1 || sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
# Configure PostgreSQL for local-only access (security)
PG_VERSION=$(sudo -u postgres psql -t -c "SHOW server_version_num;" | xargs)
PG_MAJOR_VERSION=$(echo "$PG_VERSION" | cut -c1-2)
PG_HBA_FILE="/etc/postgresql/$PG_MAJOR_VERSION/main/pg_hba.conf"
if [ -f "$PG_HBA_FILE" ]; then
# Ensure local connections use md5 (password) authentication
if ! grep -q "^local.*$DB_NAME.*$DB_USER.*md5" "$PG_HBA_FILE"; then
# Add local connection rule if it doesn't exist
echo "local $DB_NAME $DB_USER md5" | sudo tee -a "$PG_HBA_FILE" > /dev/null
fi
# Ensure host connections are restricted to localhost only
if ! grep -q "^host.*$DB_NAME.*$DB_USER.*127.0.0.1/32.*md5" "$PG_HBA_FILE"; then
echo "host $DB_NAME $DB_USER 127.0.0.1/32 md5" | sudo tee -a "$PG_HBA_FILE" > /dev/null
fi
# Reload PostgreSQL configuration
systemctl reload postgresql 2>/dev/null || sudo -u postgres pg_ctl reload -D /var/lib/postgresql/$PG_MAJOR_VERSION/main 2>/dev/null || true
fi
echo "✅ PostgreSQL database configured"
echo "🔧 Setting up environment file..."
if [ ! -f ".env" ]; then
# Generate secure random strings
SESSION_SECRET=$(openssl rand -base64 32)
ADMIN_API_KEY=$(openssl rand -hex 16)
cat > .env << EOF
# Server Configuration
API_PORT=8000
NODE_ENV=production
SESSION_SECRET=$SESSION_SECRET
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=$DB_NAME
DB_USER=$DB_USER
DB_PASSWORD=$DB_PASSWORD
# Notification Settings
NOTIFICATION_ENABLED=true
NOTIFICATION_METHOD=dbus
# Monitoring
PING_INTERVAL=60
ALERT_COOLDOWN=300
# API Authentication
ADMIN_API_KEY=$ADMIN_API_KEY
# Admin Credentials (set during first-time setup)
# ADMIN_PASSWORD=
# ADMIN_USERNAME=
EOF
echo "✅ .env file created with secure values"
else
if [ "$IS_UPDATE" = true ]; then
echo "✓ Preserving existing .env file"
# Add DB config if missing (for existing installations)
if ! grep -q "^DB_HOST=" .env; then
echo "" >> .env
echo "# Database Configuration" >> .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_NAME=$DB_NAME" >> .env
echo "DB_USER=$DB_USER" >> .env
echo "DB_PASSWORD=$DB_PASSWORD" >> .env
echo "✅ Added database configuration to .env"
fi
else
echo "⚠️ .env already exists, skipping creation"
fi
fi
echo "🔐 Setting ICMP capabilities for Node.js..."
NODE_PATH=$(which node)
setcap cap_net_raw=ep "$NODE_PATH" 2>/dev/null || true
# Get the user who ran sudo
SERVICE_USER="${SUDO_USER:-root}"
SERVICE_NAME="localping"
echo "🛠️ Creating systemd service..."
# Get Node.js path
NODE_BIN=$(which node)
# Create systemd service file to run Node.js directly (production-optimized)
cat > "/etc/systemd/system/${SERVICE_NAME}.service" << SYSTEMD_EOF
[Unit]
Description=LocalPing - System Uptime Monitor
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=$SERVICE_USER
WorkingDirectory=$INSTALL_DIR
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
Environment="NODE_ENV=production"
# Start Node.js app directly
ExecStart=$NODE_BIN $INSTALL_DIR/src/app.js
# Restart automatically on failure
Restart=on-failure
RestartSec=10
# Grant ICMP permissions for ping functionality
AmbientCapabilities=CAP_NET_RAW
CapabilityBoundingSet=CAP_NET_RAW
# Security settings
ProtectHome=true
NoNewPrivileges=true
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
# Process management
StandardOutput=journal
StandardError=journal
SyslogIdentifier=$SERVICE_NAME
[Install]
WantedBy=multi-user.target
SYSTEMD_EOF
# Set permissions
chown root:root "/etc/systemd/system/${SERVICE_NAME}.service"
chmod 644 "/etc/systemd/system/${SERVICE_NAME}.service"
# Reload systemd
systemctl daemon-reload
# Enable and start/restart the service
echo "🚀 Starting LocalPing service..."
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
# Wait for service to start
sleep 3
echo "✅ Systemd service configured"
echo ""
# Show status
echo "📊 Service Status:"
systemctl status "$SERVICE_NAME" --no-pager 2>&1 | head -n 20 || true
echo ""
if [ "$IS_UPDATE" = true ]; then
echo "✅ Update complete!"
echo ""
echo "What was updated:"
echo " ✓ Source code to latest version"
echo " ✓ Dependencies (npm packages)"
echo " ✓ Systemd service configuration"
echo ""
echo "What was preserved:"
echo " ✓ Database (data/localping.db)"
echo " ✓ Configuration (.env file)"
echo " ✓ Admin credentials and monitors"
else
echo "✅ Installation complete!"
echo ""
fi
# Get the IPv4 address
IP_ADDR=$(hostname -I | awk '{print $1}')
if [ -z "$IP_ADDR" ]; then
IP_ADDR="localhost"
fi
echo "📊 Access LocalPing:"
echo " Public Page: http://$IP_ADDR:8000"
echo " API: http://$IP_ADDR:8000/api"
echo ""
if [ "$IS_UPDATE" = false ]; then
echo "🔧 First Time Setup:"
echo " 1. Visit http://$IP_ADDR:8000/setup to complete configuration"
echo " 2. Create admin credentials and configure gateway IP"
echo " 3. You'll be redirected to login at http://$IP_ADDR:8000/admin/login"
echo " 4. Login to access the admin panel"
echo ""
else
echo "🔄 Service has been restarted with the latest code"
echo " Login at http://$IP_ADDR:8000/admin/login"
echo ""
fi
echo ""
echo "📖 Useful Commands:"
echo " View logs: journalctl -u $SERVICE_NAME -f"
echo " Service status: systemctl status $SERVICE_NAME"
echo " Start service: systemctl start $SERVICE_NAME"
echo " Stop service: systemctl stop $SERVICE_NAME"
echo " Restart service: systemctl restart $SERVICE_NAME"
echo " Install directory: $INSTALL_DIR"
echo ""
echo "ℹ️ The service will auto-start on system boot."
echo "ℹ️ Environment file located at: $INSTALL_DIR/.env"
echo ""