From a0da6c275d3b906a3c073460901f1fe81cdabba9 Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Sun, 16 Oct 2022 13:15:54 +0200 Subject: [PATCH 1/3] port orphan cleanup script to bash Signed-off-by: Kiss Miklos --- contrib/scripts/orphan-clean-up/README.md | 35 ++++++++++++++++--- .../scripts/orphan-clean-up/orphan_cleanup.sh | 20 +++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100755 contrib/scripts/orphan-clean-up/orphan_cleanup.sh diff --git a/contrib/scripts/orphan-clean-up/README.md b/contrib/scripts/orphan-clean-up/README.md index a6a25a4e7d..9f5b2d4e48 100644 --- a/contrib/scripts/orphan-clean-up/README.md +++ b/contrib/scripts/orphan-clean-up/README.md @@ -2,20 +2,47 @@ ## Overview -The Orphan Clean Up script is a basic PowerShell script to delete orphaned entities in the catalog. This script also assumes that you do not have authentication setup for your Backstage API endpoints. +The Orphan Clean Up scripts are a basic scripts to delete orphaned entities in the catalog. + +This script also assumes that you do not have authentication setup for your Backstage API endpoints. _Warning:_ There is a risk of entities being orphaned (and being deleted by this script) in case of the location having problems and returning a 404 status code. This might lead to accidental deletion of entities until the processing loop has recreated the entity. -## Requirements +## PowerShell + +A PowerShell implementation of a the orphan cleanup script. + +### Requirements This script is PowerShell based so therefore needs to be ran in a PowerShell session. If you are not able to use PowerShell the script should give you a clear idea as to how to create it with another scripting language like Bash. -## Usage +### Usage Here's how to use the script: -1. Download the script +1. Download the `OrphanCleanUp.ps1` script 2. Now start a PowerShell session 3. Next navigate to the location you downloaded the script 4. Then run this command replacing `https:\\backstage.my-company.com` with the URL of your Backstage instance: `.\OrphanCleanUp.ps1 https:\\backstage.my-company.com` 5. The script will output the number of orphaned entities it finds and then the name for each one it deletes + +## Bash + +A bash implementation implementation of a the orphan cleanup script. + +### Requirements + +This script is shell based and requires the following programs to be installed on your system: + +- curl +- jq + +### Usage + +Here's how to use the script: + +1. Download the `orphan_cleanup.sh` script +2. Now start a bash session +3. Next navigate to the location you downloaded the script +4. Then run this command replacing `https://backstage.my-company.com` with the URL of your Backstage instance: `./orphan_cleanup.sh https://backstage.my-company.com` +5. The script will output the number of orphaned entities it finds and then the name for each one it deletes diff --git a/contrib/scripts/orphan-clean-up/orphan_cleanup.sh b/contrib/scripts/orphan-clean-up/orphan_cleanup.sh new file mode 100755 index 0000000000..a5938fd445 --- /dev/null +++ b/contrib/scripts/orphan-clean-up/orphan_cleanup.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Cleanes up orphaned entities for the provided Backstage URL, defaults to the local backend +BACKSTAGE_URL=${1:-'http://localhost:7007'} +echo $BACKSTAGE_URL + +ORPHAN_API_URL="$BACKSTAGE_URL/api/catalog/entities?filter=metadata.annotations.backstage.io/orphan=true" +ORPHAN_DELETE_API_URL="$BACKSTAGE_URL/api/catalog/entities/by-uid" + +ORPHANS=$(curl -s $ORPHAN_API_URL) + +echo "" +echo "Found $(echo $ORPHANS | jq length ) orphaned entities" +echo "" + +jq -c '.[]' <<< $ORPHANS | while read ORPHAN; do + echo $ORPHAN | jq "." + echo "Deleting orphan entity: $(echo $ORPHAN | jq -r .metadata.name) of kind: $(echo $ORPHAN | jq -r .kind)" + curl -X DELETE "$ORPHAN_DELETE_API_URL/$(echo $ORPHAN | jq -r .metadata.uid)" +done From ffedbef726b0c1dff3019c8cf6001d0ec5e71b27 Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Sun, 16 Oct 2022 13:20:14 +0200 Subject: [PATCH 2/3] fix spelling Signed-off-by: Kiss Miklos --- contrib/scripts/orphan-clean-up/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/scripts/orphan-clean-up/README.md b/contrib/scripts/orphan-clean-up/README.md index 9f5b2d4e48..6fdf51bbbf 100644 --- a/contrib/scripts/orphan-clean-up/README.md +++ b/contrib/scripts/orphan-clean-up/README.md @@ -10,7 +10,7 @@ _Warning:_ There is a risk of entities being orphaned (and being deleted by this ## PowerShell -A PowerShell implementation of a the orphan cleanup script. +A PowerShell implementation of the orphan cleanup script. ### Requirements @@ -28,7 +28,7 @@ Here's how to use the script: ## Bash -A bash implementation implementation of a the orphan cleanup script. +A bash implementation of the orphan cleanup script. ### Requirements From 392c96baec43626d2d1ab84e148fa16c7aac912a Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Sun, 16 Oct 2022 23:08:08 +0200 Subject: [PATCH 3/3] set pipefail Signed-off-by: Kiss Miklos --- contrib/scripts/orphan-clean-up/orphan_cleanup.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/scripts/orphan-clean-up/orphan_cleanup.sh b/contrib/scripts/orphan-clean-up/orphan_cleanup.sh index a5938fd445..6ffe4737fb 100755 --- a/contrib/scripts/orphan-clean-up/orphan_cleanup.sh +++ b/contrib/scripts/orphan-clean-up/orphan_cleanup.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -euo pipefail + # Cleanes up orphaned entities for the provided Backstage URL, defaults to the local backend BACKSTAGE_URL=${1:-'http://localhost:7007'} echo $BACKSTAGE_URL