-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[supervisord] | ||
logfile=/var/log/supervisor/supervisord.log ; Logfile path | ||
logfile_maxbytes=50MB ; Maximum log file bytes before rotating | ||
logfile_backups=10 ; Number of backups to keep | ||
loglevel=info ; Log level (info, debug, warn, trace) | ||
pidfile=/var/run/supervisor/supervisord.pid ; Location of the PID file | ||
nodaemon=false ; Run in the foreground (Docker best practice) | ||
minfds=1024 ; Minimum number of file descriptors | ||
minprocs=200 ; Minimum number of processes | ||
|
||
[supervisorctl] | ||
serverurl=unix:///var/run/supervisor/supervisord.sock ; Path to the UNIX socket for supervisorctl to connect to supervisord | ||
|
||
[unix_http_server] | ||
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} ; Command to start the process | ||
autostart=${autostart} ; Whether to start the process at supervisord start | ||
autorestart=${autorestart} ; Whether to restart the process on exit | ||
stderr_logfile=${stderr_logfile} ; Path for stderr logfile | ||
stdout_logfile=${stdout_logfile} ; Path for stdout logfile | ||
environment=${envs} ; Environment variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,81 @@ | ||
#!/bin/bash | ||
|
||
# Function to check if systemd should be enabled | ||
should_enable_systemd() { | ||
if [ -f "$CONFIG_FILE" ]; then | ||
# Define paths | ||
CONFIG_FILE="/etc/worker/services.yml" | ||
TEMPLATE_FILE="/home/${USER}/etc/supervisor.default" | ||
FINAL_CONFIG="/home/${USER}/etc/supervisord.conf" | ||
|
||
# Function to check for service configurations | ||
should_generate_config() { | ||
if [ -f "$CONFIG_FILE" ] && [ "$(yq e '.services | length' "$CONFIG_FILE")" -gt 0 ]; then | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
} | ||
|
||
# Function to parse service information from YAML configuration | ||
# Helper function to parse and process each service configuration | ||
parse_service_info() { | ||
local service_yaml="$1" | ||
name=$(echo "$service_yaml" | jq -r '.name' -) | ||
exec_start=$(echo "$service_yaml" | jq -r '.exec_start' -) | ||
after=$(echo "$service_yaml" | jq -r '.after' -) | ||
local service_json="$1" | ||
local name=$(echo "$service_json" | jq -r '.name') | ||
local command=$(echo "$service_json" | jq -r '.command') | ||
local autostart=$(echo "$service_json" | jq -r '.autostart // "false"') | ||
local autorestart=$(echo "$service_json" | jq -r '.autorestart // "false"') | ||
local stderr_logfile=$(echo "$service_json" | jq -r '.stderr_logfile // ""') | ||
local stdout_logfile=$(echo "$service_json" | jq -r '.stdout_logfile // ""') | ||
local environment=$(echo "$service_json" | jq -r '.environment // [] | join(",")') | ||
|
||
# For each service, replace placeholders and append to FINAL_CONFIG | ||
sed "s|\${process_name}|$name|g; \ | ||
s|\${command}|$command|g; \ | ||
s|\${autostart}|$autostart|g; \ | ||
s|\${autorestart}|$autorestart|g; \ | ||
s|\${stderr_logfile}|$stderr_logfile|g; \ | ||
s|\${stdout_logfile}|$stdout_logfile|g; \ | ||
s|\${envs}|$environment|g" "$TEMPLATE_FILE" >> "$FINAL_CONFIG" | ||
} | ||
|
||
# Function to create a systemd service file from a template | ||
create_service_file() { | ||
local template_file="$SERVICE_DIR/default.service" | ||
sed -e "s|\${name}|$name|g" \ | ||
-e "s|\${exec_start}|$exec_start|g" \ | ||
-e "s|\${after}|$after|g" \ | ||
"$template_file" > "${SERVICE_DIR}/${name}.service" | ||
# Function to start Supervisor with the generated configuration | ||
start_supervisor() { | ||
echo "Starting Supervisor with the generated configuration..." | ||
supervisord -c "$FINAL_CONFIG" | ||
} | ||
|
||
# Main function to generate systemd service unit files from template based on services.yml | ||
generate_and_activate_services() { | ||
if ! should_enable_systemd; then | ||
echo "Systemd is not enabled. services.yml not found." | ||
# Function to configure and start the Supervisor | ||
configure_and_execute_services() { | ||
if ! should_generate_config; then | ||
echo "No services found in $CONFIG_FILE. Skipping Supervisor configuration." | ||
return 1 | ||
fi | ||
|
||
echo "services.yml found. Generating and managing systemd service files..." | ||
# Copy the base Supervisor configuration. | ||
cp "$TEMPLATE_FILE" "$FINAL_CONFIG" | ||
|
||
# Remove the template [program:x] section from FINAL_CONFIG | ||
sed -i '/\[program:\${process_name}\]/,/^$/d' "$FINAL_CONFIG" | ||
|
||
# Convert services to JSON and process each. | ||
local services_yaml | ||
services_yaml=$(yq eval -o=json '.services[]' "$CONFIG_FILE" | jq -c .) | ||
|
||
# Use a temporary file to avoid a subshell | ||
services_yaml=$(yq e -o=json '.services[]' "$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" | ||
|
||
# Read all lines into an array, then delete the file | ||
mapfile -t services_array < "$services_file" | ||
rm -f "$services_file" | ||
|
||
# Process each service in the array | ||
for service_yaml in "${services_array[@]}"; do | ||
parse_service_info "$service_yaml" | ||
|
||
if [[ -n "$name" && -n "$exec_start" && -n "$after" ]]; then | ||
create_service_file || { echo "Failed to create service file for $name"; return 1; } | ||
echo "Service file for $name created." | ||
else | ||
echo "Missing required service fields for a service in services.yml" | ||
return 1 | ||
fi | ||
for service_json in "${services_array[@]}"; do | ||
parse_service_info "$service_json" | ||
done | ||
} | ||
|
||
# Variables (these should be defined or passed to the script) | ||
CONFIG_FILE="/etc/worker/services.yml" | ||
SERVICE_DIR="/home/${USER}/etc" | ||
|
||
# After generating the config and processing services, start Supervisor | ||
start_supervisor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters