Fix manifest templating in wild-cloud-add
This commit is contained in:
@@ -116,56 +116,54 @@ echo "Adding app '${APP_NAME}' from cache to ${DEST_APP_DIR}"
|
|||||||
# Step 1: Copy only manifest.yaml from cache first
|
# Step 1: Copy only manifest.yaml from cache first
|
||||||
MANIFEST_FILE="${CACHE_APP_DIR}/manifest.yaml"
|
MANIFEST_FILE="${CACHE_APP_DIR}/manifest.yaml"
|
||||||
if [ -f "${MANIFEST_FILE}" ]; then
|
if [ -f "${MANIFEST_FILE}" ]; then
|
||||||
|
# manifest.yaml is allowed to have gomplate variables in the defaultConfig and requiredSecrets sections.
|
||||||
|
# We need to use gomplate to process these variables before using yq.
|
||||||
echo "Copying manifest.yaml from cache"
|
echo "Copying manifest.yaml from cache"
|
||||||
cp "${MANIFEST_FILE}" "${DEST_APP_DIR}/manifest.yaml"
|
DEST_MANIFEST="${DEST_APP_DIR}/manifest.yaml"
|
||||||
|
if [ -f "${SECRETS_FILE}" ]; then
|
||||||
|
gomplate_cmd="gomplate -c .=${CONFIG_FILE} -c secrets=${SECRETS_FILE} -f ${MANIFEST_FILE} -o ${DEST_MANIFEST}"
|
||||||
|
else
|
||||||
|
gomplate_cmd="gomplate -c .=${CONFIG_FILE} -f ${MANIFEST_FILE} -o ${DEST_MANIFEST}"
|
||||||
|
fi
|
||||||
|
if ! eval "${gomplate_cmd}"; then
|
||||||
|
echo "Error processing manifest.yaml with gomplate"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
echo "Warning: manifest.yaml not found in cache for app '${APP_NAME}'"
|
echo "Warning: manifest.yaml not found in cache for app '${APP_NAME}'"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Step 2: Add missing config and secret values based on manifest
|
|
||||||
echo "Processing configuration and secrets from manifest.yaml"
|
|
||||||
|
|
||||||
# Check if the app section exists in config.yaml, if not create it
|
# Check if the app section exists in config.yaml, if not create it
|
||||||
if ! yq eval ".apps.${APP_NAME}" "${CONFIG_FILE}" >/dev/null 2>&1; then
|
if ! yq eval ".apps.${APP_NAME}" "${CONFIG_FILE}" >/dev/null 2>&1; then
|
||||||
yq eval ".apps.${APP_NAME} = {}" -i "${CONFIG_FILE}"
|
yq eval ".apps.${APP_NAME} = {}" -i "${CONFIG_FILE}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Extract defaultConfig from manifest.yaml and merge into config.yaml
|
# Check if apps section exists in the secrets.yaml, if not create it
|
||||||
if yq eval '.defaultConfig' "${DEST_APP_DIR}/manifest.yaml" | grep -q -v '^null$'; then
|
if ! yq eval ".apps" "${SECRETS_FILE}" >/dev/null 2>&1; then
|
||||||
|
yq eval ".apps = {}" -i "${SECRETS_FILE}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Merge processed defaultConfig into the app config, preserving existing values
|
||||||
|
# This preserves existing configuration values while adding missing defaults
|
||||||
|
if yq eval '.defaultConfig' "${DEST_MANIFEST}" | grep -q -v '^null$'; then
|
||||||
echo "Merging defaultConfig from manifest.yaml into .wildcloud/config.yaml"
|
echo "Merging defaultConfig from manifest.yaml into .wildcloud/config.yaml"
|
||||||
|
|
||||||
# Merge defaultConfig into the app config, preserving existing values
|
# Extract defaultConfig from the processed manifest and merge with existing app config
|
||||||
# This preserves existing configuration values while adding missing defaults
|
# The * operator merges objects, with the right side taking precedence for conflicting keys
|
||||||
temp_manifest=$(mktemp)
|
# So (.apps.${APP_NAME} // {}) preserves existing values, defaultConfig adds missing ones
|
||||||
yq eval '.defaultConfig' "${DEST_APP_DIR}/manifest.yaml" > "$temp_manifest"
|
temp_default_config=$(mktemp)
|
||||||
yq eval ".apps.${APP_NAME} = load(\"$temp_manifest\") * (.apps.${APP_NAME} // {})" -i "${CONFIG_FILE}"
|
yq eval '.defaultConfig' "${DEST_MANIFEST}" > "$temp_default_config"
|
||||||
rm "$temp_manifest"
|
yq eval ".apps.${APP_NAME} = load(\"$temp_default_config\") * (.apps.${APP_NAME} // {})" -i "${CONFIG_FILE}"
|
||||||
|
rm "$temp_default_config"
|
||||||
# Process template variables in the merged config
|
|
||||||
echo "Processing template variables in app config"
|
|
||||||
temp_config=$(mktemp)
|
|
||||||
|
|
||||||
# Build gomplate command with config context
|
|
||||||
gomplate_cmd="gomplate -c .=${CONFIG_FILE}"
|
|
||||||
|
|
||||||
# Add secrets context if secrets.yaml exists
|
|
||||||
if [ -f "${SECRETS_FILE}" ]; then
|
|
||||||
gomplate_cmd="${gomplate_cmd} -c secrets=${SECRETS_FILE}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Process the entire config file through gomplate to resolve template variables
|
|
||||||
${gomplate_cmd} -f "${CONFIG_FILE}" > "$temp_config"
|
|
||||||
mv "$temp_config" "${CONFIG_FILE}"
|
|
||||||
|
|
||||||
echo "Merged defaultConfig for app '${APP_NAME}'"
|
echo "Merged defaultConfig for app '${APP_NAME}'"
|
||||||
|
# Remove defaultConfig from the copied manifest since it's now in config.yaml.
|
||||||
# Remove defaultConfig from the copied manifest since it's now in config.yaml
|
# yq eval 'del(.defaultConfig)' -i "${DEST_MANIFEST}"
|
||||||
yq eval 'del(.defaultConfig)' -i "${DEST_APP_DIR}/manifest.yaml"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Scaffold required secrets into .wildcloud/secrets.yaml if they don't exist
|
# Scaffold required secrets into .wildcloud/secrets.yaml if they don't exist
|
||||||
if yq eval '.requiredSecrets' "${DEST_APP_DIR}/manifest.yaml" | grep -q -v '^null$'; then
|
if yq eval '.requiredSecrets' "${DEST_MANIFEST}" | grep -q -v '^null$'; then
|
||||||
echo "Scaffolding required secrets for app '${APP_NAME}'"
|
echo "Scaffolding required secrets for app '${APP_NAME}'"
|
||||||
|
|
||||||
# Ensure .wildcloud/secrets.yaml exists
|
# Ensure .wildcloud/secrets.yaml exists
|
||||||
@@ -176,16 +174,6 @@ if yq eval '.requiredSecrets' "${DEST_APP_DIR}/manifest.yaml" | grep -q -v '^nul
|
|||||||
echo "" >> "${SECRETS_FILE}"
|
echo "" >> "${SECRETS_FILE}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if apps section exists, if not create it
|
|
||||||
if ! yq eval ".apps" "${SECRETS_FILE}" >/dev/null 2>&1; then
|
|
||||||
yq eval ".apps = {}" -i "${SECRETS_FILE}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if app section exists, if not create it
|
|
||||||
if ! yq eval ".apps.${APP_NAME}" "${SECRETS_FILE}" >/dev/null 2>&1; then
|
|
||||||
yq eval ".apps.${APP_NAME} = {}" -i "${SECRETS_FILE}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Add random values for each required secret if not already present
|
# Add random values for each required secret if not already present
|
||||||
while read -r secret_path; do
|
while read -r secret_path; do
|
||||||
current_value=$(yq eval ".${secret_path} // \"null\"" "${SECRETS_FILE}")
|
current_value=$(yq eval ".${secret_path} // \"null\"" "${SECRETS_FILE}")
|
||||||
@@ -195,7 +183,7 @@ if yq eval '.requiredSecrets' "${DEST_APP_DIR}/manifest.yaml" | grep -q -v '^nul
|
|||||||
random_secret=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32)
|
random_secret=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32)
|
||||||
yq eval ".${secret_path} = \"${random_secret}\"" -i "${SECRETS_FILE}"
|
yq eval ".${secret_path} = \"${random_secret}\"" -i "${SECRETS_FILE}"
|
||||||
fi
|
fi
|
||||||
done < <(yq eval '.requiredSecrets[]' "${DEST_APP_DIR}/manifest.yaml")
|
done < <(yq eval '.requiredSecrets[]' "${DEST_MANIFEST}")
|
||||||
|
|
||||||
echo "Required secrets scaffolded for app '${APP_NAME}'"
|
echo "Required secrets scaffolded for app '${APP_NAME}'"
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user