-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-helper.ps1
More file actions
222 lines (198 loc) · 5.82 KB
/
docker-helper.ps1
File metadata and controls
222 lines (198 loc) · 5.82 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
# Docker helper script for Phishing Detection Bot (PowerShell version)
param(
[Parameter(Position=0)]
[string]$Command
)
# Function to print colored output
function Write-Status {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor Blue
}
function Write-Success {
param([string]$Message)
Write-Host "[SUCCESS] $Message" -ForegroundColor Green
}
function Write-Warning {
param([string]$Message)
Write-Host "[WARNING] $Message" -ForegroundColor Yellow
}
function Write-Error {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor Red
}
# Check if .env file exists
function Test-EnvFile {
if (-not (Test-Path ".env")) {
Write-Error ".env file not found!"
Write-Status "Creating .env template..."
@"
# Discord Bot Token (required)
BOT_TOKEN=your_bot_token_here
# Developer User ID (optional)
DEV=your_discord_user_id_here
"@ | Out-File -FilePath ".env" -Encoding UTF8
Write-Warning "Please edit .env file and add your BOT_TOKEN before running the bot!"
exit 1
}
}
# Help function
function Show-Help {
@"
Docker Helper Script for Phishing Detection Bot
Usage: .\docker-helper.ps1 [COMMAND]
Commands:
build Build the Docker image
run Run the bot in production mode
dev Run the bot in development mode with live reloading
stop Stop the running bot container
restart Restart the bot container
logs Show bot logs (follow mode)
logs-tail Show last 100 lines of logs
clean Remove bot containers and images
shell Open a shell in the bot container
health Check bot health status
update Pull latest code and rebuild
Examples:
.\docker-helper.ps1 build # Build the Docker image
.\docker-helper.ps1 run # Start the bot in production
.\docker-helper.ps1 dev # Start bot in development mode
.\docker-helper.ps1 logs # Follow logs in real-time
.\docker-helper.ps1 clean # Clean up Docker resources
"@
}
# Build the Docker image
function Build-Image {
Write-Status "Building Docker image..."
docker-compose build
if ($LASTEXITCODE -eq 0) {
Write-Success "Docker image built successfully!"
} else {
Write-Error "Failed to build Docker image!"
exit 1
}
}
# Run in production mode
function Start-Production {
Test-EnvFile
Write-Status "Starting bot in production mode..."
docker-compose up -d
if ($LASTEXITCODE -eq 0) {
Write-Success "Bot started in production mode!"
Write-Status "Use '.\docker-helper.ps1 logs' to view logs"
} else {
Write-Error "Failed to start bot!"
exit 1
}
}
# Run in development mode
function Start-Development {
Test-EnvFile
Write-Status "Starting bot in development mode..."
docker-compose -f docker-compose.dev.yml up
}
# Stop containers
function Stop-Containers {
Write-Status "Stopping bot containers..."
docker-compose down
docker-compose -f docker-compose.dev.yml down 2>$null
Write-Success "Bot containers stopped!"
}
# Restart containers
function Restart-Containers {
Write-Status "Restarting bot..."
docker-compose restart
if ($LASTEXITCODE -eq 0) {
Write-Success "Bot restarted!"
} else {
Write-Error "Failed to restart bot!"
exit 1
}
}
# Show logs
function Show-Logs {
docker-compose logs -f
}
# Show tail logs
function Show-LogsTail {
docker-compose logs --tail=100
}
# Clean up
function Remove-Resources {
Write-Warning "This will remove all bot containers and images!"
$confirmation = Read-Host "Are you sure? (y/N)"
if ($confirmation -eq "y" -or $confirmation -eq "Y") {
Write-Status "Cleaning up..."
docker-compose down --rmi all --volumes
docker-compose -f docker-compose.dev.yml down --rmi all --volumes 2>$null
Write-Success "Cleanup completed!"
} else {
Write-Status "Cleanup cancelled."
}
}
# Open shell
function Open-Shell {
Write-Status "Opening shell in bot container..."
$result = docker-compose exec discord-bot /bin/bash
if ($LASTEXITCODE -ne 0) {
Write-Warning "Container not running, starting temporary container..."
docker-compose run --rm discord-bot /bin/bash
}
}
# Check health
function Test-Health {
Write-Status "Checking bot health..."
$containerStatus = docker-compose ps | Select-String "Up"
if ($containerStatus) {
$healthCheck = @"
import sys
sys.path.insert(0, '/app')
sys.path.insert(0, '/app/src')
try:
import discord
from src.core.config import config
print('✅ Bot health check passed!')
except Exception as e:
print(f'❌ Health check failed: {e}')
sys.exit(1)
"@
docker-compose exec discord-bot python -c $healthCheck
} else {
Write-Error "Bot container is not running!"
exit 1
}
}
# Update and rebuild
function Update-Rebuild {
Write-Status "Pulling latest changes..."
git pull
Write-Status "Rebuilding Docker image..."
docker-compose build --no-cache
Write-Status "Restarting bot..."
docker-compose up -d
Write-Success "Update completed!"
}
# Main script logic
switch ($Command) {
"build" { Build-Image }
"run" { Start-Production }
"dev" { Start-Development }
"stop" { Stop-Containers }
"restart" { Restart-Containers }
"logs" { Show-Logs }
"logs-tail" { Show-LogsTail }
"clean" { Remove-Resources }
"shell" { Open-Shell }
"health" { Test-Health }
"update" { Update-Rebuild }
{ $_ -in "help", "--help", "-h" } { Show-Help }
default {
if ([string]::IsNullOrEmpty($Command)) {
Show-Help
} else {
Write-Error "Unknown command: $Command"
""
Show-Help
exit 1
}
}
}