3434from jupyterhub .spawner import set_user_setuid
3535import jupyterhub
3636
37- @gen .coroutine
38- def run_command (cmd , input = None , env = None ):
39- proc = Subprocess (cmd , shell = True , env = env , stdin = Subprocess .STREAM , stdout = Subprocess .STREAM ,stderr = Subprocess .STREAM )
40- inbytes = None
41- if input :
42- inbytes = input .encode ()
43- try :
44- yield proc .stdin .write (inbytes )
45- except StreamClosedError as exp :
46- # Apparently harmless
47- pass
48- proc .stdin .close ()
49- out = yield proc .stdout .read_until_close ()
50- eout = yield proc .stderr .read_until_close ()
51- proc .stdout .close ()
52- proc .stderr .close ()
53- eout = eout .decode ().strip ()
54- try :
55- err = yield proc .wait_for_exit ()
56- except CalledProcessError :
57- #self.log.error("Subprocess returned exitcode %s" % proc.returncode)
58- #self.log.error(eout)
59- raise RuntimeError (eout )
60- if err != 0 :
61- return err # exit error?
62- else :
63- out = out .decode ().strip ()
64- return out
6537
6638class BatchSpawnerBase (Spawner ):
6739 """Base class for spawners using resource manager batch job submission mechanisms
@@ -174,6 +146,35 @@ def parse_job_id(self, output):
174146 def cmd_formatted_for_batch (self ):
175147 return ' ' .join (self .cmd + self .get_args ())
176148
149+ @gen .coroutine
150+ def run_command (self , cmd , input = None , env = None ):
151+ proc = Subprocess (cmd , shell = True , env = env , stdin = Subprocess .STREAM , stdout = Subprocess .STREAM ,stderr = Subprocess .STREAM )
152+ inbytes = None
153+ if input :
154+ inbytes = input .encode ()
155+ try :
156+ yield proc .stdin .write (inbytes )
157+ except StreamClosedError as exp :
158+ # Apparently harmless
159+ pass
160+ proc .stdin .close ()
161+ out = yield proc .stdout .read_until_close ()
162+ eout = yield proc .stderr .read_until_close ()
163+ proc .stdout .close ()
164+ proc .stderr .close ()
165+ eout = eout .decode ().strip ()
166+ try :
167+ err = yield proc .wait_for_exit ()
168+ except CalledProcessError :
169+ #self.log.error("Subprocess returned exitcode %s" % proc.returncode)
170+ #self.log.error(eout)
171+ raise RuntimeError (eout )
172+ if err != 0 :
173+ return err # exit error?
174+ else :
175+ out = out .decode ().strip ()
176+ return out
177+
177178 @gen .coroutine
178179 def submit_batch_script (self ):
179180 subvars = self .get_req_subvars ()
@@ -184,7 +185,7 @@ def submit_batch_script(self):
184185 script = self .batch_script .format (** subvars )
185186 self .log .info ('Spawner submitting job using ' + cmd )
186187 self .log .info ('Spawner submitted script:\n ' + script )
187- out = yield run_command (cmd , input = script , env = self .get_env ())
188+ out = yield self . run_command (cmd , input = script , env = self .get_env ())
188189 try :
189190 self .log .info ('Job submitted. cmd: ' + cmd + ' output: ' + out )
190191 self .job_id = self .parse_job_id (out )
@@ -210,7 +211,7 @@ def read_job_state(self):
210211 cmd = self .batch_query_cmd .format (** subvars )
211212 self .log .debug ('Spawner querying job: ' + cmd )
212213 try :
213- out = yield run_command (cmd )
214+ out = yield self . run_command (cmd )
214215 self .job_status = out
215216 except Exception as e :
216217 self .log .error ('Error querying job ' + self .job_id )
@@ -228,7 +229,7 @@ def cancel_batch_job(self):
228229 subvars ['job_id' ] = self .job_id
229230 cmd = self .batch_cancel_cmd .format (** subvars )
230231 self .log .info ('Cancelling job ' + self .job_id + ': ' + cmd )
231- yield run_command (cmd )
232+ yield self . run_command (cmd )
232233
233234 def load_state (self , state ):
234235 """load job_id from state"""
0 commit comments