Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Nov 20, 2024
1 parent ecc20e1 commit ef63992
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 47 deletions.
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN apt-get update && \
apt-get install -y --no-install-recommends \
tzdata=2024a-3ubuntu1.1 \
curl=8.5.0-2ubuntu10.4 \
curl=8.5.0-2ubuntu10.5 \
bash=5.2.21-2ubuntu4 \
apt-utils=2.7.14build2 \
gettext=0.21-14ubuntu2 \
Expand Down Expand Up @@ -94,16 +94,18 @@ RUN mkdir -p /home/${USER}/.gnupg && \
mkdir -p /home/${USER}/etc /home/${USER}/.cd/configs && \
chown -R ${USER}:${USER} /home/${USER}

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

# Copy the bin, etc, and lib directories
COPY ./etc/home /home/${USER}/etc
COPY ./src/configs /home/${USER}/.cd/configs
COPY ./lib /usr/local/lib
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
# 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 /home/${USER}/etc /home/${USER}/.cd/configs
chown -R ${USER}:${USER} /usr/local/lib /usr/src/app/src/configs /home/${USER}/etc /home/${USER}/.cd/configs

# Switch to non-root user
USER ${USER}
Expand Down
35 changes: 16 additions & 19 deletions lib/environment.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash

# Include necessary modules
# shellcheck source=/dev/null
source /usr/local/lib/utils.sh
source /usr/local/lib/auth.sh
source /usr/local/lib/secrets.sh
Expand All @@ -10,48 +9,46 @@ source /usr/local/lib/worker_config.sh

# Main function to coordinate environment setup
configure_environment() {
log_info "Starting environment configuration..."

# Load and resolve the worker configuration
local resolved_config
resolved_config=$(load_and_resolve_worker_config)
if [ -z "$resolved_config" ]; then
log_error "Failed to resolve worker configuration."
resolved_config=$(load_and_parse_config)
if [[ -z "$resolved_config" ]]; then
log_error "Configuration loading failed. Exiting..."
return 1
fi

# Verify the config file exists at the expected path
local config_path
config_path=$(get_worker_config_path)
if [[ ! -f "$config_path" ]]; then
log_error "Configuration file not found at: $config_path"
return 1
fi
log_info "Config file found: $config_path"
log_info "Worker configuration loaded successfully."

# Extract actors section and authenticate
# Extract and authenticate actors
local actors
actors=$(get_worker_section "$resolved_config" "config.actors")
actors=$(get_config_section "$resolved_config" "actors")
if [[ $? -eq 0 && -n "$actors" ]]; then
log_info "Authenticating actors from configuration..."
if ! authenticate_actors "$actors"; then
log_error "Failed to authenticate actors."
return 1
fi
else
log_info "No actors defined or required for authentication."
log_info "No actors defined in the configuration."
fi

# Extract secrets section and fetch secrets if available
# Extract and fetch secrets
local secrets
secrets=$(get_worker_section "$resolved_config" "config.secrets")
secrets=$(get_config_section "$resolved_config" "secrets")
if [[ $? -eq 0 && -n "$secrets" ]]; then
log_info "Fetching secrets from configuration..."
if ! fetch_secrets "$secrets"; then
log_error "Failed to fetch secrets."
return 1
fi
else
log_info "No secrets found or required in the configuration."
log_info "No secrets defined in the configuration."
fi

# Clean up actors and sensitive environment variables
# Perform cleanup
log_info "Cleaning up sensitive data..."
if ! cleanup_actors; then
log_error "Failed to clean up actors."
return 1
Expand Down
94 changes: 70 additions & 24 deletions lib/worker_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,64 @@ log_error() {
echo "[ERROR] $1"
}

# Function to get the path to the worker.yml configuration file
get_worker_config_path() {
local config_path="/home/${USER}/.cd/configs/worker.yml"

# 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"

# Function to ensure configuration file exists
ensure_config_exists() {
local config_path="$1"
if [[ ! -f "$config_path" ]]; then
log_error "Configuration file not found: $config_path"
return 1
fi

echo "$config_path"
}

# Function to load the worker configuration from YAML and convert it to JSON
load_and_resolve_worker_config() {
local config_path
config_path=$(get_worker_config_path)
# Function to merge the built-in and user-provided configuration files
merge_worker_configs() {
log_info "Merging worker configurations..."

# Check if the config_path retrieval was successful
if [[ -z "$config_path" ]]; then
return 1
# Ensure the built-in config exists
ensure_config_exists "$BUILT_IN_CONFIG" || return 1

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

# Merge user config with built-in config, prioritizing user config
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."
cp "$BUILT_IN_CONFIG" "$MERGED_CONFIG"
fi

# Convert the YAML configuration to JSON using yq
log_info "Configuration merged successfully: $MERGED_CONFIG"
}

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

# Convert the merged YAML configuration to JSON using yq
local json_output
if ! json_output=$(yq eval -o=json "$config_path" 2>/dev/null); then
log_error "Failed to parse YAML from $config_path. yq returned an error."
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."
return 1
fi

if [[ -z "$json_output" ]]; then
log_error "YAML parsed to an empty JSON output."
log_error "Merged YAML parsed to an empty JSON output."
return 1
fi

echo "$json_output"
}

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

Expand All @@ -58,18 +77,45 @@ get_worker_section() {

# Attempt to extract the section and handle missing/null cases
local extracted_section
extracted_section=$(echo "$config_json" | jq -r ".${section} // empty")
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

# Return success if the section is empty or null
if [[ -z "$extracted_section" || "$extracted_section" == "null" ]]; then
return 0 # No error, section simply doesn't exist
log_info "Section '${section}' is not defined in the configuration."
return 0
fi

echo "$extracted_section"
}

# Example usage of the above functions
# You can comment this out if it’s just a library
# 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
configure_environment
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 ef63992

Please sign in to comment.