Skip to content

Commit f458d05

Browse files
authored
Fix the script to fix two scenarios: (#1304)
1. No source list file needs to be updated 2. Multiple source list files need to be updated
1 parent 1376d17 commit f458d05

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

rstudio/rstudio.sh

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,37 @@
1717

1818
set -euxo pipefail
1919

20+
function os_id() ( set +x ; grep '^ID=' /etc/os-release | cut -d= -f2 | xargs ; )
21+
function os_version() ( set +x ; grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | xargs ; )
22+
function os_codename() ( set +x ; grep '^VERSION_CODENAME=' /etc/os-release | cut -d= -f2 | xargs ; )
23+
24+
function version_ge() ( set +x ; [ "$1" = "$(echo -e "$1\n$2" | sort -V | tail -n1)" ] ; )
25+
function version_gt() ( set +x ; [ "$1" = "$2" ] && return 1 || version_ge $1 $2 ; )
26+
function version_le() ( set +x ; [ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ] ; )
27+
function version_lt() ( set +x ; [ "$1" = "$2" ] && return 1 || version_le $1 $2 ; )
28+
29+
readonly -A supported_os=(
30+
['debian']="10 11 12"
31+
['rocky']="8 9"
32+
['ubuntu']="18.04 20.04 22.04"
33+
)
34+
35+
# dynamically define OS version test utility functions
36+
if [[ "$(os_id)" == "rocky" ]];
37+
then _os_version=$(os_version | sed -e 's/[^0-9].*$//g')
38+
else _os_version="$(os_version)"; fi
39+
for os_id_val in 'rocky' 'ubuntu' 'debian' ; do
40+
eval "function is_${os_id_val}() ( set +x ; [[ \"$(os_id)\" == '${os_id_val}' ]] ; )"
41+
42+
for osver in $(echo "${supported_os["${os_id_val}"]}") ; do
43+
eval "function is_${os_id_val}${osver%%.*}() ( set +x ; is_${os_id_val} && [[ \"${_os_version}\" == \"${osver}\" ]] ; )"
44+
eval "function ge_${os_id_val}${osver%%.*}() ( set +x ; is_${os_id_val} && version_ge \"${_os_version}\" \"${osver}\" ; )"
45+
eval "function le_${os_id_val}${osver%%.*}() ( set +x ; is_${os_id_val} && version_le \"${_os_version}\" \"${osver}\" ; )"
46+
done
47+
done
48+
49+
function is_debuntu() ( set +x ; is_debian || is_ubuntu ; )
50+
2051
# Only run on the master node
2152
ROLE="$(/usr/share/google/get_metadata_value attributes/dataproc-role)"
2253

@@ -52,22 +83,24 @@ if (! test -v DATAPROC_IMAGE_VERSION) && test -v DATAPROC_VERSION; then
5283
DATAPROC_IMAGE_VERSION="${DATAPROC_VERSION}"
5384
fi
5485

55-
function remove_old_backports {
86+
function repair_old_backports {
87+
if ! is_debuntu ; then return ; fi
5688
# This script uses 'apt-get update' and is therefore potentially dependent on
5789
# backports repositories which have been archived. In order to mitigate this
5890
# problem, we will remove any reference to backports repos older than oldstable
5991

6092
# https://github.com/GoogleCloudDataproc/initialization-actions/issues/1157
61-
oldstable=$(curl -s https://deb.debian.org/debian/dists/oldstable/Release | awk '/^Codename/ {print $2}');
62-
stable=$(curl -s https://deb.debian.org/debian/dists/stable/Release | awk '/^Codename/ {print $2}');
63-
64-
matched_files="$(grep -rsil '\-backports' /etc/apt/sources.list*)"
65-
if [[ -n "$matched_files" ]]; then
66-
for filename in "$matched_files"; do
67-
grep -e "$oldstable-backports" -e "$stable-backports" "$filename" || \
68-
sed -i -e 's/^.*-backports.*$//' "$filename"
69-
done
70-
fi
93+
debdists="https://deb.debian.org/debian/dists"
94+
oldoldstable=$(curl -s "${debdists}/oldoldstable/Release" | awk '/^Codename/ {print $2}');
95+
oldstable=$( curl -s "${debdists}/oldstable/Release" | awk '/^Codename/ {print $2}');
96+
stable=$( curl -s "${debdists}/stable/Release" | awk '/^Codename/ {print $2}');
97+
98+
matched_files=( $(grep -rsil '\-backports' /etc/apt/sources.list*||:) )
99+
100+
for filename in "${matched_files[@]}"; do
101+
perl -pi -e "s{^(deb[^\s]*) https?://[^/]+/debian ${oldoldstable}-backports }
102+
{\$1 https://archive.debian.org/debian ${oldoldstable}-backports }g" "${filename}"
103+
done
71104
}
72105

73106
function update_apt_get() {
@@ -116,7 +149,7 @@ function install_package() {
116149

117150
if [[ "${ROLE}" == 'Master' ]]; then
118151
if [[ ${OS_ID} == debian ]] && [[ $(echo "${DATAPROC_IMAGE_VERSION} <= 2.1" | bc -l) == 1 ]]; then
119-
remove_old_backports
152+
repair_old_backports
120153
fi
121154
if [[ -n ${USER_PASSWORD} ]] && ((${#USER_PASSWORD} < 7)); then
122155
echo "You must specify a password of at least 7 characters for user '$USER_NAME' through metadata 'rstudio-password'."

0 commit comments

Comments
 (0)