Skip to content

Commit 0688553

Browse files
committed
introduce nxf_launch dir
instead of writing to the root of the user home tree, create a subdirectory based on the workflow name, and inject it as the work dir of the jupyterhub-singleuser process via popen_kwargs
1 parent 5144258 commit 0688553

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

nextflowspawner/__init__.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,67 @@
55
import os
66
import pwd
77

8-
from jupyterhub.spawner import LocalProcessSpawner
8+
from jupyterhub.spawner import LocalProcessSpawner, set_user_setuid
99
from subprocess import run, CalledProcessError
1010
from traitlets import default, Dict, Unicode
1111
from urllib.parse import urlparse
1212

1313
class NextflowSpawner(LocalProcessSpawner):
1414

15-
@property
16-
def nxf_home(self):
17-
print(self.user.name)
18-
user_home = pwd.getpwnam(self.user.name).pw_dir
19-
print(user_home)
20-
return os.getenv('NXF_HOME', f"{user_home}/.nextflow")
21-
2215
default_url = Unicode('/nextflow', help="entrypoint for https://github.com/phue/jupyter-nextflow-proxy")
2316
workflow_url = Unicode(config=True, help="The url of the pipeline repository.")
24-
17+
18+
home_dir = Unicode(help="The user home directory")
19+
20+
@default('home_dir')
21+
def _default_home_dir(self):
22+
return pwd.getpwnam(self.user.name).pw_dir
23+
24+
nxf_home = Unicode(help="The directory where nextflow assets are stored.")
25+
26+
@default('nxf_home')
27+
def _default_nxf_home(self):
28+
return os.getenv('NXF_HOME', f"{self.home_dir}/.nextflow")
29+
30+
nxf_launch = Unicode(help="The directory where the pipeline is launched.")
31+
32+
@default('nxf_launch')
33+
def _default_nxf_launch(self):
34+
path = f"{self.home_dir}/{self.workflow_url.split('/').pop()}"
35+
if not os.path.exists(path):
36+
os.makedirs(path)
37+
os.chown(path, pwd.getpwnam(self.user.name).pw_uid, pwd.getpwnam(self.user.name).pw_uid)
38+
return path
39+
40+
popen_kwargs = Dict(help="Extra keyword arguments to pass to Popen.")
41+
42+
@default('popen_kwargs')
43+
def _default_popen_kwargs(self):
44+
return {'cwd': self.nxf_launch}
45+
2546
schema = Dict(config=True, help="The pipeline JSON schema.")
2647

2748
@default('schema')
2849
def _default_schema(self):
2950
path = f"{self.nxf_home}/assets/{urlparse(self.workflow_url).path[1:]}/nextflow_schema.json"
3051

3152
try:
32-
run(['nextflow', 'pull', self.workflow_url], check=True)
53+
run(
54+
args=['nextflow', 'pull', self.workflow_url],
55+
check=True,
56+
user=self.user.name,
57+
cwd=self.home_dir,
58+
env={**os.environ, 'NXF_HOME': self.nxf_home}
59+
)
3360
with open(path) as nxf_schema:
3461
return json.load(nxf_schema)
3562
except CalledProcessError:
3663
print(f"{self.workflow_url} does not seem to exist")
3764
except FileNotFoundError:
3865
print(f"{self.workflow_url} does not seem to provide a nextflow_schema.json")
3966

40-
# def make_preexec_fn(self, name):
41-
# if os.getuid():
42-
# # if we are already running as non-root user, do nothing
43-
# pass
44-
# else:
45-
# # otherwise drop privileges
46-
# return super().set_user_setuid(name, chdir=True)
67+
def make_preexec_fn(self, name):
68+
return set_user_setuid(name, chdir=False)
4769

4870
def _get_params_from_schema(self, schema, key=None):
4971
params = {}
@@ -93,10 +115,10 @@ def _write_params_file(self, config):
93115
# generate sha-1 hash from json payload for use as unique filename
94116
json_sha = hashlib.sha1(json_string.encode()).hexdigest()
95117

96-
with open(f'{json_sha}.json', 'w', encoding='utf-8') as fout:
118+
with open(f'{self.nxf_home}/nextflowspawner_{json_sha}.json', 'w', encoding='utf-8') as fout:
97119
fout.write(json_string)
98120

99-
return f'{json_sha}.json'
121+
return f'{self.nxf_home}/nextflowspawner_{json_sha}.json'
100122

101123
def _options_form_default(self):
102124
params = self._get_params_from_schema(self.schema)
@@ -139,4 +161,4 @@ def get_env(self):
139161
env['NXF_HOME'] = self.nxf_home
140162
env['NXF_USER_WORKFLOW'] = self.workflow_url
141163
env['NXF_USER_PARAMS'] = self._write_params_file(self.user_options)
142-
return env
164+
return env

0 commit comments

Comments
 (0)