Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
400 lines (343 loc) · 12.7 KB
/
Copy pathdeploy.sh
File metadata and controls
400 lines (343 loc) · 12.7 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
# ====================
# Observability Stack Deployment Script
# ====================
# WARNING: Review and customize this script for your environment before use.
# This script automates the deployment of an observability stack including
# OpenTelemetry Collector, ClickHouse, and Grafana.
# ====================
set -e # Exit on any error
# Configuration - Override these via environment variables if needed
DATA_DIR="${DATA_DIR:-/opt/observability-stack}"
CLICKHOUSE_PASSWORD_PLACEHOLDER="${CLICKHOUSE_PASSWORD_PLACEHOLDER:-CLICKHOUSE_PASSWORD_PLACEHOLDER}"
GRAFANA_PASSWORD_PLACEHOLDER="${GRAFANA_PASSWORD_PLACEHOLDER:-GRAFANA_PASSWORD_PLACEHOLDER}"
echo "========================================="
echo "Observability Stack Deployment"
echo "========================================="
echo ""
# Check if running as root
if [[ $EUID -eq 0 ]]; then
echo "Error: This script should not be run as root."
echo "Please run as a non-root user with sudo privileges."
exit 1
fi
# Install Docker if not present
install_docker() {
if ! command -v docker &> /dev/null; then
echo "Docker not found. Installing Docker..."
# Detect OS and install Docker
if [ -f /etc/amazon-linux-release ]; then
# Amazon Linux
echo "Detected Amazon Linux. Installing Docker..."
sudo yum update -y
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER || true
elif [ -f /etc/debian_version ]; then
# Ubuntu/Debian
echo "Detected Debian/Ubuntu. Installing Docker..."
sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER || true
else
# Generic Linux
echo "Attempting to install Docker via get.docker.com..."
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER || true
rm get-docker.sh
fi
echo ""
echo "Docker installed successfully."
echo "IMPORTANT: Please log out and back in, then run this script again"
echo "to apply Docker group changes."
exit 0
else
echo "✓ Docker is already installed."
fi
}
# Install Docker Compose
install_docker_compose() {
if ! command -v docker-compose &> /dev/null; then
echo "Docker Compose not found. Installing..."
# Download latest Docker Compose from GitHub releases
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Verify installation
if docker-compose --version &> /dev/null; then
echo "✓ Docker Compose installed successfully."
else
echo "Error: Docker Compose installation failed."
exit 1
fi
else
echo "✓ Docker Compose is already installed."
fi
}
# Create required directories
setup_directories() {
echo ""
echo "Setting up data directories..."
# Create directory structure
sudo mkdir -p ${DATA_DIR}/data/{clickhouse,grafana}
# Set ownership to current user
sudo chown -R $USER:$USER ${DATA_DIR}
echo "✓ Directories created at: ${DATA_DIR}"
echo " - ${DATA_DIR}/data/clickhouse"
echo " - ${DATA_DIR}/data/grafana"
}
# Update configuration files with secure passwords
update_passwords() {
echo ""
echo "Generating secure passwords..."
# Generate cryptographically secure random passwords
CLICKHOUSE_PASS=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
GRAFANA_PASS=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
# Check if .env file exists
if [ ! -f .env ]; then
echo "Error: .env file not found in current directory."
echo "Please ensure .env file exists with password placeholders."
exit 1
fi
# Update .env file
if grep -q "${CLICKHOUSE_PASSWORD_PLACEHOLDER}" .env; then
sed -i "s/${CLICKHOUSE_PASSWORD_PLACEHOLDER}/${CLICKHOUSE_PASS}/g" .env
echo "✓ Updated ClickHouse password in .env"
else
echo "Warning: ClickHouse password placeholder not found in .env"
fi
if grep -q "${GRAFANA_PASSWORD_PLACEHOLDER}" .env; then
sed -i "s/${GRAFANA_PASSWORD_PLACEHOLDER}/${GRAFANA_PASS}/g" .env
echo "✓ Updated Grafana password in .env"
else
echo "Warning: Grafana password placeholder not found in .env"
fi
# Update ClickHouse users.xml if it exists
if [ -f clickhouse/users.xml ]; then
if grep -q "${CLICKHOUSE_PASSWORD_PLACEHOLDER}" clickhouse/users.xml; then
sed -i "s/${CLICKHOUSE_PASSWORD_PLACEHOLDER}/${CLICKHOUSE_PASS}/g" clickhouse/users.xml
echo "✓ Updated ClickHouse password in users.xml"
fi
fi
# Update Grafana datasource configuration if it exists
if [ -f grafana/provisioning/datasources/datasources.yml ]; then
if grep -q "${CLICKHOUSE_PASSWORD_PLACEHOLDER}" grafana/provisioning/datasources/datasources.yml; then
sed -i "s/${CLICKHOUSE_PASSWORD_PLACEHOLDER}/${CLICKHOUSE_PASS}/g" grafana/provisioning/datasources/datasources.yml
echo "✓ Updated ClickHouse password in Grafana datasource"
fi
fi
echo ""
echo "========================================="
echo "Generated Credentials"
echo "========================================="
echo "ClickHouse Password: $CLICKHOUSE_PASS"
echo "Grafana Password: $GRAFANA_PASS"
echo "========================================="
echo ""
echo "IMPORTANT: These credentials have been saved to ./.credentials"
echo "Please store them securely and delete the file after saving elsewhere."
echo ""
# Save credentials to secure file
cat > .credentials <<EOF
========================================
Observability Stack Credentials
========================================
Generated on: $(date)
ClickHouse Password: $CLICKHOUSE_PASS
Grafana Password: $GRAFANA_PASS
========================================
IMPORTANT: Delete this file after saving credentials securely!
========================================
EOF
# Restrict file permissions to owner only
chmod 600 .credentials
}
# Deploy the stack using Docker Compose
deploy_stack() {
echo ""
echo "Deploying observability stack..."
# Check if docker-compose.yml exists
if [ ! -f docker-compose.yml ]; then
echo "Error: docker-compose.yml not found in current directory."
exit 1
fi
# Pull latest images
echo "Pulling latest Docker images..."
docker-compose pull
# Start services in detached mode
echo "Starting services..."
docker-compose up -d
echo "✓ Services started successfully."
}
# Wait for services to be ready
wait_for_services() {
echo ""
echo "Waiting for services to become ready..."
echo "This may take a few minutes..."
echo ""
# Wait for ClickHouse
echo "[1/3] Checking ClickHouse..."
for i in {1..60}; do
if docker-compose exec -T clickhouse clickhouse-client --query "SELECT 1" &>/dev/null; then
echo "✓ ClickHouse is ready."
break
fi
if [ $i -eq 60 ]; then
echo "Error: ClickHouse did not become ready in time (5 minutes)."
echo "Check logs with: docker-compose logs clickhouse"
exit 1
fi
echo " Waiting for ClickHouse... (${i}/60)"
sleep 5
done
# Wait for OpenTelemetry Collector
echo "[2/3] Checking OpenTelemetry Collector..."
for i in {1..30}; do
if curl -sf http://localhost:13133/ &>/dev/null; then
echo "✓ OpenTelemetry Collector is ready."
break
fi
if [ $i -eq 30 ]; then
echo "Error: OpenTelemetry Collector did not become ready in time (2.5 minutes)."
echo "Check logs with: docker-compose logs otel-collector"
exit 1
fi
echo " Waiting for OpenTelemetry Collector... (${i}/30)"
sleep 5
done
# Wait for Grafana
echo "[3/3] Checking Grafana..."
for i in {1..30}; do
if curl -sf http://localhost:3000/api/health &>/dev/null; then
echo "✓ Grafana is ready."
break
fi
if [ $i -eq 30 ]; then
echo "Error: Grafana did not become ready in time (2.5 minutes)."
echo "Check logs with: docker-compose logs grafana"
exit 1
fi
echo " Waiting for Grafana... (${i}/30)"
sleep 5
done
echo ""
echo "✓ All services are up and running!"
}
# Detect public IP for cloud deployments
get_public_ip() {
local PUBLIC_IP=""
# Try AWS EC2 metadata service
PUBLIC_IP=$(curl -s --connect-timeout 2 http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null)
# If AWS metadata fails, try public IP detection service
if [ -z "$PUBLIC_IP" ]; then
PUBLIC_IP=$(curl -s --connect-timeout 2 https://checkip.amazonaws.com 2>/dev/null | tr -d '\n')
fi
# If still no IP, try another service
if [ -z "$PUBLIC_IP" ]; then
PUBLIC_IP=$(curl -s --connect-timeout 2 https://api.ipify.org 2>/dev/null)
fi
# Fallback to localhost
if [ -z "$PUBLIC_IP" ]; then
PUBLIC_IP="localhost"
fi
echo "$PUBLIC_IP"
}
# Display completion information
show_completion() {
local PUBLIC_IP=$(get_public_ip)
echo ""
echo "========================================="
echo "Deployment Completed Successfully!"
echo "========================================="
echo ""
echo "Service Access Information:"
echo "----------------------------------------"
echo "Grafana Dashboard:"
echo " URL: http://${PUBLIC_IP}:3000"
echo " Username: admin"
echo " Password: (see ./.credentials file)"
echo ""
echo "OpenTelemetry Endpoints:"
echo " HTTP (traces/logs): http://${PUBLIC_IP}:4318"
echo " gRPC (metrics): grpc://${PUBLIC_IP}:4317"
echo " Health Check: http://${PUBLIC_IP}:13133"
echo ""
echo "ClickHouse:"
echo " Host: ${PUBLIC_IP}:9000"
echo " HTTP: http://${PUBLIC_IP}:8123"
echo "========================================="
echo ""
echo "Container Status:"
echo "----------------------------------------"
docker-compose ps
echo ""
echo "========================================="
echo "Next Steps:"
echo "========================================="
echo ""
echo "1. Configure Your Application"
echo " Set OTLP_HTTP_ENDPOINT to: http://${PUBLIC_IP}:4318"
echo " (Use http://localhost:4318 for local development)"
echo ""
echo "2. Access Grafana"
echo " Open: http://${PUBLIC_IP}:3000"
echo " Login with credentials from ./.credentials file"
echo ""
echo "3. Start Your Application"
echo " Your app will automatically send telemetry data"
echo ""
echo "4. Explore Dashboards"
echo " View pre-configured dashboards or create custom ones"
echo ""
echo "========================================="
echo "Useful Commands:"
echo "========================================="
echo ""
echo "View all logs:"
echo " docker-compose logs -f"
echo ""
echo "View specific service logs:"
echo " docker-compose logs -f [clickhouse|otel-collector|grafana]"
echo ""
echo "Stop all services:"
echo " docker-compose down"
echo ""
echo "Restart all services:"
echo " docker-compose restart"
echo ""
echo "Access ClickHouse CLI:"
echo " docker-compose exec clickhouse clickhouse-client"
echo ""
echo "Check service health:"
echo " docker-compose ps"
echo ""
echo "========================================="
echo ""
}
# Main deployment orchestration
main() {
echo "Starting deployment process..."
echo ""
# Run deployment steps
install_docker
install_docker_compose
setup_directories
update_passwords
deploy_stack
wait_for_services
show_completion
echo ""
echo "✓ Observability stack deployment finished successfully!"
echo ""
echo "Remember to:"
echo " 1. Save credentials from ./.credentials file"
echo " 2. Delete ./.credentials file after saving"
echo " 3. Configure firewall/security groups for required ports"
echo ""
}
# Execute main function
main "$@"