cloud-cleaner: introduce script to wipe all CI artifacts
In case the job fails completely, resources might leak. This patch together with the one introducing predictable names for CI artifacts will make sure we wipe all resources after each CI run.
This commit is contained in:
parent
8209f71bd3
commit
6b137d0ac5
4 changed files with 91 additions and 0 deletions
65
cmd/cloud-cleaner/main.go
Normal file
65
cmd/cloud-cleaner/main.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// +build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Azure/go-autorest/autorest/azure/auth"
|
||||
"github.com/osbuild/osbuild-composer/internal/boot/azuretest"
|
||||
)
|
||||
|
||||
func panicErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func printErr(err error) {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateCIArtifactName generates a new identifier for CI artifacts which is based
|
||||
// on environment variables specified by Jenkins
|
||||
// note: in case of migration to sth else like Github Actions, change it to whatever variables GH Action provides
|
||||
func GenerateCIArtifactName(prefix string) (string, error) {
|
||||
distroCode := os.Getenv("DISTRO_CODE")
|
||||
changeId := os.Getenv("CHANGE_ID")
|
||||
buildId := os.Getenv("BUILD_ID")
|
||||
if changeId == "" || buildId == "" || distroCode == "" {
|
||||
return "", fmt.Errorf("The environment variables must specify CHANGE_ID, BUILD_ID, and DISTRO_CODE")
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s-%s-%s", prefix, distroCode, changeId, buildId), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Running a cloud cleanup")
|
||||
|
||||
// Load Azure credentials
|
||||
creds, err := azuretest.GetAzureCredentialsFromEnv()
|
||||
panicErr(err)
|
||||
if creds == nil {
|
||||
panic("empty credentials")
|
||||
}
|
||||
// Get test ID
|
||||
testID, err := GenerateCIArtifactName("")
|
||||
panicErr(err)
|
||||
// Delete the vhd image
|
||||
imageName := "image-" + testID + ".vhd"
|
||||
fmt.Println("Running delete image from Azure, this should fail if the test succedded")
|
||||
err = azuretest.DeleteImageFromAzure(creds, imageName)
|
||||
printErr(err)
|
||||
|
||||
// Delete all remaining resources (see the full list in the CleanUpBootedVM function)
|
||||
fmt.Println("Running clean up booted VM, this should fail if the test succedded")
|
||||
parameters := azuretest.NewDeploymentParameters(creds, imageName, testID, "")
|
||||
clientCredentialsConfig := auth.NewClientCredentialsConfig(creds.ClientID, creds.ClientSecret, creds.TenantID)
|
||||
authorizer, err := clientCredentialsConfig.Authorizer()
|
||||
panicErr(err)
|
||||
err = azuretest.CleanUpBootedVM(creds, parameters, authorizer, testID)
|
||||
printErr(err)
|
||||
}
|
||||
|
|
@ -125,6 +125,7 @@ go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-weldr-te
|
|||
go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-image-tests %{goipath}/cmd/osbuild-image-tests
|
||||
go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-composer-cloud-tests %{goipath}/cmd/osbuild-composer-cloud-tests
|
||||
go test -c -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/osbuild-auth-tests %{goipath}/cmd/osbuild-auth-tests
|
||||
go build -tags=integration -ldflags="${TEST_LDFLAGS}" -o _bin/cloud-cleaner %{goipath}/cmd/cloud-cleaner
|
||||
|
||||
%endif
|
||||
|
||||
|
|
@ -166,6 +167,7 @@ install -m 0755 -vp _bin/osbuild-image-tests %{buildroot}%{_l
|
|||
install -m 0755 -vp _bin/osbuild-composer-cloud-tests %{buildroot}%{_libexecdir}/tests/osbuild-composer/
|
||||
install -m 0755 -vp _bin/osbuild-auth-tests %{buildroot}%{_libexecdir}/tests/osbuild-composer/
|
||||
install -m 0755 -vp test/cmd/* %{buildroot}%{_libexecdir}/tests/osbuild-composer/
|
||||
install -m 0755 -vp _bin/cloud-cleaner %{buildroot}%{_libexecdir}/osbuild-composer/
|
||||
install -m 0755 -vp tools/image-info %{buildroot}%{_libexecdir}/osbuild-composer/
|
||||
install -m 0755 -vp tools/run-koji-container.sh %{buildroot}%{_libexecdir}/osbuild-composer/
|
||||
|
||||
|
|
@ -339,6 +341,7 @@ Integration tests to be run on a pristine-dedicated system to test the osbuild-c
|
|||
%files tests
|
||||
%{_libexecdir}/tests/osbuild-composer/
|
||||
%{_datadir}/tests/osbuild-composer/
|
||||
%{_libexecdir}/osbuild-composer/cloud-cleaner
|
||||
%{_libexecdir}/osbuild-composer/image-info
|
||||
%{_libexecdir}/osbuild-composer/run-koji-container.sh
|
||||
|
||||
|
|
|
|||
16
schutzbot/Jenkinsfile
vendored
16
schutzbot/Jenkinsfile
vendored
|
|
@ -147,6 +147,10 @@ pipeline {
|
|||
post {
|
||||
always {
|
||||
preserve_logs('fedora31-image')
|
||||
sh (
|
||||
label: "Run cloud cleaner just in case something failed",
|
||||
script: "schutzbot/run_cloud_cleaner.sh"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -199,6 +203,10 @@ pipeline {
|
|||
post {
|
||||
always {
|
||||
preserve_logs('fedora32-image')
|
||||
sh (
|
||||
label: "Run cloud cleaner just in case something failed",
|
||||
script: "schutzbot/run_cloud_cleaner.sh"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -266,6 +274,10 @@ pipeline {
|
|||
post {
|
||||
always {
|
||||
preserve_logs('rhel8-image')
|
||||
sh (
|
||||
label: "Run cloud cleaner just in case something failed",
|
||||
script: "schutzbot/run_cloud_cleaner.sh"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -323,6 +335,10 @@ pipeline {
|
|||
post {
|
||||
always {
|
||||
preserve_logs('rhel83-image')
|
||||
sh (
|
||||
label: "Run cloud cleaner just in case something failed",
|
||||
script: "schutzbot/run_cloud_cleaner.sh"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
7
schutzbot/run_cloud_cleaner.sh
Executable file
7
schutzbot/run_cloud_cleaner.sh
Executable file
|
|
@ -0,0 +1,7 @@
|
|||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
CLEANER_CMD="env $(cat "$AZURE_CREDS") CHANGE_ID=$CHANGE_ID BUILD_ID=$BUILD_ID DISTRO_CODE=$DISTRO_CODE /usr/libexec/osbuild-composer/cloud-cleaner"
|
||||
|
||||
echo "🧹 Running the cloud cleaner"
|
||||
$CLEANER_CMD
|
||||
Loading…
Add table
Add a link
Reference in a new issue