takserver/entrypoint.sh
Merith-TK 4f4bc97f3d
All checks were successful
Build Docker Image on Commit / build-and-publish (push) Successful in 2m43s
enforce proper file ownership
2025-07-05 03:55:58 +01:00

354 lines
No EOL
12 KiB
Bash

#!/bin/bash
set -e
# Colors for logging
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${GREEN}[INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_debug() {
echo -e "${BLUE}[DEBUG]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_info "Starting TAK Server Hybrid Container..."
# Ensure proper ownership of key directories
log_debug "Enforcing ownership permissions..."
chown -R tak:tak /opt/tak 2>/dev/null || true
chown -R postgres:postgres /var/lib/postgresql 2>/dev/null || true
log_debug "Ownership permissions enforced"
# Source helper scripts
log_debug "Sourcing helper scripts..."
source /scripts/tak-version.sh
log_debug "Helper scripts loaded successfully"
# Check if TAK server files are provided
log_info "Checking for TAK server files..."
log_debug "Looking for files in /takserver-zip directory..."
if [ ! -d "/takserver-zip" ]; then
log_error "Mount point /takserver-zip does not exist!"
log_error "Please ensure you mount a directory containing TAK server files"
log_error "Example: docker run -v /path/to/tak/files:/takserver-zip:ro ..."
exit 1
fi
# Check if directory is empty
if [ ! "$(ls -A /takserver-zip 2>/dev/null)" ]; then
log_error "Directory /takserver-zip is empty!"
log_error "Please place takserver-docker-X.Y-RELEASE-Z.zip files in the mounted directory"
log_debug "Directory contents:"
ls -l /takserver-zip/ || log_debug "Cannot list directory contents"
exit 1
fi
# List all files in the directory for debugging
log_debug "Contents of /takserver-zip:"
ls -l /takserver-zip/ | while read line; do
log_debug " $line"
done
# Check for zip files specifically
ZIP_COUNT=$(find /takserver-zip -name "*.zip" -type f | wc -l)
log_debug "Found $ZIP_COUNT zip files in /takserver-zip"
if [ $ZIP_COUNT -eq 0 ]; then
log_error "No zip files found in /takserver-zip!"
log_error "Please ensure you have takserver-docker-X.Y-RELEASE-Z.zip files"
log_debug "Available files:"
find /takserver-zip -type f | while read file; do
log_debug " $(basename $file)"
done
while true; do sleep 3600; done
fi
# Check for TAK server specific zip files
TAK_ZIP_COUNT=$(find /takserver-zip -name "takserver-docker-*-RELEASE-*.zip" -type f | wc -l)
log_debug "Found $TAK_ZIP_COUNT TAK server zip files"
if [ $TAK_ZIP_COUNT -eq 0 ]; then
log_error "No TAK server zip files found matching pattern 'takserver-docker-*-RELEASE-*.zip'!"
log_error "Available zip files:"
find /takserver-zip -name "*.zip" -type f | while read file; do
log_error " $(basename $file)"
done
log_error "Required format: takserver-docker-X.Y-RELEASE-Z.zip"
log_error "Example: takserver-docker-5.4-RELEASE-19.zip"
exit 1
fi
# Use the helper script to find the latest TAK release
log_info "Finding latest TAK server release..."
LATEST_ZIP=$(find_latest_tak_release /takserver-zip)
if [ $? -ne 0 ]; then
log_error "Failed to find latest TAK release: $LATEST_ZIP"
log_info "Available TAK releases:"
list_tak_releases /takserver-zip
exit 1
fi
log_info "Found latest TAK server release: $(basename $LATEST_ZIP)"
log_info "Version: $(get_tak_version $LATEST_ZIP)"
log_debug "Full path: $LATEST_ZIP"
# Extract the zip file if not already extracted
if [ ! -f /opt/tak/configureInDocker.sh ]; then
log_info "TAK server files not found - extracting from ZIP..."
log_debug "Using extraction script: /scripts/tak-extract.sh"
# Use the extraction script
if /scripts/tak-extract.sh extract "$LATEST_ZIP" /opt/tak; then
log_info "TAK server extraction completed successfully"
# Set proper permissions
/scripts/tak-extract.sh permissions /opt/tak tak:tak
# Verify installation
if /scripts/tak-extract.sh verify /opt/tak; then
log_info "TAK server installation verified"
else
log_error "TAK server installation verification failed!"
exit 1
fi
else
log_error "Failed to extract TAK server files!"
exit 1
fi
else
log_info "TAK server files already exist - skipping extraction"
log_debug "Found existing configureInDocker.sh at /opt/tak/configureInDocker.sh"
# Ensure permissions are correct on existing files
log_debug "Ensuring correct permissions on existing TAK files..."
/scripts/tak-extract.sh permissions /opt/tak tak:tak
# Also enforce ownership at the directory level
log_debug "Enforcing TAK directory ownership..."
chown -R tak:tak /opt/tak
fi
# Start PostgreSQL
log_info "Starting PostgreSQL..."
# Ensure PostgreSQL directories have correct ownership
log_debug "Enforcing PostgreSQL directory ownership..."
chown -R postgres:postgres /var/lib/postgresql
chown -R postgres:postgres /var/run/postgresql 2>/dev/null || true
# Initialize PostgreSQL if not already done
if [ ! -f /var/lib/postgresql/15/main/PG_VERSION ]; then
log_info "Initializing PostgreSQL database cluster..."
if sudo -u postgres /usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/15/main; then
log_info "PostgreSQL initialized successfully"
# Configure PostgreSQL
log_info "Configuring PostgreSQL..."
# Create postgresql.conf
log_debug "Creating postgresql.conf..."
cat > /tmp/postgresql.conf <<EOF
listen_addresses = '*'
port = 5432
max_connections = 100
shared_buffers = 128MB
log_timezone = 'UTC'
timezone = 'UTC'
EOF
sudo -u postgres cp /tmp/postgresql.conf /var/lib/postgresql/15/main/postgresql.conf
# Create pg_hba.conf
log_debug "Creating pg_hba.conf..."
cat > /tmp/pg_hba.conf <<EOF
# PostgreSQL Client Authentication Configuration File
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 md5
EOF
sudo -u postgres cp /tmp/pg_hba.conf /var/lib/postgresql/15/main/pg_hba.conf
# Verify config files were created
if [ -f /var/lib/postgresql/15/main/postgresql.conf ]; then
log_debug "postgresql.conf created successfully"
else
log_error "Failed to create postgresql.conf!"
exit 1
fi
if [ -f /var/lib/postgresql/15/main/pg_hba.conf ]; then
log_debug "pg_hba.conf created successfully"
else
log_error "Failed to create pg_hba.conf!"
exit 1
fi
log_info "PostgreSQL configuration completed"
# Ensure ownership is correct after configuration
log_debug "Ensuring PostgreSQL ownership after configuration..."
chown -R postgres:postgres /var/lib/postgresql/15/main
else
log_error "Failed to initialize PostgreSQL!"
exit 1
fi
else
log_info "PostgreSQL already initialized"
# Check if config files exist, create them if they don't
if [ ! -f /var/lib/postgresql/15/main/postgresql.conf ]; then
log_info "Creating missing postgresql.conf..."
# Create postgresql.conf
log_debug "Creating postgresql.conf..."
cat > /tmp/postgresql.conf <<EOF
listen_addresses = '*'
port = 5432
max_connections = 100
shared_buffers = 128MB
log_timezone = 'UTC'
timezone = 'UTC'
EOF
sudo -u postgres cp /tmp/postgresql.conf /var/lib/postgresql/15/main/postgresql.conf
fi
if [ ! -f /var/lib/postgresql/15/main/pg_hba.conf ]; then
log_info "Creating missing pg_hba.conf..."
# Create pg_hba.conf
log_debug "Creating pg_hba.conf..."
cat > /tmp/pg_hba.conf <<EOF
# PostgreSQL Client Authentication Configuration File
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 md5
EOF
sudo -u postgres cp /tmp/pg_hba.conf /var/lib/postgresql/15/main/pg_hba.conf
fi
log_debug "Configuration files checked/created"
# Ensure ownership is correct after any config file creation
log_debug "Ensuring PostgreSQL ownership after config check..."
chown -R postgres:postgres /var/lib/postgresql/15/main
fi
# Start PostgreSQL server
log_info "Starting PostgreSQL server..."
# Verify config files exist before starting
log_debug "Checking configuration files..."
if [ -f /var/lib/postgresql/15/main/postgresql.conf ]; then
log_debug "postgresql.conf exists"
else
log_error "postgresql.conf missing!"
exit 1
fi
if [ -f /var/lib/postgresql/15/main/pg_hba.conf ]; then
log_debug "pg_hba.conf exists"
else
log_error "pg_hba.conf missing!"
exit 1
fi
# Show the contents of the data directory
log_debug "Contents of PostgreSQL data directory:"
ls -la /var/lib/postgresql/15/main/ | while read line; do
log_debug " $line"
done
# Start PostgreSQL with verbose logging to catch any errors
log_debug "Starting PostgreSQL with command: sudo -u postgres /usr/lib/postgresql/15/bin/postgres -D /var/lib/postgresql/15/main"
sudo -u postgres /usr/lib/postgresql/15/bin/postgres -D /var/lib/postgresql/15/main &
POSTGRES_PID=$!
log_debug "PostgreSQL started with PID: $POSTGRES_PID"
# Give PostgreSQL a moment to start
sleep 2
# Check if the process is still running
if kill -0 $POSTGRES_PID 2>/dev/null; then
log_debug "PostgreSQL process is still running"
else
log_error "PostgreSQL process died immediately!"
log_error "Checking system logs for errors..."
# Try to get any error output
journalctl -u postgresql --no-pager --lines=10 2>/dev/null || log_debug "No journalctl available"
exit 1
fi
# Wait for PostgreSQL to start and setup database
log_info "Waiting for PostgreSQL to be ready..."
POSTGRES_READY=0
for i in {1..30}; do
if pg_isready -h localhost -p 5432 >/dev/null 2>&1; then
POSTGRES_READY=1
log_info "PostgreSQL is ready after ${i} seconds"
break
fi
log_debug "PostgreSQL not ready yet, waiting... (attempt $i/30)"
sleep 1
done
if [ $POSTGRES_READY -eq 0 ]; then
log_error "PostgreSQL failed to become ready after 30 seconds!"
log_error "Checking PostgreSQL process status..."
if kill -0 $POSTGRES_PID 2>/dev/null; then
log_error "PostgreSQL process is running but not accepting connections"
else
log_error "PostgreSQL process has died"
fi
exit 1
fi
# Use the database setup script
log_info "Setting up database..."
if /scripts/db-setup.sh setup; then
log_info "Database setup completed successfully"
else
log_error "Database setup failed!"
exit 1
fi
# Run database configuration if it exists
if [ -f "/opt/tak/db-utils/configureInDocker.sh" ]; then
log_info "Configuring TAK database..."
/opt/tak/db-utils/configureInDocker.sh &
DB_CONFIG_PID=$!
log_debug "TAK database configuration started with PID: $DB_CONFIG_PID"
sleep 10
else
log_warn "TAK database configuration script not found, skipping..."
fi
# Start TAK server
if [ -f "/opt/tak/configureInDocker.sh" ]; then
log_info "Starting TAK server..."
log_debug "Executing: /opt/tak/configureInDocker.sh init"
exec /opt/tak/configureInDocker.sh init
else
log_error "TAK server configuration script not found!"
log_error "Expected file: /opt/tak/configureInDocker.sh"
log_error "The extracted TAK server files may be incomplete or corrupted."
log_debug "Contents of /opt/tak:"
ls -la /opt/tak/ | while read line; do
log_debug " $line"
done
exit 1
fi