VPN-Session-History-FastAPI/run_container.sh
2025-04-10 21:40:30 +02:00

73 lines
No EOL
2.1 KiB
Bash
Executable file

#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Set container name
CONTAINER_NAME="vpn-session-viewer"
# Determine if we use podman or docker
if command_exists podman; then
CONTAINER_CMD="podman"
VOLUME_FLAG=":Z"
echo "Using Podman for container management."
elif command_exists docker; then
CONTAINER_CMD="docker"
VOLUME_FLAG=""
echo "Using Docker for container management."
else
echo "Error: Neither Podman nor Docker found. Please install one of them first."
exit 1
fi
# Stop and remove container if it exists
echo "Checking for existing container..."
if $CONTAINER_CMD ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "Stopping and removing existing ${CONTAINER_NAME} container..."
$CONTAINER_CMD stop ${CONTAINER_NAME}
$CONTAINER_CMD rm ${CONTAINER_NAME}
fi
# Create volume if it doesn't exist
echo "Creating volume for VPN logs storage..."
$CONTAINER_CMD volume create vpn-logs
# Build the container image
echo "Building container image..."
$CONTAINER_CMD build -t ${CONTAINER_NAME}:latest .
# Set up local logs directory to mount
LOGS_DIR="./logs"
if [ ! -d "$LOGS_DIR" ]; then
echo "Creating logs directory..."
mkdir -p "$LOGS_DIR"
fi
# Run the container
echo "Starting container..."
$CONTAINER_CMD run --name ${CONTAINER_NAME} \
-p 8000:8000 \
-v "$LOGS_DIR":/home/appuser/app/logs${VOLUME_FLAG} \
--security-opt no-new-privileges:true \
--cap-drop ALL \
--user 1000:1000 \
-d ${CONTAINER_NAME}:latest
# Check if container started successfully
if [ $? -eq 0 ]; then
echo "Container started successfully!"
echo "VPN Session Viewer is available at: http://localhost:8000"
echo ""
echo "Container logs:"
$CONTAINER_CMD logs ${CONTAINER_NAME}
echo ""
echo "Note: Log files should be placed in the ./logs directory."
echo " The format should be: {gateway-name}_{ISO-timestamp}.logs"
echo " Example: firewall-1_2025-04-10T17:04:51Z.logs"
else
echo "Failed to start container."
exit 1
fi