Files
scripts/cacherebuild.sh

147 lines
5.1 KiB
Bash
Raw Permalink Normal View History

2021-10-07 18:23:07 +02:00
#!/bin/bash
BLTPATH="$(pwd)/" # path of the build script
FORCE=yes # yes | force = remove cache and create new one
FORCED_MONTH_OFFSET=0 # cache is valid one month. This allows creation in advance
MAKEFORALLAPPS="no" # yes = make all app combinations. It might be too much. If not set, hardcoded values are choosen
PARALLEL_BUILDS="" # choose how many you want to run in parallel. Leave empty for auto
USE_SCREEN="no" # run commands in screen
2022-03-28 19:41:52 +02:00
FORCE_RELEASE="jammy" # we only build supported releases caches. her you can add unsupported ones which you wish to experiment
2022-02-01 11:33:25 +01:00
EXCLUDE_RELEASE="buster hirsute" # we might not want to make caches for old stable releses
2021-10-07 18:23:07 +02:00
FORCE_DESKTOP="cinnamon" # we only build supported desktop caches. here you can add unsupported ones which you wish to build anyway
PURGEDAYS="4" # delete files that are older then n days and are not used anymore
2021-10-13 19:55:44 +02:00
CLEANING="${1:-no}"
FILE_OUT="${2:-filelist.txt}"
2021-11-29 20:59:23 +01:00
BUILD_VARIANTS="${3:-lepotato bananapi}"
2021-11-02 17:38:50 +01:00
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
2021-10-07 18:23:07 +02:00
2021-11-02 17:38:50 +01:00
# load functions
source "$SCRIPT_DIR/cacherebuild-functions.sh"
2021-10-07 18:23:07 +02:00
2021-11-02 17:38:50 +01:00
# Main
2021-10-07 18:23:07 +02:00
# load config file to override default values
2021-11-02 17:38:50 +01:00
[[ -f cacherebuild.conf ]] && source "$SCRIPT_DIR/cacherebuild.conf" || display_alert "Using defaults" "" "info"
2021-10-07 18:23:07 +02:00
# hardcoded variables and calculations
2021-12-26 14:58:30 +01:00
DAYSBEFORE=6
2021-10-07 18:23:07 +02:00
START_TIME=$(date +%s)
MONTH=$(date -d "$M" '+%m' | sed 's/\<0//g')
DAYINMONTH=$(date -d "$D" '+%d' | sed 's/\<0//g')
REBUILDDAY=$(date -d "${MONTH}/1 + 1 month - ${DAYSBEFORE} day" "+%d")
MEM_INFO=$(($(LC_ALL=C free -w 2>/dev/null | grep "^Mem" | awk '{print $2}' || LC_ALL=C free | grep "^Mem"| awk '{print $2}')/1024))
r=0
2021-11-29 21:14:53 +01:00
truncate --size=0 ${FILE_OUT}
2021-10-07 18:23:07 +02:00
# jump to build script folder
cd ${BLTPATH}
display_alert "Starting rootfs cache rebuilt" "$(date)" "info"
display_alert "Currently present cache files" "$(ls -l ${BLTPATH}cache/rootfs/*.lz4 2> /dev/null | wc -l)" "info"
2021-11-02 17:38:50 +01:00
# Update script just in case
2021-10-07 18:23:07 +02:00
git pull -q 2> /dev/null
if [[ $? -ne 0 ]]; then
display_alert "Updating build script" "git pull" "err"
exit 1
else
display_alert "Updating build script" "git pull" "info"
fi
2021-11-02 17:38:50 +01:00
# Calculate parallel build tasks
2021-10-07 18:23:07 +02:00
display_alert "System memory" "$(($MEM_INFO/1024))Gb" "info"
if [[ -z ${PARALLEL_BUILDS} ]]; then
PARALLEL_BUILDS=$(awk '{printf("%d",$1/12000)}' <<<${MEM_INFO})
display_alert "Calculated parallel builds" "$PARALLEL_BUILDS" "info"
else
display_alert "Selected parallel builds" "$PARALLEL_BUILDS" "info"
fi
2021-11-02 17:38:50 +01:00
# Rebuild or update
2021-10-07 18:23:07 +02:00
if [[ "${FORCE}" == "force" ]]; then
2021-11-02 17:38:50 +01:00
display_alert "Cache will be removed and rebuild" "" "info"
2021-10-07 18:23:07 +02:00
else
2021-11-02 17:38:50 +01:00
display_alert "Cache will be updated" "" "info"
2021-10-07 18:23:07 +02:00
fi
# when we should start building for next month
if [[ $DAYINMONTH -gt $REBUILDDAY ]]; then
display_alert "${DAYSBEFORE} days before next month" "building for next month FORCED_MONTH_OFFSET=1" "info"
FORCED_MONTH_OFFSET=1
fi
2021-11-02 17:38:50 +01:00
# first week we clean older files then 14 days
2021-10-07 18:23:07 +02:00
if [[ $DAYINMONTH -lt 7 ]]; then
display_alert "First seven (7) days we clean files of previous month" "cleaning files older then 14 days" "info"
find ${BLTPATH}cache/rootfs/ -type f -mtime +14 -exec sudo rm -f {} \;
fi
# don't start if previous run is still running
while :
do
sleep 3
CURRENT_TIME=$(date +%s)
display_alert "Waiting for cleanup" "yes" "info"
if [[ $(df | grep /.tmp | wc -l) -lt 1 ]]; then
break
fi
done
# removing previous cache if forced
[[ "${FORCE}" == "force" ]] && sudo rm -f ${BLTPATH}cache/rootfs/*
# run main rebuild function
releases
#
# wait until all build PIDS are done
#
while true
do
i=0
for pids in $PIDS
do
if ps -p $pids > /dev/null; then
i=$((i+1))
fi
done
[[ $i -eq 0 ]] && break
done
#
# clean all build that are not labelled as .current and are older then 4 days
#
2021-11-02 17:38:50 +01:00
if [[ "${FORCED_MONTH_OFFSET}" -eq 0 && "${CLEANING}" == "yes" ]]; then
2021-10-07 18:23:07 +02:00
display_alert "Clean all build that are not labelled as current." "cleanup" "info"
2021-10-13 19:55:44 +02:00
# create a diff between marked as current and others
BRISI=($(diff <(find ${BLTPATH}cache/rootfs -name "*.lz4.current" | sed "s/.current//" | sort) <(find ${BLTPATH}cache/rootfs -name "*.lz4" | sort) | grep ">" | sed "s/> //"))
for brisi in "${BRISI[@]}"; do
if [[ $(find "$brisi" -mtime +${PURGEDAYS} -print) ]]; then
display_alert "File is older then ${PURGEDAYS} days. Deleting." "$(basename $brisi)" "info"
sudo rm $brisi
else
display_alert "File is not older then ${PURGEDAYS} days" "$(basename $brisi)" "info"
fi
done
# remove .current mark
sudo rm -f ${BLTPATH}cache/rootfs/*.current
2021-10-07 18:23:07 +02:00
fi
2021-10-13 19:55:44 +02:00
# calculate execution time
display_alert "Currently present cache files" "$(ls -l ${BLTPATH}cache/rootfs/*.lz4 2> /dev/null| wc -l)" "info"
2021-10-07 18:23:07 +02:00
# removing previous tmp build directories
sudo rm -rf ${BLTPATH}.tmp