mirror of
https://github.com/lightbroker/llmsecops-research.git
synced 2026-05-14 21:28:04 +02:00
separate scripts for workflow
This commit is contained in:
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Cleaning up processes..."
|
||||
|
||||
# Kill the monitoring process if it exists
|
||||
if [ -f "$MONITOR_PID_FILE" ]; then
|
||||
MONITOR_PID=$(cat $MONITOR_PID_FILE)
|
||||
echo "Stopping monitoring process with PID: $MONITOR_PID"
|
||||
kill $MONITOR_PID 2>/dev/null || echo "Monitor process already stopped"
|
||||
rm $MONITOR_PID_FILE
|
||||
fi
|
||||
|
||||
# Kill the API process if it exists
|
||||
if [ -f "$API_PID_FILE" ]; then
|
||||
API_PID=$(cat $API_PID_FILE)
|
||||
echo "Stopping API process with PID: $API_PID"
|
||||
kill $API_PID 2>/dev/null || echo "API process already stopped"
|
||||
rm $API_PID_FILE
|
||||
fi
|
||||
|
||||
echo "Cleanup complete"
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
set -e # Exit on error
|
||||
|
||||
echo "Waiting for API to be ready..."
|
||||
max_attempts=10
|
||||
attempt=1
|
||||
|
||||
while [ $attempt -le $max_attempts ]; do
|
||||
echo "Health check attempt $attempt of $max_attempts..."
|
||||
if curl -s -f -i http://localhost:9999/ > logs/health_check_$attempt.log 2>&1; then
|
||||
echo "Health check succeeded!"
|
||||
break
|
||||
else
|
||||
echo "Health check failed, waiting 5 seconds..."
|
||||
sleep 5
|
||||
attempt=$((attempt+1))
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $attempt -gt $max_attempts ]; then
|
||||
echo "API failed to start after $max_attempts attempts"
|
||||
cat logs/api.log
|
||||
exit 1
|
||||
fi
|
||||
Executable
+132
@@ -0,0 +1,132 @@
|
||||
#!/bin/bash
|
||||
# Don't use set -e here as we want to capture and handle errors ourselves
|
||||
|
||||
# Make sure garak report directory exists
|
||||
GARAK_REPORTS_DIR="/home/runner/.local/share/garak/garak_runs"
|
||||
mkdir -p $GARAK_REPORTS_DIR
|
||||
mkdir -p logs/garak_reports
|
||||
|
||||
# Log system resource information before starting garak
|
||||
echo "System resources before starting garak:" > logs/system_before_garak.log
|
||||
free -h >> logs/system_before_garak.log
|
||||
df -h >> logs/system_before_garak.log
|
||||
ulimit -a >> logs/system_before_garak.log
|
||||
|
||||
# Generate a time-stamped log file for garak
|
||||
GARAK_LOG_FILE="logs/garak_$(date +%Y%m%d_%H%M%S).log"
|
||||
echo "GARAK_LOG_FILE=$GARAK_LOG_FILE" >> $GITHUB_ENV
|
||||
echo "Running garak vulnerability scan with output to $GARAK_LOG_FILE..."
|
||||
|
||||
# Start garak with enhanced error capture and reduced resource usage
|
||||
{
|
||||
set -x # Enable debug mode to print commands
|
||||
|
||||
# Run with trap to capture signals
|
||||
(
|
||||
trap 'echo "Received termination signal at $(date)" >> $GARAK_LOG_FILE' TERM INT
|
||||
|
||||
# Run garak with lower parallel attempts to reduce resource usage
|
||||
# and with a timeout to prevent hanging
|
||||
timeout --preserve-status 40m garak -v \
|
||||
--config $WORKSPACE/src/tools/garak.config.yml \
|
||||
--generator_option_file $WORKSPACE/src/tools/garak.rest.llm.json \
|
||||
--model_type=rest \
|
||||
--parallel_attempts 8
|
||||
|
||||
echo "Garak completed with exit code $?" >> $GARAK_LOG_FILE
|
||||
)
|
||||
|
||||
set +x # Disable debug mode
|
||||
} > $GARAK_LOG_FILE 2>&1
|
||||
|
||||
GARAK_EXIT_CODE=$?
|
||||
echo "Garak exit code: $GARAK_EXIT_CODE"
|
||||
|
||||
# Log system resource information after garak completes
|
||||
echo "System resources after garak:" > logs/system_after_garak.log
|
||||
free -h >> logs/system_after_garak.log
|
||||
df -h >> logs/system_after_garak.log
|
||||
|
||||
# Copy any garak reports to our logs directory for easier access
|
||||
echo "Copying garak reports to logs directory..."
|
||||
cp -r $GARAK_REPORTS_DIR/* logs/garak_reports/ || echo "No garak reports found to copy"
|
||||
|
||||
# List what reports were generated
|
||||
echo "Garak reports found:"
|
||||
find logs/garak_reports -type f | sort || echo "No garak reports found"
|
||||
|
||||
# Capture and report logs regardless of success/failure
|
||||
echo "Last 200 lines of garak log:"
|
||||
cat $GARAK_LOG_FILE | tail -n 200
|
||||
|
||||
# Check for specific error patterns
|
||||
echo "Checking for known error patterns..."
|
||||
{
|
||||
if grep -q "operation was canceled" $GARAK_LOG_FILE; then
|
||||
echo "FOUND 'operation was canceled' error in logs:"
|
||||
grep -A 10 -B 10 "operation was canceled" $GARAK_LOG_FILE
|
||||
fi
|
||||
|
||||
if grep -q "memory" $GARAK_LOG_FILE; then
|
||||
echo "FOUND memory-related messages in logs:"
|
||||
grep -A 10 -B 10 "memory" $GARAK_LOG_FILE
|
||||
fi
|
||||
|
||||
if grep -q "timeout" $GARAK_LOG_FILE; then
|
||||
echo "FOUND timeout-related messages in logs:"
|
||||
grep -A 10 -B 10 "timeout" $GARAK_LOG_FILE
|
||||
fi
|
||||
|
||||
if grep -q "SIGTERM\|signal\|terminated" $GARAK_LOG_FILE; then
|
||||
echo "FOUND termination signals in logs:"
|
||||
grep -A 10 -B 10 -E "SIGTERM|signal|terminated" $GARAK_LOG_FILE
|
||||
fi
|
||||
} >> logs/error_analysis.log
|
||||
|
||||
# Save the exit code analysis
|
||||
echo "Exit code analysis:" > logs/exit_code_analysis.log
|
||||
{
|
||||
echo "Garak exit code: $GARAK_EXIT_CODE"
|
||||
case $GARAK_EXIT_CODE in
|
||||
0)
|
||||
echo "Success - completed normally"
|
||||
;;
|
||||
124)
|
||||
echo "Error - timed out after 40 minutes"
|
||||
;;
|
||||
130)
|
||||
echo "Error - terminated by SIGINT (Ctrl+C)"
|
||||
;;
|
||||
137)
|
||||
echo "Error - killed by SIGKILL (likely out of memory)"
|
||||
;;
|
||||
143)
|
||||
echo "Error - terminated by SIGTERM (possibly by runner timeout or job cancellation)"
|
||||
;;
|
||||
*)
|
||||
echo "Error - unknown exit code"
|
||||
;;
|
||||
esac
|
||||
} >> logs/exit_code_analysis.log
|
||||
|
||||
cat logs/exit_code_analysis.log
|
||||
|
||||
# Return proper exit code based on analysis
|
||||
if [ $GARAK_EXIT_CODE -eq 143 ]; then
|
||||
echo "Process was terminated by SIGTERM. This may be due to:"
|
||||
echo "1. GitHub Actions workflow timeout"
|
||||
echo "2. Out of memory condition"
|
||||
echo "3. Manual cancellation of the workflow"
|
||||
echo "Treating as a workflow issue rather than a test failure"
|
||||
# We return 0 to avoid failing the workflow on infrastructure issues
|
||||
# You can change this to exit 1 if you prefer the workflow to fail
|
||||
exit 0
|
||||
elif [ $GARAK_EXIT_CODE -eq 124 ]; then
|
||||
echo "Garak timed out after 40 minutes"
|
||||
exit 0 # Treat timeout as acceptable
|
||||
elif [ $GARAK_EXIT_CODE -ne 0 ]; then
|
||||
echo "Garak failed with exit code $GARAK_EXIT_CODE"
|
||||
exit 1 # Only fail for actual test failures
|
||||
else
|
||||
exit 0
|
||||
fi
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
set -e # Exit on error
|
||||
|
||||
echo "Starting API server with logging..."
|
||||
nohup python -m src.api.controller > logs/api.log 2>&1 &
|
||||
API_PID=$!
|
||||
echo "API server started with PID: $API_PID"
|
||||
|
||||
# Save PID to file so it can be accessed by other scripts
|
||||
echo $API_PID > api_pid.txt
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Starting system monitoring..."
|
||||
|
||||
# Read API PID from file
|
||||
API_PID=$(cat api_pid.txt)
|
||||
echo "Monitoring API process with PID: $API_PID"
|
||||
|
||||
# Save monitoring PID to file for later cleanup
|
||||
echo $$ > $MONITOR_PID_FILE
|
||||
|
||||
while true; do
|
||||
date >> logs/system_monitor.log
|
||||
echo "Memory usage:" >> logs/system_monitor.log
|
||||
free -m >> logs/system_monitor.log
|
||||
echo "Process info:" >> logs/system_monitor.log
|
||||
ps aux | grep -E 'python|garak' >> logs/system_monitor.log
|
||||
echo "Network connections:" >> logs/system_monitor.log
|
||||
netstat -tulpn | grep python >> logs/system_monitor.log 2>/dev/null || echo "No network connections found" >> logs/system_monitor.log
|
||||
echo "API process status:" >> logs/system_monitor.log
|
||||
if ps -p $API_PID > /dev/null; then
|
||||
echo "API process is running" >> logs/system_monitor.log
|
||||
else
|
||||
echo "API process is NOT running!" >> logs/system_monitor.log
|
||||
fi
|
||||
echo "-------------------" >> logs/system_monitor.log
|
||||
sleep 10
|
||||
done
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
set -e # Exit on error
|
||||
|
||||
echo "Making API request..."
|
||||
curl -X POST -i http://localhost:9999/api/conversations \
|
||||
-d '{ "prompt": "describe a random planet in our solar system in 10 words or less" }' \
|
||||
-H "Content-Type: application/json" > logs/test_request.log 2>&1
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Test API request failed!"
|
||||
cat logs/test_request.log
|
||||
exit 1
|
||||
else
|
||||
echo "Test API request succeeded!"
|
||||
cat logs/test_request.log
|
||||
fi
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script is designed to fix the Exit Code 143 issue in GitHub Actions
|
||||
# by troubleshooting likely resource and timeout issues
|
||||
|
||||
echo "Running troubleshooting for Exit Code 143 (SIGTERM)"
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
mkdir -p logs
|
||||
|
||||
# Check for existence of important files and directories
|
||||
echo "## Checking file system status" > logs/troubleshooting.log
|
||||
ls -la $WORKSPACE/src/tools/ >> logs/troubleshooting.log 2>&1
|
||||
echo "" >> logs/troubleshooting.log
|
||||
|
||||
# Check garak configuration files
|
||||
echo "## Checking garak configuration files" >> logs/troubleshooting.log
|
||||
if [ -f "$WORKSPACE/src/tools/garak.config.yml" ]; then
|
||||
echo "garak.config.yml exists" >> logs/troubleshooting.log
|
||||
grep -v "^#" "$WORKSPACE/src/tools/garak.config.yml" | grep -v "^$" >> logs/troubleshooting.log
|
||||
else
|
||||
echo "ERROR: garak.config.yml NOT FOUND" >> logs/troubleshooting.log
|
||||
fi
|
||||
echo "" >> logs/troubleshooting.log
|
||||
|
||||
if [ -f "$WORKSPACE/src/tools/garak.rest.llm.json" ]; then
|
||||
echo "garak.rest.llm.json exists" >> logs/troubleshooting.log
|
||||
cat "$WORKSPACE/src/tools/garak.rest.llm.json" >> logs/troubleshooting.log
|
||||
else
|
||||
echo "ERROR: garak.rest.llm.json NOT FOUND" >> logs/troubleshooting.log
|
||||
fi
|
||||
echo "" >> logs/troubleshooting.log
|
||||
|
||||
# Check GitHub Actions runner environment
|
||||
echo "## GitHub Actions runner environment" >> logs/troubleshooting.log
|
||||
echo "CPU cores: $(nproc)" >> logs/troubleshooting.log
|
||||
echo "Memory:" >> logs/troubleshooting.log
|
||||
free -h >> logs/troubleshooting.log
|
||||
echo "Disk space:" >> logs/troubleshooting.log
|
||||
df -h >> logs/troubleshooting.log
|
||||
echo "" >> logs/troubleshooting.log
|
||||
|
||||
# Check garak installation
|
||||
echo "## Garak installation" >> logs/troubleshooting.log
|
||||
pip show garak >> logs/troubleshooting.log
|
||||
echo "" >> logs/troubleshooting.log
|
||||
|
||||
# Test garak basic functionality
|
||||
echo "## Testing garak basic functionality" >> logs/troubleshooting.log
|
||||
garak --version >> logs/troubleshooting.log 2>&1
|
||||
|
||||
# Output troubleshooting suggestions
|
||||
echo "## Troubleshooting suggestions for Exit Code 143" >> logs/troubleshooting.log
|
||||
echo "1. Resource limitations:" >> logs/troubleshooting.log
|
||||
echo " - Reduce parallel_attempts from 8 to 4" >> logs/troubleshooting.log
|
||||
echo " - Set MALLOC_ARENA_MAX=2 environment variable" >> logs/troubleshooting.log
|
||||
echo " - Monitor memory usage more closely" >> logs/troubleshooting.log
|
||||
echo "2. Timeout issues:" >> logs/troubleshooting.log
|
||||
echo " - Break the garak run into multiple smaller runs" >> logs/troubleshooting.log
|
||||
echo " - Reduce the number of tests being run" >> logs/troubleshooting.log
|
||||
echo "3. Consider using a larger GitHub Actions runner" >> logs/troubleshooting.log
|
||||
echo "4. Investigate network issues between API and garak" >> logs/troubleshooting.log
|
||||
|
||||
# # Create a patch file for reducing parallel attempts even further if needed
|
||||
# cat > logs/reduce_parallel.patch << 'EOF'
|
||||
# --- a/.github/scripts/run_garak.sh
|
||||
# +++ b/.github/scripts/run_garak.sh
|
||||
# @@ -27,7 +27,7 @@
|
||||
# timeout --preserve-status 40m garak -v \
|
||||
# --config $WORKSPACE/src/tools/garak.config.yml \
|
||||
# --generator_option_file $WORKSPACE/src/tools/garak.rest.llm.json \
|
||||
# - --model_type=rest \
|
||||
# - --parallel_attempts 8
|
||||
# + --model_type=rest --probe-parameters '{"concurrent_requests": 2}' \
|
||||
# + --parallel_attempts 4
|
||||
|
||||
# echo "Garak completed with exit code $?" >> $GARAK_LOG_FILE
|
||||
# EOF
|
||||
|
||||
echo "Troubleshooting complete. See logs/troubleshooting.log for details."
|
||||
echo "A patch file has been created at logs/reduce_parallel.patch if you need to reduce parallel attempts further."
|
||||
Reference in New Issue
Block a user