-
Notifications
You must be signed in to change notification settings - Fork 1
/
apt-get-current
executable file
·52 lines (41 loc) · 1.99 KB
/
apt-get-current
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
#!/bin/bash -e
WORKDIR="/var/lib/apt-current/"
function clean_lists {
rm -f /var/lib/apt/lists/* &>/dev/null || true
rm -f /var/lib/apt/lists/partial/* || true
}
# find the command and see if it's one that may require an update check
CMD=""
for var in "$@"
do
[[ $var == -* ]] || { CMD="$var"; break; }
done
[[ "${CMD}" == "clean-lists" ]] && { clean_lists ; exit 0; }
[[ "${CMD}" == "install" || "${CMD}" == "dist-upgrade" || "${CMD}" == "upgrade" ]] || { apt-get "$@" ; exit $? ; }
PERFORM_UPDATE=false
# check whether our latest apt-get update is too old
. /etc/apt-current.conf
MAXDELTA=${MAXDELTA:-10800}
# either they're true or we must force it to the false literal
[ "true" == "${CLEANUP_DOWNLOADED_PACKAGES}" ] || { CLEANUP_DOWNLOADED_PACKAGES="false" ; }
[ "true" == "${CLEANUP_DOWNLOADED_LISTS}" ] || { CLEANUP_DOWNLOADED_LISTS="false" ; }
# if too much time has passed since last update, force it
CURDATE="$(date +%s)"
PREVDATE=$(cat ${WORKDIR}/timestamp)
[ "$(( ${CURDATE} - ${PREVDATE} ))" -gt "${MAXDELTA}" ] && PERFORM_UPDATE=true
# check whether our sources.list have changed; in this situation, always update
SOURCES="/etc/apt/sources.list $(find /etc/apt/sources.list.d/ -name "*.list" -o -name "*.sources" | sort)"
CURSOURCES_HASH="$(echo $SOURCES | xargs cat | md5sum | cut -f 1 -d ' ')"
[ "${CURSOURCES_HASH}" == "$(cat ${WORKDIR}/latest-sources)" ] || PERFORM_UPDATE=true
# apt configuration has changed; force update
CURCONFIG="$(apt-config dump | md5sum | cut -f 1 -d ' ')"
[ "${CURCONFIG}" == "$(cat ${WORKDIR}/latest-config)" ] || PERFORM_UPDATE=true
# downloaded lists are empty (maybe they have been cleaned), must update if we're trying to install something.
[ "$(find /var/lib/apt/lists/ -maxdepth 1 -type f -not -name lock | wc -l)" -eq 0 ] && [[ "${CMD}" == "install" ]] && PERFORM_UPDATE=true
${PERFORM_UPDATE} && apt-get -y update
set +e
apt-get "$@"
EXIT_CODE="$?"
${CLEANUP_DOWNLOADED_PACKAGES} && apt-get -y clean
${CLEANUP_DOWNLOADED_LISTS} && clean_lists
exit ${EXIT_CODE}