Skip to content

Commit

Permalink
Add clean metadata for upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
to-bar committed Mar 12, 2021
1 parent a64b759 commit 7933e4e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,32 @@ remove_installed_packages() {
fi
}

remove_yum_cache_for_untracked_repos() {
local basearch releasever
basearch=$(uname --machine)
releasever=$(rpm -q --provides "$(rpm -q --whatprovides 'system-release(releasever)')" | grep "system-release(releasever)" | cut -d ' ' -f 3)
local cachedir find_output
cachedir=$(grep --only-matching --perl-regexp '(?<=^cachedir=)[^#\n]+' /etc/yum.conf)
cachedir="${cachedir/\$basearch/$basearch}"
cachedir="${cachedir/\$releasever/$releasever}"
find_output=$(find "$cachedir" -mindepth 1 -maxdepth 1 -type d -exec basename '{}' ';')
local -a repos_with_cache=()
if [ -n "$find_output" ]; then
readarray -t repos_with_cache <<< "$find_output"
fi
local all_repos_output
all_repos_output=$(yum repolist -v all | grep --only-matching --perl-regexp '(?<=^Repo-id)[^/]+' | sed -e 's/^[[:space:]:]*//')
local -a all_repos=()
readarray -t all_repos <<< "$all_repos_output"
if (( ${#repos_with_cache[@]} > 0 )); then
for cached_repo in "${repos_with_cache[@]}"; do
if ! _in_array "$cached_repo" "${all_repos[@]}"; then
run_cmd rm -rf "$cachedir/$cached_repo"
fi
done
fi
}

# Runs command as array with printing it, doesn't support commands with shell operators (such as pipe or redirection)
# params: <command to execute> [--no-exit-on-error]
run_cmd() {
Expand Down Expand Up @@ -426,6 +452,15 @@ _get_shell_escaped_array() {
fi
}

# params: <value to test> <array>
_in_array() {
local value=${1}
shift
local array=( "$@" )

(( ${#array[@]} > 0 )) && printf '%s\n' "${array[@]}" | grep -q -Fx "$value"
}

# Prints string in format that can be reused as shell input (escapes non-printable characters)
_print_array_as_shell_escaped_string() {
local output
Expand Down Expand Up @@ -719,6 +754,9 @@ if ! is_package_installed 'epel-release'; then
install_package 'https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm' 'epel-release'
fi

# clean metadata for upgrades (when the same package can be downloaded from changed repo)
run_cmd remove_yum_cache_for_untracked_repos

run_cmd yum -y makecache fast

# --- Download packages ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,32 @@ remove_installed_packages() {
fi
}

remove_yum_cache_for_untracked_repos() {
local basearch releasever
basearch=$(uname --machine)
releasever=$(rpm -q --provides "$(rpm -q --whatprovides 'system-release(releasever)')" | grep "system-release(releasever)" | cut -d ' ' -f 3)
local cachedir find_output
cachedir=$(grep --only-matching --perl-regexp '(?<=^cachedir=)[^#\n]+' /etc/yum.conf)
cachedir="${cachedir/\$basearch/$basearch}"
cachedir="${cachedir/\$releasever/$releasever}"
find_output=$(find "$cachedir" -mindepth 1 -maxdepth 1 -type d -exec basename '{}' ';')
local -a repos_with_cache=()
if [ -n "$find_output" ]; then
readarray -t repos_with_cache <<< "$find_output"
fi
local all_repos_output
all_repos_output=$(yum repolist -v all | grep --only-matching --perl-regexp '(?<=^Repo-id)[^/]+' | sed -e 's/^[[:space:]:]*//')
local -a all_repos=()
readarray -t all_repos <<< "$all_repos_output"
if (( ${#repos_with_cache[@]} > 0 )); then
for cached_repo in "${repos_with_cache[@]}"; do
if ! _in_array "$cached_repo" "${all_repos[@]}"; then
run_cmd rm -rf "$cachedir/$cached_repo"
fi
done
fi
}

# Runs command as array with printing it, doesn't support commands with shell operators (such as pipe or redirection)
# params: <command to execute> [--no-exit-on-error]
run_cmd() {
Expand Down Expand Up @@ -443,6 +469,15 @@ _get_shell_escaped_array() {
fi
}

# params: <value to test> <array>
_in_array() {
local value=${1}
shift
local array=( "$@" )

(( ${#array[@]} > 0 )) && printf '%s\n' "${array[@]}" | grep -q -Fx "$value"
}

# Prints string in format that can be reused as shell input (escapes non-printable characters)
_print_array_as_shell_escaped_string() {
local output
Expand Down Expand Up @@ -742,6 +777,9 @@ if ! is_package_installed 'epel-release'; then
install_package 'https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm' 'epel-release'
fi

# clean metadata for upgrades (when the same package can be downloaded from changed repo)
run_cmd remove_yum_cache_for_untracked_repos

run_cmd yum -y makecache fast

# --- Download packages ---
Expand Down

0 comments on commit 7933e4e

Please sign in to comment.