-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-binary.sh
More file actions
executable file
·312 lines (260 loc) · 8.64 KB
/
install-binary.sh
File metadata and controls
executable file
·312 lines (260 loc) · 8.64 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
#!/bin/bash
# Ubuntu Resource Monitor - Binary Installation Script
# Downloads pre-built binary from GitHub Releases (no Rust/Cargo needed!)
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
GITHUB_REPO="dadashussein/monitoring"
INSTALL_DIR="/opt/ubuntu-resource-monitor"
SERVICE_NAME="ubuntu-resource-monitor"
BINARY_NAME="ubuntu_resource_api"
# Default values
DEFAULT_PORT="8080"
DEFAULT_ADDRESS="0.0.0.0"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--port)
PORT="$2"
shift 2
;;
--address)
BIND_ADDRESS="$2"
shift 2
;;
--nginx-available)
NGINX_AVAILABLE="$2"
shift 2
;;
--nginx-enabled)
NGINX_ENABLED="$2"
shift 2
;;
--docker-socket)
DOCKER_SOCKET="$2"
shift 2
;;
--non-interactive)
NON_INTERACTIVE=true
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Set defaults if not provided
PORT=${PORT:-$DEFAULT_PORT}
BIND_ADDRESS=${BIND_ADDRESS:-$DEFAULT_ADDRESS}
NGINX_AVAILABLE=${NGINX_AVAILABLE:-/etc/nginx/sites-available}
NGINX_ENABLED=${NGINX_ENABLED:-/etc/nginx/sites-enabled}
DOCKER_SOCKET=${DOCKER_SOCKET:-unix:///var/run/docker.sock}
# Print colored message
print_message() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
print_message "$RED" "❌ Bu skript root olaraq çalışdırılmalıdır. 'sudo' istifadə edin."
exit 1
fi
}
# Detect system architecture
detect_arch() {
local arch=$(uname -m)
case $arch in
x86_64)
ARCH="x86_64"
;;
aarch64|arm64)
ARCH="aarch64"
;;
armv7l)
ARCH="armv7"
;;
*)
print_message "$RED" "❌ Dəstəklənməyən arxitektura: $arch"
exit 1
;;
esac
print_message "$GREEN" "✅ Arxitektura: $ARCH"
}
# Get latest release version
get_latest_version() {
print_message "$BLUE" "🔍 Ən son versiya yoxlanılır..."
# Try to get latest release from GitHub API
LATEST_VERSION=$(curl -s "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$LATEST_VERSION" ]; then
print_message "$YELLOW" "⚠️ GitHub API-dən versiya alına bilmədi. v1.0.0 istifadə edilir."
LATEST_VERSION="v1.0.0"
fi
print_message "$GREEN" "✅ Versiya: $LATEST_VERSION"
}
# Download binary
download_binary() {
print_message "$BLUE" "📥 Binary yüklənir..."
# Construct download URL
BINARY_URL="https://github.com/$GITHUB_REPO/releases/download/$LATEST_VERSION/ubuntu_resource_api-$ARCH-unknown-linux-gnu"
print_message "$YELLOW" "URL: $BINARY_URL"
# Create temp directory
TMP_DIR=$(mktemp -d)
cd "$TMP_DIR"
# Download binary
if ! curl -L -o "$BINARY_NAME" "$BINARY_URL"; then
print_message "$RED" "❌ Binary yüklənə bilmədi."
print_message "$YELLOW" "💡 Əgər release yoxdursa, əvvəlcə 'make release' ilə yaradın."
rm -rf "$TMP_DIR"
exit 1
fi
# Make executable
chmod +x "$BINARY_NAME"
print_message "$GREEN" "✅ Binary yükləndi"
}
# Get user input for configuration
get_configuration() {
# Skip if non-interactive mode or stdin is not a terminal
if [ "$NON_INTERACTIVE" = true ] || [ ! -t 0 ]; then
print_message "$GREEN" "📝 Konfiqurasiya (non-interactive):"
echo " Server: $BIND_ADDRESS:$PORT"
echo " Nginx Available: $NGINX_AVAILABLE"
echo " Nginx Enabled: $NGINX_ENABLED"
echo " Docker Socket: $DOCKER_SOCKET"
echo ""
return
fi
print_message "$BLUE" "⚙️ Konfiqurasiya"
echo ""
# Get bind address
read -p "Server adresi (default: $BIND_ADDRESS): " INPUT_ADDRESS
BIND_ADDRESS=${INPUT_ADDRESS:-$BIND_ADDRESS}
# Get port
read -p "Server portu (default: $PORT): " INPUT_PORT
PORT=${INPUT_PORT:-$PORT}
# Nginx paths
read -p "Nginx sites-available yolu (default: $NGINX_AVAILABLE): " INPUT_NGINX_AVAILABLE
NGINX_AVAILABLE=${INPUT_NGINX_AVAILABLE:-$NGINX_AVAILABLE}
read -p "Nginx sites-enabled yolu (default: $NGINX_ENABLED): " INPUT_NGINX_ENABLED
NGINX_ENABLED=${INPUT_NGINX_ENABLED:-$NGINX_ENABLED}
# Docker socket
read -p "Docker socket yolu (default: $DOCKER_SOCKET): " INPUT_DOCKER_SOCKET
DOCKER_SOCKET=${INPUT_DOCKER_SOCKET:-$DOCKER_SOCKET}
echo ""
print_message "$GREEN" "📝 Konfiqurasiya:"
echo " Server: $BIND_ADDRESS:$PORT"
echo " Nginx Available: $NGINX_AVAILABLE"
echo " Nginx Enabled: $NGINX_ENABLED"
echo " Docker Socket: $DOCKER_SOCKET"
echo ""
read -p "Davam etmək istəyirsiniz? (y/n): " CONFIRM
if [[ ! $CONFIRM =~ ^[Yy]$ ]]; then
print_message "$YELLOW" "Quraşdırma ləğv edildi."
rm -rf "$TMP_DIR"
exit 0
fi
}
# Install the application
install_application() {
print_message "$BLUE" "📦 Tətbiq quraşdırılır..."
# Create installation directory
mkdir -p "$INSTALL_DIR"
# Copy binary
cp "$BINARY_NAME" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
# Clean up temp directory
cd /
rm -rf "$TMP_DIR"
print_message "$GREEN" "✅ Tətbiq quraşdırıldı: $INSTALL_DIR"
}
# Create systemd service
create_service() {
print_message "$BLUE" "🔧 Systemd servisi yaradılır..."
cat > "/etc/systemd/system/${SERVICE_NAME}.service" <<EOF
[Unit]
Description=Ubuntu Resource Monitor
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/$BINARY_NAME
Restart=always
RestartSec=10
# Environment variables
Environment="SERVER_BIND_ADDRESS=$BIND_ADDRESS:$PORT"
Environment="NGINX_SITES_AVAILABLE=$NGINX_AVAILABLE"
Environment="NGINX_SITES_ENABLED=$NGINX_ENABLED"
Environment="DOCKER_SOCKET_PATH=$DOCKER_SOCKET"
Environment="RUST_LOG=info"
# Security settings
NoNewPrivileges=false
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd
systemctl daemon-reload
print_message "$GREEN" "✅ Systemd servisi yaradıldı"
}
# Start the service
start_service() {
print_message "$BLUE" "🚀 Servis başladılır..."
# Enable service to start on boot
systemctl enable "$SERVICE_NAME"
# Start service
systemctl start "$SERVICE_NAME"
# Wait a moment for service to start
sleep 2
# Check status
if systemctl is-active --quiet "$SERVICE_NAME"; then
print_message "$GREEN" "✅ Servis uğurla başladıldı!"
else
print_message "$RED" "❌ Servis başlamadı. Status yoxlayın: systemctl status $SERVICE_NAME"
exit 1
fi
}
# Print success message
print_success() {
echo ""
print_message "$GREEN" "🎉 Quraşdırma tamamlandı!"
echo ""
print_message "$BLUE" "📊 Dashboard: http://$BIND_ADDRESS:$PORT/dashboard"
print_message "$BLUE" "🔄 Nginx Manager: http://$BIND_ADDRESS:$PORT/nginx"
print_message "$BLUE" "🐳 Docker Manager: http://$BIND_ADDRESS:$PORT/docker"
echo ""
print_message "$YELLOW" "Faydalı əmrlər:"
echo " Servisi dayandır: sudo systemctl stop $SERVICE_NAME"
echo " Servisi başlat: sudo systemctl start $SERVICE_NAME"
echo " Servisi yenidən başlat: sudo systemctl restart $SERVICE_NAME"
echo " Status yoxla: sudo systemctl status $SERVICE_NAME"
echo " Logları gör: sudo journalctl -u $SERVICE_NAME -f"
echo " Servisi sil: sudo bash uninstall.sh"
echo ""
}
# Main installation flow
main() {
print_message "$GREEN" "╔════════════════════════════════════════╗"
print_message "$GREEN" "║ Ubuntu Resource Monitor - Installer ║"
print_message "$GREEN" "║ (Binary Installation - No Rust) ║"
print_message "$GREEN" "╚════════════════════════════════════════╝"
echo ""
check_root
detect_arch
get_latest_version
get_configuration
download_binary
install_application
create_service
start_service
print_success
}
# Run main function
main