Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 121 additions & 9 deletions scripts/translate_homelab_to_compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ events {
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
include mime.types;
default_type application/octet-stream;

# Logging
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
Expand All @@ -315,27 +315,53 @@ http {
keepalive_timeout 65;
types_hash_max_size 2048;

# Health check endpoint
server {
listen 80 default_server;
server_name _;

location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
}

# Include service configurations
include /etc/nginx/conf.d/*.conf;
include conf.d/*.conf;
}
EOF

# Get base domain for service URLs
local base_domain
base_domain=$(get_base_domain "$HOMELAB_CONFIG")

# Generate configuration for each service that needs reverse proxy
while IFS= read -r service; do
[[ -z "$service" ]] && continue

local port
port=$(yq ".services[\"$service\"].port" "$HOMELAB_CONFIG" 2>/dev/null)
# Check if this is a web service that needs nginx proxy
if is_web_service "$service" "$HOMELAB_CONFIG"; then
local port
port=$(yq ".services[\"$service\"].port" "$HOMELAB_CONFIG" 2>/dev/null | tr -d '"')

# Only create proxy config for services on ports 80/443 (web services)
if [[ "$port" == "80" || "$port" == "443" ]]; then
log_info " Creating nginx config for: $service"
# Skip if no port (can't proxy without knowing the port)
if [[ -z "$port" || "$port" == "null" ]]; then
log_info " Skipping nginx config for $service: no port specified"
continue
fi

# Get custom domain or use default pattern
local service_domain
service_domain=$(yq ".services[\"$service\"].domain // \"${service}.${base_domain}\"" "$HOMELAB_CONFIG" 2>/dev/null | tr -d '"')

log_info " Creating nginx config for: $service (${service_domain})"

cat > "$nginx_dir/conf.d/${service}.conf" <<EOF
# Service: $service
server {
listen 80;
server_name ${service}.local;
server_name ${service_domain};

location / {
proxy_pass http://$service:$port;
Expand Down Expand Up @@ -566,6 +592,11 @@ main() {
OUTPUT_DIR="$2"
shift 2
;;
generate-nginx-bundles)
# Special command to only generate nginx bundles
generate_nginx_bundles "$HOMELAB_CONFIG"
exit $?
;;
-h|--help)
usage
exit 0
Expand All @@ -582,6 +613,87 @@ main() {
translate_homelab_to_compose
}

# Function: is_web_service
# Description: Determines if a service should be exposed via nginx proxy
# Arguments: $1 - service name, $2 - config file path
# Returns: 0 if service should be exposed, 1 if not
is_web_service() {
local service="$1"
local config_file="${2:-$HOMELAB_CONFIG}"

# Check if service has a custom domain configured
local custom_domain
custom_domain=$(yq ".services[\"$service\"].domain" "$config_file" 2>/dev/null | tr -d '"')

if [[ -n "$custom_domain" && "$custom_domain" != "null" ]]; then
return 0 # Has explicit domain configuration
fi

# Check if service has a port (indicating it's a service that could be exposed)
# and doesn't explicitly disable web exposure
local port
port=$(yq ".services[\"$service\"].port" "$config_file" 2>/dev/null | tr -d '"')

local web_enabled
web_enabled=$(yq ".services[\"$service\"].web" "$config_file" 2>/dev/null | tr -d '"')

# Default web to true if not specified
if [[ -z "$web_enabled" || "$web_enabled" == "null" ]]; then
web_enabled="true"
fi

# Service is a web service if:
# 1. It has a port AND
# 2. web is not explicitly set to false
if [[ -n "$port" && "$port" != "null" && "$web_enabled" != "false" ]]; then
return 0 # Service should be exposed
fi

return 1 # Service should not be exposed via nginx
}

# Function: get_base_domain
# Description: Gets the base domain from environment or config
# Arguments: $1 - config file path
# Returns: Base domain string
get_base_domain() {
local config_file="${1:-$HOMELAB_CONFIG}"

# Try environment first, then config, then default
local base_domain="${BASE_DOMAIN:-}"

if [[ -z "$base_domain" ]]; then
base_domain=$(yq ".environment.BASE_DOMAIN // \"homelab.local\"" "$config_file" 2>/dev/null | tr -d '"')
fi

echo "$base_domain"
}

# Function: generate_nginx_bundles
# Description: Generates nginx bundles for all machines
# Arguments: $1 - config file path
# Returns: 0 on success, 1 on failure
generate_nginx_bundles() {
local config_file="${1:-$HOMELAB_CONFIG}"

log_info "Generating nginx bundles for all machines..."

# Get all machines
local machines
machines=$(get_machine_list "$config_file") || return 1

# Generate nginx config for each machine
while IFS= read -r machine; do
[[ -z "$machine" ]] && continue

log_info "Generating nginx bundle for: $machine"
generate_nginx_config_for_machine "$machine" "$config_file" || return 1
done <<< "$machines"

log_success "Generated nginx bundles for all machines"
return 0
}

# Only run main if script is executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
Expand Down
22 changes: 22 additions & 0 deletions selfhosted.sh
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,27 @@ case "$1" in
;;
esac
;;
generate-nginx)
# Generate nginx bundles for Docker Compose deployment
case "$2" in
bundles)
"$PROJECT_ROOT/scripts/translate_homelab_to_compose.sh" generate-nginx-bundles "${3:-homelab.yaml}"
;;
help|"")
echo "💡 Generate-Nginx Commands:"
echo " bundles [config] - Generate nginx bundles for all machines"
echo ""
echo "💡 Examples:"
echo " $0 generate-nginx bundles homelab.yaml # Generate nginx bundles"
echo " $0 generate-nginx bundles # Use default homelab.yaml"
;;
*)
echo "❌ Unknown generate-nginx command: $2"
echo "💡 Run '$0 generate-nginx help' for available commands"
exit 1
;;
esac
;;
config)
case "$2" in
init) config_init ;;
Expand Down Expand Up @@ -794,6 +815,7 @@ case "$1" in
echo " service - Manage service configurations"
echo " deploy - Deploy services to infrastructure"
echo " deploy-compose - Multi-machine Docker Compose deployment coordination"
echo " generate-nginx - Generate nginx bundles for Docker Compose"
echo " config - Manage environment and configuration"
echo " help - Show detailed help"
echo ""
Expand Down
Loading