Skip to content

Instantly share code, notes, and snippets.

@markjacksonfishing
Created July 4, 2024 17:31
Show Gist options
  • Save markjacksonfishing/a9ffd37693aef85cd2c4378b05ac70a8 to your computer and use it in GitHub Desktop.
Save markjacksonfishing/a9ffd37693aef85cd2c4378b05ac70a8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Log file
LOGFILE="backstage_debug.log"
# Function to log messages
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOGFILE"
}
# Function to check if a command exists
command_exists() {
command -v "$1" &>/dev/null
}
# Ensure necessary tools are installed
log "Checking for necessary tools..."
if ! command_exists yarn; then
log "yarn is not installed. Please install yarn before running this script."
exit 1
fi
if ! command_exists node; then
log "Node.js is not installed. Please install Node.js before running this script."
exit 1
fi
if ! command_exists kubectl; then
log "kubectl is not installed. Please install kubectl before running this script."
exit 1
fi
# Ask for necessary information
read -p "Enter the path to your Backstage app (e.g., /path/to/backstageapp): " BACKSTAGE_PATH
read -p "Enter the Kubernetes plugin name (default: @backstage/plugin-kubernetes-backend): " PLUGIN_NAME
PLUGIN_NAME=${PLUGIN_NAME:-@backstage/plugin-kubernetes-backend}
read -p "Enter the Kubernetes cluster context (e.g., my-cluster): " KUBE_CONTEXT
# Log user inputs
log "Backstage app path: $BACKSTAGE_PATH"
log "Kubernetes plugin name: $PLUGIN_NAME"
log "Kubernetes cluster context: $KUBE_CONTEXT"
# Change to the Backstage app directory
cd "$BACKSTAGE_PATH" || { log "Failed to change directory to $BACKSTAGE_PATH"; exit 1; }
# Check Node.js version
NODE_VERSION=$(node -v)
log "Node.js version: $NODE_VERSION"
# Check if plugin is installed
log "Checking if $PLUGIN_NAME is installed..."
if yarn list "$PLUGIN_NAME" &>/dev/null; then
log "$PLUGIN_NAME is installed."
else
log "$PLUGIN_NAME is not installed. Installing..."
yarn add "$PLUGIN_NAME"
fi
# Rebuild the backend
log "Rebuilding the backend..."
yarn install &>> "$LOGFILE"
yarn build &>> "$LOGFILE"
BUILD_STATUS=$?
if [ $BUILD_STATUS -ne 0 ]; then
log "Build failed. Check the log for details."
echo "Build failed. Please check the log file: $LOGFILE"
exit 1
fi
# Check import statements in index.ts
log "Checking import statements in index.ts..."
INDEX_FILE="$BACKSTAGE_PATH/packages/backend/src/index.ts"
if grep -q "import kubernetes from '$PLUGIN_NAME'" "$INDEX_FILE"; then
log "Import statement for $PLUGIN_NAME found in index.ts."
else
log "Import statement for $PLUGIN_NAME not found in index.ts. Adding..."
echo "import kubernetes from '$PLUGIN_NAME';" >> "$INDEX_FILE"
fi
# Check configuration in app-config.yaml
log "Checking configuration in app-config.yaml..."
CONFIG_FILE="$BACKSTAGE_PATH/app-config.yaml"
if grep -q "kubernetes:" "$CONFIG_FILE"; then
log "Kubernetes configuration found in app-config.yaml."
else
log "Kubernetes configuration not found in app-config.yaml. Adding example configuration..."
cat <<EOL >> "$CONFIG_FILE"
kubernetes:
serviceLocatorMethod:
type: 'multiTenant'
clusterLocatorMethods:
- type: 'config'
clusters:
- name: 'example-cluster'
url: 'https://example-cluster.com'
authProvider: 'serviceAccount'
serviceAccountToken: 'your-token'
skipTLSVerify: true
EOL
fi
# Check Kubernetes cluster for required labels
log "Checking Kubernetes cluster for required labels..."
kubectl config use-context "$KUBE_CONTEXT" &>> "$LOGFILE"
KUBE_RESOURCES=$(kubectl get pods,services -A -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.labels}{"\n"}{end}' | tee -a "$LOGFILE")
if echo "$KUBE_RESOURCES" | grep -q 'backstage.io/kubernetes-id'; then
log "Required labels found in Kubernetes resources."
else
log "Required labels not found in Kubernetes resources. Adding example labels..."
kubectl label pod,service --all backstage.io/kubernetes-id=example-id --overwrite
fi
# Check if resources are reachable
log "Checking if Kubernetes resources are reachable..."
if kubectl get pods,services -A &>/dev/null; then
log "Kubernetes resources are reachable."
else
log "Failed to reach Kubernetes resources. Check your cluster context and network settings."
fi
# Final log and summary
log "Debugging completed. Review the findings and suggestions below:"
echo "Debugging completed. Review the findings and suggestions below:" | tee -a "$LOGFILE"
# Summarize possible problems and suggestions
echo -e "\nPossible Problems and Suggestions:" | tee -a "$LOGFILE"
if ! yarn list "$PLUGIN_NAME" &>/dev/null; then
echo "- $PLUGIN_NAME was not initially installed. Ensure it is listed in your package.json." | tee -a "$LOGFILE"
fi
if ! grep -q "import kubernetes from '$PLUGIN_NAME'" "$INDEX_FILE"; then
echo "- Import statement for $PLUGIN_NAME was missing in index.ts. Ensure it is correctly imported." | tee -a "$LOGFILE"
fi
if ! grep -q "kubernetes:" "$CONFIG_FILE"; then
echo "- Kubernetes configuration was missing in app-config.yaml. Add the correct configuration for your clusters." | tee -a "$LOGFILE"
fi
if [ $BUILD_STATUS -ne 0 ]; then
echo "- The backend build failed. Check the log for errors and ensure all dependencies are correctly installed." | tee -a "$LOGFILE"
fi
if ! echo "$KUBE_RESOURCES" | grep -q 'backstage.io/kubernetes-id'; then
echo "- Required Kubernetes labels were missing. Ensure your resources are labeled with 'backstage.io/kubernetes-id'." | tee -a "$LOGFILE"
fi
if ! kubectl get pods,services -A &>/dev/null; then
echo "- Failed to reach Kubernetes resources. Check your cluster context and network settings." | tee -a "$LOGFILE"
fi
log "Script execution completed."
# Provide links to Backstage and Kubernetes documentation
echo -e "\nFor more details, refer to the following documentation:" | tee -a "$LOGFILE"
echo "Backstage Documentation: https://backstage.io/docs/overview/what-is-backstage/" | tee -a "$LOGFILE"
echo "Kubernetes Documentation: https://kubernetes.io/docs/home/" | tee -a "$LOGFILE"
# End of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment