Skip to content

Commit

Permalink
improvements for worker_config module
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Nov 26, 2024
1 parent ef63992 commit 4fd12f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 86 deletions.
12 changes: 5 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,11 @@ RUN groupadd -g ${GID} ${USER} && \
WORKDIR /home/${USER}

# Create necessary directories and set permissions for GPG and other files
RUN mkdir -p /home/${USER}/.gnupg && \
chmod 700 /home/${USER}/.gnupg && \
mkdir -p /home/${USER}/etc /home/${USER}/.cd/configs && \
chown -R ${USER}:${USER} /home/${USER}
RUN mkdir -p /etc/worker /home/${USER}/.cd/configs && \
chown -R ${USER}:${USER} /home/${USER} /etc/worker

# Copy built-in worker.yml to the container
COPY ./src/configs/worker.yml /usr/src/app/src/configs/worker.yml
COPY ./src/configs/worker.yml /etc/worker/worker.yml

# Copy the bin, etc, and lib directories
COPY ./etc/home /home/${USER}/etc
Expand All @@ -104,8 +102,8 @@ COPY ./bin/entrypoint.sh /usr/local/bin/entrypoint.sh
COPY ./bin/test.sh /usr/local/bin/test.sh

# Set executable permissions and ownership for scripts and configs
RUN chmod +x /usr/local/lib/* /usr/local/bin/entrypoint.sh /usr/local/bin/test.sh && \
chown -R ${USER}:${USER} /usr/local/lib /usr/src/app/src/configs /home/${USER}/etc /home/${USER}/.cd/configs
RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/test.sh && \
chown -R ${USER}:${USER} /usr/local/lib /etc/worker /home/${USER}/etc /home/${USER}/.cd/configs

# Switch to non-root user
USER ${USER}
Expand Down
2 changes: 1 addition & 1 deletion Makefile.variables
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Variables
IMAGE_NAME ?= udx-worker/udx-worker
IMAGE_NAME ?= usabilitydynamics/udx-worker
TAG ?= latest
DOCKER_IMAGE := $(IMAGE_NAME):$(TAG)
CONTAINER_NAME ?= udx-worker-container
Expand Down
93 changes: 15 additions & 78 deletions lib/worker_config.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
#!/bin/bash

# Utility functions for logging
log_info() {
echo "[INFO] $1"
}

log_error() {
echo "[ERROR] $1"
}

# Paths for configurations
BUILT_IN_CONFIG="/usr/src/app/src/configs/worker.yml"
USER_CONFIG="/home/${USER}/.cd/configs/worker.yml"
MERGED_CONFIG="/home/${USER}/.cd/configs/merged_worker.yml"
BUILT_IN_CONFIG="/etc/worker/worker.yml"
USER_CONFIG="/home/udx/.cd/configs/worker.yml"
MERGED_CONFIG="/home/udx/.cd/configs/merged_worker.yml"

# Function to ensure configuration file exists
# Ensure configuration file exists
ensure_config_exists() {
local config_path="$1"
if [[ ! -f "$config_path" ]]; then
Expand All @@ -23,34 +12,37 @@ ensure_config_exists() {
fi
}

# Function to merge the built-in and user-provided configuration files
# Merge built-in and user-provided configurations
merge_worker_configs() {
log_info "Merging worker configurations..."

# Ensure the built-in config exists
# Ensure built-in config exists
ensure_config_exists "$BUILT_IN_CONFIG" || return 1

# Ensure target directory exists
mkdir -p "$(dirname "$MERGED_CONFIG")"

if [[ -f "$USER_CONFIG" ]]; then
log_info "User-provided configuration detected. Merging with the built-in configuration."
log_info "User configuration detected. Merging with the built-in configuration."

# Merge user config with built-in config, prioritizing user config
# Merge configurations, prioritizing user-provided values
if ! yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' "$BUILT_IN_CONFIG" "$USER_CONFIG" > "$MERGED_CONFIG"; then
log_error "Failed to merge configurations. yq returned an error."
return 1
fi
else
log_info "No user-provided configuration detected. Using the built-in configuration."
log_info "No user configuration provided. Using built-in configuration only."
cp "$BUILT_IN_CONFIG" "$MERGED_CONFIG"
fi

log_info "Configuration merged successfully: $MERGED_CONFIG"
log_info "Merged configuration created successfully at $MERGED_CONFIG"
}

# Function to load the merged configuration and convert it to JSON
# Load and parse the merged configuration
load_and_parse_config() {
merge_worker_configs || return 1

# Convert the merged YAML configuration to JSON using yq
# Convert merged YAML to JSON
local json_output
if ! json_output=$(yq eval -o=json "$MERGED_CONFIG" 2>/dev/null); then
log_error "Failed to parse merged YAML from $MERGED_CONFIG. yq returned an error."
Expand All @@ -64,58 +56,3 @@ load_and_parse_config() {

echo "$json_output"
}

# Function to extract a specific section from the JSON configuration
get_config_section() {
local config_json="$1"
local section="$2"

if [[ -z "$config_json" ]]; then
log_error "Empty configuration JSON provided."
return 1
fi

# Attempt to extract the section and handle missing/null cases
local extracted_section
extracted_section=$(echo "$config_json" | jq -r ".config.${section} // empty" 2>/dev/null)

if [[ $? -ne 0 ]]; then
log_error "Failed to parse section '${section}' from configuration."
return 1
fi

if [[ -z "$extracted_section" || "$extracted_section" == "null" ]]; then
log_info "Section '${section}' is not defined in the configuration."
return 0
fi

echo "$extracted_section"
}

# Debugging helper: Validate JSON structure
validate_json() {
local json="$1"
if ! echo "$json" | jq empty 2>/dev/null; then
log_error "Invalid JSON structure detected."
return 1
fi
}

# Example usage
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
log_info "Loading and resolving worker configuration..."
config_json=$(load_and_parse_config) || exit 1
validate_json "$config_json" || exit 1
log_info "Worker configuration loaded successfully."

# Extract and process sections
actors=$(get_config_section "$config_json" "actors")
if [[ $? -eq 0 && -n "$actors" ]]; then
log_info "Actors loaded: $actors"
fi

secrets=$(get_config_section "$config_json" "secrets")
if [[ $? -eq 0 && -n "$secrets" ]]; then
log_info "Secrets loaded: $secrets"
fi
fi

0 comments on commit 4fd12f3

Please sign in to comment.