Skip to content

Commit

Permalink
improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Feb 12, 2025
1 parent 287843b commit b05d124
Show file tree
Hide file tree
Showing 30 changed files with 542 additions and 197 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ COPY etc/configs /usr/local/configs
COPY lib /usr/local/lib
COPY bin/entrypoint.sh /usr/local/bin/entrypoint.sh

# Make all shell scripts executable
RUN chmod +x /usr/local/lib/*.sh /usr/local/bin/entrypoint.sh

# Set permissions during build
RUN chmod +x /usr/local/bin/entrypoint.sh && \
chown -R ${UID}:${GID} /usr/local/configs && \
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ clean:
# Test Docker container
test: clean
@echo "Setting up test environment..."
@$(MAKE) run VOLUMES="$(TEST_WORKER_CONFIG):/home/$(USER)/worker.yaml:ro $(TESTS_TASKS_DIR):/home/$(USER)/tasks:ro $(TESTS_MAIN_SCRIPT):/home/$(USER)/main.sh:ro" COMMAND="/home/$(USER)/main.sh"
@$(MAKE) run VOLUMES="$(TEST_WORKER_CONFIG):/home/$(USER)/worker.yaml:ro $(TEST_SERVICES_CONFIG):/home/$(USER)/services.yaml:ro $(TESTS_TASKS_DIR):/home/$(USER)/tasks:ro $(TESTS_MAIN_SCRIPT):/home/$(USER)/main.sh:ro ./src/tests/utils.sh:/usr/local/tests/utils.sh:ro" COMMAND="/home/$(USER)/main.sh"
@$(MAKE) log FOLLOW_LOGS=true
@$(MAKE) clean

Expand Down
1 change: 1 addition & 0 deletions Makefile.variables
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ENV_FILE ?= .env
TESTS_MAIN_SCRIPT ?= ./src/tests/main.sh
TESTS_TASKS_DIR ?= ./src/tests/tasks
TEST_WORKER_CONFIG ?= ./src/tests/configs/worker.yaml
TEST_SERVICES_CONFIG ?= ./src/tests/configs/services.yaml
USER = udx
VOLUMES ?= ./src/scripts:/home/$(USER)
DEBUG ?= false
Expand Down
77 changes: 73 additions & 4 deletions bin/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,61 @@
# shellcheck disable=SC1091
source /usr/local/lib/utils.sh

# Global variable to track if shutdown is in progress
SHUTDOWN_IN_PROGRESS=0

# Signal handlers for graceful shutdown
handle_shutdown() {
local signal=$1

# Prevent multiple shutdown attempts
if [ "$SHUTDOWN_IN_PROGRESS" -eq 1 ]; then
log_info "Shutdown already in progress..."
return
fi
SHUTDOWN_IN_PROGRESS=1

log_info "⏹️ $signal received - initiating graceful shutdown..."

# Stop supervisor itself gracefully
if [ -f /var/run/supervisord.pid ]; then
log_info "Stopping all supervisor services..."
supervisorctl stop all

# Wait for services to stop (max 30 seconds)
local timeout=30
local elapsed=0
while [ $elapsed -lt $timeout ]; do
if ! supervisorctl status | grep -Eq 'RUNNING|STOPPING|STARTING'; then
log_info "All services stopped successfully"
break
fi
sleep 1
elapsed=$((elapsed + 1))
done

if [ $elapsed -eq $timeout ]; then
log_error "Entrypoint" "❌ Timeout waiting for services to stop"
fi

# Stop supervisord itself
log_info "Stopping supervisord..."
kill -TERM "$(cat /var/run/supervisord.pid)"
wait "$(cat /var/run/supervisord.pid)" 2>/dev/null || true
fi

# Kill any remaining child processes
pkill -P $$

log_info "Shutdown complete"
exit 0
}

# Set up signal handlers
trap 'handle_shutdown SIGTERM' TERM
trap 'handle_shutdown SIGINT' INT
trap 'handle_shutdown SIGQUIT' QUIT

udx_logo

log_info "Welcome to UDX Worker Container. Initializing environment..."
Expand Down Expand Up @@ -47,18 +102,32 @@ wait_for_services() {
return 1
}

# Initialize signal handlers and prepare environment
log_info "Initializing signal handlers for graceful shutdown..."

# Main execution path
if [ "$#" -gt 0 ]; then
log_info "Executing command: $*"

if [[ "$1" =~ \.sh$ ]]; then
"$@" # Execute the provided command
log_info "Shell script execution completed. Exiting."
exit 0
# Execute shell scripts in a subshell to maintain signal handling
("$@")
exit_code=$?
log_info "Shell script execution completed with exit code $exit_code"
exit "$exit_code"
else
handle_services
"$@" # Execute the provided command
# Start the command in background and wait for it
"$@" &
command_pid=$!
wait "$command_pid"
exit_code=$?
exit "$exit_code"
fi
else
handle_services
# Keep the script running and wait for signals
while [ "$SHUTDOWN_IN_PROGRESS" -eq 0 ]; do
sleep 1
done
fi
10 changes: 5 additions & 5 deletions lib/auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ authenticate_actors() {
log_info "Reading base64 encoded JSON credentials"
creds=$(echo "$creds" | base64 --decode)
else
log_error "Credentials format not recognized for $provider. Skipping..."
log_error "Authentication" "Credentials format not recognized for $provider. Skipping..."
continue
fi

Expand All @@ -70,20 +70,20 @@ authenticate_actors() {
if command -v "$auth_function" > /dev/null; then

if ! authenticate_provider "$provider" "$auth_function" "$creds"; then
log_error "Authentication failed for provider $provider."
log_error "Authentication" "Authentication failed for provider $provider."
return 1
fi
configured_providers+=("$provider")
else
log_error "Authentication function $auth_function not found for $provider. Skipping..."
log_error "Authentication" "Authentication function $auth_function not found for $provider. Skipping..."
continue
fi
else
log_error "Authentication script $auth_script not found for $provider. Skipping..."
log_error "Authentication" "Authentication script $auth_script not found for $provider. Skipping..."
continue
fi
else
log_error "Invalid JSON credentials for $provider. Skipping..."
log_error "Authentication" "Invalid JSON credentials for $provider. Skipping..."
continue
fi
done
Expand Down
9 changes: 6 additions & 3 deletions lib/auth/aws.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# shellcheck source=/usr/local/lib/utils.sh disable=SC1091
source /usr/local/lib/utils.sh

# Example usage of the function
# aws_authenticate "/path/to/your/aws_creds.json"

Expand All @@ -12,7 +15,7 @@ aws_authenticate() {
creds_content=$(cat "$creds_json")

if [[ -z "$creds_content" ]]; then
echo "[ERROR] No AWS credentials provided." >&2
log_error "AWS Authentication" "No AWS credentials provided."
return 1
fi

Expand All @@ -24,7 +27,7 @@ aws_authenticate() {
sessionToken=$(echo "$creds_content" | jq -r '.SessionToken')

if [[ -z "$accessKeyId" || -z "$secretAccessKey" ]]; then
echo "[ERROR] Missing required AWS credentials." >&2
log_error "AWS Authentication" "Missing required AWS credentials."
return 1
fi

Expand All @@ -35,5 +38,5 @@ aws_authenticate() {
export AWS_SESSION_TOKEN="$sessionToken"
fi

echo "[INFO] AWS credentials set successfully."
log_success "AWS Authentication" "AWS credentials set successfully."
}
10 changes: 5 additions & 5 deletions lib/auth/azure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ azure_authenticate() {
creds_content=$(cat "$creds_json")

if [[ -z "$creds_content" ]]; then
echo "[ERROR] No Azure credentials provided." >&2
log_error "Azure Authentication" "No Azure credentials provided."
return 1
fi

Expand All @@ -30,20 +30,20 @@ azure_authenticate() {
tenantId=$(echo "$creds_content" | jq -r '.tenantId')

if [[ -z "$clientId" || -z "$clientSecret" || -z "$subscriptionId" || -z "$tenantId" ]]; then
echo "[ERROR] Missing required Azure credentials." >&2
log_error "Azure Authentication" "Missing required Azure credentials."
return 1
fi

log_info "Authenticating Azure service principal..."
if ! az login --service-principal -u "$clientId" -p "$clientSecret" --tenant "$tenantId" >/dev/null 2>&1; then
echo "[ERROR] Azure service principal authentication failed." >&2
log_error "Azure Authentication" "Azure service principal authentication failed."
return 1
fi

if ! az account set --subscription "$subscriptionId" >/dev/null 2>&1; then
echo "[ERROR] Failed to set Azure subscription." >&2
log_error "Azure Authentication" "Failed to set Azure subscription."
return 1
fi

log_info "Azure service principal authenticated and subscription set."
log_success "Azure Authentication" "Azure service principal authenticated and subscription set."
}
15 changes: 9 additions & 6 deletions lib/auth/bitwarden.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# shellcheck source=/usr/local/lib/utils.sh disable=SC1091
source /usr/local/lib/utils.sh

# Function to authenticate Bitwarden using API key or master password
#
# Example usage of the function
Expand All @@ -9,7 +12,7 @@ bitwarden_authenticate() {
local creds_file="$1"

if [[ ! -f "$creds_file" ]]; then
echo "[ERROR] Credentials file not found: $creds_file" >&2
log_error "Bitwarden Authentication" "Credentials file not found: $creds_file"
return 1
fi

Expand All @@ -18,7 +21,7 @@ bitwarden_authenticate() {
creds_content=$(cat "$creds_file")

if [[ -z "$creds_content" ]]; then
echo "[ERROR] Credentials file is empty: $creds_file" >&2
log_error "Bitwarden Authentication" "Credentials file is empty: $creds_file"
return 1
fi

Expand All @@ -28,7 +31,7 @@ bitwarden_authenticate() {
master_password=$(echo "$creds_content" | jq -r '.masterPassword // empty')

if [[ -z "$api_key" && -z "$master_password" ]]; then
echo "[ERROR] Either API key or master password must be provided in the credentials file." >&2
log_error "Bitwarden Authentication" "Either API key or master password must be provided in the credentials file."
return 1
fi

Expand All @@ -40,18 +43,18 @@ bitwarden_authenticate() {
local email
email=$(echo "$creds_content" | jq -r '.email // empty')
if [[ -z "$email" ]]; then
echo "[ERROR] Email must be provided with the master password." >&2
log_error "Bitwarden Authentication" "Email must be provided with the master password."
return 1
fi
session_key=$(bw login "$email" "$master_password" --raw 2>/dev/null)
fi

if [[ -z "$session_key" ]]; then
echo "[ERROR] Failed to authenticate with Bitwarden." >&2
log_error "Bitwarden Authentication" "Failed to authenticate with Bitwarden."
return 1
fi

echo "[INFO] Bitwarden authentication successful. Session key obtained."
log_success "Bitwarden Authentication" "Authenticated with Bitwarden."
export BW_SESSION="$session_key"
return 0
}
15 changes: 9 additions & 6 deletions lib/auth/gcp.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# shellcheck source=/usr/local/lib/utils.sh disable=SC1091
source /usr/local/lib/utils.sh

# Function to authenticate GCP service accounts
#
# Example usage of the function
Expand All @@ -15,7 +18,7 @@ gcp_authenticate() {
creds_content=$(cat "$creds_json")

if [[ -z "$creds_content" ]]; then
echo "[ERROR] No GCP credentials provided." >&2
log_error "GCP Authentication" "No GCP credentials provided."
return 1
fi

Expand All @@ -27,7 +30,7 @@ gcp_authenticate() {
projectId=$(echo "$creds_content" | jq -r '.project_id')

if [[ -z "$clientEmail" || -z "$privateKey" || -z "$projectId" ]]; then
echo "[ERROR] Missing required GCP credentials." >&2
log_error "GCP Authentication" "Missing required GCP credentials."
return 1
fi

Expand All @@ -41,20 +44,20 @@ gcp_authenticate() {
jq -n --arg clientEmail "$clientEmail" --arg privateKey "$privateKey" --arg projectId "$projectId" \
'{client_email: $clientEmail, private_key: $privateKey, project_id: $projectId}' > "$temp_creds_file"

echo "[INFO] Authenticating GCP service account..."
log_info "GCP Authentication" "Authenticating GCP service account..."
if ! gcloud auth activate-service-account "$clientEmail" --key-file="$temp_creds_file" >/dev/null 2>&1; then
echo "[ERROR] GCP service account authentication failed." >&2
log_error "GCP Authentication" "GCP service account authentication failed."
rm -f "$temp_creds_file"
return 1
fi

if ! gcloud config set project "$projectId" >/dev/null 2>&1; then
echo "[ERROR] Failed to set GCP project." >&2
log_error "GCP Authentication" "Failed to set GCP project."
rm -f "$temp_creds_file"
return 1
fi

echo "[INFO] GCP service account authenticated and project set."
log_success "GCP Authentication" "GCP service account authenticated and project set."

# Clean up temporary credentials file
rm -f "$temp_creds_file"
Expand Down
9 changes: 5 additions & 4 deletions lib/cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/bin/bash

# Include utility functions and worker config utilities
# shellcheck source=/dev/null
source /usr/local/lib/utils.sh
# Include worker config utilities first
# shellcheck source=/dev/null
source /usr/local/lib/worker_config.sh

# shellcheck source=/dev/null
source /usr/local/lib/utils.sh

# Generic function to clean up authentication for any provider
cleanup_provider() {
local provider=$1
Expand Down Expand Up @@ -46,7 +47,7 @@ cleanup_provider() {
if echo "$logout_output" | grep -q -E "No credentials available to revoke|No active sessions|No active accounts"; then
log_info "No active $name credentials to revoke."
else
log_error "Failed to log out of $name: $logout_output"
log_error "Cleanup" "Failed to log out of $name: $logout_output"
return 1
fi
else
Expand Down
5 changes: 4 additions & 1 deletion lib/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ for module in /usr/local/lib/cli/*.sh; do
source "$module"
done

# shellcheck source=/usr/local/lib/utils.sh disable=SC1091
source /usr/local/lib/utils.sh

# CLI Interface
case $1 in
env)
Expand All @@ -21,7 +24,7 @@ case $1 in
service_handler "$@"
;;
*)
echo "Usage: $0 {service|env|...}"
log_error "CLI" "Unknown command: $1"
exit 1
;;
esac
Loading

0 comments on commit b05d124

Please sign in to comment.