Skip to content

Commit 9939606

Browse files
authored
Implement an ability to use AlmaLinux local repositories (#239)
* Add -l|--local-repo option to provide an ability to use AlmaLinux local repositories in case if migrated system does not have internet access Correct AlmaLinux public PGP key URL handling via ALMA_PUBKEY_URL environment variable * Check AlmaLinux release package and GPG public key symlinks are reachable REPO_URL must include full path (if any) to the AlmaLinux repositories * Remove trailing slash (if any) from path of AlmaLinux local mirror URL
1 parent 3b01da0 commit 9939606

File tree

1 file changed

+88
-8
lines changed

1 file changed

+88
-8
lines changed

almalinux-deploy.sh

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ ALT_DIR="/etc/alternatives"
2020
MINIMAL_SUPPORTED_VERSION='8.4'
2121
VERSION='0.1.13'
2222
DOWNGRADE='NO'
23+
REPO_URL=https://repo.almalinux.org/almalinux
24+
LOCAL_REPO='NO'
2325

2426
BRANDING_PKGS=("centos-backgrounds" "centos-logos" "centos-indexhtml" \
2527
"centos-logos-ipa" "centos-logos-httpd" \
@@ -83,10 +85,9 @@ EXCLUDE_PKGS=
8385
module_list_enabled=""
8486
module_list_installed=""
8587
is_container=0
88+
date_time_stamp="$(date -u '+%Y%m%d%H%M%S')"
8689

8790
setup_log_files() {
88-
local date_time_stamp
89-
date_time_stamp="$(date -u '+%Y%m%d%H%M%S')"
9091
local log_file=/var/log/almalinux-deploy.log
9192
local debug_log_file=/var/log/almalinux-deploy.debug.log
9293

@@ -174,6 +175,8 @@ show_usage() {
174175
echo ' -f , --full perform yum upgrade to 8.5 if necessary'
175176
echo ' -d , --downgrade option to allow downgrade from CentOS Stream'
176177
echo ' -v , --version print version information and exit'
178+
echo ' -l=URL/path , --local-repo=URL/path use AlmaLinux local repositories at URL/path, like http://mirror.example.com/almalinux'
179+
echo ' in case if migrated system does not have internet access'
177180
echo ' -e=pkg1*,pkg2 , --exclude=pkg1*,pkg2* list of packages separated with comma to exclude on dnf distro-sync'
178181
}
179182

@@ -292,6 +295,76 @@ enable_repos() {
292295
save_status_of_stage "enable_repos"
293296
}
294297

298+
# Checks AlmaLinux local repositories are reachable.
299+
#
300+
# $1 - operational system type.
301+
# $2 - system architecture.
302+
check_local_repo() {
303+
local -r os_version="${1:0:1}"
304+
local -r arch="${2}"
305+
local check_repos="BaseOS AppStream"
306+
local check_links="almalinux-release-latest-${os_version}.${arch}.rpm RPM-GPG-KEY-AlmaLinux-${os_version}"
307+
308+
# Append the list of repositories to check as they will be enabled during dnf distro-sync
309+
case "${os_version}" in
310+
8*)
311+
check_repos+=" PowerTools"
312+
;;
313+
9*)
314+
check_repos+=" CRB"
315+
;;
316+
esac
317+
# Check only if local repository URL is set
318+
if [[ "${LOCAL_REPO}" == 'YES' ]]; then
319+
if get_status_of_stage "check_local_repo"; then
320+
return 0
321+
fi
322+
# Check metadata is reachable for specific repositories
323+
for check_repo in ${check_repos}; do
324+
if ! curl --head --silent --fail "${REPO_URL}/${os_version}/${check_repo}/${arch}/os/repodata/repomd.xml" > /dev/null 2>&1; then
325+
report_step_error "Check AlmaLinux repository ${check_repo} is reachable"
326+
exit 1
327+
fi
328+
done
329+
# Check AlmaLinux release package and GPG public key symlinks are reachable
330+
for check_link in ${check_links}; do
331+
if ! curl --head --silent --fail "${REPO_URL}/${check_link}" > /dev/null 2>&1; then
332+
report_step_error "Check ${check_link} symlink is reachable"
333+
exit 1
334+
fi
335+
done
336+
report_step_done "Check AlmaLinux core stuff is reachable at ${REPO_URL} "
337+
save_status_of_stage "check_local_repo"
338+
fi
339+
}
340+
341+
# Switches AlmaLinux repositories to local copy.
342+
#
343+
# $1 - operational system type.
344+
# $2 - system architecture.
345+
switch_to_local_repo() {
346+
local -r os_version="${1:0:1}"
347+
local -r arch="${2}"
348+
349+
# Switch only if local repository URL is set
350+
if [[ "${LOCAL_REPO}" == 'YES' ]]; then
351+
if get_status_of_stage "switch_to_local_repo"; then
352+
return 0
353+
fi
354+
355+
echo "Repository configuration files are preserved into /etc/yum.repos.d/*.repo.${date_time_stamp}"
356+
while IFS= read -r -d '' repo_file; do
357+
# Switch from mirrorlist to baseurl, and replace repo.almalinux.org there
358+
sed --in-place=".${date_time_stamp}" -e "s|^mirrorlist=|# mirrorlist=|" \
359+
-e "s|^# *baseurl=https://repo.almalinux.org/almalinux/|baseurl=${REPO_URL}/|" \
360+
"${repo_file}"
361+
done < <(find /etc/yum.repos.d -name 'almalinux*.repo' -print0)
362+
363+
report_step_done "Switch AlmaLinux repositories to ${REPO_URL}"
364+
save_status_of_stage "switch_to_local_repo"
365+
fi
366+
}
367+
295368
# Prints control type and version.
296369
get_panel_info() {
297370
local panel_type=''
@@ -417,7 +490,7 @@ assert_dnf_plugins_core() {
417490
get_release_file_url() {
418491
local -r os_version="${1:0:1}"
419492
local -r arch="${2}"
420-
echo "${ALMA_RELEASE_URL:-https://repo.almalinux.org/almalinux/almalinux-release-latest-${os_version}.${arch}.rpm}"
493+
echo "${ALMA_RELEASE_URL:-${REPO_URL}/almalinux-release-latest-${os_version}.${arch}.rpm}"
421494
}
422495

423496
# Returns a latest almalinux-repos RPM package download URL.
@@ -429,7 +502,7 @@ get_release_file_url() {
429502
get_repos_file_url() {
430503
local -r os_version="${1:0:1}"
431504
local -r arch="${2}"
432-
echo "${ALMA_REPOS_URL:-https://repo.almalinux.org/almalinux/almalinux-repos-latest-${os_version}.${arch}.rpm}"
505+
echo "${ALMA_REPOS_URL:-${REPO_URL}/almalinux-repos-latest-${os_version}.${arch}.rpm}"
433506
}
434507

435508
# Returns a latest almalinux-gpg-keys RPM package download URL.
@@ -441,7 +514,7 @@ get_repos_file_url() {
441514
get_gpg_keys_file_url() {
442515
local -r os_version="${1:0:1}"
443516
local -r arch="${2}"
444-
echo "${ALMA_REPOS_URL:-https://repo.almalinux.org/almalinux/almalinux-gpg-keys-latest-${os_version}.${arch}.rpm}"
517+
echo "${ALMA_REPOS_URL:-${REPO_URL}/almalinux-gpg-keys-latest-${os_version}.${arch}.rpm}"
445518
}
446519

447520
# Downloads and installs the AlmaLinux public PGP key.
@@ -454,8 +527,7 @@ install_rpm_pubkey() {
454527
fi
455528
local -r tmp_dir="${1}"
456529
local -r os_version="${2:0:1}"
457-
local -r ALMA_PUBKEY_URL="https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux-${os_version}"
458-
local -r pubkey_url="${ALMA_PUBKEY_URL:-https://repo.almalinux.org/almalinux/RPM-GPG-KEY-AlmaLinux}"
530+
local -r pubkey_url="${ALMA_PUBKEY_URL:-${REPO_URL}/RPM-GPG-KEY-AlmaLinux-${os_version}}"
459531
local -r pubkey_path="${tmp_dir}/RPM-GPG-KEY-AlmaLinux"
460532
local -r step='Download RPM-GPG-KEY-AlmaLinux'
461533
local output
@@ -1216,7 +1288,7 @@ remove_redhat_repo_files() {
12161288
local repo
12171289
for repo in "${REDHAT_REPO_FILES[@]}"; do
12181290
if [ -e "${repo}" ]; then
1219-
mv -f "${repo}" "${repo}.$(date -u '+%s')"
1291+
mv -f "${repo}" "${repo}.${date_time_stamp}"
12201292
fi
12211293
done
12221294
report_step_done "Remove RHEL repositories' files if any"
@@ -1242,6 +1314,7 @@ main() {
12421314
assert_supported_system "${os_type}" "${os_version}" "${arch}"
12431315
assert_supported_filesystem
12441316
assert_dnf_plugins_core
1317+
check_local_repo "${os_version}" "${arch}"
12451318
get_enabled_repos
12461319

12471320
read -r panel_type panel_version < <(get_panel_info)
@@ -1298,6 +1371,7 @@ main() {
12981371

12991372
backup_alternatives
13001373
reset_wrong_module_streams
1374+
switch_to_local_repo "${os_version}" "${arch}"
13011375
enable_repos "${os_version}"
13021376
distro_sync "${os_version}"
13031377
restore_module_streams
@@ -1340,6 +1414,12 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
13401414
EXCLUDE_PKGS="${opt#*=}"
13411415
shift
13421416
;;
1417+
-l=* | --local-repo=*)
1418+
LOCAL_REPO='YES'
1419+
REPO_URL="${opt#*=}"
1420+
REPO_URL="${REPO_URL%/}" # remove trailing slash
1421+
shift
1422+
;;
13431423
*)
13441424
echo "Error: unknown option ${opt}" >&2
13451425
exit 2

0 commit comments

Comments
 (0)