2023-02-26 05:33:53 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2023-02-26 09:06:41 +00:00
|
|
|
# --------------------------------------
|
|
|
|
# -- Gitea variables
|
|
|
|
# --------------------------------------
|
|
|
|
my $gitea_url=$ENV{'GITEA_URL'} || 'https://git.badhouseplants.net/api/v1';
|
|
|
|
my $gitea_org=$ENV{'GITEA_ORG'} || 'badhouseplants';
|
|
|
|
my $gitea_package=$ENV{'GITEA_PACKAGE'} || 'badhouseplants-net';
|
|
|
|
my $gitea_api="$gitea_url/packages/$gitea_org/container/$gitea_package";
|
2023-04-24 15:50:46 +00:00
|
|
|
my $gitea_list_api="$gitea_url/packages/$gitea_org?page=1&type=container&q=badhouseplants-net";
|
2023-02-26 06:27:44 +00:00
|
|
|
my $gitea_token=$ENV{'GITEA_TOKEN'};
|
2023-02-26 09:06:41 +00:00
|
|
|
my $gitea_user=$ENV{'GITEA_USER'} || $ENV{'DRONE_COMMIT_AUTHOR'};
|
|
|
|
# ---------------------------------------
|
2023-04-24 15:50:46 +00:00
|
|
|
# -- Get tags from Gitea
|
2023-02-26 09:06:41 +00:00
|
|
|
# ---------------------------------------
|
2023-04-24 16:01:13 +00:00
|
|
|
my $builds = "curl -X 'GET' \"$gitea_list_api\" -H 'accept: application/json' -H \"Authorization: token $gitea_token\" | jq -r '.[].version'";
|
2023-02-26 06:27:44 +00:00
|
|
|
my @builds_out = `$builds`;
|
|
|
|
chomp @builds_out;
|
2023-02-26 09:06:41 +00:00
|
|
|
# ---------------------------------------
|
|
|
|
# -- Get a list of all commits + 'latest'
|
|
|
|
# ---------------------------------------
|
2023-04-24 15:50:46 +00:00
|
|
|
my $commits = "argocd app list -o yaml -l application=badhouseplants | yq '.[].metadata.labels.commit_sha'";
|
2023-02-26 09:06:41 +00:00
|
|
|
my @commits_out = `$commits`;
|
|
|
|
chomp @commits_out;
|
|
|
|
push @commits_out, 'latest';
|
2023-03-06 20:42:35 +00:00
|
|
|
# --------------------------------------
|
|
|
|
# -- Rclone variables
|
|
|
|
# -------------------------------------
|
|
|
|
my $dirs = "rclone lsf badhouseplants-minio:/badhouseplants-net";
|
|
|
|
my @dirs_out = `$dirs`;
|
|
|
|
chomp @dirs_out;
|
2023-02-26 09:06:41 +00:00
|
|
|
# ---------------------------------------
|
|
|
|
# -- Compare builds to commits
|
|
|
|
# -- And remove obsolete imgages from
|
|
|
|
# -- registry
|
|
|
|
# ---------------------------------------
|
2023-03-06 20:42:35 +00:00
|
|
|
print "Cleaning up the container registry\n";
|
2023-02-26 06:27:44 +00:00
|
|
|
foreach my $line (@builds_out)
|
2023-02-26 05:33:53 +00:00
|
|
|
{
|
2023-03-06 20:42:35 +00:00
|
|
|
print "Checking if $line is in @commits_out\n\n";
|
|
|
|
if ( ! grep( /^$line$/, @commits_out ) ) {
|
2023-04-24 15:50:46 +00:00
|
|
|
my $cmd = "curl -X 'DELETE' -s \"$gitea_api/$line\" -H 'accept: application/json' -H \"Authorization: token $gitea_token\" || true";
|
2023-03-06 20:42:35 +00:00
|
|
|
print "Removing ${line}\n\n";
|
|
|
|
my $output = `$cmd`;
|
|
|
|
print "$output \n";
|
|
|
|
}
|
2023-02-26 05:33:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 20:42:35 +00:00
|
|
|
print "Cleaning up Minio\n";
|
|
|
|
foreach my $line (@dirs_out)
|
|
|
|
{
|
|
|
|
print "Checking if $line is in @commits_out\n\n";
|
|
|
|
chop($line);
|
|
|
|
if ( ! grep( /^$line$/, @commits_out ) ) {
|
|
|
|
my $cmd = "rclone purge badhouseplants-minio:/badhouseplants-net/$line";
|
|
|
|
print "Removing ${line}\n\n";
|
|
|
|
my $output = `$cmd`;
|
|
|
|
print "$output \n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|