Skip to content

Commit

Permalink
feature: Add sagemaker-code-editor package (aws#185)
Browse files Browse the repository at this point in the history
Co-authored-by: Clayton Parnell <[email protected]>
  • Loading branch information
2 people authored and nikhilumeshsargur committed Apr 2, 2024
1 parent f3563d9 commit 56be466
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
.idea
.DS_Store
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
conda-forge::sagemaker-code-editor[version='>=1.0.0']
conda-forge::amazon_sagemaker_sql_editor[version='>=0.1.0,<2']
conda-forge::amazon-sagemaker-sql-magic[version='>=0.1.0,<2']
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
conda-forge::sagemaker-code-editor[version='>=1.0.0']
conda-forge::amazon_sagemaker_sql_editor[version='>=0.1.0,<2']
conda-forge::amazon-sagemaker-sql-magic[version='>=0.1.0,<2']
37 changes: 31 additions & 6 deletions template/v1/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ RUN apt-get update && \
:
RUN echo "source /usr/local/bin/_activate_current_env.sh" | tee --append /etc/profile

# CodeEditor - create server, user data dirs
RUN mkdir -p /opt/amazon/sagemaker/sagemaker-code-editor-server-data /opt/amazon/sagemaker/sagemaker-code-editor-user-data \
&& chown $MAMBA_USER:$MAMBA_USER /opt/amazon/sagemaker/sagemaker-code-editor-server-data /opt/amazon/sagemaker/sagemaker-code-editor-user-data

# Merge in OS directory tree contents.
RUN mkdir -p ${DIRECTORY_TREE_STAGE_DIR}
COPY dirs/ ${DIRECTORY_TREE_STAGE_DIR}/
RUN rsync -a ${DIRECTORY_TREE_STAGE_DIR}/ / && \
rm -rf ${DIRECTORY_TREE_STAGE_DIR}

# CodeEditor - download the extensions
RUN mkdir -p /etc/code-editor/extensions && \
while IFS= read -r url; do \
echo "Downloading extension from ${url}..." && \
wget --no-check-certificate -P /etc/code-editor/extensions "${url}"; \
done < /etc/code-editor/extensions.txt

USER $MAMBA_USER
COPY --chown=$MAMBA_USER:$MAMBA_USER $ENV_IN_FILENAME *.in /tmp/

Expand All @@ -64,6 +81,17 @@ RUN micromamba install -y --name base --file /tmp/$ENV_IN_FILENAME && \
ARG MAMBA_DOCKERFILE_ACTIVATE=1
RUN sudo ln -s $(which python3) /usr/bin/python

# Configure CodeEditor - Install extensions and set preferences
RUN \
extensionloc=/opt/amazon/sagemaker/sagemaker-code-editor-server-data/extensions && mkdir -p "${extensionloc}" \
# Loop through all vsix files in /etc/code-editor/extensions and install them
&& for ext in /etc/code-editor/extensions/*.vsix; do \
echo "Installing extension ${ext}..."; \
sagemaker-code-editor --install-extension "${ext}" --extensions-dir "${extensionloc}" --server-data-dir /opt/amazon/sagemaker/sagemaker-code-editor-server-data --user-data-dir /opt/amazon/sagemaker/sagemaker-code-editor-user-data; \
done \
# Copy the settings
&& cp /etc/code-editor/code_editor_machine_settings.json /opt/amazon/sagemaker/sagemaker-code-editor-server-data/data/Machine/settings.json

# Install glue kernels, and move to shared directory
# Also patching base kernel so Studio background code doesn't start session silently
RUN install-glue-kernels && \
Expand Down Expand Up @@ -91,18 +119,15 @@ RUN HOME_DIR="/home/${NB_USER}/licenses" \
&& ${HOME_DIR}/oss_compliance/generate_oss_compliance.sh ${HOME_DIR} python \
&& rm -rf ${HOME_DIR}/oss_compliance*

# Merge in OS directory tree contents.
RUN mkdir -p ${DIRECTORY_TREE_STAGE_DIR}
COPY dirs/ ${DIRECTORY_TREE_STAGE_DIR}/
RUN rsync -a ${DIRECTORY_TREE_STAGE_DIR}/ / && \
rm -rf ${DIRECTORY_TREE_STAGE_DIR}

# Create logging directories for supervisor
RUN mkdir -p $SAGEMAKER_LOGGING_DIR && \
chmod a+rw $SAGEMAKER_LOGGING_DIR && \
mkdir -p ${STUDIO_LOGGING_DIR} && \
chown ${NB_USER}:${MAMBA_USER} ${STUDIO_LOGGING_DIR}

# Clean up CodeEditor artifacts
RUN rm -rf /etc/code-editor

# Create supervisord runtime directory
RUN mkdir -p /var/run/supervisord && \
chmod a+rw /var/run/supervisord
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.terminal.activateEnvironment": false,
"python.defaultInterpreterPath": "/opt/conda/bin/python"
}
3 changes: 3 additions & 0 deletions template/v1/dirs/etc/code-editor/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://open-vsx.org/api/ms-toolsai/jupyter/2023.9.100/file/ms-toolsai.jupyter-2023.9.100.vsix
https://open-vsx.org/api/ms-python/python/2023.20.0/file/ms-python.python-2023.20.0.vsix
https://open-vsx.org/api/amazonwebservices/aws-toolkit-vscode/1.99.0/file/amazonwebservices.aws-toolkit-vscode-1.99.0.vsix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[include]
files = supervisord-common.conf

[program:codeeditorserver]
directory=%(ENV_HOME)s
command=start-code-editor
autostart=true
autorestart=true
stdout_logfile=/dev/fd/1 ; Redirect web server logs to stdout
stdout_logfile_maxbytes = 0 ; Fix: https://github.com/Supervisor/supervisor/issues/935
stderr_logfile_maxbytes = 0 ; Fix: https://github.com/Supervisor/supervisor/issues/935
18 changes: 18 additions & 0 deletions template/v1/dirs/etc/supervisor/conf.d/supervisord-common.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[supervisord]
nodaemon=true

pidfile=/var/run/supervisord/supervisord.pid
logfile=%(ENV_STUDIO_LOGGING_DIR)s/%(ENV_SAGEMAKER_APP_TYPE_LOWERCASE)s/supervisord/supervisord.log
logfile_maxbytes=5MB
logfile_backups=10
redirect_stderr=true

[unix_http_server]
file=/var/run/supervisord/supervisor.sock
chmod=0700

[supervisorctl]
serverurl=unix:///var/run/supervisord/supervisor.sock

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[include]
files = supervisord-common.conf

[program:jupyterlabserver]
directory=%(ENV_HOME)s
command=start-jupyter-server
stopasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
16 changes: 16 additions & 0 deletions template/v1/dirs/usr/local/bin/entrypoint-code-editor
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -e

# Generate and execute the shell code to modifies shell variables to include
# micromamba commands (e.g. using `micromamba activate` to activate environments)
eval "$(micromamba shell hook --shell=bash)"

# Activate conda environment 'base', where supervisord is installed
micromamba activate base

# Set up SAGEMAKER_APP_TYPE_LOWERCASE based on SAGEMAKER_APP_TYPE
export SAGEMAKER_APP_TYPE_LOWERCASE=$(echo $SAGEMAKER_APP_TYPE | tr '[:upper:]' '[:lower:]')

mkdir -p $STUDIO_LOGGING_DIR/$SAGEMAKER_APP_TYPE_LOWERCASE/supervisord
exec supervisord -c /etc/supervisor/conf.d/supervisord-code-editor.conf -n
25 changes: 25 additions & 0 deletions template/v1/dirs/usr/local/bin/start-code-editor
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
set -e

eval "$(micromamba shell hook --shell=bash)"

# Activate conda environment 'base', which is the default environment for sagemaker-distribution
micromamba activate base

# Start code-editor server
if [ -n "$SAGEMAKER_APP_TYPE_LOWERCASE" ]; then
# SAGEMAKER_APP_TYPE is set, indicating the server is running within a SageMaker
# app. Configure the base url to be `/<app-type-in-lower-case>/default`.
sagemaker-code-editor --host 0.0.0.0 --port 8888 \
--without-connection-token \
--base-path "/$SAGEMAKER_APP_TYPE_LOWERCASE/default" \
--server-data-dir /opt/amazon/sagemaker/sagemaker-code-editor-server-data \
--extensions-dir /opt/amazon/sagemaker/sagemaker-code-editor-server-data/extensions \
--user-data-dir /opt/amazon/sagemaker/sagemaker-code-editor-user-data
else
sagemaker-code-editor --host 0.0.0.0 --port 8888 \
--without-connection-token \
--server-data-dir /opt/amazon/sagemaker/sagemaker-code-editor-server-data \
--extension-dir /opt/amazon/sagemaker/sagemaker-code-editor-server-data/extensions \
--user-data-dir /opt/amazon/sagemaker/sagemaker-code-editor-user-data
fi
6 changes: 6 additions & 0 deletions test/test_artifacts/v1/sagemaker-code-editor.test.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG COSMOS_IMAGE
FROM $COSMOS_IMAGE

ARG MAMBA_DOCKERFILE_ACTIVATE=1

CMD ["sagemaker-code-editor", "--version"]
2 changes: 2 additions & 0 deletions test/test_dockerfile_based_harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
("jupyter-dash.test.Dockerfile", ['jupyter-dash']),
("jupyterlab-lsp.test.Dockerfile", ['jupyterlab-lsp']),
("jupyter-lsp-server.test.Dockerfile", ['jupyter-lsp-server']),
("sagemaker-code-editor.test.Dockerfile", ['sagemaker-code-editor']),
("notebook.test.Dockerfile", ['notebook']),
("glue-sessions.test.Dockerfile", ['aws-glue-sessions']),
("altair.test.Dockerfile", ['altair']),
Expand Down Expand Up @@ -66,6 +67,7 @@ def test_dockerfiles_for_cpu(dockerfile_path: str, required_packages: List[str],
("jupyter-dash.test.Dockerfile", ['jupyter-dash']),
("jupyterlab-lsp.test.Dockerfile", ['jupyterlab-lsp']),
("jupyter-lsp-server.test.Dockerfile", ['jupyter-lsp-server']),
("sagemaker-code-editor.test.Dockerfile", ['sagemaker-code-editor']),
("notebook.test.Dockerfile", ['notebook']),
("glue-sessions.test.Dockerfile", ['aws-glue-sessions']),
("altair.test.Dockerfile", ['altair']),
Expand Down

0 comments on commit 56be466

Please sign in to comment.