chore(bootstrap): Added base script for adding new apps to clusters
This commit is contained in:
44
init-app.sh
44
init-app.sh
@@ -1,8 +1,9 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# init-app.sh
|
# init-app.sh
|
||||||
# Creates clusters/<cluster>/apps/<application>, renders templates (APPLICATION_NAME, DESCRIPTION, CLUSTER_NAME),
|
# Creates clusters/<cluster>/apps/<application>, renders templates (APPLICATION_NAME, DESCRIPTION, CLUSTER_NAME),
|
||||||
# appends the app at the end of the parent clusters/<cluster>/apps/kustomization.yaml list (removing trailing blank line if present),
|
# appends the app to the end of the parent clusters/<cluster>/apps/kustomization.yaml list (removing trailing blank line if present),
|
||||||
# and stages changes in Git.
|
# creates a placeholder under manifests/<cluster>/<application>,
|
||||||
|
# and stages changes in Git (including the placeholder file).
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
@@ -58,9 +59,12 @@ if ! echo "$DESCRIPTION" | grep -Eq '^[A-Za-z0-9 .,]+$'; then
|
|||||||
echo "Error: Description may only contain spaces, letters (A–Z, a–z), numbers (0–9), dots (.), and commas (,)." >&2; exit 1
|
echo "Error: Description may only contain spaces, letters (A–Z, a–z), numbers (0–9), dots (.), and commas (,)." >&2; exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Paths
|
||||||
TARGET_DIR="${SCRIPT_DIR}/clusters/${CLUSTER_NAME}/apps/${APPLICATION_NAME}"
|
TARGET_DIR="${SCRIPT_DIR}/clusters/${CLUSTER_NAME}/apps/${APPLICATION_NAME}"
|
||||||
|
MANIFESTS_DIR="${SCRIPT_DIR}/manifests/${CLUSTER_NAME}/${APPLICATION_NAME}"
|
||||||
|
PARENT_KUSTOMIZATION="${SCRIPT_DIR}/clusters/${CLUSTER_NAME}/apps/kustomization.yaml"
|
||||||
|
|
||||||
# Abort if target directory already exists
|
# Abort if clusters target already exists
|
||||||
if [[ -d "$TARGET_DIR" ]]; then
|
if [[ -d "$TARGET_DIR" ]]; then
|
||||||
echo "Error: Directory already exists: $TARGET_DIR" >&2; exit 1
|
echo "Error: Directory already exists: $TARGET_DIR" >&2; exit 1
|
||||||
fi
|
fi
|
||||||
@@ -77,8 +81,9 @@ for f in "$APP_PROJECT_SRC" "$APPLICATION_SRC" "$KUSTOMIZATION_SRC"; do
|
|||||||
done
|
done
|
||||||
if [[ "$missing" == true ]]; then exit 1; fi
|
if [[ "$missing" == true ]]; then exit 1; fi
|
||||||
|
|
||||||
# Create target directory
|
# Create target directories
|
||||||
mkdir -p "$TARGET_DIR"
|
mkdir -p "$TARGET_DIR"
|
||||||
|
mkdir -p "$MANIFESTS_DIR"
|
||||||
|
|
||||||
# Prepare safe replacements
|
# Prepare safe replacements
|
||||||
safe_app_name="${APPLICATION_NAME//\\/\\\\}"; safe_app_name="${safe_app_name//&/\\&}"
|
safe_app_name="${APPLICATION_NAME//\\/\\\\}"; safe_app_name="${safe_app_name//&/\\&}"
|
||||||
@@ -109,39 +114,46 @@ mv "$tmp2" "$APPLICATION_DEST"
|
|||||||
KUSTOMIZATION_DEST="${TARGET_DIR}/kustomization.yaml"
|
KUSTOMIZATION_DEST="${TARGET_DIR}/kustomization.yaml"
|
||||||
cp "$KUSTOMIZATION_SRC" "$KUSTOMIZATION_DEST"
|
cp "$KUSTOMIZATION_SRC" "$KUSTOMIZATION_DEST"
|
||||||
|
|
||||||
|
# Create placeholder in manifests/<cluster>/<application>
|
||||||
|
PLACEHOLDER_FILE="${MANIFESTS_DIR}/.placeholder"
|
||||||
|
if [[ ! -f "$PLACEHOLDER_FILE" ]]; then
|
||||||
|
printf "Placeholder for %s/%s\n" "$CLUSTER_NAME" "$APPLICATION_NAME" > "$PLACEHOLDER_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Created: $TARGET_DIR"
|
echo "Created: $TARGET_DIR"
|
||||||
echo "Wrote: $APP_PROJECT_DEST (APPLICATION_NAME='${APPLICATION_NAME}', DESCRIPTION set)"
|
echo "Wrote: $APP_PROJECT_DEST (APPLICATION_NAME='${APPLICATION_NAME}', DESCRIPTION set)"
|
||||||
echo "Wrote: $APPLICATION_DEST (APPLICATION_NAME='${APPLICATION_NAME}', CLUSTER_NAME='${CLUSTER_NAME}')"
|
echo "Wrote: $APPLICATION_DEST (APPLICATION_NAME='${APPLICATION_NAME}', CLUSTER_NAME='${CLUSTER_NAME}')"
|
||||||
echo "Wrote: $KUSTOMIZATION_DEST (copied as-is)"
|
echo "Wrote: $KUSTOMIZATION_DEST (copied as-is)"
|
||||||
|
echo "Created: $PLACEHOLDER_FILE"
|
||||||
|
|
||||||
# Parent kustomization.yaml: remove trailing blank line if present, then append new item
|
# Parent kustomization.yaml: remove trailing blank line if present, then append new item
|
||||||
PARENT_KUSTOMIZATION="${SCRIPT_DIR}/clusters/${CLUSTER_NAME}/apps/kustomization.yaml"
|
|
||||||
if [[ ! -f "$PARENT_KUSTOMIZATION" ]]; then
|
if [[ ! -f "$PARENT_KUSTOMIZATION" ]]; then
|
||||||
echo "Error: Parent kustomization not found: ${PARENT_KUSTOMIZATION}" >&2; exit 1
|
echo "Error: Parent kustomization not found: ${PARENT_KUSTOMIZATION}" >&2; exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 1) If the last line is empty, delete it (BSD sed)
|
# Remove trailing empty last line (BSD sed)
|
||||||
# This prints all lines except a final empty line at EOF.
|
|
||||||
# Explanation:
|
|
||||||
# - $: matches the last line
|
|
||||||
# - /^$/: last line is empty
|
|
||||||
# - d: delete that line
|
|
||||||
sed -i '' -e '${/^$/d;}' "$PARENT_KUSTOMIZATION"
|
sed -i '' -e '${/^$/d;}' "$PARENT_KUSTOMIZATION"
|
||||||
|
|
||||||
# 2) Append the new application line at the end (with two-space indent to match your file)
|
# Append the new application entry at end (two-space indent)
|
||||||
printf " - %s\n" "$APPLICATION_NAME" >> "$PARENT_KUSTOMIZATION"
|
printf " - %s\n" "$APPLICATION_NAME" >> "$PARENT_KUSTOMIZATION"
|
||||||
|
|
||||||
echo "Updated parent kustomization.yaml: appended '${APPLICATION_NAME}' at end of resources list."
|
echo "Updated parent kustomization.yaml: appended '${APPLICATION_NAME}' at end of resources list."
|
||||||
|
|
||||||
# Stage changes in Git
|
# Stage changes in Git (including placeholder file)
|
||||||
if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
if git -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||||
REPO_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)"
|
REPO_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)"
|
||||||
REL_TARGET="${TARGET_DIR#$REPO_ROOT/}"
|
REL_TARGET="${TARGET_DIR#$REPO_ROOT/}"
|
||||||
REL_PARENT="${PARENT_KUSTOMIZATION#$REPO_ROOT/}"
|
REL_PARENT="${PARENT_KUSTOMIZATION#$REPO_ROOT/}"
|
||||||
git -C "$REPO_ROOT" add "$REL_TARGET" "$REL_PARENT"
|
REL_MANIFESTS_DIR="${MANIFESTS_DIR#$REPO_ROOT/}"
|
||||||
echo "Git: staged new directory/files and parent kustomization: $REL_TARGET, $REL_PARENT"
|
REL_PLACEHOLDER="${PLACEHOLDER_FILE#$REPO_ROOT/}"
|
||||||
|
git -C "$REPO_ROOT" add "$REL_TARGET" "$REL_PARENT" "$REL_MANIFESTS_DIR" "$REL_PLACEHOLDER"
|
||||||
|
echo "Git: staged new app dir, parent kustomization, manifests dir, and placeholder:"
|
||||||
|
echo " $REL_TARGET"
|
||||||
|
echo " $REL_PARENT"
|
||||||
|
echo " $REL_MANIFESTS_DIR"
|
||||||
|
echo " $REL_PLACEHOLDER"
|
||||||
echo "Next: commit with a message, e.g.:"
|
echo "Next: commit with a message, e.g.:"
|
||||||
echo " git -C \"$REPO_ROOT\" commit -m \"feat(${CLUSTER_NAME}): add ${APPLICATION_NAME} app (\"${DESCRIPTION}\") and update kustomization\""
|
echo " git -C \"$REPO_ROOT\" commit -m \"feat(${CLUSTER_NAME}): add ${APPLICATION_NAME} app (\"${DESCRIPTION}\") and manifests placeholder; update kustomization\""
|
||||||
else
|
else
|
||||||
echo "Note: Not inside a Git repository (no .git found). Skipping git add."
|
echo "Note: Not inside a Git repository (no .git found). Skipping git add."
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user