Skip to content

Commit 1238f67

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 4747946 commit 1238f67

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

README.md

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

161161
* 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.
162162
* 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.
163-
* 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
164163
* 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
165164
* SlurmSpawner: add the `req_reservation` option. #
166165

@@ -172,6 +171,7 @@ Added (developer)
172171
Changed
173172

174173
* Update minimum requirements to JupyterHub 0.8.1 and Python 3.4.
174+
* 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, #??
175175
* 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.
176176
* 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 envioronment 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.
177177

batchspawner/batchspawner.py

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

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

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

152167
batch_script = Unicode('',
153168
help="Template for job submission script. Traits on this class named like req_xyz "
@@ -170,8 +185,13 @@ def get_req_subvars(self):
170185
subvars = {}
171186
for t in reqlist:
172187
subvars[t[4:]] = getattr(self, t)
173-
if subvars.get('keepvars_extra'):
174-
subvars['keepvars'] += ',' + subvars['keepvars_extra']
188+
# 'keepvars' is special: 'keepvars' goes through as the
189+
# variable, but 'keepvars_default' is prepended to it.
190+
# 'keepvars_default' is JH-required stuff so you have to try
191+
# extra hard to override it.
192+
if subvars.get('keepvars'):
193+
subvars['keepvars_default'] += ',' + subvars['keepvars']
194+
subvars['keepvars'] = subvars['keepvars_default']
175195
return subvars
176196

177197
batch_submit_cmd = Unicode('',

batchspawner/tests/test_spawners.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def test_lfs(db, io_loop):
455455
def test_keepvars(db, io_loop):
456456
# req_keepvars
457457
spawner_kwargs = {
458-
'req_keepvars': 'ABCDE',
458+
'req_keepvars_default': 'ABCDE',
459459
}
460460
batch_script_re_list = [
461461
re.compile(r'--export=ABCDE', re.X|re.M),
@@ -464,10 +464,32 @@ def test_keepvars(db, io_loop):
464464
spawner_kwargs=spawner_kwargs,
465465
batch_script_re_list=batch_script_re_list)
466466

467-
# req_keepvars AND req_keepvars together
467+
# req_keepvars
468468
spawner_kwargs = {
469469
'req_keepvars': 'ABCDE',
470-
'req_keepvars_extra': 'XYZ',
470+
}
471+
batch_script_re_list = [
472+
re.compile(r'--export=.*ABCDE', re.X|re.M),
473+
]
474+
run_typical_slurm_spawner(db, io_loop,
475+
spawner_kwargs=spawner_kwargs,
476+
batch_script_re_list=batch_script_re_list)
477+
478+
# req_keepvars
479+
spawner_kwargs = {
480+
'admin_environment': 'ABCDE',
481+
}
482+
batch_script_re_list = [
483+
re.compile(r'^((?!ABCDE).)*$', re.X|re.S),
484+
]
485+
run_typical_slurm_spawner(db, io_loop,
486+
spawner_kwargs=spawner_kwargs,
487+
batch_script_re_list=batch_script_re_list)
488+
489+
# req_keepvars AND req_keepvars together
490+
spawner_kwargs = {
491+
'req_keepvars_default': 'ABCDE',
492+
'req_keepvars': 'XYZ',
471493
}
472494
batch_script_re_list = [
473495
re.compile(r'--export=ABCDE,XYZ', re.X|re.M),

0 commit comments

Comments
 (0)