Skip to content

Instantly share code, notes, and snippets.

@edavidaja
Last active June 9, 2023 16:15
Show Gist options
  • Save edavidaja/5996ffeb042df2642c77c065c07f023d to your computer and use it in GitHub Desktop.
Save edavidaja/5996ffeb042df2642c77c065c07f023d to your computer and use it in GitHub Desktop.
a script for users to run that migrates their package libraries on Workbench
#!/bin/bash
set -e
BCYAN='\033[1;36m'
BRED='\033[1;31m'
NC='\033[0m' # No Color
BOLD='\033[1m'
print_heading() {
echo ""
echo -e "${BCYAN}# -----------------------------------------------------------------------"
echo "# ${1}"
echo -e "# -----------------------------------------------------------------------${NC}"
}
print_bold() {
echo -e "${BOLD}${1}${NC}"
}
# -----------------------------------------------------------------------------
print_heading "Upgrading R and Python libraries to Jammy"
# -----------------------------------------------------------------------------
cat << EOF
This script will update all of your R and Python libraries to be compatbile
with Ubuntu 22.04 Jammy. It take several minutes to run based on the number
of libraries you have installed. Do NOT interupt the script while it is running.
It will udpate all your R and Python packages in your home directory:
~/R
~/.local/lib/python3.*
It will not update any R and Python packages your have install in renv (R) or
venv (Python) environments.
EOF
echo -e "${BRED}Continue? (Y/N):${NC}"
read -p "" confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
# -----------------------------------------------------------------------------
print_heading "System checks"
# -----------------------------------------------------------------------------
print_bold "Verifying operating system is Ubuntu 22.04 Jammy..."
EXPECTED_OS="Jammy"
cat /etc/*release | grep "$EXPECTED_OS" || { echo -e "${BRED}Error: You are not on Jammy, exiting!"; exit 1; }
# -----------------------------------------------------------------------------
print_heading " Reinstall R packages for Ubuntu Jammy"
# -----------------------------------------------------------------------------
# | R | Old Image | New Image |
# | ----- | --------- | --------- |
# | 4.2.3 | | X |
# | 4.2.0 | X | |
# | 4.1.3 | | X |
# | 4.1.2 | X | |
# | 4.0.5 | | X |
R_VERSIONS=("4.0.5" "4.1.3" "4.2.3")
user=$USER
echo The current user is: $user
# Capture existing packages for each R version
for v in "${R_VERSIONS[@]}";
do
print_bold "Capturing packages for R ${v}..."
R_LIBS_USER="${HOME}/R/x86_64-pc-linux-gnu-library/${v:0:3}"
"/opt/R/${v}/bin/R" --quiet -e "my_packages <- installed.packages(lib.loc = c('"${R_LIBS_USER}"')); write.csv(my_packages, '$user-r-packages-${v}.csv')"
done
# Delete all of the existing R packages
print_bold "Deleting existing R packages from Ubuntu Bionic..."
rm -rf "${HOME}/R"
# Reinstall R packages
for v in "${R_VERSIONS[@]}";
do
print_bold "Capturing packages for R ${v}..."
R_LIBS_USER="${HOME}/R/x86_64-pc-linux-gnu-library/${v:0:3}"
mkdir -p "${R_LIBS_USER}"
"/opt/R/${v}/bin/R" --quiet -e "my_packages <- read.csv('$user-r-packages-${v}.csv'); install.packages(my_packages\$Package, lib = '"${R_LIBS_USER}"', repos = c('https://colorado.posit.co/rspm/all/__linux__/jammy/latest'))"
done
# Delete CSV files for cleanup
for v in "${R_VERSIONS[@]}";
do
print_bold "Delete CSV files for cleanup..."
rm -rf "/usr/home/tmp/$user-r-packages-${v}.csv"
done
# -----------------------------------------------------------------------------
print_heading " Reinstall Python packages for Ubuntu Jammy"
# -----------------------------------------------------------------------------
# | Python | Old Image | New Image |
# | ------- | --------- | --------- |
# | 3.10.11 | | X |
# | 3.10.4 | X | |
# | 3.9.6 | X | |
# | 3.9.14 | | X |
# | 3.8.15 | | X |
PYTHON_VERSIONS=("3.8.15" "3.9.14" "3.10.11")
# Create requirements.txt for each python version
for v in "${PYTHON_VERSIONS[@]}";
do
print_bold "Capturing packages for Python ${v}..."
"/opt/python/${v}/bin/python" -m pip freeze --user > "$user-requirements-${v}.txt"
done
# Delete all of the existing Python packages
print_bold "Deleting existing Python packages from Ubuntu Bionic..."
rm -rf "$(find "${HOME}/.local/lib/" -maxdepth 1 -type d -name 'python3.*')"
# Reinstall Python packages
for v in "${PYTHON_VERSIONS[@]}";
do
print_bold "Reinstalling Python packages Python ${v}..."
"/opt/python/${v}/bin/python" -m pip install -r "$user-requirements-${v}.txt"
done
# Delete Requirements files for cleanup
for v in "${PYTHON_VERSIONS[@]}";
do
print_bold "Delete Requriements files for cleanup..."
rm -rf "/usr/home/tmp/$user-requirements-${v}.txt"
done
# -----------------------------------------------------------------------------
print_heading " ✅ Upgrade Complete ✅"
# -----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment