Skip to content
Snippets Groups Projects
Commit a855452e authored by Jens Henrik Goebbert's avatar Jens Henrik Goebbert
Browse files

addind day5

parent fccaec4f
No related branches found
No related tags found
No related merge requests found
File added
File added
This diff is collapsed.
%% Cell type:markdown id:2ea39561-7031-4e43-922c-439caa7957c6 tags:
![jsc-logo.jpg](attachment:67258d94-84e6-4a0c-ae8f-c74332ec082e.jpg)
Author: [Jens Henrik Göbbert](mailto:j.goebbert@fz-juelich.de)
------------------------------------
%% Cell type:markdown id:ce6bf220-69b3-47db-92c4-87eeb22395d9 tags:
# Supercomputing with JupyterLab - Tips & Tricks
This is the first time you are using JupyterLab on an HPC system? Let us show you some tips & tricks.
This notebook might be special to the environment at Juelich Supercomputing Centre, Forschungszentrum Juelich, Germany.
-------------------------
%% Cell type:markdown id:contained-explanation tags:
### How to manually start and connect to JupyterLab
%% Cell type:markdown id:small-panic tags:
#### Exercise 1:
%% Cell type:markdown id:simple-syndrome tags:
1. Open a Terminal
- if you use the terminal from within a running JupyterLab, you need to clean up a bit
```
unset JUPYTER_PATH
unset JUPYTERLAB_DIR
unset JUPYTER_DATA_DIR
unset JUPYTER_CONFIG_PATH
```
2. Decide what JupyterLab you want to start
- a) preinstalled JupyterLab from a module
- b) JupyterLab from your own conda-environment
%% Cell type:markdown id:breeding-probe tags:
#### A) Using a preinstalled JupyterLab from a module
Run the following commands to prepare your shell
```bash
module purge
module use $OTHERSTAGES
module load Stages/2022
module load GCCcore/.11.2.0
module load JupyterCollection
```
Start JupyterLab
```bash
jupyter lab --no-browser
```
%% Cell type:markdown id:round-flush tags:
#### B) JupyterLab from your own conda-environment
Prepare preinstalled conda environment:
```bash
export CONDA_TARGET_DIR=/p/project/coecschool22/goebbert1/miniconda3
CONDA_ENV=my_jupyterlab3
# >>> conda initialize >>>
__conda_setup="$('/p/project/coecschool22/goebbert1/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/p/project/coecschool22/goebbert1/miniconda3/etc/profile.d/conda.sh" ]; then
. "/p/project/coecschool22/goebbert1/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/p/project/coecschool22/goebbert1/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
```
%% Cell type:markdown id:spiritual-mustang tags:
Load conda environment
```bash
# verify that PYTHONPATH only points to directories of packages
# that are compatible with the Python interpreter in Miniconda3
unset PYTHONPATH
# just to show that jupyterlab starts
# we have to ensure that the bash-kernel shell
# does not have JUPYTER*- environment variables set from the running JupyterLab (which serves this notebook)
unset JUPYTER_PATH
unset JUPYTERLAB_DIR
unset JUPYTER_DATA_DIR
unset JUPYTER_CONFIG_PATH
# Activate your Python virtual environment
echo ${CONDA_TARGET_DIR}
echo ${CONDA_ENV}
source "${CONDA_TARGET_DIR}"/bin/activate "${CONDA_ENV}"
# Ensure python packages installed in conda are always prefered, not necessary if module purge is used
#export PYTHONPATH=${CONDA_PREFIX}/lib/python3.6/site-packages:${PYTHONPATH}
echo $PYTHONPATH
```
Start JupyterLab
```bash
jupyter lab --no-browser
```
%% Cell type:markdown id:boolean-tamil tags:
---------
### Tunnel the new JupyterLab to your local machine
**Linux or Mac:**
If your operating system is Linux or Mac use:
%% Cell type:code id:olive-madonna tags:
``` python
ssh -N -L <LOCAL_PORT>:<JLAB_NODE>:<JLAB_PORT> <USERID>@<LOGIN_NODE>.fz-juelich.de
# example: ssh -N -L 8888:juwels04:8888 goebbert1@juwels01.fz-juelich.de
# if you want to tunnel directly to <LOGIN_NODE> only, then you must set JLAB_NODE to "localhost"
```
%% Cell type:markdown id:scientific-commission tags:
**Attention:**
- LOGIN_NODE - Hostname of login node from the view of your local machine
- JLAB_NODE - Hostname of the node running JupyterLab from the view of LOGIN_NODE
- LOCAL_PORT - port on your local machine
- JLAB_PORT - port on the node running JupyterLab
%% Cell type:markdown id:under-willow tags:
**Windows:**
In case your operating system is Windows, the setup of the tunnel depends on your ssh client.
Here a short overview on how-to setup a tunnel with **PuTTY** is given.
It is assumed that PuTTY is already configured in a way that a general ssh connection to JUWELS is possible.
That means that host name, user name and the private ssh key (using PuTTY's Pageant) are correctly set.
You already made a first connection to JUWELS using PUTTY.
To establish the ssh tunnel start PUTTY and enter the "SSH-->tunnels" tab in the PuTTY configuration window before connecting to JUWELS.
You have to enter the source port (eg. <LOCAL_PORT> = 8888) and the destination (eg. juwels01.fz-juelich.de:8888) and than press add.
After pressing add, the tunnel should appear in the list of forwarded ports and you can establish the tunnel by pressing the open button.
%% Cell type:markdown id:ordered-tract tags:
![](PuTTY-add-tunnel.png)
%% Cell type:markdown id:unexpected-sharing tags:
![](PuTTY-tunnel-added.png)
%% Cell type:markdown id:ece9406a-a238-4c26-8a8e-f3f582d3a5e6 tags:
![jsc-logo.jpg](attachment:67258d94-84e6-4a0c-ae8f-c74332ec082e.jpg)
Author: [Jens Henrik Göbbert](mailto:j.goebbert@fz-juelich.de)
------------------------------------
%% Cell type:markdown id:d36452be-8e07-4876-ab0d-ba4d58c4c0cf tags:
# Supercomputing with JupyterLab - Tips & Tricks
This is the first time you are using JupyterLab on an HPC system? Let us show you some tips & tricks.
This notebook might be special to the environment at Juelich Supercomputing Centre, Forschungszentrum Juelich, Germany.
-------------------------
%% Cell type:markdown id:stuffed-collapse tags:
### Switch the default JupyterLab installation
%% Cell type:markdown id:spectacular-placement tags:
#### Exercise 1:
1. Open `$JUPYTER_LOG_DIR/.start`
2. Check for call of optional start-script
3. Create own start-script
- `$HOME/.jupyter/start_jupyter-jsc.sh`
%% Cell type:markdown id:successful-intranet tags:
```bash
module purge
module load Stages/2022
module load GCCcore/.11.2.0
module use /p/usersoftware/swmanage/goebbert1/stage2022/jupyter33/easybuild/$SYSTEMNAME/modules/all/Compiler/GCCcore/11.2.0/
module load Python
module load JupyterCollection/2022.3.3
```
%% Cell type:markdown id:civil-stupid tags:
Starting a JupyterLab through Jupyter-JSC now on the cluster using this $HOME
- will now source `$HOME/.jupyter/start_jupyter-jsc.sh` before it starts JupyterLab
- and therefore will now use `JupyterCollection/2022.3.3` from `Stage/2022`.
%% Cell type:markdown id:extra-tooth tags:
#### Exercise 2:
What is this `JupyterCollection` ?
1. Check `which jupyter`
2. Check the jupyter installation
- `cd $EBROOTJUPYTERCOLLECTION/easybuild/`
- `vim JupyterCollection-2021.3.2-gcccoremkl-10.3.0-2021.2.0.eb`
3. Check the easybuild files of our Jupyter module on Github:
- https://github.com/easybuilders/JSC/
day5_extras/2_my_own_jupyterlab/PuTTY-add-tunnel.png

23.5 KiB

day5_extras/2_my_own_jupyterlab/PuTTY-tunnel-added.png

26.2 KiB

%% Cell type:markdown id:exclusive-nursery tags:
![jsc-logo.jpg](attachment:67258d94-84e6-4a0c-ae8f-c74332ec082e.jpg)
Author: [Jens Henrik Göbbert](mailto:j.goebbert@fz-juelich.de)
------------------------------------
%% Cell type:markdown id:worldwide-presence tags:
# Supercomputing with JupyterLab - Tips & Tricks
This is the first time you are using JupyterLab on an HPC system? Let us show you some tips & tricks.
This notebook might be special to the environment at Juelich Supercomputing Centre, Forschungszentrum Juelich, Germany.# JupyterLab Tour
-------------------------
%% Cell type:markdown id:retired-crest tags:
#### Exercise 1:
**ONLY FOR HDF-CLOUD**
**!!DANGEROUS!! YOU ARE MOUNTING WRITEABLE**
Mount the $HOMEs from GPFS on the HDF Cloud
%% Cell type:code id:abd7420b-36b8-4f8b-91fc-cc10f6d45013 tags:
``` python
import json
import os
from contextlib import closing
import pyunicore.client as unicore_client
import requests
```
%% Cell type:markdown id:2da2cb6f-d6c6-4bbe-a7bb-8dd488615181 tags:
During the login process in Jupyter-JSC, you have authenticated yourself against the identity management system [*Unity-IdM*](https://www.unity-idm.eu) .
This IdM issues temporary tokens (comparable to user+password) on request, which can be use for authentication to other services that are also connected to the same IdM.
Here we use the JupyterHub-API (+your JUPYTERHUB_API_TOKEN) to get a new temporary token for the next steps.
%% Cell type:code id:643d4c6b-927e-4d25-a3b0-1e6755a6cae3 tags:
``` python
def get_access_token():
remote_node = os.getenv("REMOTE_NODE")
remote_hub_port = os.getenv("REMOTE_PORT")
hub_api_url = f"http://{remote_node}:{remote_hub_port}/hub/api/user"
headers = {"Authorization": "token {}".format(os.getenv("JUPYTERHUB_API_TOKEN"))}
with closing(requests.get(hub_api_url, headers=headers)) as r:
if r.status_code == 200:
resp = json.loads(r.content.decode("utf-8"))
else:
raise Exception(
"Could not receive access token: {} {}".format(
r.status_code, r.content.decode("utf-8")
)
)
# No HPC accounts -> no access token in this script
if (
"auth_state" in resp.keys()
and "oauth_user" in resp["auth_state"].keys()
and "hpc_infos_attribute" in resp["auth_state"]["oauth_user"]
and len(resp["auth_state"]["oauth_user"]["hpc_infos_attribute"]) > 0
):
return resp["auth_state"]["access_token"]
else:
return None
```
%% Cell type:markdown id:540563d1-59ca-4d28-86f7-171c98310a68 tags:
Having that `access_token` we can now ask unicore for information about the HPC account of the user the token belongs to.
We will receive the `uid` and the base of all its home directories as `remote_base_dir`.
That information is required to ask the service [*UFTP*](https://apps.fz-juelich.de/jsc/hps/judac/uftp.html) all details needed for the mound command.
%% Cell type:markdown id:aa5c5156-9afd-4b7b-a78f-7bb40aef2222 tags:
**ATTENTION:** Set `ro` or `rw` for mounting readonly or writable
%% Cell type:code id:78c3f65d-1039-482f-900b-c9b0173b490d tags:
``` python
def get_mount_command(access_token):
_auth = "https://uftp.fz-juelich.de:9112/UFTP_Auth/rest/auth/"
_tr = unicore_client.Transport(access_token)
_info = _tr.get(url=_auth)
_uid = _info["JUDAC"]["uid"]
remote_base_dir = "/p/home/jusers/%s" % _uid
# authenticate
_req = {
"persistent": "true",
"serverPath": "%s/___UFTP___MULTI___FILE___SESSION___MODE___" % remote_base_dir,
}
_reply = _tr.post(url=_auth + "/JUDAC", json=_req).json()
uftp_pwd = _reply["secret"]
uftp_host = _reply["serverHost"]
uftp_port = _reply["serverPort"]
return f"curlftpfs -s -o rw,uid=1094,gid=100,ftp_method=singlecwd,enable_epsv,user=anonymous:{uftp_pwd} {uftp_host}:{uftp_port}"
```
%% Cell type:markdown id:e7f6af90-bd33-4a55-9659-973bcaa3da67 tags:
------------------------------------
Let's bring all pieces together:
%% Cell type:code id:b0fa1183-2983-4702-94b1-b4567fea4df3 tags:
``` python
access_token = get_access_token()
if not access_token:
print("Error: No access token")
mount_cmd = get_mount_command(access_token)
# e.g. curlftpfs -s -o ro,uid=1094,gid=100,ftp_method=singlecwd,enable_epsv,user=anonymous:fwngibbflwngfllaycpc judacsrv.fz-juelich.de:64333
print(mount_cmd.partition("anonymous:")[0], "<...>") # do not print the token
```
%% Output
curlftpfs -s -o rw,uid=1094,gid=100,ftp_method=singlecwd,enable_epsv,user= <...>
%% Cell type:code id:45c2ff24-a735-459a-8b53-ceeb74752635 tags:
``` python
%%bash -s "$mount_cmd"
mkdir -p $HOME/uftp-mount-rw
$1 $HOME/uftp-mount-rw
```
%% Cell type:code id:30a5b1e5-4f11-465a-a27d-e0b2c5973ec0 tags:
``` python
# ...
!ls -l $HOME/uftp-mount-rw
```
%% Output
total 68
drwx------ 3 jovyan users 4096 Mar 14 23:53 deep
drwx------ 3 jovyan users 4096 Mar 31 21:42 hdfml
drwx------ 3 jovyan users 4096 Mar 25 11:11 judac
drwx------ 3 jovyan users 16384 Apr 5 2022 jureca
drwx------ 3 jovyan users 4096 Aug 14 2021 juropa3exp
drwx------ 3 jovyan users 16384 Mar 31 23:15 jusuf
drwx------ 3 jovyan users 16384 Apr 1 10:45 juwels
drwx------ 3 jovyan users 4096 Mar 9 21:50 shared
%% Cell type:code id:5a4ff2ac-8731-4ce2-ab79-e89b6d3887a6 tags:
``` python
# unmount
!fusermount -u $HOME/uftp-mount-rw
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment