socktop-webterm/docker/init-config.sh

189 lines
7.9 KiB
Bash
Raw Normal View History

2025-11-30 10:33:43 +00:00
#!/bin/bash
set -e
# Init script to copy configuration files to the correct locations
# This handles the discrepancy between where K8s mounts configs
# and where the socktop package expects them (HOME directory)
echo "==================================="
echo "Initializing socktop webterm config"
echo "==================================="
# Determine the actual HOME directory for the socktop user
SOCKTOP_HOME=$(eval echo ~socktop)
echo "Socktop HOME: ${SOCKTOP_HOME}"
2025-11-30 11:49:09 +00:00
echo "Current user: $(whoami) (UID: $(id -u))"
2025-11-30 10:33:43 +00:00
# Check if we're running as root
if [ "$(id -u)" -eq 0 ]; then
2025-11-30 11:49:09 +00:00
echo "Running as root, will create directories and set permissions"
# Check if socktop home exists and try to ensure it's accessible
2025-11-30 11:49:09 +00:00
if [ ! -d "${SOCKTOP_HOME}" ]; then
echo "Creating ${SOCKTOP_HOME}..."
mkdir -p "${SOCKTOP_HOME}"
chown socktop:socktop "${SOCKTOP_HOME}" 2>/dev/null || echo " ⚠ Could not change ownership of home directory (may be restricted)"
else
echo " ✓ Home directory exists"
# Try to fix ownership if possible, but don't fail if we can't
chown socktop:socktop "${SOCKTOP_HOME}" 2>/dev/null || echo " ⚠ Could not change ownership of home directory (may be restricted by security context)"
2025-11-30 11:49:09 +00:00
fi
# Create config directories with proper structure
2025-11-30 11:49:09 +00:00
echo "Creating config directories..."
mkdir -p "${SOCKTOP_HOME}/.config/socktop/certs" 2>/dev/null || true
mkdir -p "${SOCKTOP_HOME}/.config/alacritty" 2>/dev/null || true
# Try to fix ownership recursively, ignore errors
chown -R socktop:socktop "${SOCKTOP_HOME}/.config" 2>/dev/null || echo " ⚠ Could not change ownership of .config directory (may be restricted)"
# Ensure directories are writable by socktop user at minimum
chmod -R u+rwX "${SOCKTOP_HOME}/.config" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
echo " ✓ Created directories"
else
2025-11-30 11:49:09 +00:00
echo "Running as non-root user ($(id -u)), creating directories"
# Try to create directories - will work if HOME is writable
2025-11-30 11:49:09 +00:00
mkdir -p "${SOCKTOP_HOME}/.config/socktop/certs" 2>/dev/null || {
echo " ⚠ Could not create directories - checking if they already exist..."
if [ -d "${SOCKTOP_HOME}/.config/socktop/certs" ]; then
echo " ✓ Directories already exist"
else
echo " ✗ Failed to create directories and they don't exist"
echo " Attempting to continue anyway..."
2025-11-30 11:49:09 +00:00
fi
}
mkdir -p "${SOCKTOP_HOME}/.config/alacritty" 2>/dev/null || true
fi
2025-11-30 10:33:43 +00:00
# Copy configuration files
2025-11-30 10:33:43 +00:00
echo "Copying configuration files..."
# Copy profiles.json
if [ -f "/home/socktop/.config/socktop/profiles.json" ]; then
TARGET="${SOCKTOP_HOME}/.config/socktop/profiles.json"
# Remove existing file if it exists
rm -f "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
if [ "$(id -u)" -eq 0 ]; then
# Running as root - copy and set ownership
cp -f /home/socktop/.config/socktop/profiles.json "${TARGET}" 2>/dev/null || {
echo " ⚠ Failed to copy profiles.json, trying alternative method..."
cat /home/socktop/.config/socktop/profiles.json > "${TARGET}" 2>/dev/null || echo " ✗ Could not copy profiles.json"
}
chown socktop:socktop "${TARGET}" 2>/dev/null || true
chmod 644 "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
else
# Running as socktop user
cp -f /home/socktop/.config/socktop/profiles.json "${TARGET}" 2>/dev/null || {
cat /home/socktop/.config/socktop/profiles.json > "${TARGET}" 2>/dev/null || echo " ✗ Could not copy profiles.json"
}
fi
if [ -f "${TARGET}" ]; then
echo " ✓ Copied profiles.json"
2025-11-30 11:49:09 +00:00
fi
2025-11-30 10:33:43 +00:00
else
echo " ⚠ profiles.json not found at mount point"
fi
# Copy alacritty.toml
if [ -f "/home/socktop/.config/alacritty/alacritty.toml" ]; then
TARGET="${SOCKTOP_HOME}/.config/alacritty/alacritty.toml"
rm -f "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
if [ "$(id -u)" -eq 0 ]; then
cp -f /home/socktop/.config/alacritty/alacritty.toml "${TARGET}" 2>/dev/null || cat /home/socktop/.config/alacritty/alacritty.toml > "${TARGET}" 2>/dev/null || true
chown socktop:socktop "${TARGET}" 2>/dev/null || true
chmod 644 "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
else
cp -f /home/socktop/.config/alacritty/alacritty.toml "${TARGET}" 2>/dev/null || cat /home/socktop/.config/alacritty/alacritty.toml > "${TARGET}" 2>/dev/null || true
fi
if [ -f "${TARGET}" ]; then
echo " ✓ Copied alacritty.toml"
2025-11-30 11:49:09 +00:00
fi
2025-11-30 10:33:43 +00:00
else
echo " ⚠ alacritty.toml not found at mount point"
fi
# Copy catppuccin-frappe.toml
if [ -f "/home/socktop/.config/alacritty/catppuccin-frappe.toml" ]; then
TARGET="${SOCKTOP_HOME}/.config/alacritty/catppuccin-frappe.toml"
rm -f "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
if [ "$(id -u)" -eq 0 ]; then
cp -f /home/socktop/.config/alacritty/catppuccin-frappe.toml "${TARGET}" 2>/dev/null || cat /home/socktop/.config/alacritty/catppuccin-frappe.toml > "${TARGET}" 2>/dev/null || true
chown socktop:socktop "${TARGET}" 2>/dev/null || true
chmod 644 "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
else
cp -f /home/socktop/.config/alacritty/catppuccin-frappe.toml "${TARGET}" 2>/dev/null || cat /home/socktop/.config/alacritty/catppuccin-frappe.toml > "${TARGET}" 2>/dev/null || true
fi
if [ -f "${TARGET}" ]; then
echo " ✓ Copied catppuccin-frappe.toml"
2025-11-30 11:49:09 +00:00
fi
2025-11-30 10:33:43 +00:00
else
echo " ⚠ catppuccin-frappe.toml not found at mount point"
fi
# Copy certificates if they exist
if [ -d "/home/socktop/.config/socktop/certs" ]; then
2025-11-30 11:49:09 +00:00
echo "Copying certificates..."
2025-11-30 10:33:43 +00:00
for cert in /home/socktop/.config/socktop/certs/*.pem; do
if [ -f "$cert" ]; then
TARGET="${SOCKTOP_HOME}/.config/socktop/certs/$(basename "$cert")"
rm -f "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
if [ "$(id -u)" -eq 0 ]; then
cp -f "$cert" "${TARGET}" 2>/dev/null || cat "$cert" > "${TARGET}" 2>/dev/null || true
chown socktop:socktop "${TARGET}" 2>/dev/null || true
chmod 644 "${TARGET}" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
else
cp -f "$cert" "${TARGET}" 2>/dev/null || cat "$cert" > "${TARGET}" 2>/dev/null || true
fi
if [ -f "${TARGET}" ]; then
echo " ✓ Copied $(basename "$cert")"
2025-11-30 11:49:09 +00:00
fi
2025-11-30 10:33:43 +00:00
fi
done
else
echo " No certificates directory found (optional)"
fi
# Fix paths in profiles.json if it exists
if [ -f "${SOCKTOP_HOME}/.config/socktop/profiles.json" ]; then
echo "Rewriting paths in profiles.json..."
# Replace /home/socktop with actual HOME directory and ensure certs/ subdirectory
sed -i "s|/home/socktop/.config/socktop/rpi-|${SOCKTOP_HOME}/.config/socktop/certs/rpi-|g" "${SOCKTOP_HOME}/.config/socktop/profiles.json" 2>/dev/null || {
echo " ⚠ Could not rewrite paths in-place, trying alternative method..."
sed "s|/home/socktop/.config/socktop/rpi-|${SOCKTOP_HOME}/.config/socktop/certs/rpi-|g" "${SOCKTOP_HOME}/.config/socktop/profiles.json" > "${SOCKTOP_HOME}/.config/socktop/profiles.json.tmp" 2>/dev/null && \
mv "${SOCKTOP_HOME}/.config/socktop/profiles.json.tmp" "${SOCKTOP_HOME}/.config/socktop/profiles.json" 2>/dev/null || \
echo " ✗ Could not rewrite paths"
}
2025-11-30 11:49:09 +00:00
if [ "$(id -u)" -eq 0 ]; then
chown socktop:socktop "${SOCKTOP_HOME}/.config/socktop/profiles.json" 2>/dev/null || true
2025-11-30 11:49:09 +00:00
fi
2025-11-30 10:33:43 +00:00
echo " ✓ Updated certificate paths"
fi
2025-11-30 11:49:09 +00:00
# Verify final permissions
echo "Verifying permissions..."
ls -la "${SOCKTOP_HOME}/.config/" 2>&1 || echo " ⚠ Could not list config directory"
2025-11-30 10:33:43 +00:00
echo "==================================="
echo "Configuration initialization complete"
echo "==================================="
2025-11-30 11:49:09 +00:00
# Switch to socktop user only if running as root
if [ "$(id -u)" -eq 0 ]; then
2025-11-30 11:49:09 +00:00
echo "Switching to socktop user and executing: $@"
exec runuser -u socktop -- "$@"
else
echo "Already running as non-root user ($(whoami)), continuing..."
exec "$@"
fi