Improved the script to add more useful packages and improved the entrypoint script.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Ameer Hamza Khan
2025-07-27 13:09:14 +00:00
parent eada31f646
commit bb6dc6ab2e
3 changed files with 101 additions and 60 deletions

View File

@@ -1,33 +1,78 @@
#!/usr/bin/env bash
set -x
term_handler() {
eval $USER_COMMAND_EXIT
eval $SYS_COMMAND_EXIT
# Gracefully update package lists if network is available
echo "Checking network connectivity..."
if ping -c 1 -W 5 8.8.8.8 >/dev/null 2>&1 || ping -c 1 -W 5 1.1.1.1 >/dev/null 2>&1; then
echo "Network detected, refreshing package lists..."
if sudo apt update; then
echo "Package lists updated successfully"
else
echo "Warning: apt update failed despite network connectivity"
echo "Falling back to cached package lists"
fi
else
echo "No network connectivity detected, using cached package lists"
fi
exit 143; # 128 + 15 -- SIGTERM
# Global variable to track background processes
TAIL_PID=""
term_handler() {
echo "Received termination signal, cleaning up..."
# Kill the tail process if it's running
if [[ -n "$TAIL_PID" ]]; then
kill "$TAIL_PID" 2>/dev/null
fi
# Run user-defined exit commands
eval "$COMMAND_EXIT"
echo "Cleanup completed, exiting..."
exit 0
}
# setup handler when the container is exited
trap 'kill ${!}; term_handler' SIGTERM
# Setup signal handlers for graceful shutdown
trap 'term_handler' SIGTERM SIGINT
# setup home directory for the current user. It is useful for attaching vscode with container.
user_name=$(whoami)
user_home="/home/$user_name"
sudo mkdir -p $user_home
sudo chown -R $(id -u):$(id -g) $user_home
cp -r /etc/skel/. $user_home
if [[ $LOG_FILE != "/dev/null" ]];
then
sudo touch $LOG_FILE
sudo chown -R $(id -u):$(id -g) $LOG_FILE
# Only create home directory if it doesn't exist (handles mounted /etc/passwd case)
if [[ ! -d "$user_home" ]]; then
sudo mkdir -p "$user_home"
sudo chown -R "$(id -u):$(id -g)" "$user_home"
# Copy skeleton files only if home directory was created
cp -r /etc/skel/. "$user_home" 2>/dev/null || true
fi
if [[ $LOG_FILE != "/dev/null" ]]; then
sudo touch "$LOG_FILE"
sudo chown -R "$(id -u):$(id -g)" "$LOG_FILE"
fi
echo "Starting SSH service..."
sudo service ssh start
eval $USER_COMMAND_INIT
eval $SYS_COMMAND_INIT
eval $USER_COMMAND_SETUP
# Run initialization commands
eval "$COMMAND_INIT"
tail -f $LOG_FILE & wait ${!}
# Start the main process loop
if [[ $LOG_FILE == "/dev/null" ]]; then
# If no log file, just wait for signals
echo "Container ready, waiting for signals..."
while true; do
sleep 1 &
wait $!
done
else
# If log file specified, tail it
echo "Container ready, tailing log file: $LOG_FILE"
tail -f "$LOG_FILE" &
TAIL_PID=$!
# Wait for the tail process or signals
wait $TAIL_PID
fi