diff --git a/bin/wild-central-generate-setup b/bin/wild-central-generate-setup index 4a1e77a..577970e 100755 --- a/bin/wild-central-generate-setup +++ b/bin/wild-central-generate-setup @@ -1,87 +1,36 @@ #!/bin/bash -# Setup. - set -e set -o pipefail -# Initialize wildcloud environment. - if [ ! -d ".wildcloud" ]; then echo "Error: You must run this script from a wild-cloud directory" exit 1 fi -WILDCLOUD_CONFIG_FILE="./config.yaml" -if [ ! -f ${WILDCLOUD_CONFIG_FILE} ]; then - echo "Error: ${WILDCLOUD_CONFIG_FILE} not found" +WILDCLOUD_ROOT=$(wild-config wildcloud.root) || exit 1 +TEMPLATE_DIR="${WILDCLOUD_ROOT}/central-setup/dnsmasq" +SETUP_DIR="cluster/dnsmasq" + +if [ ! -d "${TEMPLATE_DIR}" ]; then + echo "Error: Template directory not found at ${TEMPLATE_DIR}" exit 1 fi -WILDCLOUD_ROOT=$(yq eval '.wildcloud.root' ${WILDCLOUD_CONFIG_FILE}) -if [ -z "${WILDCLOUD_ROOT}" ] || [ "${WILDCLOUD_ROOT}" = "null" ]; then - echo "Error: wildcloud.root not found in ${WILDCLOUD_CONFIG_FILE}" - exit 1 -fi - -# --- - -# Function to process a file with gomplate. -process_file() { - local src_file="$1" - local dest_file="$2" - - if [[ "${src_file}" == *.yaml ]] || [[ "${src_file}" == *.ipxe ]] || [[ "${src_file}" == *.conf ]]; then - echo "Processing YAML file: ${dest_file}" - gomplate -d config=./config.yaml -f "${src_file}" > "${dest_file}" - else - cp "${src_file}" "${dest_file}" - fi -} - -# The source templates for dnsmasq setup. -DNSMASQ_TEMPLATE_DIR="${WILDCLOUD_ROOT}/central-setup/dnsmasq" -if [ ! -d "${DNSMASQ_TEMPLATE_DIR}" ]; then - echo "Error: DNSMasq setup directory not found at ${DNSMASQ_TEMPLATE_DIR}" - exit 1 -fi - -# Where to put the processed DNSMasq files. -DNSMASQ_SETUP_DIR="cluster/dnsmasq" - -# Optionally remove the setup directory if it already exists. -if [ -d "${DNSMASQ_SETUP_DIR}" ]; then - echo "Warning: Destination directory ${DNSMASQ_SETUP_DIR} already exists" - read -p "Do you want to overwrite it? (y/N): " -n 1 -r +if [ -d "${SETUP_DIR}" ]; then + echo "Warning: ${SETUP_DIR} already exists" + read -p "Overwrite? (y/N): " -n 1 -r echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - echo "Installation cancelled" - exit 1 - fi - rm -rf "${DNSMASQ_SETUP_DIR}" + [[ ! $REPLY =~ ^[Yy]$ ]] && exit 1 + rm -rf "${SETUP_DIR}" fi -mkdir -p $DNSMASQ_SETUP_DIR +# Copy and process templates +cp -r "${TEMPLATE_DIR}" "${SETUP_DIR}" -# Compile templates to setup directory. -find "${DNSMASQ_TEMPLATE_DIR}" -type d | while read -r src_dir; do - rel_path="${src_dir#${DNSMASQ_TEMPLATE_DIR}}" - rel_path="${rel_path#/}" # Remove leading slash if present - if [ -n "${rel_path}" ]; then - mkdir -p "${DNSMASQ_SETUP_DIR}/${rel_path}" - fi -done - -find "${DNSMASQ_TEMPLATE_DIR}" -type f | while read -r src_file; do - rel_path="${src_file#${DNSMASQ_TEMPLATE_DIR}}" - rel_path="${rel_path#/}" # Remove leading slash if present - dest_file="${DNSMASQ_SETUP_DIR}/${rel_path}" - - # Ensure destination directory exists - dest_dir=$(dirname "${dest_file}") - mkdir -p "${dest_dir}" - - process_file "${src_file}" "${dest_file}" +find "${SETUP_DIR}" -type f \( -name "*.yaml" -o -name "*.ipxe" -o -name "*.conf" \) | while read -r file; do + echo "Processing: ${file}" + wild-compile-template < "${file}" > "${file}.tmp" && mv "${file}.tmp" "${file}" done echo "Successfully created dnsmasq setup files from templates." diff --git a/bin/wild-compile-template b/bin/wild-compile-template new file mode 100755 index 0000000..877737d --- /dev/null +++ b/bin/wild-compile-template @@ -0,0 +1,54 @@ +#!/bin/bash + +set -e +set -o pipefail + +# Usage function +usage() { + echo "Usage: wild-compile-template [options]" + echo "" + echo "Compile a gomplate template from stdin using ./config.yaml as context." + echo "" + echo "Examples:" + echo " echo 'Hello {{.config.cluster.name}}' | wild-compile-template" + echo " cat template.yml | wild-compile-template" + echo "" + echo "Options:" + echo " -h, --help Show this help message" +} + +# Parse arguments +while [[ $# -gt 0 ]]; do + case $1 in + -h|--help) + usage + exit 0 + ;; + -*) + echo "Unknown option $1" + usage + exit 1 + ;; + *) + echo "Too many arguments" + usage + exit 1 + ;; + esac +done + +if [ ! -f "./config.yaml" ]; then + echo "Error: ./config.yaml not found in current directory" >&2 + exit 1 +fi + +# Build gomplate command with config context (enables .config shorthand) +gomplate_cmd="gomplate -c config=./config.yaml" + +# Add secrets context if secrets.yaml exists (enables .secrets shorthand) +if [ -f "./secrets.yaml" ]; then + gomplate_cmd="${gomplate_cmd} -c secrets=./secrets.yaml" +fi + +# Execute gomplate with stdin +${gomplate_cmd} \ No newline at end of file