Skip to content

Commit

Permalink
new(build): prune deb-dev and rpm-dev directories
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed Feb 7, 2024
1 parent 195116f commit b091522
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
54 changes: 53 additions & 1 deletion scripts/publish-deb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,47 @@ update_repo() {
popd > /dev/null
}

reduce_dir_size() {
local DIR=$1
local MAX_SIZE_GB=$2
local EXTENSION=$3
local MAX_SIZE=$((MAX_SIZE_GB*1024*1024)) # Convert GB to KB for du command

# Check if directory exists
if [[ ! -d "$DIR" ]]; then
echo "The directory $DIR does not exist."
return 1
fi

# Calculate current directory size in KB
local CUR_SIZE=$(du -sk "$DIR" | cut -f1)

# Check if we need to delete any files
if ((CUR_SIZE <= MAX_SIZE)); then
return 0
fi

# Calculate size to delete in bytes
local DEL_SIZE=$(( (CUR_SIZE - MAX_SIZE) * 1024 ))

local ACC_SIZE=0
find "$DIR" -maxdepth 1 -type f -name "*.$EXTENSION" -printf "%T+ %s %p\n" | sort | while read -r date size file; do
if ((ACC_SIZE + size < DEL_SIZE)); then
rm "$file"
ACC_SIZE=$((ACC_SIZE + size))

local asc_file="$file.asc"
if [[ -e "$asc_file" ]]; then
local asc_size=$(stat --format="%s" "$asc_file")
rm "$asc_file"
ACC_SIZE=$((ACC_SIZE + asc_size))
fi
else
break
fi
done
}

# parse options
while getopts ":f::r::s" opt; do
case "${opt}" in
Expand Down Expand Up @@ -188,6 +229,11 @@ if [ "${sign_all}" ]; then
sign_repo ${tmp_repo_path} ${debSuite}
fi

# remove old dev packages if necessary
if [[ ${repo} == "deb-dev" ]]; then
reduce_dir_size "${tmp_repo_path}/${debSuite}" 10 deb
fi

# update the repo by adding new packages
if ! [ ${#files[@]} -eq 0 ]; then
for file in "${files[@]}"; do
Expand All @@ -211,4 +257,10 @@ fi

# sync dists
aws s3 sync ${tmp_repo_path}/dists ${s3_bucket_repo}/dists --delete --acl public-read
aws cloudfront create-invalidation --distribution-id ${AWS_CLOUDFRONT_DIST_ID} --paths ${cloudfront_path}/dists/*
aws cloudfront create-invalidation --distribution-id ${AWS_CLOUDFRONT_DIST_ID} --paths ${cloudfront_path}/dists/*

# delete packages that have been pruned
# the dryrun option is there so we can check that we're doing the right thing, can be removed after testing
if [[ ${repo} == "deb-dev" ]]; then
aws s3 sync "${tmp_repo_path}/${debSuite}" ${s3_bucket_repo} --dryrun --delete
fi
54 changes: 53 additions & 1 deletion scripts/publish-rpm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ update_repo() {
popd > /dev/null
}

reduce_dir_size() {
local DIR=$1
local MAX_SIZE_GB=$2
local EXTENSION=$3
local MAX_SIZE=$((MAX_SIZE_GB*1024*1024)) # Convert GB to KB for du command

# Check if directory exists
if [[ ! -d "$DIR" ]]; then
echo "The directory $DIR does not exist."
return 1
fi

# Calculate current directory size in KB
local CUR_SIZE=$(du -sk "$DIR" | cut -f1)

# Check if we need to delete any files
if ((CUR_SIZE <= MAX_SIZE)); then
return 0
fi

# Calculate size to delete in bytes
local DEL_SIZE=$(( (CUR_SIZE - MAX_SIZE) * 1024 ))

local ACC_SIZE=0
find "$DIR" -maxdepth 1 -type f -name "*.$EXTENSION" -printf "%T+ %s %p\n" | sort | while read -r date size file; do
if ((ACC_SIZE + size < DEL_SIZE)); then
rm "$file"
ACC_SIZE=$((ACC_SIZE + size))

local asc_file="$file.asc"
if [[ -e "$asc_file" ]]; then
local asc_size=$(stat --format="%s" "$asc_file")
rm "$asc_file"
ACC_SIZE=$((ACC_SIZE + asc_size))
fi
else
break
fi
done
}

# parse options
while getopts ":f::r::s" opt; do
case "${opt}" in
Expand Down Expand Up @@ -115,6 +156,11 @@ if [ "${sign_all}" ]; then
sign_repo ${tmp_repo_path}
fi

# remove old dev packages if necessary
if [[ ${repo} == "rpm-dev" ]]; then
reduce_dir_size ${tmp_repo_path} 10 rpm
fi

# update the repo by adding new packages
if ! [ ${#files[@]} -eq 0 ]; then
for file in "${files[@]}"; do
Expand All @@ -138,4 +184,10 @@ fi

# sync repodata
aws s3 sync ${tmp_repo_path}/repodata ${s3_bucket_repo}/repodata --delete --acl public-read
aws cloudfront create-invalidation --distribution-id ${AWS_CLOUDFRONT_DIST_ID} --paths ${cloudfront_path}/repodata/*
aws cloudfront create-invalidation --distribution-id ${AWS_CLOUDFRONT_DIST_ID} --paths ${cloudfront_path}/repodata/*

# delete packages that have been pruned
# the dryrun option is there so we can check that we're doing the right thing, can be removed after testing
if [[ ${repo} == "rpm-dev" ]]; then
aws s3 sync ${tmp_repo_path} ${s3_bucket_repo} --dryrun --delete
fi

0 comments on commit b091522

Please sign in to comment.