-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathstart_redis.ps1
More file actions
84 lines (71 loc) · 2.66 KB
/
start_redis.ps1
File metadata and controls
84 lines (71 loc) · 2.66 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
# --- CONFIGURATION ---
# Path to your .env file
$envFilePath = Join-Path $PSScriptRoot "src\server\.env"
# Default WSL distro name
$wslDistroName = "Ubuntu"
# --- FUNCTIONS ---
# Helper: Extract REDIS_PASSWORD from .env
function Get-RedisPassword {
param([string]$envPath)
if (-not (Test-Path $envPath)) {
throw "❌ Could not find .env file at: $envPath"
}
$envContent = Get-Content $envPath
$passwordLine = $envContent | Select-String -Pattern "^\s*REDIS_PASSWORD\s*=\s*['""]?(.*?)['""]?\s*$"
if ($passwordLine) {
return $passwordLine.Matches[0].Groups[1].Value.Trim()
}
else {
throw "❌ Could not find REDIS_PASSWORD in $envPath"
}
}
# Helper: Check if Redis is installed in WSL
function Check-RedisInstalled {
param([string]$distro)
Write-Host "🔍 Checking if Redis is installed in WSL ($distro)..."
$checkCommand = "which redis-server"
$checkResult = wsl -d $distro --% bash -c "$checkCommand"
if (-not $checkResult) {
throw "❌ Redis server is not installed in WSL ($distro). Please install it using: sudo apt install redis-server"
}
else {
Write-Host "✅ Redis is installed at: $checkResult" -ForegroundColor Green
}
}
# Helper: Start Redis server in WSL
function Start-RedisServer {
param([string]$distro, [string]$password)
Write-Host "▶️ Starting Redis server in WSL ($distro)..."
$startCommand = "redis-server --bind 0.0.0.0 --requirepass `"$password`""
wsl -d $distro --% bash -c "$startCommand"
Start-Sleep -Seconds 2
}
# Helper: Test Redis connectivity
function Test-RedisConnection {
param([string]$distro, [string]$password)
Write-Host "🔄 Testing Redis connection with redis-cli PING..."
$pingCommand = "redis-cli -a `"$password`" PING"
$pingResult = wsl -d $distro --% bash -c "$pingCommand"
if ($pingResult -eq "PONG") {
Write-Host "✅ Redis is running and responding correctly: $pingResult" -ForegroundColor Green
}
else {
Write-Host "❌ Redis did not respond correctly. Output: $pingResult" -ForegroundColor Red
throw "Failed to authenticate with Redis. Check your REDIS_PASSWORD and Redis configuration."
}
}
# --- MAIN LOGIC ---
try {
# Get Redis password from .env
$redisPassword = Get-RedisPassword -envPath $envFilePath
# Check if Redis is installed in WSL
Check-RedisInstalled -distro $wslDistroName
# Start Redis in WSL
Start-RedisServer -distro $wslDistroName -password $redisPassword
# Test connection
Test-RedisConnection -distro $wslDistroName -password $redisPassword
}
catch {
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}