Skip to content

Instantly share code, notes, and snippets.

@sofianhamiti
Last active October 4, 2023 18:47
Show Gist options
  • Save sofianhamiti/0bce9c2ba6aaf05f6ff0fb26f46e31a8 to your computer and use it in GitHub Desktop.
Save sofianhamiti/0bce9c2ba6aaf05f6ff0fb26f46e31a8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# /home/ec2-user/anaconda3/envs/JupyterSystemEnv/bin/pip install -U keytar jupyter-server-proxy
echo == INSTALLING CODE-SERVER ==
curl -fsSL https://code-server.dev/install.sh | sh -s -- --version=4.12.0
#########################################
### INTEGRATE CODE-SERVER WITH JUPYTER
#########################################
echo == UPDATING THE JUPYTER SERVER CONFIG ==
cat >>/home/ec2-user/.jupyter/jupyter_notebook_config.py <<EOC
c.ServerProxy.servers = {
'vscode': {
'launcher_entry': {
'enabled': True,
'title': 'VS Code',
},
'command': ['code-server', '--auth', 'none', '--disable-telemetry', '--bind-addr', '0.0.0.0:{port}'],
'environment' : {'XDG_DATA_HOME' : '/home/ec2-user/SageMaker/vscode-config'},
'absolute_url': False,
'timeout': 30
}
}
EOC
echo == INSTALL SUCCESSFUL. RESTARTING JUPYTER ==
# RESTART THE JUPYTER SERVER
systemctl restart jupyter-server
@teticio
Copy link

teticio commented May 25, 2022

Thanks for this! I had to change the last line to initctl restart jupyter-server in order for it to work.

@sofianhamiti
Copy link
Author

Thanks for the feedback Robert. Glad it helps.
Yes, are you using an instance based on Amazon Linux 1 by any chance?
The Jupyter restart command is slightly different indeed. https://github.com/aws-samples/amazon-sagemaker-notebook-instance-customization#51-restart-jupyterlab

@tbugfinder
Copy link

Thanks for this - that's an excellent addition!
In order to have a persistent configuration after stop/start actions on SageMaker Notebook Instances, I've added environment configuration:

'environment' : {'XDG_DATA_HOME' : '/home/ec2-user/SageMaker/<preferred subdir>'}

Could you also add the configuration so that it shows up within the JupyterLab environment?

@sofianhamiti
Copy link
Author

Thanks your contribution @tbugfinder. I have added the configuration in the script. Great work!

@zburq
Copy link

zburq commented Mar 16, 2023

This is great. Thank you @sofianhamiti.
Assuming 'environment' : {'XDG_DATA_HOME' : '/home/ec2-user/SageMaker/<preferred subdir>'} installs VS Code in Sagemaker's EBS, it should persist through restarts, no?

And if so, do I need to add this to Lifecycle Configuration?

@sofianhamiti
Copy link
Author

@zburq that's correct yes. You extensions and setting will persist if you point XDG_DATA_HOME to a folder under /home/ec2-user/SageMaker/

You can add this script to a lifecycle config to automate the install between Stop/Start of the instance.
I personally just run the script when starting the instance, as it's very quick and simple to run.

@zburq
Copy link

zburq commented Mar 19, 2023

@sofianhamiti sorry - I don't understand. If VSCode is installed in EBS, and persists even after restart, why do you run the script when you restart the instance?

@scotteggs
Copy link

Has anyone run into this error message

Failed to restart jupyter-server.service: The name org.freedesktop.PolicyKit1 was not provided by any .service files
See system logs and 'systemctl status jupyter-server.service' for details.

I've looked around and don't seem to be able to find a solution to this.

@scotteggs
Copy link

Replying to my own comment, it seems that simply running the final command w/ sudo allowed me to progress. Thanks for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment