Skip to content

Commit

Permalink
fix supervisor config generation and templates
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Jan 6, 2025
1 parent 1484299 commit c91a919
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
10 changes: 1 addition & 9 deletions etc/home/supervisor.default → etc/home/supervisor.common.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,4 @@ serverurl=unix:///var/run/supervisor/supervisord.sock
file=/var/run/supervisor/supervisord.sock ; path to your socket file

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[program:${process_name}]
command=${command}
autostart=${autostart}
autorestart=${autorestart}
stderr_logfile=/var/log/supervisor/${process_name}.err.log
stdout_logfile=/var/log/supervisor/${process_name}.out.log
environment=${envs}
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
7 changes: 7 additions & 0 deletions etc/home/supervisor.program.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[program:${process_name}]
command=${command}
autostart=${autostart}
autorestart=${autorestart}
stderr_logfile=/var/log/supervisor/${process_name}.err.log
stdout_logfile=/var/log/supervisor/${process_name}.out.log
environment=${envs}
28 changes: 9 additions & 19 deletions lib/process_manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# Define paths
CONFIG_FILE="/etc/worker/services.yml"
TEMPLATE_FILE="/home/${USER}/etc/supervisor.default"
COMMON_TEMPLATE_FILE="/home/${USER}/etc/supervisor.common.conf"
PROGRAM_TEMPLATE_FILE="/home/${USER}/etc/supervisor.program.conf"
FINAL_CONFIG="/home/${USER}/etc/supervisord.conf"

# Function to check for service configurations
Expand Down Expand Up @@ -32,11 +33,14 @@ parse_service_info() {
autorestart=$(echo "$service_json" | jq -r '.autorestart // "false"')
environment=$(echo "$service_json" | jq -r '.environment // [] | join(",")')

# Add an additional newline for better separation and readability
echo -e "\n" >> "$FINAL_CONFIG" # Adds two newlines to the end of the file

sed "s|\${process_name}|$name|g; \
s|\${command}|$command|g; \
s|\${autostart}|$autostart|g; \
s|\${autorestart}|$autorestart|g; \
s|\${envs}|$environment|g" "$TEMPLATE_FILE" >> "$FINAL_CONFIG"
s|\${envs}|$environment|g" "$PROGRAM_TEMPLATE_FILE" >> "$FINAL_CONFIG"
}

# Function to start Supervisor with the generated configuration
Expand All @@ -52,33 +56,19 @@ configure_and_execute_services() {
return 1
fi

# Copy the base Supervisor configuration.
cp "$TEMPLATE_FILE" "$FINAL_CONFIG"

# Remove the template [program:x] section from FINAL_CONFIG
# shellcheck disable=SC2016
sed -i '/\[program:\${process_name}\]/,/^$/d' "$FINAL_CONFIG"
# Copy the base Supervisor common configuration only once at the start.
cp "$COMMON_TEMPLATE_FILE" "$FINAL_CONFIG"

# Convert enabled services to JSON and process each.
local services_yaml
# Filter only services with enabled: true
services_yaml=$(yq e -o=json '.services[] | select(.enabled == true)' "$CONFIG_FILE" | jq -c .)

if [ -z "$services_yaml" ]; then
echo "Failed to parse services from $CONFIG_FILE or no services defined."
return 1
fi

# Use a temporary file to avoid subshell issues
local services_file
services_file=$(mktemp)

echo "$services_yaml" > "$services_file"
mapfile -t services_array < "$services_file"
rm -f "$services_file"

# Process each service in the array
for service_json in "${services_array[@]}"; do
echo "$services_yaml" | while read -r service_json; do
parse_service_info "$service_json"
done

Expand Down

0 comments on commit c91a919

Please sign in to comment.