-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#492 #493
#492 #493
Changes from all commits
1d71f1b
9a11b3a
b7f5c19
4bb425c
6215c40
5d2c895
9693872
e127fa5
51b1d6f
97f19a1
dcc0c2d
014f4c4
db06c9b
97cabd1
c7543b9
9a9f171
b586b87
09d63d3
010a5a1
fe4bf57
4b02464
c569100
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import logging | ||
import typer | ||
import os | ||
import subprocess | ||
import tempfile | ||
import sys | ||
|
||
logger = logging.getLogger() | ||
|
||
|
@@ -12,13 +16,102 @@ def hpc_notebook(): | |
pass | ||
|
||
|
||
def start_apptainer(notebooks_dir, container_image): | ||
process = None | ||
try: | ||
with tempfile.TemporaryDirectory() as working_directory: | ||
tmp_dir = os.path.join(working_directory, "tmp") | ||
os.makedirs(tmp_dir, exist_ok=True) | ||
|
||
# Step 2: Set environment variables | ||
os.environ["SINGULARITY_TMPDIR"] = tmp_dir | ||
os.environ["APPTAINER_TMPDIR"] = tmp_dir | ||
os.environ["JUPYTER_RUNTIME_DIR"] = tmp_dir | ||
os.environ["JUPYTER_DATA_DIR"] = tmp_dir | ||
os.environ["JUPYTER_CONFIG_DIR"] = tmp_dir | ||
|
||
run_command = [ | ||
"apptainer", | ||
"run", | ||
"--bind", | ||
f"{notebooks_dir}:/notebooks", | ||
container_image, | ||
"jupyter", | ||
"notebook", | ||
"--no-browser", | ||
"--ip=0.0.0.0", | ||
] | ||
process = subprocess.Popen(run_command) | ||
process.wait() | ||
logger.info( | ||
"Jupyter Notebook started successfully in the Apptainer container." | ||
) | ||
except KeyboardInterrupt: | ||
if process: | ||
process.terminate() | ||
process.wait() | ||
logger.info("Operation cancelled by user (Ctrl-C).") | ||
typer.echo("Operation cancelled by user (Ctrl-C).") | ||
sys.exit(0) | ||
|
||
|
||
@hpc_notebook_app.command() | ||
def start(): | ||
def rerun(container_image: str = "hpc-notebook.sif"): | ||
current_directory = os.getcwd() | ||
notebooks_dir = os.path.join(current_directory, "notebooks") | ||
|
||
if not os.path.exists(container_image): | ||
logger.error("Not found.") | ||
typer.echo("Not found") | ||
else: | ||
start_apptainer(notebooks_dir, container_image) | ||
|
||
|
||
@hpc_notebook_app.command() | ||
def start(container_image: str = "hpc-notebook.sif"): | ||
"""Open a notebook file in HPC.""" | ||
print("🚧🌱🚧 Under Construction 🚧🌱🚧") | ||
|
||
# Get the absolute path of the definition file located three levels up from the current script's directory | ||
definition_file = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "../../../scripts/Singularity.def") | ||
) | ||
|
||
current_directory = os.getcwd() | ||
notebooks_dir = os.path.join(current_directory, "notebooks") | ||
|
||
os.makedirs(notebooks_dir, exist_ok=True) | ||
|
||
try: | ||
# Ensure the definition file exists | ||
if not os.path.isfile(definition_file): | ||
logger.error(f"Definition file {definition_file} not found.") | ||
typer.echo(f"🚧🌱🚧 Definition file {definition_file} not found 🚧🌱🚧") | ||
raise typer.Exit(code=1) | ||
|
||
# Step 3: Build the Apptainer container | ||
build_command = ["apptainer", "build", container_image, definition_file] | ||
subprocess.run(build_command, check=True) | ||
logger.info("Apptainer container built successfully.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this command give you a way to reuse an image if you've already built it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I add a rerun command |
||
|
||
# Step 4: Run the Apptainer container and start Jupyter Notebook | ||
start_apptainer(notebooks_dir, container_image) | ||
except subprocess.CalledProcessError as e: | ||
logger.error(f"Failed to start Jupyter Notebook: {e}") | ||
typer.echo("🚧🌱🚧 Failed to start Jupyter Notebook 🚧🌱🚧") | ||
except KeyboardInterrupt: | ||
logger.info("Operation cancelled by user (Ctrl-C).") | ||
typer.echo("Operation cancelled by user (Ctrl-C).") | ||
sys.exit(0) | ||
except Exception as e: | ||
logger.error(f"An error occurred: {e}") | ||
typer.echo("🚧🌱🚧 An unexpected error occurred 🚧🌱🚧") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How should the user shut down their Singularity container when they're done? It's ok to not build in really robust cleanup handling right now, but ideally we could handle the happy path of the user hitting ctrl-c. Or at least we should print instructions to the user for how they can manually end their session. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JoeyC12 Can you address this question? |
||
|
||
|
||
@hpc_notebook_app.command() | ||
def publish(): | ||
"""Publish your hpc-notebook.""" | ||
print("🚧🌱🚧 Under Construction 🚧🌱🚧") | ||
|
||
|
||
if __name__ == "__main__": | ||
hpc_notebook_app() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bootstrap: docker | ||
From: python:3.8-slim | ||
|
||
%post | ||
# install Jupyter Notebook | ||
apt-get update && apt-get install -y \ | ||
python3-pip | ||
|
||
pip install jupyter | ||
|
||
# create notebook directory | ||
mkdir /notebooks | ||
|
||
%environment | ||
|
||
export PATH=/usr/local/bin:$PATH | ||
export HOME=/root | ||
|
||
%runscript | ||
|
||
jupyter notebook --notebook-dir=/notebooks --ip 0.0.0.0 --no-browser | ||
|
||
%startscript | ||
|
||
exec /bin/bash -c "jupyter notebook --notebook-dir=/notebooks --ip 0.0.0.0 --no-browser" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this approach rely on the user being in the root directory of the project where
Singularity.def
is? Let's make sure the user can run this command in any directory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed