@@ -14,7 +14,7 @@ RSTUDIO_USER="$2"
1414
1515# Get directory in which this script is stored and define URL from which to download goofys
1616FILES_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " > /dev/null 2>&1 && pwd ) "
17- GOOFYS_URL=" https://github.com/kahing/goofys/releases/download/v0.21 .0/goofys"
17+ GOOFYS_URL=" https://github.com/kahing/goofys/releases/download/v0.24 .0/goofys"
1818
1919# Define a function to determine what type of environment this is (RStudio, or EC2 Linux)
2020env_type () {
@@ -24,23 +24,74 @@ env_type() {
2424 elif [ -f " /usr/bin/nextflow" ]
2525 then
2626 printf " nextflow"
27+ elif [ -d " /home/ec2-user/SageMaker" ]
28+ then
29+ printf " sagemaker"
2730 else
2831 printf " ec2-linux"
2932 fi
3033}
3134
35+ # Define a function to update Jupyter configuration files
36+ update_jupyter_config () {
37+
38+ config_file=" $1 "
39+
40+ # HACK: Update the default SessionManager class used by Jupyter notebooks
41+ # so that it runs the S3 mount script the first time sessions are listed
42+ cat << EOF | cut -b5- >> "$config_file "
43+
44+ import subprocess
45+ from notebook.services.sessions.sessionmanager import SessionManager as BaseSessionManager
46+
47+ class SessionManager(BaseSessionManager):
48+ def list_sessions(self, *args, **kwargs):
49+ """Override default list_sessions() method"""
50+ self.mount_studies()
51+ result = super(SessionManager, self).list_sessions(*args, **kwargs)
52+ return result
53+
54+ def mount_studies(self):
55+ """Execute mount_s3.sh if it hasn't already been run"""
56+ if not hasattr(self, 'studies_mounted'):
57+ mounting_result = subprocess.run(
58+ "mount_s3.sh",
59+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT
60+ )
61+
62+ # Log results
63+ if mounting_result.stdout:
64+ for line in mounting_result.stdout.decode("utf-8").split("\n"):
65+ if line: # Skip empty lines
66+ self.log.info(line)
67+
68+ self.studies_mounted = True
69+
70+ c.NotebookApp.session_manager_class = SessionManager
71+ EOF
72+ }
73+
3274# Install dependencies
33- yum install -y jq-1.5
34- curl -LSs -o " /usr/local/bin/goofys" " $GOOFYS_URL "
75+ echo " Installing JQ"
76+ sudo mv " ${FILES_DIR} /offline-packages/jq-1.5-linux64" " /usr/local/bin/jq"
77+ chmod +x " /usr/local/bin/jq"
78+ echo " Finish installing jq"
79+
80+ echo " Copying Goofys from bootstrap.sh"
81+ cp " ${FILES_DIR} /offline-packages/goofys" /usr/local/bin/goofys
3582chmod +x " /usr/local/bin/goofys"
3683
3784# Install ec2 instance connect agent
3885sudo yum install ec2-instance-connect-1.1
3986
4087# Create S3 mount script and config file
88+ echo " Mounting S3"
4189chmod +x " ${FILES_DIR} /bin/mount_s3.sh"
4290ln -s " ${FILES_DIR} /bin/mount_s3.sh" " /usr/local/bin/mount_s3.sh"
4391printf " %s" " $S3_MOUNTS " > " /usr/local/etc/s3-mounts.json"
92+ echo " Finish mounting S3"
93+
94+ OS_VERSION=` cat /etc/os-release | grep VERSION= | sed ' s/VERSION="//' | sed ' s/"//' `
4495
4596# Apply updates to environments based on environment type
4697case " $( env_type) " in
@@ -56,6 +107,31 @@ case "$(env_type)" in
56107 yum install -y fuse-2.9.2
57108 printf " \n# Mount S3 study data\nmount_s3.sh\n\n" >> " /home/ec2-user/.bash_profile"
58109 ;;
110+ " sagemaker" ) # Update config and restart Jupyter
111+ if [ $OS_VERSION = ' 2' ]
112+ then
113+ echo " Installing fuse for AL2"
114+ cd " ${FILES_DIR} /offline-packages/sagemaker/fuse-2.9.4_AL2"
115+ sudo yum --disablerepo=* localinstall -y * .rpm
116+ echo " Finish installing fuse"
117+ echo " Installing boto3 for AL2"
118+ cd " ${FILES_DIR} /offline-packages/sagemaker/boto3"
119+ sudo yum --disablerepo=* localinstall -y python2-boto3-1.4.4-1.amzn2.noarch.rpm
120+ echo " Finish installing boto3"
121+ else
122+ echo " Installing fuse for AL1"
123+ cd " ${FILES_DIR} /offline-packages/sagemaker/fuse-2.9.4"
124+ sudo yum --disablerepo=* localinstall -y * .rpm
125+ echo " Finish installing fuse"
126+ fi
127+ update_jupyter_config " /home/ec2-user/.jupyter/jupyter_notebook_config.py"
128+ if [ $OS_VERSION = ' 2' ]
129+ then
130+ systemctl restart jupyter-server
131+ else
132+ initctl restart jupyter-server --no-wait
133+ fi
134+ ;;
59135esac
60136
61137exit 0
0 commit comments