Skip to content

Commit a0cf49f

Browse files
committed
production build and entrypoints optimizations
1 parent ff7f1f0 commit a0cf49f

File tree

10 files changed

+303
-35
lines changed

10 files changed

+303
-35
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
/sentinel-kit_server_frontend/node_modules/*
2323
/sentinel-kit_server_frontend/dist/*
2424
/sentinel-kit_server_frontend/package-lock.json
25+
/sentinel-kit_server_frontend/.production_build_complete
26+
/sentinel-kit_server_frontend/.source_hash
2527
/sentinel-kit_server_backend/composer.lock
2628
/sentinel-kit_server_backend/.initial_setup_done
29+
/sentinel-kit_server_backend/.cache_ready
30+
/sentinel-kit_server_backend/.composer_hash
31+
/sentinel-kit_server_backend/.source_hash
2732
/sentinel-kit_server_backend/migrations/*
2833
/sentinel-kit_server_backend/var/*
2934
/sentinel-kit_server_backend/vendor/*

config/docker-config/Dockerfile.backend

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ COPY sentinel-kit_server_backend/composer.lock* /var/www/html/
3434

3535
WORKDIR /var/www/html
3636

37-
RUN composer install --no-scripts --no-autoloader && \
38-
composer dump-autoload --no-scripts --optimize
37+
# Pre-install Composer dependencies for production optimization
38+
ARG APP_ENV=prod
39+
RUN if [ "$APP_ENV" = "prod" ]; then \
40+
composer install --no-scripts --no-autoloader --no-dev --optimize-autoloader && \
41+
composer dump-autoload --no-scripts --optimize; \
42+
else \
43+
composer install --no-scripts --no-autoloader && \
44+
composer dump-autoload --no-scripts --optimize; \
45+
fi
3946

4047
COPY config/docker-config/backend-entrypoint.sh /opt/server-backend/backend-entrypoint.sh
4148
RUN chmod +x /opt/server-backend/backend-entrypoint.sh

config/docker-config/Dockerfile.frontend

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,13 @@ RUN chmod +x /usr/local/bin/frontend-entrypoint.sh
1212

1313
WORKDIR /app
1414

15+
COPY sentinel-kit_server_frontend/package*.json ./
16+
17+
ARG APP_ENV=prod
18+
RUN if [ "$APP_ENV" = "prod" ]; then \
19+
npm install --silent; \
20+
fi
21+
22+
COPY sentinel-kit_server_frontend/ ./
23+
1524
ENTRYPOINT ["/usr/local/bin/frontend-entrypoint.sh"]

config/docker-config/backend-entrypoint.sh

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,42 @@ echo "Environment: $APP_ENV"
66
chown -R www-data:www-data /var/www/html
77
chown -R www-data:www-data /detection-rules
88

9+
calculate_source_hash() {
10+
find /var/www/html/src /var/www/html/config -type f \( -name "*.php" -o -name "*.yaml" -o -name "*.yml" \) 2>/dev/null | \
11+
sort | xargs cat | sha256sum | cut -d' ' -f1
12+
}
13+
14+
15+
check_composer_changes() {
16+
if [ ! -f "/var/www/html/.composer_hash" ]; then
17+
return 1
18+
fi
19+
local current_hash
20+
current_hash=$(cat /var/www/html/composer.json /var/www/html/composer.lock 2>/dev/null | sha256sum | cut -d' ' -f1)
21+
local stored_hash
22+
stored_hash=$(cat /var/www/html/.composer_hash 2>/dev/null)
23+
[ "$current_hash" = "$stored_hash" ]
24+
}
25+
926
su -s /bin/bash www-data << 'EOF'
1027
setup_symfony() {
1128
MARKER_FILE="/var/www/html/.initial_setup_done"
29+
CACHE_MARKER="/var/www/html/.cache_ready"
30+
1231
rm -rf /var/www/html/var/cache
1332
rm -rf /var/www/html/public/bundles
1433
rm -rf /detection-rules/elastalert/*
1534
16-
if [ ! -d "/var/www/html/vendor" ] || [ ! -f "/var/www/html/vendor/autoload.php" ]; then
17-
echo "Installing Composer dependencies..."
18-
composer install --no-scripts
35+
36+
if ! check_composer_changes || [ ! -d "/var/www/html/vendor" ] || [ ! -f "/var/www/html/vendor/autoload.php" ]; then
37+
echo "Installing/updating Composer dependencies..."
38+
composer install --no-scripts --optimize-autoloader
1939
composer dump-autoload --optimize
40+
41+
# Save composer hash
42+
cat /var/www/html/composer.json /var/www/html/composer.lock 2>/dev/null | sha256sum | cut -d' ' -f1 > /var/www/html/.composer_hash
2043
else
21-
echo "Composer dependencies already installed."
44+
echo "Composer dependencies are up-to-date - using cached vendor/."
2245
fi
2346
2447
echo "Waiting for database to be ready..."
@@ -60,7 +83,7 @@ setup_symfony() {
6083
fi
6184
6285
echo "Generating JWT keypair..."
63-
if ! php /var/www/html/bin/console lexik:jwt:generate-keypair; then
86+
if ! php /var/www/html/bin/console lexik:jwt:generate-keypair --overwrite; then
6487
echo "ERROR: Failed to generate JWT keypair"
6588
exit 1
6689
fi
@@ -91,13 +114,30 @@ setup_symfony() {
91114
fi
92115
fi
93116
117+
current_hash=$(calculate_source_hash)
94118
if [ "$APP_ENV" = "prod" ]; then
95-
echo "Warming up production cache..."
96-
php /var/www/html/bin/console cache:clear --env=prod --no-debug
97-
php /var/www/html/bin/console cache:warmup --env=prod --no-debug
119+
if [ -f "$CACHE_MARKER" ] && [ -f "/var/www/html/.source_hash" ]; then
120+
stored_hash=$(cat /var/www/html/.source_hash 2>/dev/null)
121+
if [ "$current_hash" = "$stored_hash" ]; then
122+
echo "Source code unchanged - production cache is up-to-date, skipping rebuild."
123+
else
124+
echo "Source code changed - warming up production cache..."
125+
php /var/www/html/bin/console cache:clear --env=prod --no-debug
126+
php /var/www/html/bin/console cache:warmup --env=prod --no-debug
127+
echo "$current_hash" > /var/www/html/.source_hash
128+
touch "$CACHE_MARKER"
129+
fi
130+
else
131+
echo "Initial cache warmup for production..."
132+
php /var/www/html/bin/console cache:clear --env=prod --no-debug
133+
php /var/www/html/bin/console cache:warmup --env=prod --no-debug
134+
echo "$current_hash" > /var/www/html/.source_hash
135+
touch "$CACHE_MARKER"
136+
fi
98137
else
99-
echo "Clearing development cache..."
138+
echo "Development mode - clearing cache..."
100139
php /var/www/html/bin/console cache:clear
140+
echo "$current_hash" > /var/www/html/.source_hash
101141
fi
102142
}
103143

config/docker-config/frontend-entrypoint.sh

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,70 @@
33
echo "=== Sentinel Kit Frontend Entrypoint ==="
44
echo "Environment: $APP_ENV"
55

6-
echo "Installing npm dependencies..."
7-
86
if [ "$APP_ENV" = "prod" ]; then
97
echo "Starting frontend in PRODUCTION mode"
108

11-
if [ ! -d "/app/dist" ]; then
12-
npm install
13-
echo "Building application for production..."
14-
npm run build
9+
MARKER_FILE="/app/.production_build_complete"
10+
SOURCE_HASH_FILE="/app/.source_hash"
11+
12+
if [ -d "/app/src" ]; then
13+
CURRENT_HASH=$(find /app/src -type f \( -name "*.vue" -o -name "*.js" -o -name "*.ts" -o -name "*.json" \) -exec md5sum {} \; 2>/dev/null | md5sum | cut -d' ' -f1)
14+
else
15+
CURRENT_HASH=""
1516
fi
1617

17-
if [ ! -d "/app/dist" ]; then
18-
echo "ERROR: Build failed - dist directory not found!"
19-
exit 1
18+
NEED_BUILD=false
19+
20+
if [ ! -f "$MARKER_FILE" ] || [ ! -d "/app/dist" ] || [ ! -f "/app/dist/index.html" ]; then
21+
echo "Production build not found or incomplete."
22+
NEED_BUILD=true
23+
elif [ -n "$CURRENT_HASH" ] && [ -f "$SOURCE_HASH_FILE" ]; then
24+
STORED_HASH=$(cat "$SOURCE_HASH_FILE")
25+
if [ "$CURRENT_HASH" != "$STORED_HASH" ]; then
26+
echo "Source code changes detected - rebuild required."
27+
NEED_BUILD=true
28+
fi
29+
elif [ -n "$CURRENT_HASH" ] && [ ! -f "$SOURCE_HASH_FILE" ]; then
30+
echo "No source hash found - rebuild to establish baseline."
31+
NEED_BUILD=true
32+
fi
33+
34+
if [ "$NEED_BUILD" = true ]; then
35+
echo "Building application for production..."
36+
37+
if [ ! -d "/app/node_modules" ] || [ ! -f "/app/package-lock.json" ]; then
38+
echo "Installing npm dependencies..."
39+
npm install
40+
else
41+
echo "Dependencies already installed - using cached node_modules."
42+
fi
43+
44+
echo "Compiling application..."
45+
npm run build
46+
47+
if [ ! -d "/app/dist" ] || [ ! -f "/app/dist/index.html" ]; then
48+
echo "ERROR: Build failed - dist directory or index.html not found!"
49+
exit 1
50+
fi
51+
52+
if [ -n "$CURRENT_HASH" ]; then
53+
echo "$CURRENT_HASH" > "$SOURCE_HASH_FILE"
54+
fi
55+
touch "$MARKER_FILE"
56+
echo "Production build completed and marked."
57+
else
58+
echo "Production build is up-to-date - skipping build for faster startup."
2059
fi
2160

2261
echo "Starting nginx on port 3000..."
2362
nginx -g 'daemon off;'
2463

2564
else
2665
echo "Starting in DEVELOPMENT mode"
66+
67+
echo "Installing npm dependencies..."
2768
npm install
69+
2870
echo "Starting Vite dev server on port 3000..."
2971
npm run dev -- --host '0.0.0.0' --port 3000
3072
fi

docker-compose.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ services:
2626
restart: on-failure
2727
networks:
2828
- sentinel-kit-network
29+
depends_on:
30+
sentinel-kit-app-backend:
31+
condition: service_healthy
2932

3033
sentinel-kit-app-backend:
3134
container_name: sentinel-kit-app-backend
@@ -65,7 +68,8 @@ services:
6568
networks:
6669
- sentinel-kit-network
6770
depends_on:
68-
- sentinel-kit-db-mysql
71+
sentinel-kit-db-mysql:
72+
condition: service_healthy
6973

7074
sentinel-kit-server-rules-scanner:
7175
container_name: sentinel-kit-server-rules-scanner

launcher.ps1

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
param(
2-
[ValidateSet("run", "stop", "build", "clean-data", "console", "logs", "status", "help")]
2+
[ValidateSet("start", "stop", "build", "clean-data", "console", "logs", "status", "help")]
33
[string]$Command = "help",
44
[string]$ServiceName = "",
55
[switch]$Follow,
@@ -23,7 +23,7 @@ function Show-Help {
2323
Write-Host " .\launcher.ps1 <command>" -ForegroundColor White
2424
Write-Host ""
2525
Write-Host "COMMANDS:" -ForegroundColor $InfoColor
26-
Write-Host " run Start the Docker stack" -ForegroundColor White
26+
Write-Host " start Start the Docker stack" -ForegroundColor White
2727
Write-Host " stop Stop the running Docker stack" -ForegroundColor White
2828
Write-Host " build Build and start the Docker stack" -ForegroundColor White
2929
Write-Host " clean-data Clean all user data and stop containers" -ForegroundColor White
@@ -86,6 +86,55 @@ function Convert-ShellScriptsToLF {
8686
}
8787
}
8888

89+
function Wait-ForHealthyServices {
90+
Write-Host "Waiting for backend and frontend to be healthy..." -ForegroundColor $InfoColor
91+
92+
$maxAttempts = 60
93+
$attempt = 0
94+
$backendHealthy = $false
95+
$frontendHealthy = $false
96+
97+
while ($attempt -lt $maxAttempts) {
98+
$attempt++
99+
100+
if (-not $backendHealthy) {
101+
$backendHealth = Invoke-Expression "docker compose ps --format json sentinel-kit-app-backend" -ErrorAction SilentlyContinue
102+
if ($LASTEXITCODE -eq 0 -and $backendHealth) {
103+
$healthStatus = ($backendHealth | ConvertFrom-Json).Health
104+
if ($healthStatus -eq "healthy") {
105+
Write-Host " Backend is healthy!" -ForegroundColor $SuccessColor
106+
$backendHealthy = $true
107+
}
108+
}
109+
}
110+
111+
if (-not $frontendHealthy) {
112+
$frontendHealth = Invoke-Expression "docker compose ps --format json sentinel-kit-app-frontend" -ErrorAction SilentlyContinue
113+
if ($LASTEXITCODE -eq 0 -and $frontendHealth) {
114+
$healthStatus = ($frontendHealth | ConvertFrom-Json).Health
115+
if ($healthStatus -eq "healthy") {
116+
Write-Host " Frontend is healthy!" -ForegroundColor $SuccessColor
117+
$frontendHealthy = $true
118+
}
119+
}
120+
}
121+
122+
if ($backendHealthy -and $frontendHealthy) {
123+
Write-Host "All critical services are healthy and ready!" -ForegroundColor $SuccessColor
124+
return $true
125+
}
126+
127+
if ($attempt % 10 -eq 0) {
128+
Write-Host " Still waiting... (attempt $attempt/$maxAttempts)" -ForegroundColor $WarningColor
129+
}
130+
131+
Start-Sleep -Seconds 10
132+
}
133+
134+
Write-Host "Timeout: Services did not become healthy within the expected time." -ForegroundColor $ErrorColor
135+
return $false
136+
}
137+
89138
if ($Help) {
90139
Show-Help
91140
exit 0
@@ -95,7 +144,7 @@ switch ($Command.ToLower()) {
95144
"help" {
96145
Show-Help
97146
}
98-
"run" {
147+
"start" {
99148
Write-Host ""
100149
Write-Host "========== STARTING SENTINELKIT ==========" -ForegroundColor $HeaderColor
101150

@@ -109,12 +158,21 @@ switch ($Command.ToLower()) {
109158

110159
if ($RunningContainers) {
111160
Write-Host "Docker stack is already running." -ForegroundColor $InfoColor
161+
if (Wait-ForHealthyServices) {
162+
Write-Host "Success! The Docker stack is running and healthy." -ForegroundColor $SuccessColor
163+
} else {
164+
Write-Host "Warning: Some services may not be fully healthy yet." -ForegroundColor $WarningColor
165+
}
112166
} else {
113167
Write-Host "Starting the Docker stack..." -ForegroundColor $InfoColor
114168
Invoke-Expression "docker compose up -d"
115169

116170
if ($LASTEXITCODE -eq 0) {
117-
Write-Host "Success! The Docker stack has been launched." -ForegroundColor $SuccessColor
171+
if (Wait-ForHealthyServices) {
172+
Write-Host "Success! The Docker stack has been launched and is healthy." -ForegroundColor $SuccessColor
173+
} else {
174+
Write-Host "Warning: Docker stack started but some services are not healthy." -ForegroundColor $WarningColor
175+
}
118176
} else {
119177
Write-Host "Error starting containers." -ForegroundColor $ErrorColor
120178
}
@@ -157,7 +215,11 @@ switch ($Command.ToLower()) {
157215
Invoke-Expression "docker compose up -d --build --force-recreate"
158216

159217
if ($LASTEXITCODE -eq 0) {
160-
Write-Host "Success! The Docker stack has been built and started." -ForegroundColor $SuccessColor
218+
if (Wait-ForHealthyServices) {
219+
Write-Host "Success! The Docker stack has been built, started and is healthy." -ForegroundColor $SuccessColor
220+
} else {
221+
Write-Host "Warning: Docker stack built and started but some services are not healthy." -ForegroundColor $WarningColor
222+
}
161223
} else {
162224
Write-Host "Error building containers." -ForegroundColor $ErrorColor
163225
}
@@ -174,7 +236,7 @@ switch ($Command.ToLower()) {
174236

175237
if (-not $backendContainer) {
176238
Write-Host "Backend container is not running." -ForegroundColor $ErrorColor
177-
Write-Host "Please start the stack first using: .\launcher.ps1 run" -ForegroundColor $WarningColor
239+
Write-Host "Please start the stack first using: .\launcher.ps1 start" -ForegroundColor $WarningColor
178240
exit 1
179241
}
180242

@@ -311,9 +373,14 @@ switch ($Command.ToLower()) {
311373
Write-Host "Starting cleanup process..." -ForegroundColor $WarningColor
312374

313375
$itemsToRemove = @(
376+
"./sentinel-kit_server_frontend/.production_build_complete",
377+
"./sentinel-kit_server_frontend/.source_hash",
314378
"./sentinel-kit_server_frontend/node_modules",
315379
"./sentinel-kit_server_frontend/package-lock.json",
316380
"./sentinel-kit_server_frontend/dist",
381+
"./sentinel-kit_server_backend/.cache_ready",
382+
"./sentinel-kit_server_backend/.composer_hash",
383+
"./sentinel-kit_server_backend/.source_hash",
317384
"./sentinel-kit_server_backend/.initial_setup_done",
318385
"./sentinel-kit_server_backend/composer.lock",
319386
"./sentinel-kit_server_backend/symfony.lock",

0 commit comments

Comments
 (0)