forked from denoland/denokv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-complete.sh
More file actions
executable file
Β·437 lines (386 loc) Β· 14.3 KB
/
setup-complete.sh
File metadata and controls
executable file
Β·437 lines (386 loc) Β· 14.3 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/bin/bash
# Complete DenoKV Setup Script for Rocky Linux
# This script does everything: PostgreSQL setup, environment setup, and starts DenoKV in background
# Author: Assistant
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m'
# Configuration
DENOKV_USER="denokv"
DENOKV_PASSWORD="denokv_password"
DENOKV_DATABASE="denokv"
POSTGRES_DATA_DIR="/var/lib/pgsql/data"
DENOKV_PORT="4512"
DENOKV_ADDR="0.0.0.0:${DENOKV_PORT}"
DENOKV_LOG_FILE="denokv.log"
DENOKV_PID_FILE="denokv.pid"
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
print_step() { echo -e "${PURPLE}[STEP]${NC} $1"; }
# Determine if we need sudo based on current user
if [[ $EUID -eq 0 ]]; then
SUDO_CMD="" # No sudo needed when running as root
else
if ! command -v sudo >/dev/null 2>&1; then
print_error "sudo is required but not installed. Please install sudo first."
exit 1
fi
SUDO_CMD="sudo" # Use sudo when running as regular user
fi
echo "π Complete DenoKV Setup for Rocky Linux"
echo "========================================="
echo ""
# Step 1: Clean up unnecessary scripts
print_step "Step 1: Cleaning up unnecessary scripts..."
rm -f fresh-postgres-setup.sh setup-rocky-linux.sh start-denokv-background.sh start-denokv-simple.sh test_*.ts 2>/dev/null || true
print_success "Unnecessary scripts removed!"
# Step 2: Stop and remove existing PostgreSQL
print_step "Step 2: Setting up PostgreSQL..."
# Stop PostgreSQL services
$SUDO_CMD systemctl stop postgresql 2>/dev/null || true
$SUDO_CMD systemctl disable postgresql 2>/dev/null || true
$SUDO_CMD pkill -f postgres 2>/dev/null || true
sleep 2
# Remove PostgreSQL packages and data
$SUDO_CMD dnf remove -y postgresql* 2>/dev/null || true
$SUDO_CMD rm -rf /var/lib/pgsql /var/lib/postgresql /var/lib/postgres 2>/dev/null || true
$SUDO_CMD rm -rf /etc/postgresql /etc/postgresql-common /usr/lib/postgresql 2>/dev/null || true
$SUDO_CMD userdel postgres 2>/dev/null || true
$SUDO_CMD groupdel postgres 2>/dev/null || true
$SUDO_CMD rm -rf /tmp/.s.PGSQL.* /var/run/postgresql 2>/dev/null || true
print_success "PostgreSQL completely removed!"
# Step 3: Install latest PostgreSQL
print_status "Installing latest PostgreSQL packages..."
$SUDO_CMD dnf update -y
# Install PostgreSQL (latest available version)
print_status "Installing PostgreSQL (latest available)..."
$SUDO_CMD dnf install -y postgresql postgresql-server postgresql-contrib postgresql-devel
# Verify PostgreSQL version
POSTGRES_VERSION=$(postgres --version 2>/dev/null | grep -o '[0-9]\+\.[0-9]\+' | head -1 || echo "Unknown")
print_success "PostgreSQL $POSTGRES_VERSION installed"
# Step 4: Initialize PostgreSQL
print_status "Initializing PostgreSQL database..."
$SUDO_CMD mkdir -p "$POSTGRES_DATA_DIR"
$SUDO_CMD mkdir -p /var/run/postgresql
$SUDO_CMD chown -R postgres:postgres /var/lib/pgsql
$SUDO_CMD chown -R postgres:postgres /var/run/postgresql
$SUDO_CMD chmod 700 "$POSTGRES_DATA_DIR"
$SUDO_CMD chmod 755 /var/run/postgresql
# Initialize PostgreSQL
print_status "Initializing PostgreSQL database..."
$SUDO_CMD postgresql-setup --initdb
# Step 5: Configure PostgreSQL
print_status "Configuring PostgreSQL..."
$SUDO_CMD tee "$POSTGRES_DATA_DIR/pg_hba.conf" > /dev/null << 'EOF'
# PostgreSQL Client Authentication Configuration File
local all all trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
host all all ::1/128 trust
host all all ::/0 trust
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
EOF
$SUDO_CMD tee "$POSTGRES_DATA_DIR/postgresql.conf" > /dev/null << 'EOF'
# PostgreSQL configuration for DenoKV
listen_addresses = 'localhost'
port = 5432
max_connections = 100
shared_buffers = 128MB
effective_cache_size = 512MB
log_destination = 'stderr'
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d
log_rotation_size = 10MB
log_min_duration_statement = 1000
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8'
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
default_text_search_config = 'pg_catalog.english'
EOF
# Step 6: Start PostgreSQL
print_status "Starting PostgreSQL service..."
$SUDO_CMD systemctl enable postgresql
$SUDO_CMD systemctl start postgresql
# Wait for PostgreSQL to be ready
print_status "Waiting for PostgreSQL to be ready..."
for i in {1..30}; do
if $SUDO_CMD -u postgres psql -c "SELECT 1;" >/dev/null 2>&1; then
print_success "PostgreSQL is ready!"
break
fi
if [ $i -eq 30 ]; then
print_error "PostgreSQL failed to start after 30 seconds"
exit 1
fi
sleep 1
done
# Step 7: Create DenoKV database and user
print_status "Creating DenoKV database and user..."
$SUDO_CMD -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres_password';" 2>/dev/null || true
$SUDO_CMD -u postgres psql -c "CREATE USER denokv WITH PASSWORD 'denokv_password';" 2>/dev/null || true
$SUDO_CMD -u postgres psql -c "CREATE DATABASE denokv OWNER denokv;" 2>/dev/null || true
$SUDO_CMD -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE denokv TO denokv;"
$SUDO_CMD -u postgres psql -c "GRANT ALL PRIVILEGES ON SCHEMA public TO denokv;" 2>/dev/null || true
print_success "PostgreSQL setup completed!"
# Step 8: Set up environment variables
print_step "Step 3: Setting up environment variables..."
# Create environment file
cat > .env << EOF
# DenoKV PostgreSQL Configuration
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=denokv
POSTGRES_USER=denokv
POSTGRES_PASSWORD=denokv_password
# DenoKV Server Configuration
DENOKV_PORT=4512
DENOKV_ACCESS_TOKEN=$(openssl rand -hex 16)
# Development Configuration
RUST_LOG=info
DENO_ENV=production
EOF
# Add environment variables to shell profile
if [ -f ~/.bashrc ]; then
echo "" >> ~/.bashrc
echo "# DenoKV Environment Variables" >> ~/.bashrc
echo "export POSTGRES_HOST=localhost" >> ~/.bashrc
echo "export POSTGRES_PORT=5432" >> ~/.bashrc
echo "export POSTGRES_DB=denokv" >> ~/.bashrc
echo "export POSTGRES_USER=denokv" >> ~/.bashrc
echo "export POSTGRES_PASSWORD=denokv_password" >> ~/.bashrc
echo "export DENOKV_PORT=4512" >> ~/.bashrc
echo "export DENOKV_ACCESS_TOKEN=\$DENOKV_ACCESS_TOKEN" >> ~/.bashrc
echo "export RUST_LOG=info" >> ~/.bashrc
echo "export DENO_ENV=production" >> ~/.bashrc
fi
# Source environment variables for current session
export POSTGRES_HOST=localhost
export POSTGRES_PORT=5432
export POSTGRES_DB=denokv
export POSTGRES_USER=denokv
export POSTGRES_PASSWORD=denokv_password
export DENOKV_PORT=4512
export DENOKV_ACCESS_TOKEN=$DENOKV_ACCESS_TOKEN
export RUST_LOG=info
export DENO_ENV=production
print_success "Environment variables configured!"
echo ""
echo "π Generated Access Token: $DENOKV_ACCESS_TOKEN"
echo "π This token has been saved to .env file and systemd service"
# Step 9: Build DenoKV and setup systemd service
print_step "Step 4: Building DenoKV and setting up systemd service..."
# Stop any existing DenoKV processes
print_status "Stopping any existing DenoKV processes..."
$SUDO_CMD systemctl stop denokv.service 2>/dev/null || true
if [ -f "$DENOKV_PID_FILE" ]; then
PID=$(cat "$DENOKV_PID_FILE")
kill "$PID" 2>/dev/null || true
rm -f "$DENOKV_PID_FILE"
fi
# Build DenoKV
print_status "Building DenoKV..."
if [ ! -f "target/release/denokv" ]; then
cargo build --release
if [ $? -ne 0 ]; then
print_error "Failed to build DenoKV"
exit 1
fi
fi
print_success "DenoKV binary ready"
# Create denokv user if it doesn't exist
if ! id "denokv" &>/dev/null; then
print_status "Creating denokv user..."
$SUDO_CMD useradd -r -s /bin/false -d /var/lib/denokv denokv
$SUDO_CMD mkdir -p /var/lib/denokv
$SUDO_CMD chown denokv:denokv /var/lib/denokv
$SUDO_CMD chmod 755 /var/lib/denokv
fi
# Install DenoKV binary to system location
print_status "Installing DenoKV binary..."
$SUDO_CMD cp target/release/denokv /usr/local/bin/denokv
$SUDO_CMD chmod +x /usr/local/bin/denokv
$SUDO_CMD chown root:root /usr/local/bin/denokv
# Create systemd service file
print_status "Creating systemd service..."
$SUDO_CMD tee /etc/systemd/system/denokv.service > /dev/null << EOF
[Unit]
Description=DenoKV Server
After=network.target postgresql.service
Requires=postgresql.service
[Service]
Type=simple
User=denokv
Group=denokv
WorkingDirectory=/var/lib/denokv
ExecStart=/usr/local/bin/denokv serve --access-token ${DENOKV_ACCESS_TOKEN} --addr ${DENOKV_ADDR}
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=denokv
# Environment variables
Environment=RUST_LOG=info
Environment=DENO_ENV=production
Environment=DENO_KV_DATABASE_TYPE=postgres
Environment=DENO_KV_POSTGRES_URL=postgresql://denokv:denokv_password@localhost:5432/denokv
Environment=DENO_KV_ACCESS_TOKEN=${DENOKV_ACCESS_TOKEN}
Environment=POSTGRES_HOST=localhost
Environment=POSTGRES_PORT=5432
Environment=POSTGRES_DB=denokv
Environment=POSTGRES_USER=denokv
Environment=POSTGRES_PASSWORD=denokv_password
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/var/lib/denokv
[Install]
WantedBy=multi-user.target
EOF
# Ensure working directory exists and has proper permissions
print_status "Setting up DenoKV working directory..."
$SUDO_CMD mkdir -p /var/lib/denokv
$SUDO_CMD chown denokv:denokv /var/lib/denokv
$SUDO_CMD chmod 755 /var/lib/denokv
# Reload systemd and enable service
print_status "Enabling DenoKV systemd service..."
$SUDO_CMD systemctl daemon-reload
$SUDO_CMD systemctl enable denokv.service
# Start the service
print_status "Starting DenoKV service..."
$SUDO_CMD systemctl start denokv.service
# Wait for service to start
print_status "Waiting for DenoKV to start..."
sleep 3
# Check if service is running
if $SUDO_CMD systemctl is-active --quiet denokv.service; then
print_success "DenoKV systemd service started successfully!"
# Get the PID
DENOKV_PID=$($SUDO_CMD systemctl show -p MainPID --value denokv.service)
echo "$DENOKV_PID" > "$DENOKV_PID_FILE"
# Test the connection
print_status "Testing DenoKV connection..."
sleep 2
if curl -s http://localhost:$DENOKV_PORT/ > /dev/null; then
print_success "DenoKV is responding on port $DENOKV_PORT"
else
print_warning "DenoKV may not be fully ready yet, but service is running"
fi
else
print_error "Failed to start DenoKV systemd service"
print_status "Checking service status..."
$SUDO_CMD systemctl status denokv.service --no-pager
exit 1
fi
# Final summary
echo ""
print_success "π Complete DenoKV setup finished!"
echo ""
echo "π Setup Summary:"
echo "=================="
echo "β
PostgreSQL: Fresh installation with denokv database"
echo "β
Environment: Variables configured and exported"
echo "β
DenoKV: Built and running as systemd service"
echo "β
Systemd: Service created and enabled for auto-start"
echo "β
Cleanup: Unnecessary scripts removed"
echo ""
echo "π§ Service Information:"
echo "======================="
echo "Service: denokv.service"
echo "Status: $(systemctl is-active denokv.service)"
echo "PID: $DENOKV_PID"
echo "Port: $DENOKV_PORT"
echo "Address: $DENOKV_ADDR"
echo "User: denokv"
echo "Binary: /usr/local/bin/denokv"
echo ""
echo "π Environment Variables:"
echo "=========================="
echo "POSTGRES_HOST=localhost"
echo "POSTGRES_PORT=5432"
echo "POSTGRES_DB=denokv"
echo "POSTGRES_USER=denokv"
echo "POSTGRES_PASSWORD=denokv_password"
echo "DENOKV_PORT=4512"
echo "DENOKV_ACCESS_TOKEN=$DENOKV_ACCESS_TOKEN"
echo ""
echo "π§ Systemd Management Commands:"
echo "==============================="
echo "Start: sudo systemctl start denokv.service"
echo "Stop: sudo systemctl stop denokv.service"
echo ""
echo "π Access Token: $DENOKV_ACCESS_TOKEN"
echo "π Use this token to connect to DenoKV from your applications"
echo ""
echo "π§ͺ Test Script:"
echo "==============="
echo "Create a test script with:"
echo "cat > test_denokv.ts << 'EOF'"
echo "const KV_URL = \"http://localhost:4512\";"
echo "const ACCESS_TOKEN = \"$DENOKV_ACCESS_TOKEN\";"
echo ""
echo "async function testDenoKV() {"
echo " try {"
echo " console.log('π Testing DenoKV connection...');"
echo " "
echo " // Open KV connection using native Deno KV API"
echo " const kv = await Deno.openKv(KV_URL, { accessToken: ACCESS_TOKEN });"
echo " console.log('β
KV connection opened successfully');"
echo " "
echo " // Test KV operations"
echo " const testKey = ['test', 'key'];"
echo " const testValue = 'Hello DenoKV!';"
echo " "
echo " // Set a value"
echo " await kv.set(testKey, testValue);"
echo " console.log('β
Set operation successful - Key:', testKey, 'Value:', testValue);"
echo " "
echo " // Get the value"
echo " const result = await kv.get(testKey);"
echo " console.log('β
Get operation successful - Retrieved:', result.value);"
echo " "
echo " // Clean up"
echo " await kv.delete(testKey);"
echo " console.log('β
Delete operation successful - Removed key:', testKey);"
echo " "
echo " // Close connection"
echo " kv.close();"
echo " console.log('π All DenoKV tests passed!');"
echo " } catch (error) {"
echo " console.error('β Test failed:', error);"
echo " }"
echo "}"
echo ""
echo "testDenoKV();"
echo "EOF"
echo ""
echo "Run test with: deno run --allow-net --unstable-kv test_denokv.ts"
echo "Restart: sudo systemctl restart denokv.service"
echo "Status: sudo systemctl status denokv.service"
echo "Logs: sudo journalctl -u denokv.service -f"
echo "Enable: sudo systemctl enable denokv.service"
echo "Disable: sudo systemctl disable denokv.service"
echo ""
echo "π Test Connection:"
echo "==================="
echo "curl http://localhost:$DENOKV_PORT/"
echo "curl http://102.37.137.29:$DENOKV_PORT/"
echo ""
echo "π DenoKV is ready for production use!"
echo " - Auto-starts on boot"
echo " - Auto-restarts on crash"
echo " - Runs as secure system user"
echo " - Integrated with systemd logging"