From e3113f369d2612f0f3dd9dfb55c592c8eef1bdbd Mon Sep 17 00:00:00 2001 From: jakinto Date: Fri, 7 Jun 2024 17:05:14 +0200 Subject: [PATCH] Add support for the new JupyterLab on Amazon SageMaker Studio (#4664) * Added support for new the JupyterLab on Amazon SageMaker Studio * Added support for the new JupyterLab on Amazon SageMaker Studio --------- Co-authored-by: Jacinto Esteban --- aws_sagemaker_studio/streamlit_demo/run.sh | 14 +- aws_sagemaker_studio/streamlit_demo/setup.sh | 12 +- aws_sagemaker_studio/streamlit_demo/status.sh | 137 +++++++++--------- 3 files changed, 91 insertions(+), 72 deletions(-) diff --git a/aws_sagemaker_studio/streamlit_demo/run.sh b/aws_sagemaker_studio/streamlit_demo/run.sh index 3c7f6456e0..08525a75a3 100644 --- a/aws_sagemaker_studio/streamlit_demo/run.sh +++ b/aws_sagemaker_studio/streamlit_demo/run.sh @@ -6,6 +6,10 @@ GREEN='\033[1;32m' NC='\033[0m' S3_PATH=$1 +echo "***************************************************************************" +echo "*** If you are NOT on Amazon SageMaker Studio Classic use 'bash run.sh' ***" +echo "***************************************************************************" + # Run the Streamlit app and save the output to "temp.txt" streamlit run app.py > temp.txt & @@ -17,8 +21,6 @@ sleep 5 PORT=$(grep "Network URL" temp.txt | awk -F':' '{print $NF}' | awk '{print $1}' | tail -c 5) echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Port Number ${PORT}" - - # Get Studio domain information DOMAIN_ID=$(jq .DomainId /opt/ml/metadata/resource-metadata.json || exit 1) RESOURCE_NAME=$(jq .ResourceName /opt/ml/metadata/resource-metadata.json || exit 1) @@ -104,8 +106,14 @@ fi echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Studio Url ${STUDIO_URL}" +JUPYTER_TYPE= +if grep -q '^NAME="Ubuntu"' /etc/os-release; then + JUPYTER_TYPE="jupyterlab" +else + JUPYTER_TYPE="jupyter" +fi -link="${STUDIO_URL}/jupyter/${RESOURCE_NAME}/proxy/${PORT}/" +link="${STUDIO_URL}/${JUPYTER_TYPE}/${RESOURCE_NAME}/proxy/${PORT}/" echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Starting Streamlit App" echo -e "${CYAN}${CURRENTDATE}: [INFO]: ${GREEN}${link}${NC}" diff --git a/aws_sagemaker_studio/streamlit_demo/setup.sh b/aws_sagemaker_studio/streamlit_demo/setup.sh index 5b6c41456a..b1a7222755 100644 --- a/aws_sagemaker_studio/streamlit_demo/setup.sh +++ b/aws_sagemaker_studio/streamlit_demo/setup.sh @@ -1,4 +1,10 @@ pip install --no-cache-dir -r requirements.txt -sudo yum install -y iproute -sudo yum install -y jq -sudo yum install -y lsof \ No newline at end of file +if grep -q '^NAME="Ubuntu"' /etc/os-release; then + sudo apt-get install -y iproute2 + sudo apt-get install -y jq + sudo apt-get install -y lsof +else + sudo yum install -y iproute + sudo yum install -y jq + sudo yum install -y lsof +fi diff --git a/aws_sagemaker_studio/streamlit_demo/status.sh b/aws_sagemaker_studio/streamlit_demo/status.sh index e7c1c4c760..94477b1733 100644 --- a/aws_sagemaker_studio/streamlit_demo/status.sh +++ b/aws_sagemaker_studio/streamlit_demo/status.sh @@ -8,6 +8,9 @@ GREEN='\033[1;32m' NC='\033[0m' S3_PATH=$1 +echo "******************************************************************************" +echo "*** If you are NOT on Amazon SageMaker Studio Classic use 'bash status.sh' ***" +echo "******************************************************************************" # Get Studio domain information DOMAIN_ID=$(jq .DomainId /opt/ml/metadata/resource-metadata.json || exit 1) @@ -26,7 +29,6 @@ REGION=$(echo "${RESOURCE_ARN_ARRAY[3]}") # Check if it's Collaborative Space SPACE_NAME=$(jq .SpaceName /opt/ml/metadata/resource-metadata.json || exit 1) - # Find the process IDs of all running Streamlit instances streamlit_pids=$(pgrep streamlit) @@ -44,81 +46,84 @@ for pid in $streamlit_pids; do done - -echo "These are the Streamlit Apps Currently Running: " -# Loop through the array and print each port number -for PORT in "${port_array[@]}"; do +# if it's not a collaborative space +if [ -z "$SPACE_NAME" ] || [ $SPACE_NAME == "null" ] ; +then + # If it's a user-profile access + echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Domain Id ${DOMAIN_ID}" + STUDIO_URL="https://${DOMAIN_ID}.studio.${REGION}.sagemaker.aws" - # if it's not a collaborative space - if [ -z "$SPACE_NAME" ] || [ $SPACE_NAME == "null" ] ; - then - # If it's a user-profile access - STUDIO_URL="https://${DOMAIN_ID}.studio.${REGION}.sagemaker.aws" - - # It is a collaborative space - else +# It is a collaborative space +else - SEM=true - SPACE_ID= + SEM=true + SPACE_ID= - # Check if Space Id was previously configured - if [ -f /tmp/space-metadata.json ]; then - SAVED_SPACE_ID=$(jq .SpaceId /tmp/space-metadata.json || exit 1) - SAVED_SPACE_ID=`sed -e 's/^"//' -e 's/"$//' <<< "$SAVED_SPACE_ID"` + # Check if Space Id was previously configured + if [ -f /tmp/space-metadata.json ]; then + SAVED_SPACE_ID=$(jq .SpaceId /tmp/space-metadata.json || exit 1) + SAVED_SPACE_ID=`sed -e 's/^"//' -e 's/"$//' <<< "$SAVED_SPACE_ID"` - if [ -z "$SAVED_SPACE_ID" ] || [ $SAVED_SPACE_ID == "null" ]; then - ASK_INPUT=true - else - ASK_INPUT=false - fi - else + if [ -z "$SAVED_SPACE_ID" ] || [ $SAVED_SPACE_ID == "null" ]; then ASK_INPUT=true + else + ASK_INPUT=false fi - - # If Space Id is not available, ask for it - while [[ $SPACE_ID = "" ]] ; do - # If Space Id already configured, skeep the ask - if [ "$ASK_INPUT" = true ]; then - echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Please insert the Space Id from your url. e.g. https://${GREEN}${NC}.studio.${REGION}.sagemaker.aws/jupyter/default/lab" - read SPACE_ID - SEM=true - else - SPACE_ID=$SAVED_SPACE_ID - fi - - if ! [ -z "$SPACE_ID" ] && ! [ $SPACE_ID == "null" ] ; - then - while $SEM; do - echo "${SPACE_ID}" - read -p "Should this be used as Space Id? (y/N) " yn - case $yn in - [Yy]* ) - - jq -n --arg space_id $SPACE_ID '{"SpaceId":$space_id}' > /tmp/space-metadata.json - - STUDIO_URL="https://${SPACE_ID}.studio.${REGION}.sagemaker.aws" - - SEM=false - ;; - [Nn]* ) - SPACE_ID= - ASK_INPUT=true - SEM=false - ;; - * ) echo "Please answer yes or no.";; - esac - done - fi - done + else + ASK_INPUT=true fi - link="${STUDIO_URL}/jupyter/${RESOURCE_NAME}/proxy/${PORT}/" + # If Space Id is not available, ask for it + while [[ $SPACE_ID = "" ]] ; do + # If Space Id already configured, skeep the ask + if [ "$ASK_INPUT" = true ]; then + echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Please insert the Space Id from your url. e.g. https://${GREEN}${NC}.studio.${REGION}.sagemaker.aws/jupyter/default/lab" + read SPACE_ID + SEM=true + else + SPACE_ID=$SAVED_SPACE_ID + fi - echo -e "${GREEN}${link}${NC}" -done -exit 0 + if ! [ -z "$SPACE_ID" ] && ! [ $SPACE_ID == "null" ] ; + then + while $SEM; do + echo "${SPACE_ID}" + read -p "Should this be used as Space Id? (y/N) " yn + case $yn in + [Yy]* ) + + jq -n --arg space_id $SPACE_ID '{"SpaceId":$space_id}' > /tmp/space-metadata.json + + STUDIO_URL="https://${SPACE_ID}.studio.${REGION}.sagemaker.aws" + + SEM=false + ;; + [Nn]* ) + SPACE_ID= + ASK_INPUT=true + SEM=false + ;; + * ) echo "Please answer yes or no.";; + esac + done + fi + done fi +JUPYTER_TYPE= +if grep -q '^NAME="Ubuntu"' /etc/os-release; then + JUPYTER_TYPE="jupyterlab" +else + JUPYTER_TYPE="jupyter" +fi +echo "These are the Streamlit Apps Currently Running: " +# Loop through the array and print each port number +for PORT in "${port_array[@]}"; do + link="${STUDIO_URL}/${JUPYTER_TYPE}/${RESOURCE_NAME}/proxy/${PORT}/" + + echo -e "${CYAN}${CURRENTDATE}: [INFO]: ${GREEN}${link}${NC}" - +done +exit 0 +fi