forked from armbian/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacherebuild.sh
executable file
·146 lines (115 loc) · 5.14 KB
/
cacherebuild.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/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
FORCE_RELEASE="jammy" # we only build supported releases caches. her you can add unsupported ones which you wish to experiment
EXCLUDE_RELEASE="buster hirsute" # we might not want to make caches for old stable releses
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
CLEANING="${1:-no}"
FILE_OUT="${2:-filelist.txt}"
BUILD_VARIANTS="${3:-lepotato bananapi}"
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# load functions
source "$SCRIPT_DIR/cacherebuild-functions.sh"
# Main
# load config file to override default values
[[ -f cacherebuild.conf ]] && source "$SCRIPT_DIR/cacherebuild.conf" || display_alert "Using defaults" "" "info"
# hardcoded variables and calculations
DAYSBEFORE=6
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
truncate --size=0 ${FILE_OUT}
# 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"
# Update script just in case
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
# Calculate parallel build tasks
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
# Rebuild or update
if [[ "${FORCE}" == "force" ]]; then
display_alert "Cache will be removed and rebuild" "" "info"
else
display_alert "Cache will be updated" "" "info"
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
# first week we clean older files then 14 days
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
#
if [[ "${FORCED_MONTH_OFFSET}" -eq 0 && "${CLEANING}" == "yes" ]]; then
display_alert "Clean all build that are not labelled as current." "cleanup" "info"
# 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
fi
# calculate execution time
display_alert "Currently present cache files" "$(ls -l ${BLTPATH}cache/rootfs/*.lz4 2> /dev/null| wc -l)" "info"
# removing previous tmp build directories
sudo rm -rf ${BLTPATH}.tmp