Skip to content

Commit

Permalink
added enabled flag for services config
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Dec 29, 2024
1 parent 3294161 commit c9e9ec8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ configure_environment() {
# Perform process manager setup
log_info "Setting up process manager..."
if should_generate_config; then
echo "Generating Supervisor configurations..."
echo "Preparing Supervisor configurations..."
configure_and_execute_services
else
echo "No services found in $CONFIG_FILE. Skipping Supervisor configuration."
Expand Down
14 changes: 11 additions & 3 deletions lib/process_manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ 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
local enabled_services_count
# Extract enabled services into JSON format
services_yaml=$(yq e -o=json '.services[] | select(.enabled == true)' "$CONFIG_FILE")
# Count the number of items in the JSON array, trimming any newlines or spaces
enabled_services_count=$(echo "$services_yaml" | jq -c '. | length' | tr -d '\n')

# Check if the configuration file exists and there is at least one enabled service
if [ -f "$CONFIG_FILE" ] && [ "${enabled_services_count:-0}" -gt 0 ]; then
return 0
else
return 1
Expand Down Expand Up @@ -52,9 +59,10 @@ configure_and_execute_services() {
# shellcheck disable=SC2016
sed -i '/\[program:\${process_name}\]/,/^$/d' "$FINAL_CONFIG"

# Convert services to JSON and process each.
# Convert enabled services to JSON and process each.
local services_yaml
services_yaml=$(yq e -o=json '.services[]' "$CONFIG_FILE" | jq -c .)
# 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."
Expand Down
6 changes: 4 additions & 2 deletions src/configs/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
kind: workerService
version: udx.io/worker-v1/service
services:
- name: "webapp"
- name: "app_service"
enabled: "false"
command: "sh /usr/local/scripts/process_example.sh"
autostart: "true"
autorestart: "false"
environment:
- "KEY1=value1"
- "KEY2=value2"
- name: "another_service"
enabled: "false"
command: "sh /usr/local/scripts/process_example.sh"
autostart: "true"
autorestart: "true"
environment: [] # Even if empty, define it as an empty array
environment: []

0 comments on commit c9e9ec8

Please sign in to comment.