spawner/src/glicid_spawner/spawner.py

62 lines
1.7 KiB
Python
Raw Normal View History

2024-01-29 17:47:47 +01:00
"""GLiCID spawner module."""
from batchspawner import SlurmSpawner
from jinja2 import Environment, PackageLoader, select_autoescape
from traitlets import Unicode
from .micromamba import get_envs
2024-01-30 18:30:31 +01:00
from .resources import CPU, GPU, RAM
2024-01-29 17:47:47 +01:00
class GlicidSpawner(SlurmSpawner):
"""Glicid SLURM Spawner."""
batchspawner_singleuser_cmd = Unicode(
'glicid-spawner-singleuser',
help='Glicid spawner singleuser command.',
).tag(config=True)
def _options_form_default(self) -> str:
"""JupyterHub rendered form template."""
environment = Environment(
loader=PackageLoader('glicid_spawner'),
autoescape=select_autoescape(),
)
template = environment.get_template('interactive.html')
2024-01-29 17:47:47 +01:00
return template.render(
username=self.user.name,
python_envs=get_envs(self.user.name),
2024-01-30 18:30:31 +01:00
cpu_available=CPU,
ram_available=RAM,
gpu_available=GPU,
)
2024-01-29 17:47:47 +01:00
def options_from_form(self, formdata) -> dict:
"""Export options from form."""
2024-01-30 18:30:31 +01:00
# Index of user resources choices
i_cpu = int(formdata['cpu'][0])
i_ram = int(formdata['ram'][0])
i_gpu = int(formdata['gpu'][0])
duration = min(
CPU[i_cpu].max_duration,
RAM[i_ram].max_duration,
GPU[i_gpu].max_duration,
)
# Export options
options = {}
options['pyenv'] = formdata['python-env'][0]
2024-01-30 18:30:31 +01:00
options['cpus-per-task'] = CPU[i_cpu].description
options['mem'] = RAM[i_ram].description.replace(' ', '')
2024-01-30 18:30:31 +01:00
if i_gpu:
2024-01-30 18:34:58 +01:00
options['gres'] = 'gpu:' + GPU[i_gpu].description
2024-01-30 18:30:31 +01:00
options['time'] = f'{duration:02d}:00:00'
2024-01-29 17:47:47 +01:00
return options