-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_notebooks.sh
More file actions
executable file
·132 lines (104 loc) · 5.06 KB
/
install_notebooks.sh
File metadata and controls
executable file
·132 lines (104 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
export USER="$(whoami)"
export LABS="/home/$USER"
export RESOURCES="$LABS/resources"
export SAVES="$LABS/saves"
export EDUCATION="/home/$USER/.education"
# Check if the student is running as root.
if [ "$EUID" -eq 0 ]; then
echo "Please do not run this script as sudo."
exit 1
fi
### NOTICE: rsync is required for this script to run smoothly. SPHERE's current Ubuntu has reached EOL,
### and rsync will not install. These next few commands are to use an ESR version of rsync.
sudo bash -c 'CODENAME=$( . /etc/os-release; echo "$UBUNTU_CODENAME" ); cat > /etc/apt/sources.list <<EOF
deb http://archive.ubuntu.com/ubuntu ${CODENAME} main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu ${CODENAME}-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu ${CODENAME}-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu ${CODENAME}-security main restricted universe multiverse
EOF' >/dev/null 2>&1
sudo apt-get -qq clean
# Define the directory where the repository should be checked out.
REPO_URL="https://github.com/UMDLARS/sphere"
# Installing some dependencies.
echo "Installing dependencies."
sudo apt-get -qq update
sudo apt-get -qq install git rsync >/dev/null
# sudo is required for these, since the notebooks are ran as root. Hiding the warning about installing packages as root.
echo "Installing required Jupyter extensions."
# NOTE: New changes to SPHERE moved stuff into a venv. We will need to apply some changes before we can install this.
# Making a change in a config file to fix a known issue, enabling the venv, then installing the packages.
# This "sed" command is a workaround, but apparently doesn't fix the issue when installing ipywidgets.
sudo sed -i "s|include-system-site-packages = false|include-system-site-packages = true|g" /usr/local/jupyter/venv/pyvenv.cfg
source /usr/local/jupyter/venv/bin/activate
pip install -q ipywidgets jupyterlab_widgets 2>/dev/null
deactivate
echo "Beginning installation of notebooks."
# Use a temporary directory for cloning.
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR" || exit
# Clone the repository.
git clone "$REPO_URL"
if [ $? -ne 0 ]; then
echo -e "\033[0;31mFailed to clone the repository. Exiting.\033[0m"
exit 1
fi
cd sphere || exit
# Checking to see if the notebooks have already been made.
if [ $(find "$LABS" -maxdepth 1 -type f -name "*.ipynb" | wc -l) -eq 8 ]; then
echo -e "\033[0;31mYour notebooks already exist. Updating your notebooks...\033[0m"
else
echo -e "\033[0;31mSome (or all) of your notebooks appear to be missing. Adding them...\033[0m"
fi
# Saving the student's logs.
[ -f "$EDUCATION/${USER}_logs.txt" ] && mv "$EDUCATION/${USER}_logs.txt" /tmp
# Excluding the following files:
## saves/ so that students don't lose progress.
## pass.txt so that students don't lose their configuration.
## .local/ so that the kernel doesn't break when running the command.
## All hidden files (.*) so that we don't break SSH and Jupyter configs.
sudo rsync -a --delete \
--exclude='saves/' \
--exclude='pass.txt' \
--exclude='.local/' \
--exclude='.*' \
--exclude='.*/' \
notebooks/ ~ >/dev/null
# Ensure 'saves/' exists.
mkdir -p $LABS/saves
# Move the lab resources.
if [ -d "/home/$USER/.education" ]; then
echo -e "\033[0;31mLab resources for your notebooks already exist. Applying updates...\033[0m"
# Delete the education directory so that it can be updated. mv will not work if there are already files.
sudo rm -rf /home/$USER/.education/*
else
echo -e "\033[0;31mLab resources do not exist on your XDC. Creating them...\033[0m"
sudo mkdir -p "/home/$USER/.education"
sudo chown -R "$USER:$USER" "/home/$USER/.education"
fi
# Find and copy all directories ending with 'jup' to the $EDUCATION directory.
for dir in $(find . -maxdepth 1 -type d -name "*jup"); do
dir_name=$(basename "$dir")
mv $dir_name $EDUCATION
done
# Move the grades back for the student.
[ -f /tmp/"${USER}_logs.txt" ] && mv /tmp/"${USER}_logs.txt" $EDUCATION
# Finally, copy all of the notebook function files (should be four of them) into the student's XDC.
sudo mv runlab startexp stopexp runr /home
sudo mv grader.py /home/$USER/.education
sudo chmod a+x /home/runlab /home/startexp /home/stopexp /home/runr /home/$USER/.education/grader.py
# Copying over a "functions" file that SPHERE uses. This fixes a timeout error that runr encounters.
sudo cp /share/functions /home
# Cleanup temporary directory.
rm -rf "$TEMP_DIR"
# Configure all labs to work with the current username.
pushd "$LABS" > /dev/null 2>&1
for notebook in *.ipynb; do
sed -i "s/USERNAME_GOES_HERE/$USER/g" "$notebook"
done
# Change the USERNAME_GOES_HERE occurrence in the port forwarding script.
find /home/$USER/resources/ -name "*.py" -exec sed -i "s|USERNAME_GOES_HERE|$USER|g" {} \+
popd > /dev/null 2>&1
# And finally, for the install scripts.
find /home/$USER/.education -type f -name install -exec sed -i "s/USERNAME_GOES_HERE/$(whoami)/g" {} +
echo -e "\033[0;32mDone. You can find your notebooks in $LABS. Please refresh your browser's tab before starting a lab.\033[0m"