Skip to content

Commit

Permalink
Merge pull request #139 from nmfs-opensci/eeholmes-patch-1
Browse files Browse the repository at this point in the history
Update install-r-package.sh
  • Loading branch information
eeholmes authored Nov 4, 2024
2 parents b6145bd + 760199a commit 24af2c9
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 78 deletions.
9 changes: 6 additions & 3 deletions appendix
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
USER root

# Clean up extra files in ${REPO_DIR}
RUN rm -rf ${REPO_DIR}/book ${REPO_DIR}/docs

# repo2docker does not set this. This is the default env in repo2docker type images
ENV CONDA_ENV=notebook
# Tell applications where to open desktop apps - this allows notebooks to pop open GUIs
Expand Down Expand Up @@ -38,9 +41,6 @@ RUN mandb
RUN cp ${REPO_DIR}/custom_jupyter_server_config.json ${NB_PYTHON_PREFIX}/etc/jupyter/jupyter_server_config.d/
RUN cp ${REPO_DIR}/custom_jupyter_server_config.json ${NB_PYTHON_PREFIX}/etc/jupyter/jupyter_notebook_config.d/

# Clean up extra files in ${REPO_DIR}
RUN rm -rf ${REPO_DIR}/book ${REPO_DIR}/docs

# Copy scripts into /pyrocket_scripts directory in the image
RUN mkdir -p /pyrocket_scripts && cp -r ${REPO_DIR}/scripts /pyrocket_scripts

Expand All @@ -51,6 +51,9 @@ RUN chown -R root:root /pyrocket_scripts && \
# Convert NB_USER to ENV (from ARG) so that it passes to the child dockerfile
ENV NB_USER=${NB_USER}

# Copy the child repo files into childimage so they are available to scripts
ONBUILD COPY --chown=${NB_USER}:${NB_USER} . ${REPO_DIR}/childimage

# Revert to default user and home as pwd
USER ${NB_USER}
WORKDIR ${HOME}
9 changes: 0 additions & 9 deletions scripts/copy-files.sh

This file was deleted.

39 changes: 28 additions & 11 deletions scripts/install-apt-packages.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
#!/bin/bash
/scripts/copy-files.sh

echo "Checking for apt.txt..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -f "apt.txt"; then
package_list=$(grep -v '^\s*#' apt.txt | grep -v '^\s*$' | sed 's/\r//g; s/#.*//; s/^[[:space:]]*//; s/[[:space:]]*$//' | awk '{$1=$1};1')
apt-get update --fix-missing > /dev/null
apt-get install --yes --no-install-recommends $package_list
apt-get autoremove --purge
apt-get clean
rm -rf /var/lib/apt/lists/*
# Required User: root

echo "Running install-apt-packages.sh"
echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for apt.txt in ${REPO_DIR}/childimage/..."
if test -f "apt.txt"; then
# Check if the script is run as root
if [[ $(id -u) -ne 0 ]]; then
echo " Error: This script must be run as root." >&2 # Output error message to standard error
exit 1 # Exit with a non-zero status to indicate failure
fi

echo " Running install-apt-packages.sh as root. Proceeding with installation..."

package_list=$(grep -v '^\s*#' apt.txt | grep -v '^\s*$' | sed 's/\r//g; s/#.*//; s/^[[:space:]]*//; s/[[:space:]]*$//' | awk '{$1=$1};1')
apt-get update --fix-missing > /dev/null
apt-get install --yes --no-install-recommends $package_list
apt-get autoremove --purge
apt-get clean
rm -rf /var/lib/apt/lists/*
else
echo " No apt.txt found. Skipping package installation."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping script."
fi
58 changes: 41 additions & 17 deletions scripts/install-conda-packages.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
#!/bin/bash
/scripts/copy-files.sh
# Required User: NB_USER

echo "Checking for conda-lock.yml or environment.yml..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -f "conda-lock.yml"; then
echo "Using conda-lock.yml"
conda-lock install --name ${CONDA_ENV}
pip install --no-deps jupyter-remote-desktop-proxy
elif test -f "environment.yml"; then
echo "Using environment.yml"
mamba env update --name ${CONDA_ENV} -f environment.yml
pip install --no-deps jupyter-remote-desktop-proxy
fi
mamba clean -yaf
find ${CONDA_DIR} -follow -type f -name '*.a' -delete
find ${CONDA_DIR} -follow -type f -name '*.js.map' -delete
if ls ${NB_PYTHON_PREFIX}/lib/python*/site-packages/bokeh/server/static > /dev/null 2>&1; then
find ${NB_PYTHON_PREFIX}/lib/python*/site-packages/bokeh/server/static -follow -type f -name '*.js' ! -name '*.min.js' -delete
echo "Running install-conda-packages.sh"

echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for conda-lock.yml or environment.yml in ${REPO_DIR}/childimage/..."
if test -f "conda-lock.yml" || test -f "environment.yml"; then
# Switch to NB_USER only if the relevant files exist
if [[ $(id -u) -eq 0 ]]; then
echo " Switching to ${NB_USER} to run install-conda-packages.sh"
exec su "${NB_USER}" -c "/bin/bash $0" # Switches to NB_USER and reruns the script
fi

if test -f "conda-lock.yml"; then
echo " Using conda-lock.yml"
conda-lock install --name ${CONDA_ENV}
pip install --no-deps jupyter-remote-desktop-proxy
INSTALLATION_HAPPENED=true
elif test -f "environment.yml"; then
echo " Using environment.yml"
mamba env update --name ${CONDA_ENV} -f environment.yml
pip install --no-deps jupyter-remote-desktop-proxy
INSTALLATION_HAPPENED=true
fi

# Only run cleanup if installation occurred
if [ "$INSTALLATION_HAPPENED" = true ]; then
mamba clean -yaf
find ${CONDA_DIR} -follow -type f -name '*.a' -delete
find ${CONDA_DIR} -follow -type f -name '*.js.map' -delete
if ls ${NB_PYTHON_PREFIX}/lib/python*/site-packages/bokeh/server/static > /dev/null 2>&1; then
find ${NB_PYTHON_PREFIX}/lib/python*/site-packages/bokeh/server/static -follow -type f -name '*.js' ! -name '*.min.js' -delete
fi
fi
else
echo " No conda-lock.yml or environment.yml found. Skipping installation."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping script."
fi
27 changes: 22 additions & 5 deletions scripts/install-pip-packages.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#!/bin/bash
/scripts/copy-files.sh
# Required User: NB_USER

echo "Checking for pip requirements.txt..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -f "requirements.txt"; then
${NB_PYTHON_PREFIX}/bin/pip install --no-cache -r requirements.txt
echo "Running install-pip-packages.sh"

echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for requirements.txt in ${REPO_DIR}/childimage/..."
if test -f "requirements.txt"; then
# Switch to NB_USER only if requirements.txt exists
if [[ $(id -u) -eq 0 ]]; then
echo "Switching to ${NB_USER} to run install-pip-packages.sh"
exec su "${NB_USER}" -c "/bin/bash $0" # Switches to NB_USER and reruns the script
fi

echo " Installing pip packages from requirements.txt as ${NB_USER}..."
${NB_PYTHON_PREFIX}/bin/pip install --no-cache -r requirements.txt
else
echo " No requirements.txt found. Skipping pip installation."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping script."
fi
28 changes: 22 additions & 6 deletions scripts/install-r-package.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#!/bin/bash
/scripts/copy-files.sh
# Required User: NB_USER

echo "Checking for install.R..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -f "install.R"; then
echo "Using install.R"
Rscript install.R
echo "Running install-r-packages.sh"

echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for install.R in ${REPO_DIR}/childimage/..."
if test -f "install.R"; then
# Switch to NB_USER only if install.R exists
if [[ $(id -u) -eq 0 ]]; then
echo "Switching to ${NB_USER} to run install-r-packages.sh"
exec su "${NB_USER}" -c "/bin/bash $0" # Switches to NB_USER and reruns the script
fi

echo " Using install.R to install R packages as ${NB_USER}..."
Rscript install.R
else
echo " No install.R found. Skipping R package installation."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping script."
fi
40 changes: 28 additions & 12 deletions scripts/run-postbuild.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
#!/bin/bash
/scripts/copy-files.sh

echo "Checking for postBuild..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -f "postBuild"; then
chmod +x postBuild
./postBuild
rm -rf /tmp/*
rm -rf ${HOME}/.cache ${HOME}/.npm ${HOME}/.yarn
rm -rf ${NB_PYTHON_PREFIX}/share/jupyter/lab/staging
find ${CONDA_DIR} -follow -type f -name '*.a' -delete
find ${CONDA_DIR} -follow -type f -name '*.js.map' -delete

echo "Running run-postbuild.sh"

echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for postBuild..."
if test -f "postBuild"; then
# Check the user and output which user the script is running as
if [[ $(id -u) -eq 0 ]]; then
echo " Running run-postbuild.sh as root."
else
echo " Running run-postbuild.sh as ${NB_USER}."
fi

chmod +x postBuild
./postBuild
rm -rf /tmp/*
rm -rf ${HOME}/.cache ${HOME}/.npm ${HOME}/.yarn
rm -rf ${NB_PYTHON_PREFIX}/share/jupyter/lab/staging
find ${CONDA_DIR} -follow -type f -name '*.a' -delete
find ${CONDA_DIR} -follow -type f -name '*.js.map' -delete
else
echo " No postBuild file found. Skipping execution."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping script."
fi
44 changes: 34 additions & 10 deletions scripts/setup-desktop.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
#!/bin/bash
# Ensure files are copied before proceeding
/scripts/copy-files.sh

echo "Checking for Desktop directory..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -d Desktop; then
mkdir -p "${REPO_DIR}/Desktop"
cp -r Desktop/* "${REPO_DIR}/Desktop/" 2>/dev/null
chmod +x "${REPO_DIR}/desktop.sh"
"${REPO_DIR}/desktop.sh"
# Required User: root

echo "Running setup-desktop.sh"

echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for Desktop directory..."
if test -d "${REPO_DIR}/childimage/Desktop"; then
# Check if the script is run as root
if [[ $(id -u) -ne 0 ]]; then
echo " Error: This script must be run as root." >&2 # Output error message to standard error
exit 1 # Exit with a non-zero status to indicate failure
fi

echo " Running setup-desktop.sh as root. Proceeding with installation..."

mkdir -p "${REPO_DIR}/Desktop"
cp -r ${REPO_DIR}/childimage/Desktop/* "${REPO_DIR}/Desktop/" 2>/dev/null

# Check if desktop.sh exists before executing
if test -f "${REPO_DIR}/desktop.sh"; then
echo " Running ${REPO_DIR}/desktop.sh."
chmod +x "${REPO_DIR}/desktop.sh"
"${REPO_DIR}/desktop.sh"
else
echo " Warning: desktop.sh not found. Skipping execution."
fi
else
echo " No Desktop directory found in ${REPO_DIR}/childimage/. Skipping setup."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping script."
fi
26 changes: 21 additions & 5 deletions scripts/setup-start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
#!/bin/bash
/scripts/copy-files.sh
# Required User: NB_USER

echo "Checking for start..."
cd "${REPO_DIR}/childimage/" || exit 1
if test -f "start"; then
chmod +x start
echo "Running setup-start.sh"

echo " Checking for ${REPO_DIR}/childimage/..."
if [ -d "${REPO_DIR}/childimage/" ]; then
cd "${REPO_DIR}/childimage/" || exit 1

echo " Checking for start in ${REPO_DIR}/childimage/..."
if test -f "start"; then
echo " start found in ${REPO_DIR}/childimage/."
# Switch to NB_USER only if the start file exists
if [[ $(id -u) -eq 0 ]]; then
echo " Switching to ${NB_USER} to run setup-start.sh"
exec su "${NB_USER}" -c "/bin/bash $0" # Switches to NB_USER and reruns the script
fi
chmod +x start
else
echo " No start file found in ${REPO_DIR}/childimage/. Skipping."
fi
else
echo " Directory ${REPO_DIR}/childimage/ does not exist. Skipping."
fi

0 comments on commit 24af2c9

Please sign in to comment.