Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update acr-cleanup.sh #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ WORKDIR /app
# Instala as dependências necessárias
RUN apk --no-cache add curl openssh-client python3 py3-pip

# https://elliottback.medium.com/python-this-environment-is-externally-managed-error-and-docker-6062aac20a6e
# https://peps.python.org/pep-0668/
RUN rm /usr/lib/python*/EXTERNALLY-MANAGED

# Instala a CLI do Azure
RUN apk --no-cache add --virtual .build-deps gcc libffi-dev musl-dev openssl-dev python3-dev \
&& pip3 install azure-cli \
Expand All @@ -16,4 +20,4 @@ RUN if [ -f .env ]; then \
cp .env /app/.env; \
fi

CMD ["bash", "./acr-cleanup.sh"]
CMD ["bash", "./acr-cleanup.sh"]
47 changes: 38 additions & 9 deletions acr-cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
#!/bin/bash

# Carrega as variáveis do arquivo .env
# Carrega as variáveis do arquivo .env, se existir e se ServicePrincipalId não estiver definida
# Loads the variables from the .env file if it exists and if ServicePrincipalId is not set
if [ -f .env ] && [ -z "$ServicePrincipalId" ]; then
export $(cat .env | grep -v '^#' | xargs) > /dev/null 2>&1
fi

# Função para remover uma imagem
# Função para remover uma imagem do Azure Container Registry
# Function to remove an image from the Azure Container Registry
function remove_image {
registryName="$1"
imageName="$2"
dryRun="$3"

if [ "$dryRun" = "true" ]; then
echo "Would have deleted $imageName"
echo "Would have deleted $imageName" # Simulação de remoção
# Dry run: Simulating deletion
else
echo "Proceeding to delete image: $imageName"
echo "Proceeding to delete image: $imageName" # Remoção real
# Proceeding to delete the image
az acr repository delete --name "$registryName" --image "$imageName" --yes
fi
}

# Verifica se as variáveis estão definidas
# Verifica se as variáveis necessárias estão definidas
# Checks if the required variables are set
if [ -z "$ServicePrincipalId" ] && [ -z "$ServicePrincipalPass" ] && [ -z "$ServicePrincipalTenant" ]; then
echo "As variáveis ServicePrincipalId, ServicePrincipalPass e ServicePrincipalTenant devem ser definidas."
# The variables ServicePrincipalId, ServicePrincipalPass, and ServicePrincipalTenant must be set
echo "Execute o container Docker com o seguinte comando:"
# Run the Docker container with the following command
echo ""
echo "docker run \\"
echo " -e ServicePrincipalTenant=<valor> \\"
Expand All @@ -36,34 +43,48 @@ if [ -z "$ServicePrincipalId" ] && [ -z "$ServicePrincipalPass" ] && [ -z "$Serv
echo " <nome_da_imagem_docker>"
exit 1
fi

# Autenticação com a Azure
# Authentication with Azure
echo "Estabelecendo autenticação com a Azure..."
# Establishing authentication with Azure
az login --service-principal -u "$ServicePrincipalId" -p "$ServicePrincipalPass" --tenant "$ServicePrincipalTenant"

# Define a assinatura se especificada
# Sets the subscription if specified
if [ -n "$SubscriptionName" ]; then
echo "Definindo a assinatura para: $SubscriptionName"
# Setting the subscription to: $SubscriptionName
az account set --subscription "$SubscriptionName"
fi

# Lista e verifica os repositórios no registro Azure
# Lists and checks the repositories in the Azure registry
echo "Verificando o registro: $AzureRegistryName"
# Checking the registry: $AzureRegistryName
RepoList=($(az acr repository list --name "$AzureRegistryName" --output tsv))
for RepositoryName in "${RepoList[@]}"; do
echo "Verificando o repositório: $RepositoryName"
# Checking the repository: $RepositoryName
RepositoryTags=$(az acr repository show-tags --name "$AzureRegistryName" --repository "$RepositoryName" --orderby time_desc --output tsv)

# Excluir por contagem se o usuário especificou NoOfKeptImages
# Excluir por contagem se NoOfKeptImages for especificado
# Delete by count if NoOfKeptImages is specified
if [ "$NoOfKeptImages" -gt 0 ]; then
echo "IF NO OF KEPT IMAGES"

count=0
for tag in $RepositoryTags; do
RepositoryTagName=$(echo "$tag" | awk -F_ '{print $NF}' | awk -F. '{print $1}')

# Ignora tags "latest" e aquelas contendo "migration"
# Skip "latest" tags and those containing "migration"
if [ "$RepositoryTagName" = "latest" ] || [[ "$RepositoryTagName" == *"migration"* ]]; then
echo "Skipping tag: $RepositoryTagName"
echo "Skipping image: $RepositoryName/$tag"
continue
fi

# Remove imagens além do número especificado em NoOfKeptImages
# Remove images beyond the number specified in NoOfKeptImages
if [ $count -ge $NoOfKeptImages ]; then
ImageName="$RepositoryName:$tag"
remove_image "$AzureRegistryName" "$ImageName" "$DryRun"
Expand All @@ -74,12 +95,16 @@ for RepositoryName in "${RepoList[@]}"; do
for tag in $RepositoryTags; do
RepositoryTagName=$(echo "$tag" | awk -F_ '{print $NF}' | awk -F. '{print $1}')

# Ignora tags "latest" e "migration-latest"
# Skip "latest" and "migration-latest" tags
if [ "$RepositoryTagName" == "latest" ] || [ "$RepositoryTagName" == "migration-latest" ]; then
echo "Skipping tag latest"
echo "Skipping image: $RepositoryName/latest"
continue
fi

# Converte a tag para a data e compara com a data atual menos NoOfDays
# Convert the tag to date and compare it with the current date minus NoOfDays
RepositoryTagBuildDay=$(date -d "$RepositoryTagName" "+%Y%m%d")
ImageName="$RepositoryName:$tag"

Expand All @@ -94,7 +119,11 @@ for RepositoryName in "${RepoList[@]}"; do
((index++))
done

# Encerra a sessão da Azure
# Ends the Azure session
echo "Encerrando a sessão da Azure"
# Ending the Azure session
az logout

echo "Execução do script concluída"
echo "Execução do script concluída"
# Script execution completed
Loading