diff --git a/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/centos-7/download-requirements.sh b/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/centos-7/download-requirements.sh index 84dcbae1af..44980063d5 100644 --- a/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/centos-7/download-requirements.sh +++ b/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/centos-7/download-requirements.sh @@ -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: [--no-exit-on-error] run_cmd() { @@ -426,6 +452,15 @@ _get_shell_escaped_array() { fi } +# params: +_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 @@ -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 --- diff --git a/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/redhat-7/download-requirements.sh b/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/redhat-7/download-requirements.sh index 68c1cf3781..d4171a42ca 100644 --- a/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/redhat-7/download-requirements.sh +++ b/core/src/epicli/data/common/ansible/playbooks/roles/repository/files/download-requirements/redhat-7/download-requirements.sh @@ -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: [--no-exit-on-error] run_cmd() { @@ -443,6 +469,15 @@ _get_shell_escaped_array() { fi } +# params: +_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 @@ -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 ---