From fcdf511b7ba68282fa6a427a2df2b6855be00c6c Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Fri, 28 Jan 2022 08:08:43 -0600 Subject: [PATCH 1/3] Added Orphan Clean Up script Signed-off-by: Andre Wanlin --- contrib/scripts/README.md | 3 +++ .../scripts/orphan-clean-up/OrphanCleanUp.ps1 | 22 +++++++++++++++++++ contrib/scripts/orphan-clean-up/README.md | 19 ++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 contrib/scripts/README.md create mode 100644 contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 create mode 100644 contrib/scripts/orphan-clean-up/README.md diff --git a/contrib/scripts/README.md b/contrib/scripts/README.md new file mode 100644 index 0000000000..8ed7e09749 --- /dev/null +++ b/contrib/scripts/README.md @@ -0,0 +1,3 @@ +# Scripts + +Here you will find a variety of script contributions. diff --git a/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 b/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 new file mode 100644 index 0000000000..105dfe7bf8 --- /dev/null +++ b/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 @@ -0,0 +1,22 @@ +<# +.DESCRIPTION +Cleanes up orphaned entities for the provided Backstage URL, defaults to the local backend +#> +param( + [string]$backstageUrl = "http://localhost:7007" +) + +$orphanApiUrl = "$backstageUrl/api/catalog/entities?filter=metadata.annotations.backstage.io/orphan=true" +$orphanDeleteApiUrl = "$backstageUrl/api/catalog/entities/by-uid" + +$orphans = Invoke-RestMethod -Method Get -Uri $orphanApiUrl + +Write-Host "" +Write-Host "Found $($orphans.length) orphaned entities" +Write-Host "" + +foreach($orphan in $orphans){ + Write-Host "Deleting orphan $($orphan.metadata.name) of kind $($orphan.kind)" + + Invoke-RestMethod -Method Delete -Uri "$orphanDeleteApiUrl/$($orphan.metadata.uid)" +} \ No newline at end of file diff --git a/contrib/scripts/orphan-clean-up/README.md b/contrib/scripts/orphan-clean-up/README.md new file mode 100644 index 0000000000..444a020e1c --- /dev/null +++ b/contrib/scripts/orphan-clean-up/README.md @@ -0,0 +1,19 @@ +# Orphan Clean Up + +## Overview + +The Orphan Clean Up script is a basic PowerShell script to delete orphaned entities in the catalog. + +## 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 + +Here's how to use the script: + +1. Download the 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 From e7586f3c83de784f53aa1f8f0f8ac40c3cf6b0c5 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Fri, 28 Jan 2022 08:14:57 -0600 Subject: [PATCH 2/3] Added newline to end of script Signed-off-by: Andre Wanlin --- contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 b/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 index 105dfe7bf8..1eaaa2ab2c 100644 --- a/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 +++ b/contrib/scripts/orphan-clean-up/OrphanCleanUp.ps1 @@ -19,4 +19,4 @@ foreach($orphan in $orphans){ Write-Host "Deleting orphan $($orphan.metadata.name) of kind $($orphan.kind)" Invoke-RestMethod -Method Delete -Uri "$orphanDeleteApiUrl/$($orphan.metadata.uid)" -} \ No newline at end of file +} From 409725e9c2f7152d5ff0c3bb592f0cc4722e552c Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Mon, 31 Jan 2022 07:11:45 -0600 Subject: [PATCH 3/3] Updated readme Signed-off-by: Andre Wanlin --- contrib/scripts/orphan-clean-up/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contrib/scripts/orphan-clean-up/README.md b/contrib/scripts/orphan-clean-up/README.md index 444a020e1c..a6a25a4e7d 100644 --- a/contrib/scripts/orphan-clean-up/README.md +++ b/contrib/scripts/orphan-clean-up/README.md @@ -2,7 +2,9 @@ ## Overview -The Orphan Clean Up script is a basic PowerShell script to delete orphaned entities in the catalog. +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. + +_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