Skip to content

Commit 179dc03

Browse files
committed
Rework req_keepvars handling:
- Rework req_keepvars. Old meaning of req_keepvars didn't make sense, because it overrode mandatory variables that JupyterHub sets. Change to these two variables: - req_keepvars_default: Automatically set by JupyterHub defaults, should not be overridden. Old req_keepvars becomes this. Should only be changed if you know what you are doing. - req_keepvars: Adds to the defaults in req_keepvars_default. This is what cluster admins usually want to set.
1 parent 03efb3c commit 179dc03

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Added (user)
66

77
* Add Jinja2 templating as an option for all scripts and commands. If '{{' or `{%` is used anywhere in the string, it is used as a jinja2 template.
88
* Add new option exec_prefix, which defaults to `sudo -E -u {username}`. This replaces explicit `sudo` in every batch command - changes in local commands may be needed.
9-
* New option: `req_keepvars_extra`, which allows keeping extra variables in addition to what is defined by JupyterHub itself (addition of variables to keep instead of replacement). #99
109
* Add `req_prologue` and `req_epilogue` options to scripts which are inserted before/after the main jupyterhub-singleuser command, which allow for generic setup/cleanup without overriding the entire script. #96
1110
* SlurmSpawner: add the `req_reservation` option. #91
1211
* Add basic support for JupyterHub progress updates, but this is not used much yet. #86
@@ -25,6 +24,7 @@ Changed
2524
- Add a new option `batchspawner_singleuser_cmd` which is used as a wrapper in the single-user servers, which conveys the remote port back to JupyterHub. This is now an integral part of the spawn process.
2625
- If you have installed with `pip install -e`, you will have to re-install so that the new script `batchspawner-singleuser` is added to `$PATH`.
2726
* Update minimum requirements to JupyterHub 0.9 and Python 3.5. #143
27+
+* Change meaning of `req_keepvars`. Previously, setting this would override everything that JupyterHub requires to be set (so you'd have to make sure you set it back). Now, `req_keepvars` only *adds* to the JupyterHub defaults, and `req_keepvars_default` gets set to the JupyterHub upstream and can be used to completly override the JupyterHub defaults. #99, #??
2828
* Update Slurm batch script. Now, the single-user notebook is run in a job step, with a wrapper of `srun`. This may need to be removed using `req_srun=''` if you don't want environment variables limited.
2929
* Pass the environment dictionary to the queue and cancel commands as well. This is mostly user environment, but may be useful to these commands as well in some cases. #108, #111 If these environment variables were used for authentication as an admin, be aware that there are pre-existing security issues because they may be passed to the user via the batch submit command, see #82.
3030

batchspawner/batchspawner.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,30 @@ def _req_username_default(self):
143143
def _req_homedir_default(self):
144144
return pwd.getpwnam(self.user.name).pw_dir
145145

146-
req_keepvars = Unicode()
147-
@default('req_keepvars')
148-
def _req_keepvars_default(self):
146+
req_keepvars_default = Unicode(
147+
help="All environment variables to pass to the spawner. This is set "
148+
"to a default by JupyterHub, and if you change this list "
149+
"the previous ones are _not_ included and your submissions will "
150+
"break unless you re-add necessary variables. Consider "
151+
"req_keepvars for most use cases."
152+
)
153+
@default('req_keepvars_all')
154+
def _req_keepvars_all_default(self):
149155
return ','.join(self.get_env().keys())
150156

151-
req_keepvars_extra = Unicode(
157+
req_keepvars = Unicode(
152158
help="Extra environment variables which should be configured, "
153159
"added to the defaults in keepvars, "
154-
"comma separated list.")
160+
"comma separated list.").tag(config=True)
161+
admin_environment = Unicode(
162+
help="Comma-separated list of environment variables to be passed to "
163+
"the batch submit/cancel commands, but _not_ to the batch script "
164+
"via --export. This could be used, for example, to authenticate "
165+
"the submit command as an admin user so that it can submit jobs "
166+
"as another user. These are _not_ included in an "
167+
"--export={keepvars} type line in the batch script, but you "
168+
"should check that your batch system actually does the right "
169+
"thing here.").tag(config=True)
155170

156171
batch_script = Unicode('',
157172
help="Template for job submission script. Traits on this class named like req_xyz "
@@ -177,8 +192,13 @@ def get_req_subvars(self):
177192
subvars = {}
178193
for t in reqlist:
179194
subvars[t[4:]] = getattr(self, t)
180-
if subvars.get('keepvars_extra'):
181-
subvars['keepvars'] += ',' + subvars['keepvars_extra']
195+
# 'keepvars' is special: 'keepvars' goes through as the
196+
# variable, but 'keepvars_default' is prepended to it.
197+
# 'keepvars_default' is JH-required stuff so you have to try
198+
# extra hard to override it.
199+
if subvars.get('keepvars'):
200+
subvars['keepvars_default'] += ',' + subvars['keepvars']
201+
subvars['keepvars'] = subvars['keepvars_default']
182202
return subvars
183203

184204
batch_submit_cmd = Unicode('',

batchspawner/tests/test_spawners.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def test_lfs(db, io_loop):
491491
def test_keepvars(db, io_loop):
492492
# req_keepvars
493493
spawner_kwargs = {
494-
'req_keepvars': 'ABCDE',
494+
'req_keepvars_default': 'ABCDE',
495495
}
496496
batch_script_re_list = [
497497
re.compile(r'--export=ABCDE', re.X|re.M),
@@ -500,10 +500,32 @@ def test_keepvars(db, io_loop):
500500
spawner_kwargs=spawner_kwargs,
501501
batch_script_re_list=batch_script_re_list)
502502

503-
# req_keepvars AND req_keepvars together
503+
# req_keepvars
504504
spawner_kwargs = {
505505
'req_keepvars': 'ABCDE',
506-
'req_keepvars_extra': 'XYZ',
506+
}
507+
batch_script_re_list = [
508+
re.compile(r'--export=.*ABCDE', re.X|re.M),
509+
]
510+
run_typical_slurm_spawner(db, io_loop,
511+
spawner_kwargs=spawner_kwargs,
512+
batch_script_re_list=batch_script_re_list)
513+
514+
# req_keepvars
515+
spawner_kwargs = {
516+
'admin_environment': 'ABCDE',
517+
}
518+
batch_script_re_list = [
519+
re.compile(r'^((?!ABCDE).)*$', re.X|re.S),
520+
]
521+
run_typical_slurm_spawner(db, io_loop,
522+
spawner_kwargs=spawner_kwargs,
523+
batch_script_re_list=batch_script_re_list)
524+
525+
# req_keepvars AND req_keepvars together
526+
spawner_kwargs = {
527+
'req_keepvars_default': 'ABCDE',
528+
'req_keepvars': 'XYZ',
507529
}
508530
batch_script_re_list = [
509531
re.compile(r'--export=ABCDE,XYZ', re.X|re.M),

0 commit comments

Comments
 (0)