##// END OF EJS Templates
trigger default remote_profile_dir when profile_dir is set...
MinRK -
Show More
@@ -1,1345 +1,1354 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Facilities for launching IPython processes asynchronously.
3 Facilities for launching IPython processes asynchronously.
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * MinRK
8 * MinRK
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2011 The IPython Development Team
12 # Copyright (C) 2008-2011 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import copy
22 import copy
23 import logging
23 import logging
24 import os
24 import os
25 import pipes
25 import pipes
26 import stat
26 import stat
27 import sys
27 import sys
28 import time
28 import time
29
29
30 # signal imports, handling various platforms, versions
30 # signal imports, handling various platforms, versions
31
31
32 from signal import SIGINT, SIGTERM
32 from signal import SIGINT, SIGTERM
33 try:
33 try:
34 from signal import SIGKILL
34 from signal import SIGKILL
35 except ImportError:
35 except ImportError:
36 # Windows
36 # Windows
37 SIGKILL=SIGTERM
37 SIGKILL=SIGTERM
38
38
39 try:
39 try:
40 # Windows >= 2.7, 3.2
40 # Windows >= 2.7, 3.2
41 from signal import CTRL_C_EVENT as SIGINT
41 from signal import CTRL_C_EVENT as SIGINT
42 except ImportError:
42 except ImportError:
43 pass
43 pass
44
44
45 from subprocess import Popen, PIPE, STDOUT
45 from subprocess import Popen, PIPE, STDOUT
46 try:
46 try:
47 from subprocess import check_output
47 from subprocess import check_output
48 except ImportError:
48 except ImportError:
49 # pre-2.7, define check_output with Popen
49 # pre-2.7, define check_output with Popen
50 def check_output(*args, **kwargs):
50 def check_output(*args, **kwargs):
51 kwargs.update(dict(stdout=PIPE))
51 kwargs.update(dict(stdout=PIPE))
52 p = Popen(*args, **kwargs)
52 p = Popen(*args, **kwargs)
53 out,err = p.communicate()
53 out,err = p.communicate()
54 return out
54 return out
55
55
56 from zmq.eventloop import ioloop
56 from zmq.eventloop import ioloop
57
57
58 from IPython.config.application import Application
58 from IPython.config.application import Application
59 from IPython.config.configurable import LoggingConfigurable
59 from IPython.config.configurable import LoggingConfigurable
60 from IPython.utils.text import EvalFormatter
60 from IPython.utils.text import EvalFormatter
61 from IPython.utils.traitlets import (
61 from IPython.utils.traitlets import (
62 Any, Integer, CFloat, List, Unicode, Dict, Instance, HasTraits, CRegExp
62 Any, Integer, CFloat, List, Unicode, Dict, Instance, HasTraits, CRegExp
63 )
63 )
64 from IPython.utils.path import get_home_dir
64 from IPython.utils.path import get_home_dir
65 from IPython.utils.process import find_cmd, FindCmdError
65 from IPython.utils.process import find_cmd, FindCmdError
66
66
67 from .win32support import forward_read_events
67 from .win32support import forward_read_events
68
68
69 from .winhpcjob import IPControllerTask, IPEngineTask, IPControllerJob, IPEngineSetJob
69 from .winhpcjob import IPControllerTask, IPEngineTask, IPControllerJob, IPEngineSetJob
70
70
71 WINDOWS = os.name == 'nt'
71 WINDOWS = os.name == 'nt'
72
72
73 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
74 # Paths to the kernel apps
74 # Paths to the kernel apps
75 #-----------------------------------------------------------------------------
75 #-----------------------------------------------------------------------------
76
76
77 cmd = "from IPython.parallel.apps.%s import launch_new_instance; launch_new_instance()"
77 cmd = "from IPython.parallel.apps.%s import launch_new_instance; launch_new_instance()"
78
78
79 ipcluster_cmd_argv = [sys.executable, "-c", cmd % "ipclusterapp"]
79 ipcluster_cmd_argv = [sys.executable, "-c", cmd % "ipclusterapp"]
80
80
81 ipengine_cmd_argv = [sys.executable, "-c", cmd % "ipengineapp"]
81 ipengine_cmd_argv = [sys.executable, "-c", cmd % "ipengineapp"]
82
82
83 ipcontroller_cmd_argv = [sys.executable, "-c", cmd % "ipcontrollerapp"]
83 ipcontroller_cmd_argv = [sys.executable, "-c", cmd % "ipcontrollerapp"]
84
84
85 #-----------------------------------------------------------------------------
85 #-----------------------------------------------------------------------------
86 # Base launchers and errors
86 # Base launchers and errors
87 #-----------------------------------------------------------------------------
87 #-----------------------------------------------------------------------------
88
88
89
89
90 class LauncherError(Exception):
90 class LauncherError(Exception):
91 pass
91 pass
92
92
93
93
94 class ProcessStateError(LauncherError):
94 class ProcessStateError(LauncherError):
95 pass
95 pass
96
96
97
97
98 class UnknownStatus(LauncherError):
98 class UnknownStatus(LauncherError):
99 pass
99 pass
100
100
101
101
102 class BaseLauncher(LoggingConfigurable):
102 class BaseLauncher(LoggingConfigurable):
103 """An asbtraction for starting, stopping and signaling a process."""
103 """An asbtraction for starting, stopping and signaling a process."""
104
104
105 # In all of the launchers, the work_dir is where child processes will be
105 # In all of the launchers, the work_dir is where child processes will be
106 # run. This will usually be the profile_dir, but may not be. any work_dir
106 # run. This will usually be the profile_dir, but may not be. any work_dir
107 # passed into the __init__ method will override the config value.
107 # passed into the __init__ method will override the config value.
108 # This should not be used to set the work_dir for the actual engine
108 # This should not be used to set the work_dir for the actual engine
109 # and controller. Instead, use their own config files or the
109 # and controller. Instead, use their own config files or the
110 # controller_args, engine_args attributes of the launchers to add
110 # controller_args, engine_args attributes of the launchers to add
111 # the work_dir option.
111 # the work_dir option.
112 work_dir = Unicode(u'.')
112 work_dir = Unicode(u'.')
113 loop = Instance('zmq.eventloop.ioloop.IOLoop')
113 loop = Instance('zmq.eventloop.ioloop.IOLoop')
114
114
115 start_data = Any()
115 start_data = Any()
116 stop_data = Any()
116 stop_data = Any()
117
117
118 def _loop_default(self):
118 def _loop_default(self):
119 return ioloop.IOLoop.instance()
119 return ioloop.IOLoop.instance()
120
120
121 def __init__(self, work_dir=u'.', config=None, **kwargs):
121 def __init__(self, work_dir=u'.', config=None, **kwargs):
122 super(BaseLauncher, self).__init__(work_dir=work_dir, config=config, **kwargs)
122 super(BaseLauncher, self).__init__(work_dir=work_dir, config=config, **kwargs)
123 self.state = 'before' # can be before, running, after
123 self.state = 'before' # can be before, running, after
124 self.stop_callbacks = []
124 self.stop_callbacks = []
125 self.start_data = None
125 self.start_data = None
126 self.stop_data = None
126 self.stop_data = None
127
127
128 @property
128 @property
129 def args(self):
129 def args(self):
130 """A list of cmd and args that will be used to start the process.
130 """A list of cmd and args that will be used to start the process.
131
131
132 This is what is passed to :func:`spawnProcess` and the first element
132 This is what is passed to :func:`spawnProcess` and the first element
133 will be the process name.
133 will be the process name.
134 """
134 """
135 return self.find_args()
135 return self.find_args()
136
136
137 def find_args(self):
137 def find_args(self):
138 """The ``.args`` property calls this to find the args list.
138 """The ``.args`` property calls this to find the args list.
139
139
140 Subcommand should implement this to construct the cmd and args.
140 Subcommand should implement this to construct the cmd and args.
141 """
141 """
142 raise NotImplementedError('find_args must be implemented in a subclass')
142 raise NotImplementedError('find_args must be implemented in a subclass')
143
143
144 @property
144 @property
145 def arg_str(self):
145 def arg_str(self):
146 """The string form of the program arguments."""
146 """The string form of the program arguments."""
147 return ' '.join(self.args)
147 return ' '.join(self.args)
148
148
149 @property
149 @property
150 def running(self):
150 def running(self):
151 """Am I running."""
151 """Am I running."""
152 if self.state == 'running':
152 if self.state == 'running':
153 return True
153 return True
154 else:
154 else:
155 return False
155 return False
156
156
157 def start(self):
157 def start(self):
158 """Start the process."""
158 """Start the process."""
159 raise NotImplementedError('start must be implemented in a subclass')
159 raise NotImplementedError('start must be implemented in a subclass')
160
160
161 def stop(self):
161 def stop(self):
162 """Stop the process and notify observers of stopping.
162 """Stop the process and notify observers of stopping.
163
163
164 This method will return None immediately.
164 This method will return None immediately.
165 To observe the actual process stopping, see :meth:`on_stop`.
165 To observe the actual process stopping, see :meth:`on_stop`.
166 """
166 """
167 raise NotImplementedError('stop must be implemented in a subclass')
167 raise NotImplementedError('stop must be implemented in a subclass')
168
168
169 def on_stop(self, f):
169 def on_stop(self, f):
170 """Register a callback to be called with this Launcher's stop_data
170 """Register a callback to be called with this Launcher's stop_data
171 when the process actually finishes.
171 when the process actually finishes.
172 """
172 """
173 if self.state=='after':
173 if self.state=='after':
174 return f(self.stop_data)
174 return f(self.stop_data)
175 else:
175 else:
176 self.stop_callbacks.append(f)
176 self.stop_callbacks.append(f)
177
177
178 def notify_start(self, data):
178 def notify_start(self, data):
179 """Call this to trigger startup actions.
179 """Call this to trigger startup actions.
180
180
181 This logs the process startup and sets the state to 'running'. It is
181 This logs the process startup and sets the state to 'running'. It is
182 a pass-through so it can be used as a callback.
182 a pass-through so it can be used as a callback.
183 """
183 """
184
184
185 self.log.debug('Process %r started: %r', self.args[0], data)
185 self.log.debug('Process %r started: %r', self.args[0], data)
186 self.start_data = data
186 self.start_data = data
187 self.state = 'running'
187 self.state = 'running'
188 return data
188 return data
189
189
190 def notify_stop(self, data):
190 def notify_stop(self, data):
191 """Call this to trigger process stop actions.
191 """Call this to trigger process stop actions.
192
192
193 This logs the process stopping and sets the state to 'after'. Call
193 This logs the process stopping and sets the state to 'after'. Call
194 this to trigger callbacks registered via :meth:`on_stop`."""
194 this to trigger callbacks registered via :meth:`on_stop`."""
195
195
196 self.log.debug('Process %r stopped: %r', self.args[0], data)
196 self.log.debug('Process %r stopped: %r', self.args[0], data)
197 self.stop_data = data
197 self.stop_data = data
198 self.state = 'after'
198 self.state = 'after'
199 for i in range(len(self.stop_callbacks)):
199 for i in range(len(self.stop_callbacks)):
200 d = self.stop_callbacks.pop()
200 d = self.stop_callbacks.pop()
201 d(data)
201 d(data)
202 return data
202 return data
203
203
204 def signal(self, sig):
204 def signal(self, sig):
205 """Signal the process.
205 """Signal the process.
206
206
207 Parameters
207 Parameters
208 ----------
208 ----------
209 sig : str or int
209 sig : str or int
210 'KILL', 'INT', etc., or any signal number
210 'KILL', 'INT', etc., or any signal number
211 """
211 """
212 raise NotImplementedError('signal must be implemented in a subclass')
212 raise NotImplementedError('signal must be implemented in a subclass')
213
213
214 class ClusterAppMixin(HasTraits):
214 class ClusterAppMixin(HasTraits):
215 """MixIn for cluster args as traits"""
215 """MixIn for cluster args as traits"""
216 profile_dir=Unicode('')
216 profile_dir=Unicode('')
217 cluster_id=Unicode('')
217 cluster_id=Unicode('')
218
218
219 @property
219 @property
220 def cluster_args(self):
220 def cluster_args(self):
221 return ['--profile-dir', self.profile_dir, '--cluster-id', self.cluster_id]
221 return ['--profile-dir', self.profile_dir, '--cluster-id', self.cluster_id]
222
222
223 class ControllerMixin(ClusterAppMixin):
223 class ControllerMixin(ClusterAppMixin):
224 controller_cmd = List(ipcontroller_cmd_argv, config=True,
224 controller_cmd = List(ipcontroller_cmd_argv, config=True,
225 help="""Popen command to launch ipcontroller.""")
225 help="""Popen command to launch ipcontroller.""")
226 # Command line arguments to ipcontroller.
226 # Command line arguments to ipcontroller.
227 controller_args = List(['--log-to-file','--log-level=%i' % logging.INFO], config=True,
227 controller_args = List(['--log-to-file','--log-level=%i' % logging.INFO], config=True,
228 help="""command-line args to pass to ipcontroller""")
228 help="""command-line args to pass to ipcontroller""")
229
229
230 class EngineMixin(ClusterAppMixin):
230 class EngineMixin(ClusterAppMixin):
231 engine_cmd = List(ipengine_cmd_argv, config=True,
231 engine_cmd = List(ipengine_cmd_argv, config=True,
232 help="""command to launch the Engine.""")
232 help="""command to launch the Engine.""")
233 # Command line arguments for ipengine.
233 # Command line arguments for ipengine.
234 engine_args = List(['--log-to-file','--log-level=%i' % logging.INFO], config=True,
234 engine_args = List(['--log-to-file','--log-level=%i' % logging.INFO], config=True,
235 help="command-line arguments to pass to ipengine"
235 help="command-line arguments to pass to ipengine"
236 )
236 )
237
237
238
238
239 #-----------------------------------------------------------------------------
239 #-----------------------------------------------------------------------------
240 # Local process launchers
240 # Local process launchers
241 #-----------------------------------------------------------------------------
241 #-----------------------------------------------------------------------------
242
242
243
243
244 class LocalProcessLauncher(BaseLauncher):
244 class LocalProcessLauncher(BaseLauncher):
245 """Start and stop an external process in an asynchronous manner.
245 """Start and stop an external process in an asynchronous manner.
246
246
247 This will launch the external process with a working directory of
247 This will launch the external process with a working directory of
248 ``self.work_dir``.
248 ``self.work_dir``.
249 """
249 """
250
250
251 # This is used to to construct self.args, which is passed to
251 # This is used to to construct self.args, which is passed to
252 # spawnProcess.
252 # spawnProcess.
253 cmd_and_args = List([])
253 cmd_and_args = List([])
254 poll_frequency = Integer(100) # in ms
254 poll_frequency = Integer(100) # in ms
255
255
256 def __init__(self, work_dir=u'.', config=None, **kwargs):
256 def __init__(self, work_dir=u'.', config=None, **kwargs):
257 super(LocalProcessLauncher, self).__init__(
257 super(LocalProcessLauncher, self).__init__(
258 work_dir=work_dir, config=config, **kwargs
258 work_dir=work_dir, config=config, **kwargs
259 )
259 )
260 self.process = None
260 self.process = None
261 self.poller = None
261 self.poller = None
262
262
263 def find_args(self):
263 def find_args(self):
264 return self.cmd_and_args
264 return self.cmd_and_args
265
265
266 def start(self):
266 def start(self):
267 self.log.debug("Starting %s: %r", self.__class__.__name__, self.args)
267 self.log.debug("Starting %s: %r", self.__class__.__name__, self.args)
268 if self.state == 'before':
268 if self.state == 'before':
269 self.process = Popen(self.args,
269 self.process = Popen(self.args,
270 stdout=PIPE,stderr=PIPE,stdin=PIPE,
270 stdout=PIPE,stderr=PIPE,stdin=PIPE,
271 env=os.environ,
271 env=os.environ,
272 cwd=self.work_dir
272 cwd=self.work_dir
273 )
273 )
274 if WINDOWS:
274 if WINDOWS:
275 self.stdout = forward_read_events(self.process.stdout)
275 self.stdout = forward_read_events(self.process.stdout)
276 self.stderr = forward_read_events(self.process.stderr)
276 self.stderr = forward_read_events(self.process.stderr)
277 else:
277 else:
278 self.stdout = self.process.stdout.fileno()
278 self.stdout = self.process.stdout.fileno()
279 self.stderr = self.process.stderr.fileno()
279 self.stderr = self.process.stderr.fileno()
280 self.loop.add_handler(self.stdout, self.handle_stdout, self.loop.READ)
280 self.loop.add_handler(self.stdout, self.handle_stdout, self.loop.READ)
281 self.loop.add_handler(self.stderr, self.handle_stderr, self.loop.READ)
281 self.loop.add_handler(self.stderr, self.handle_stderr, self.loop.READ)
282 self.poller = ioloop.PeriodicCallback(self.poll, self.poll_frequency, self.loop)
282 self.poller = ioloop.PeriodicCallback(self.poll, self.poll_frequency, self.loop)
283 self.poller.start()
283 self.poller.start()
284 self.notify_start(self.process.pid)
284 self.notify_start(self.process.pid)
285 else:
285 else:
286 s = 'The process was already started and has state: %r' % self.state
286 s = 'The process was already started and has state: %r' % self.state
287 raise ProcessStateError(s)
287 raise ProcessStateError(s)
288
288
289 def stop(self):
289 def stop(self):
290 return self.interrupt_then_kill()
290 return self.interrupt_then_kill()
291
291
292 def signal(self, sig):
292 def signal(self, sig):
293 if self.state == 'running':
293 if self.state == 'running':
294 if WINDOWS and sig != SIGINT:
294 if WINDOWS and sig != SIGINT:
295 # use Windows tree-kill for better child cleanup
295 # use Windows tree-kill for better child cleanup
296 check_output(['taskkill', '-pid', str(self.process.pid), '-t', '-f'])
296 check_output(['taskkill', '-pid', str(self.process.pid), '-t', '-f'])
297 else:
297 else:
298 self.process.send_signal(sig)
298 self.process.send_signal(sig)
299
299
300 def interrupt_then_kill(self, delay=2.0):
300 def interrupt_then_kill(self, delay=2.0):
301 """Send INT, wait a delay and then send KILL."""
301 """Send INT, wait a delay and then send KILL."""
302 try:
302 try:
303 self.signal(SIGINT)
303 self.signal(SIGINT)
304 except Exception:
304 except Exception:
305 self.log.debug("interrupt failed")
305 self.log.debug("interrupt failed")
306 pass
306 pass
307 self.killer = ioloop.DelayedCallback(lambda : self.signal(SIGKILL), delay*1000, self.loop)
307 self.killer = ioloop.DelayedCallback(lambda : self.signal(SIGKILL), delay*1000, self.loop)
308 self.killer.start()
308 self.killer.start()
309
309
310 # callbacks, etc:
310 # callbacks, etc:
311
311
312 def handle_stdout(self, fd, events):
312 def handle_stdout(self, fd, events):
313 if WINDOWS:
313 if WINDOWS:
314 line = self.stdout.recv()
314 line = self.stdout.recv()
315 else:
315 else:
316 line = self.process.stdout.readline()
316 line = self.process.stdout.readline()
317 # a stopped process will be readable but return empty strings
317 # a stopped process will be readable but return empty strings
318 if line:
318 if line:
319 self.log.debug(line[:-1])
319 self.log.debug(line[:-1])
320 else:
320 else:
321 self.poll()
321 self.poll()
322
322
323 def handle_stderr(self, fd, events):
323 def handle_stderr(self, fd, events):
324 if WINDOWS:
324 if WINDOWS:
325 line = self.stderr.recv()
325 line = self.stderr.recv()
326 else:
326 else:
327 line = self.process.stderr.readline()
327 line = self.process.stderr.readline()
328 # a stopped process will be readable but return empty strings
328 # a stopped process will be readable but return empty strings
329 if line:
329 if line:
330 self.log.debug(line[:-1])
330 self.log.debug(line[:-1])
331 else:
331 else:
332 self.poll()
332 self.poll()
333
333
334 def poll(self):
334 def poll(self):
335 status = self.process.poll()
335 status = self.process.poll()
336 if status is not None:
336 if status is not None:
337 self.poller.stop()
337 self.poller.stop()
338 self.loop.remove_handler(self.stdout)
338 self.loop.remove_handler(self.stdout)
339 self.loop.remove_handler(self.stderr)
339 self.loop.remove_handler(self.stderr)
340 self.notify_stop(dict(exit_code=status, pid=self.process.pid))
340 self.notify_stop(dict(exit_code=status, pid=self.process.pid))
341 return status
341 return status
342
342
343 class LocalControllerLauncher(LocalProcessLauncher, ControllerMixin):
343 class LocalControllerLauncher(LocalProcessLauncher, ControllerMixin):
344 """Launch a controller as a regular external process."""
344 """Launch a controller as a regular external process."""
345
345
346 def find_args(self):
346 def find_args(self):
347 return self.controller_cmd + self.cluster_args + self.controller_args
347 return self.controller_cmd + self.cluster_args + self.controller_args
348
348
349 def start(self):
349 def start(self):
350 """Start the controller by profile_dir."""
350 """Start the controller by profile_dir."""
351 return super(LocalControllerLauncher, self).start()
351 return super(LocalControllerLauncher, self).start()
352
352
353
353
354 class LocalEngineLauncher(LocalProcessLauncher, EngineMixin):
354 class LocalEngineLauncher(LocalProcessLauncher, EngineMixin):
355 """Launch a single engine as a regular externall process."""
355 """Launch a single engine as a regular externall process."""
356
356
357 def find_args(self):
357 def find_args(self):
358 return self.engine_cmd + self.cluster_args + self.engine_args
358 return self.engine_cmd + self.cluster_args + self.engine_args
359
359
360
360
361 class LocalEngineSetLauncher(LocalEngineLauncher):
361 class LocalEngineSetLauncher(LocalEngineLauncher):
362 """Launch a set of engines as regular external processes."""
362 """Launch a set of engines as regular external processes."""
363
363
364 delay = CFloat(0.1, config=True,
364 delay = CFloat(0.1, config=True,
365 help="""delay (in seconds) between starting each engine after the first.
365 help="""delay (in seconds) between starting each engine after the first.
366 This can help force the engines to get their ids in order, or limit
366 This can help force the engines to get their ids in order, or limit
367 process flood when starting many engines."""
367 process flood when starting many engines."""
368 )
368 )
369
369
370 # launcher class
370 # launcher class
371 launcher_class = LocalEngineLauncher
371 launcher_class = LocalEngineLauncher
372
372
373 launchers = Dict()
373 launchers = Dict()
374 stop_data = Dict()
374 stop_data = Dict()
375
375
376 def __init__(self, work_dir=u'.', config=None, **kwargs):
376 def __init__(self, work_dir=u'.', config=None, **kwargs):
377 super(LocalEngineSetLauncher, self).__init__(
377 super(LocalEngineSetLauncher, self).__init__(
378 work_dir=work_dir, config=config, **kwargs
378 work_dir=work_dir, config=config, **kwargs
379 )
379 )
380 self.stop_data = {}
380 self.stop_data = {}
381
381
382 def start(self, n):
382 def start(self, n):
383 """Start n engines by profile or profile_dir."""
383 """Start n engines by profile or profile_dir."""
384 dlist = []
384 dlist = []
385 for i in range(n):
385 for i in range(n):
386 if i > 0:
386 if i > 0:
387 time.sleep(self.delay)
387 time.sleep(self.delay)
388 el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
388 el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
389 profile_dir=self.profile_dir, cluster_id=self.cluster_id,
389 profile_dir=self.profile_dir, cluster_id=self.cluster_id,
390 )
390 )
391
391
392 # Copy the engine args over to each engine launcher.
392 # Copy the engine args over to each engine launcher.
393 el.engine_cmd = copy.deepcopy(self.engine_cmd)
393 el.engine_cmd = copy.deepcopy(self.engine_cmd)
394 el.engine_args = copy.deepcopy(self.engine_args)
394 el.engine_args = copy.deepcopy(self.engine_args)
395 el.on_stop(self._notice_engine_stopped)
395 el.on_stop(self._notice_engine_stopped)
396 d = el.start()
396 d = el.start()
397 self.launchers[i] = el
397 self.launchers[i] = el
398 dlist.append(d)
398 dlist.append(d)
399 self.notify_start(dlist)
399 self.notify_start(dlist)
400 return dlist
400 return dlist
401
401
402 def find_args(self):
402 def find_args(self):
403 return ['engine set']
403 return ['engine set']
404
404
405 def signal(self, sig):
405 def signal(self, sig):
406 dlist = []
406 dlist = []
407 for el in self.launchers.itervalues():
407 for el in self.launchers.itervalues():
408 d = el.signal(sig)
408 d = el.signal(sig)
409 dlist.append(d)
409 dlist.append(d)
410 return dlist
410 return dlist
411
411
412 def interrupt_then_kill(self, delay=1.0):
412 def interrupt_then_kill(self, delay=1.0):
413 dlist = []
413 dlist = []
414 for el in self.launchers.itervalues():
414 for el in self.launchers.itervalues():
415 d = el.interrupt_then_kill(delay)
415 d = el.interrupt_then_kill(delay)
416 dlist.append(d)
416 dlist.append(d)
417 return dlist
417 return dlist
418
418
419 def stop(self):
419 def stop(self):
420 return self.interrupt_then_kill()
420 return self.interrupt_then_kill()
421
421
422 def _notice_engine_stopped(self, data):
422 def _notice_engine_stopped(self, data):
423 pid = data['pid']
423 pid = data['pid']
424 for idx,el in self.launchers.iteritems():
424 for idx,el in self.launchers.iteritems():
425 if el.process.pid == pid:
425 if el.process.pid == pid:
426 break
426 break
427 self.launchers.pop(idx)
427 self.launchers.pop(idx)
428 self.stop_data[idx] = data
428 self.stop_data[idx] = data
429 if not self.launchers:
429 if not self.launchers:
430 self.notify_stop(self.stop_data)
430 self.notify_stop(self.stop_data)
431
431
432
432
433 #-----------------------------------------------------------------------------
433 #-----------------------------------------------------------------------------
434 # MPI launchers
434 # MPI launchers
435 #-----------------------------------------------------------------------------
435 #-----------------------------------------------------------------------------
436
436
437
437
438 class MPILauncher(LocalProcessLauncher):
438 class MPILauncher(LocalProcessLauncher):
439 """Launch an external process using mpiexec."""
439 """Launch an external process using mpiexec."""
440
440
441 mpi_cmd = List(['mpiexec'], config=True,
441 mpi_cmd = List(['mpiexec'], config=True,
442 help="The mpiexec command to use in starting the process."
442 help="The mpiexec command to use in starting the process."
443 )
443 )
444 mpi_args = List([], config=True,
444 mpi_args = List([], config=True,
445 help="The command line arguments to pass to mpiexec."
445 help="The command line arguments to pass to mpiexec."
446 )
446 )
447 program = List(['date'],
447 program = List(['date'],
448 help="The program to start via mpiexec.")
448 help="The program to start via mpiexec.")
449 program_args = List([],
449 program_args = List([],
450 help="The command line argument to the program."
450 help="The command line argument to the program."
451 )
451 )
452 n = Integer(1)
452 n = Integer(1)
453
453
454 def __init__(self, *args, **kwargs):
454 def __init__(self, *args, **kwargs):
455 # deprecation for old MPIExec names:
455 # deprecation for old MPIExec names:
456 config = kwargs.get('config', {})
456 config = kwargs.get('config', {})
457 for oldname in ('MPIExecLauncher', 'MPIExecControllerLauncher', 'MPIExecEngineSetLauncher'):
457 for oldname in ('MPIExecLauncher', 'MPIExecControllerLauncher', 'MPIExecEngineSetLauncher'):
458 deprecated = config.get(oldname)
458 deprecated = config.get(oldname)
459 if deprecated:
459 if deprecated:
460 newname = oldname.replace('MPIExec', 'MPI')
460 newname = oldname.replace('MPIExec', 'MPI')
461 config[newname].update(deprecated)
461 config[newname].update(deprecated)
462 self.log.warn("WARNING: %s name has been deprecated, use %s", oldname, newname)
462 self.log.warn("WARNING: %s name has been deprecated, use %s", oldname, newname)
463
463
464 super(MPILauncher, self).__init__(*args, **kwargs)
464 super(MPILauncher, self).__init__(*args, **kwargs)
465
465
466 def find_args(self):
466 def find_args(self):
467 """Build self.args using all the fields."""
467 """Build self.args using all the fields."""
468 return self.mpi_cmd + ['-n', str(self.n)] + self.mpi_args + \
468 return self.mpi_cmd + ['-n', str(self.n)] + self.mpi_args + \
469 self.program + self.program_args
469 self.program + self.program_args
470
470
471 def start(self, n):
471 def start(self, n):
472 """Start n instances of the program using mpiexec."""
472 """Start n instances of the program using mpiexec."""
473 self.n = n
473 self.n = n
474 return super(MPILauncher, self).start()
474 return super(MPILauncher, self).start()
475
475
476
476
477 class MPIControllerLauncher(MPILauncher, ControllerMixin):
477 class MPIControllerLauncher(MPILauncher, ControllerMixin):
478 """Launch a controller using mpiexec."""
478 """Launch a controller using mpiexec."""
479
479
480 # alias back to *non-configurable* program[_args] for use in find_args()
480 # alias back to *non-configurable* program[_args] for use in find_args()
481 # this way all Controller/EngineSetLaunchers have the same form, rather
481 # this way all Controller/EngineSetLaunchers have the same form, rather
482 # than *some* having `program_args` and others `controller_args`
482 # than *some* having `program_args` and others `controller_args`
483 @property
483 @property
484 def program(self):
484 def program(self):
485 return self.controller_cmd
485 return self.controller_cmd
486
486
487 @property
487 @property
488 def program_args(self):
488 def program_args(self):
489 return self.cluster_args + self.controller_args
489 return self.cluster_args + self.controller_args
490
490
491 def start(self):
491 def start(self):
492 """Start the controller by profile_dir."""
492 """Start the controller by profile_dir."""
493 return super(MPIControllerLauncher, self).start(1)
493 return super(MPIControllerLauncher, self).start(1)
494
494
495
495
496 class MPIEngineSetLauncher(MPILauncher, EngineMixin):
496 class MPIEngineSetLauncher(MPILauncher, EngineMixin):
497 """Launch engines using mpiexec"""
497 """Launch engines using mpiexec"""
498
498
499 # alias back to *non-configurable* program[_args] for use in find_args()
499 # alias back to *non-configurable* program[_args] for use in find_args()
500 # this way all Controller/EngineSetLaunchers have the same form, rather
500 # this way all Controller/EngineSetLaunchers have the same form, rather
501 # than *some* having `program_args` and others `controller_args`
501 # than *some* having `program_args` and others `controller_args`
502 @property
502 @property
503 def program(self):
503 def program(self):
504 return self.engine_cmd
504 return self.engine_cmd
505
505
506 @property
506 @property
507 def program_args(self):
507 def program_args(self):
508 return self.cluster_args + self.engine_args
508 return self.cluster_args + self.engine_args
509
509
510 def start(self, n):
510 def start(self, n):
511 """Start n engines by profile or profile_dir."""
511 """Start n engines by profile or profile_dir."""
512 self.n = n
512 self.n = n
513 return super(MPIEngineSetLauncher, self).start(n)
513 return super(MPIEngineSetLauncher, self).start(n)
514
514
515 # deprecated MPIExec names
515 # deprecated MPIExec names
516 class DeprecatedMPILauncher(object):
516 class DeprecatedMPILauncher(object):
517 def warn(self):
517 def warn(self):
518 oldname = self.__class__.__name__
518 oldname = self.__class__.__name__
519 newname = oldname.replace('MPIExec', 'MPI')
519 newname = oldname.replace('MPIExec', 'MPI')
520 self.log.warn("WARNING: %s name is deprecated, use %s", oldname, newname)
520 self.log.warn("WARNING: %s name is deprecated, use %s", oldname, newname)
521
521
522 class MPIExecLauncher(MPILauncher, DeprecatedMPILauncher):
522 class MPIExecLauncher(MPILauncher, DeprecatedMPILauncher):
523 """Deprecated, use MPILauncher"""
523 """Deprecated, use MPILauncher"""
524 def __init__(self, *args, **kwargs):
524 def __init__(self, *args, **kwargs):
525 super(MPIExecLauncher, self).__init__(*args, **kwargs)
525 super(MPIExecLauncher, self).__init__(*args, **kwargs)
526 self.warn()
526 self.warn()
527
527
528 class MPIExecControllerLauncher(MPIControllerLauncher, DeprecatedMPILauncher):
528 class MPIExecControllerLauncher(MPIControllerLauncher, DeprecatedMPILauncher):
529 """Deprecated, use MPIControllerLauncher"""
529 """Deprecated, use MPIControllerLauncher"""
530 def __init__(self, *args, **kwargs):
530 def __init__(self, *args, **kwargs):
531 super(MPIExecControllerLauncher, self).__init__(*args, **kwargs)
531 super(MPIExecControllerLauncher, self).__init__(*args, **kwargs)
532 self.warn()
532 self.warn()
533
533
534 class MPIExecEngineSetLauncher(MPIEngineSetLauncher, DeprecatedMPILauncher):
534 class MPIExecEngineSetLauncher(MPIEngineSetLauncher, DeprecatedMPILauncher):
535 """Deprecated, use MPIEngineSetLauncher"""
535 """Deprecated, use MPIEngineSetLauncher"""
536 def __init__(self, *args, **kwargs):
536 def __init__(self, *args, **kwargs):
537 super(MPIExecEngineSetLauncher, self).__init__(*args, **kwargs)
537 super(MPIExecEngineSetLauncher, self).__init__(*args, **kwargs)
538 self.warn()
538 self.warn()
539
539
540
540
541 #-----------------------------------------------------------------------------
541 #-----------------------------------------------------------------------------
542 # SSH launchers
542 # SSH launchers
543 #-----------------------------------------------------------------------------
543 #-----------------------------------------------------------------------------
544
544
545 # TODO: Get SSH Launcher back to level of sshx in 0.10.2
545 # TODO: Get SSH Launcher back to level of sshx in 0.10.2
546
546
547 class SSHLauncher(LocalProcessLauncher):
547 class SSHLauncher(LocalProcessLauncher):
548 """A minimal launcher for ssh.
548 """A minimal launcher for ssh.
549
549
550 To be useful this will probably have to be extended to use the ``sshx``
550 To be useful this will probably have to be extended to use the ``sshx``
551 idea for environment variables. There could be other things this needs
551 idea for environment variables. There could be other things this needs
552 as well.
552 as well.
553 """
553 """
554
554
555 ssh_cmd = List(['ssh'], config=True,
555 ssh_cmd = List(['ssh'], config=True,
556 help="command for starting ssh")
556 help="command for starting ssh")
557 ssh_args = List(['-tt'], config=True,
557 ssh_args = List(['-tt'], config=True,
558 help="args to pass to ssh")
558 help="args to pass to ssh")
559 scp_cmd = List(['scp'], config=True,
559 scp_cmd = List(['scp'], config=True,
560 help="command for sending files")
560 help="command for sending files")
561 program = List(['date'],
561 program = List(['date'],
562 help="Program to launch via ssh")
562 help="Program to launch via ssh")
563 program_args = List([],
563 program_args = List([],
564 help="args to pass to remote program")
564 help="args to pass to remote program")
565 hostname = Unicode('', config=True,
565 hostname = Unicode('', config=True,
566 help="hostname on which to launch the program")
566 help="hostname on which to launch the program")
567 user = Unicode('', config=True,
567 user = Unicode('', config=True,
568 help="username for ssh")
568 help="username for ssh")
569 location = Unicode('', config=True,
569 location = Unicode('', config=True,
570 help="user@hostname location for ssh in one setting")
570 help="user@hostname location for ssh in one setting")
571 to_fetch = List([], config=True,
571 to_fetch = List([], config=True,
572 help="List of (remote, local) files to fetch after starting")
572 help="List of (remote, local) files to fetch after starting")
573 to_send = List([], config=True,
573 to_send = List([], config=True,
574 help="List of (local, remote) files to send before starting")
574 help="List of (local, remote) files to send before starting")
575
575
576 def _hostname_changed(self, name, old, new):
576 def _hostname_changed(self, name, old, new):
577 if self.user:
577 if self.user:
578 self.location = u'%s@%s' % (self.user, new)
578 self.location = u'%s@%s' % (self.user, new)
579 else:
579 else:
580 self.location = new
580 self.location = new
581
581
582 def _user_changed(self, name, old, new):
582 def _user_changed(self, name, old, new):
583 self.location = u'%s@%s' % (new, self.hostname)
583 self.location = u'%s@%s' % (new, self.hostname)
584
584
585 def find_args(self):
585 def find_args(self):
586 return self.ssh_cmd + self.ssh_args + [self.location] + \
586 return self.ssh_cmd + self.ssh_args + [self.location] + \
587 list(map(pipes.quote, self.program + self.program_args))
587 list(map(pipes.quote, self.program + self.program_args))
588
588
589 def _send_file(self, local, remote):
589 def _send_file(self, local, remote):
590 """send a single file"""
590 """send a single file"""
591 remote = "%s:%s" % (self.location, remote)
591 remote = "%s:%s" % (self.location, remote)
592 for i in range(10):
592 for i in range(10):
593 if not os.path.exists(local):
593 if not os.path.exists(local):
594 self.log.debug("waiting for %s" % local)
594 self.log.debug("waiting for %s" % local)
595 time.sleep(1)
595 time.sleep(1)
596 else:
596 else:
597 break
597 break
598 self.log.info("sending %s to %s", local, remote)
598 self.log.info("sending %s to %s", local, remote)
599 check_output(self.scp_cmd + [local, remote])
599 check_output(self.scp_cmd + [local, remote])
600
600
601 def send_files(self):
601 def send_files(self):
602 """send our files (called before start)"""
602 """send our files (called before start)"""
603 if not self.to_send:
603 if not self.to_send:
604 return
604 return
605 for local_file, remote_file in self.to_send:
605 for local_file, remote_file in self.to_send:
606 self._send_file(local_file, remote_file)
606 self._send_file(local_file, remote_file)
607
607
608 def _fetch_file(self, remote, local):
608 def _fetch_file(self, remote, local):
609 """fetch a single file"""
609 """fetch a single file"""
610 full_remote = "%s:%s" % (self.location, remote)
610 full_remote = "%s:%s" % (self.location, remote)
611 self.log.info("fetching %s from %s", local, full_remote)
611 self.log.info("fetching %s from %s", local, full_remote)
612 for i in range(10):
612 for i in range(10):
613 # wait up to 10s for remote file to exist
613 # wait up to 10s for remote file to exist
614 check = check_output(self.ssh_cmd + self.ssh_args + \
614 check = check_output(self.ssh_cmd + self.ssh_args + \
615 [self.location, 'test -e', remote, "&& echo 'yes' || echo 'no'"])
615 [self.location, 'test -e', remote, "&& echo 'yes' || echo 'no'"])
616 check = check.strip()
616 check = check.strip()
617 if check == 'no':
617 if check == 'no':
618 time.sleep(1)
618 time.sleep(1)
619 elif check == 'yes':
619 elif check == 'yes':
620 break
620 break
621 check_output(self.scp_cmd + [full_remote, local])
621 check_output(self.scp_cmd + [full_remote, local])
622
622
623 def fetch_files(self):
623 def fetch_files(self):
624 """fetch remote files (called after start)"""
624 """fetch remote files (called after start)"""
625 if not self.to_fetch:
625 if not self.to_fetch:
626 return
626 return
627 for remote_file, local_file in self.to_fetch:
627 for remote_file, local_file in self.to_fetch:
628 self._fetch_file(remote_file, local_file)
628 self._fetch_file(remote_file, local_file)
629
629
630 def start(self, hostname=None, user=None):
630 def start(self, hostname=None, user=None):
631 if hostname is not None:
631 if hostname is not None:
632 self.hostname = hostname
632 self.hostname = hostname
633 if user is not None:
633 if user is not None:
634 self.user = user
634 self.user = user
635
635
636 self.send_files()
636 self.send_files()
637 super(SSHLauncher, self).start()
637 super(SSHLauncher, self).start()
638 self.fetch_files()
638 self.fetch_files()
639
639
640 def signal(self, sig):
640 def signal(self, sig):
641 if self.state == 'running':
641 if self.state == 'running':
642 # send escaped ssh connection-closer
642 # send escaped ssh connection-closer
643 self.process.stdin.write('~.')
643 self.process.stdin.write('~.')
644 self.process.stdin.flush()
644 self.process.stdin.flush()
645
645
646 class SSHClusterLauncher(SSHLauncher):
646 class SSHClusterLauncher(SSHLauncher):
647
647
648 remote_profile_dir = Unicode('', config=True,
648 remote_profile_dir = Unicode('', config=True,
649 help="""The remote profile_dir to use.
649 help="""The remote profile_dir to use.
650
650
651 If not specified, use calling profile, stripping out possible leading homedir.
651 If not specified, use calling profile, stripping out possible leading homedir.
652 """)
652 """)
653
653
654 def _remote_profile_dir_default(self):
654 def _profile_dir_changed(self, name, old, new):
655 """turns /home/you/.ipython/profile_foo into .ipython/profile_foo
655 if not self.remote_profile_dir:
656 """
656 # trigger remote_profile_dir_default logic again,
657 # in case it was already triggered before profile_dir was set
658 self.remote_profile_dir = self._strip_home(new)
659
660 @staticmethod
661 def _strip_home(path):
662 """turns /home/you/.ipython/profile_foo into .ipython/profile_foo"""
657 home = get_home_dir()
663 home = get_home_dir()
658 if not home.endswith('/'):
664 if not home.endswith('/'):
659 home = home+'/'
665 home = home+'/'
660
666
661 if self.profile_dir.startswith(home):
667 if path.startswith(home):
662 return self.profile_dir[len(home):]
668 return path[len(home):]
663 else:
669 else:
664 return self.profile_dir
670 return path
671
672 def _remote_profile_dir_default(self):
673 return self._strip_home(self.profile_dir)
665
674
666 def _cluster_id_changed(self, name, old, new):
675 def _cluster_id_changed(self, name, old, new):
667 if new:
676 if new:
668 raise ValueError("cluster id not supported by SSH launchers")
677 raise ValueError("cluster id not supported by SSH launchers")
669
678
670 @property
679 @property
671 def cluster_args(self):
680 def cluster_args(self):
672 return ['--profile-dir', self.remote_profile_dir]
681 return ['--profile-dir', self.remote_profile_dir]
673
682
674 class SSHControllerLauncher(SSHClusterLauncher, ControllerMixin):
683 class SSHControllerLauncher(SSHClusterLauncher, ControllerMixin):
675
684
676 # alias back to *non-configurable* program[_args] for use in find_args()
685 # alias back to *non-configurable* program[_args] for use in find_args()
677 # this way all Controller/EngineSetLaunchers have the same form, rather
686 # this way all Controller/EngineSetLaunchers have the same form, rather
678 # than *some* having `program_args` and others `controller_args`
687 # than *some* having `program_args` and others `controller_args`
679
688
680 def _controller_cmd_default(self):
689 def _controller_cmd_default(self):
681 return ['ipcontroller']
690 return ['ipcontroller']
682
691
683 @property
692 @property
684 def program(self):
693 def program(self):
685 return self.controller_cmd
694 return self.controller_cmd
686
695
687 @property
696 @property
688 def program_args(self):
697 def program_args(self):
689 return self.cluster_args + self.controller_args
698 return self.cluster_args + self.controller_args
690
699
691 def _to_fetch_default(self):
700 def _to_fetch_default(self):
692 return [
701 return [
693 (os.path.join(self.remote_profile_dir, 'security', cf),
702 (os.path.join(self.remote_profile_dir, 'security', cf),
694 os.path.join(self.profile_dir, 'security', cf),)
703 os.path.join(self.profile_dir, 'security', cf),)
695 for cf in ('ipcontroller-client.json', 'ipcontroller-engine.json')
704 for cf in ('ipcontroller-client.json', 'ipcontroller-engine.json')
696 ]
705 ]
697
706
698 class SSHEngineLauncher(SSHClusterLauncher, EngineMixin):
707 class SSHEngineLauncher(SSHClusterLauncher, EngineMixin):
699
708
700 # alias back to *non-configurable* program[_args] for use in find_args()
709 # alias back to *non-configurable* program[_args] for use in find_args()
701 # this way all Controller/EngineSetLaunchers have the same form, rather
710 # this way all Controller/EngineSetLaunchers have the same form, rather
702 # than *some* having `program_args` and others `controller_args`
711 # than *some* having `program_args` and others `controller_args`
703
712
704 def _engine_cmd_default(self):
713 def _engine_cmd_default(self):
705 return ['ipengine']
714 return ['ipengine']
706
715
707 @property
716 @property
708 def program(self):
717 def program(self):
709 return self.engine_cmd
718 return self.engine_cmd
710
719
711 @property
720 @property
712 def program_args(self):
721 def program_args(self):
713 return self.cluster_args + self.engine_args
722 return self.cluster_args + self.engine_args
714
723
715 def _to_send_default(self):
724 def _to_send_default(self):
716 return [
725 return [
717 (os.path.join(self.profile_dir, 'security', cf),
726 (os.path.join(self.profile_dir, 'security', cf),
718 os.path.join(self.remote_profile_dir, 'security', cf))
727 os.path.join(self.remote_profile_dir, 'security', cf))
719 for cf in ('ipcontroller-client.json', 'ipcontroller-engine.json')
728 for cf in ('ipcontroller-client.json', 'ipcontroller-engine.json')
720 ]
729 ]
721
730
722
731
723 class SSHEngineSetLauncher(LocalEngineSetLauncher):
732 class SSHEngineSetLauncher(LocalEngineSetLauncher):
724 launcher_class = SSHEngineLauncher
733 launcher_class = SSHEngineLauncher
725 engines = Dict(config=True,
734 engines = Dict(config=True,
726 help="""dict of engines to launch. This is a dict by hostname of ints,
735 help="""dict of engines to launch. This is a dict by hostname of ints,
727 corresponding to the number of engines to start on that host.""")
736 corresponding to the number of engines to start on that host.""")
728
737
729 def _engine_cmd_default(self):
738 def _engine_cmd_default(self):
730 return ['ipengine']
739 return ['ipengine']
731
740
732 @property
741 @property
733 def engine_count(self):
742 def engine_count(self):
734 """determine engine count from `engines` dict"""
743 """determine engine count from `engines` dict"""
735 count = 0
744 count = 0
736 for n in self.engines.itervalues():
745 for n in self.engines.itervalues():
737 if isinstance(n, (tuple,list)):
746 if isinstance(n, (tuple,list)):
738 n,args = n
747 n,args = n
739 count += n
748 count += n
740 return count
749 return count
741
750
742 def start(self, n):
751 def start(self, n):
743 """Start engines by profile or profile_dir.
752 """Start engines by profile or profile_dir.
744 `n` is ignored, and the `engines` config property is used instead.
753 `n` is ignored, and the `engines` config property is used instead.
745 """
754 """
746
755
747 dlist = []
756 dlist = []
748 for host, n in self.engines.iteritems():
757 for host, n in self.engines.iteritems():
749 if isinstance(n, (tuple, list)):
758 if isinstance(n, (tuple, list)):
750 n, args = n
759 n, args = n
751 else:
760 else:
752 args = copy.deepcopy(self.engine_args)
761 args = copy.deepcopy(self.engine_args)
753
762
754 if '@' in host:
763 if '@' in host:
755 user,host = host.split('@',1)
764 user,host = host.split('@',1)
756 else:
765 else:
757 user=None
766 user=None
758 for i in range(n):
767 for i in range(n):
759 if i > 0:
768 if i > 0:
760 time.sleep(self.delay)
769 time.sleep(self.delay)
761 el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
770 el = self.launcher_class(work_dir=self.work_dir, config=self.config, log=self.log,
762 profile_dir=self.profile_dir, cluster_id=self.cluster_id,
771 profile_dir=self.profile_dir, cluster_id=self.cluster_id,
763 )
772 )
764 if i > 0:
773 if i > 0:
765 # only send files for the first engine on each host
774 # only send files for the first engine on each host
766 el.to_send = []
775 el.to_send = []
767
776
768 # Copy the engine args over to each engine launcher.
777 # Copy the engine args over to each engine launcher.
769 el.engine_cmd = self.engine_cmd
778 el.engine_cmd = self.engine_cmd
770 el.engine_args = args
779 el.engine_args = args
771 el.on_stop(self._notice_engine_stopped)
780 el.on_stop(self._notice_engine_stopped)
772 d = el.start(user=user, hostname=host)
781 d = el.start(user=user, hostname=host)
773 self.launchers[ "%s/%i" % (host,i) ] = el
782 self.launchers[ "%s/%i" % (host,i) ] = el
774 dlist.append(d)
783 dlist.append(d)
775 self.notify_start(dlist)
784 self.notify_start(dlist)
776 return dlist
785 return dlist
777
786
778
787
779 class SSHProxyEngineSetLauncher(SSHClusterLauncher):
788 class SSHProxyEngineSetLauncher(SSHClusterLauncher):
780 """Launcher for calling
789 """Launcher for calling
781 `ipcluster engines` on a remote machine.
790 `ipcluster engines` on a remote machine.
782
791
783 Requires that remote profile is already configured.
792 Requires that remote profile is already configured.
784 """
793 """
785
794
786 n = Integer()
795 n = Integer()
787 ipcluster_cmd = List(['ipcluster'], config=True)
796 ipcluster_cmd = List(['ipcluster'], config=True)
788
797
789 @property
798 @property
790 def program(self):
799 def program(self):
791 return self.ipcluster_cmd + ['engines']
800 return self.ipcluster_cmd + ['engines']
792
801
793 @property
802 @property
794 def program_args(self):
803 def program_args(self):
795 return ['-n', str(self.n), '--profile-dir', self.remote_profile_dir]
804 return ['-n', str(self.n), '--profile-dir', self.remote_profile_dir]
796
805
797 def _to_send_default(self):
806 def _to_send_default(self):
798 return [
807 return [
799 (os.path.join(self.profile_dir, 'security', cf),
808 (os.path.join(self.profile_dir, 'security', cf),
800 os.path.join(self.remote_profile_dir, 'security', cf))
809 os.path.join(self.remote_profile_dir, 'security', cf))
801 for cf in ('ipcontroller-client.json', 'ipcontroller-engine.json')
810 for cf in ('ipcontroller-client.json', 'ipcontroller-engine.json')
802 ]
811 ]
803
812
804 def start(self, n):
813 def start(self, n):
805 self.n = n
814 self.n = n
806 super(SSHProxyEngineSetLauncher, self).start()
815 super(SSHProxyEngineSetLauncher, self).start()
807
816
808
817
809 #-----------------------------------------------------------------------------
818 #-----------------------------------------------------------------------------
810 # Windows HPC Server 2008 scheduler launchers
819 # Windows HPC Server 2008 scheduler launchers
811 #-----------------------------------------------------------------------------
820 #-----------------------------------------------------------------------------
812
821
813
822
814 # This is only used on Windows.
823 # This is only used on Windows.
815 def find_job_cmd():
824 def find_job_cmd():
816 if WINDOWS:
825 if WINDOWS:
817 try:
826 try:
818 return find_cmd('job')
827 return find_cmd('job')
819 except (FindCmdError, ImportError):
828 except (FindCmdError, ImportError):
820 # ImportError will be raised if win32api is not installed
829 # ImportError will be raised if win32api is not installed
821 return 'job'
830 return 'job'
822 else:
831 else:
823 return 'job'
832 return 'job'
824
833
825
834
826 class WindowsHPCLauncher(BaseLauncher):
835 class WindowsHPCLauncher(BaseLauncher):
827
836
828 job_id_regexp = CRegExp(r'\d+', config=True,
837 job_id_regexp = CRegExp(r'\d+', config=True,
829 help="""A regular expression used to get the job id from the output of the
838 help="""A regular expression used to get the job id from the output of the
830 submit_command. """
839 submit_command. """
831 )
840 )
832 job_file_name = Unicode(u'ipython_job.xml', config=True,
841 job_file_name = Unicode(u'ipython_job.xml', config=True,
833 help="The filename of the instantiated job script.")
842 help="The filename of the instantiated job script.")
834 # The full path to the instantiated job script. This gets made dynamically
843 # The full path to the instantiated job script. This gets made dynamically
835 # by combining the work_dir with the job_file_name.
844 # by combining the work_dir with the job_file_name.
836 job_file = Unicode(u'')
845 job_file = Unicode(u'')
837 scheduler = Unicode('', config=True,
846 scheduler = Unicode('', config=True,
838 help="The hostname of the scheduler to submit the job to.")
847 help="The hostname of the scheduler to submit the job to.")
839 job_cmd = Unicode(find_job_cmd(), config=True,
848 job_cmd = Unicode(find_job_cmd(), config=True,
840 help="The command for submitting jobs.")
849 help="The command for submitting jobs.")
841
850
842 def __init__(self, work_dir=u'.', config=None, **kwargs):
851 def __init__(self, work_dir=u'.', config=None, **kwargs):
843 super(WindowsHPCLauncher, self).__init__(
852 super(WindowsHPCLauncher, self).__init__(
844 work_dir=work_dir, config=config, **kwargs
853 work_dir=work_dir, config=config, **kwargs
845 )
854 )
846
855
847 @property
856 @property
848 def job_file(self):
857 def job_file(self):
849 return os.path.join(self.work_dir, self.job_file_name)
858 return os.path.join(self.work_dir, self.job_file_name)
850
859
851 def write_job_file(self, n):
860 def write_job_file(self, n):
852 raise NotImplementedError("Implement write_job_file in a subclass.")
861 raise NotImplementedError("Implement write_job_file in a subclass.")
853
862
854 def find_args(self):
863 def find_args(self):
855 return [u'job.exe']
864 return [u'job.exe']
856
865
857 def parse_job_id(self, output):
866 def parse_job_id(self, output):
858 """Take the output of the submit command and return the job id."""
867 """Take the output of the submit command and return the job id."""
859 m = self.job_id_regexp.search(output)
868 m = self.job_id_regexp.search(output)
860 if m is not None:
869 if m is not None:
861 job_id = m.group()
870 job_id = m.group()
862 else:
871 else:
863 raise LauncherError("Job id couldn't be determined: %s" % output)
872 raise LauncherError("Job id couldn't be determined: %s" % output)
864 self.job_id = job_id
873 self.job_id = job_id
865 self.log.info('Job started with id: %r', job_id)
874 self.log.info('Job started with id: %r', job_id)
866 return job_id
875 return job_id
867
876
868 def start(self, n):
877 def start(self, n):
869 """Start n copies of the process using the Win HPC job scheduler."""
878 """Start n copies of the process using the Win HPC job scheduler."""
870 self.write_job_file(n)
879 self.write_job_file(n)
871 args = [
880 args = [
872 'submit',
881 'submit',
873 '/jobfile:%s' % self.job_file,
882 '/jobfile:%s' % self.job_file,
874 '/scheduler:%s' % self.scheduler
883 '/scheduler:%s' % self.scheduler
875 ]
884 ]
876 self.log.debug("Starting Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
885 self.log.debug("Starting Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
877
886
878 output = check_output([self.job_cmd]+args,
887 output = check_output([self.job_cmd]+args,
879 env=os.environ,
888 env=os.environ,
880 cwd=self.work_dir,
889 cwd=self.work_dir,
881 stderr=STDOUT
890 stderr=STDOUT
882 )
891 )
883 job_id = self.parse_job_id(output)
892 job_id = self.parse_job_id(output)
884 self.notify_start(job_id)
893 self.notify_start(job_id)
885 return job_id
894 return job_id
886
895
887 def stop(self):
896 def stop(self):
888 args = [
897 args = [
889 'cancel',
898 'cancel',
890 self.job_id,
899 self.job_id,
891 '/scheduler:%s' % self.scheduler
900 '/scheduler:%s' % self.scheduler
892 ]
901 ]
893 self.log.info("Stopping Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
902 self.log.info("Stopping Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
894 try:
903 try:
895 output = check_output([self.job_cmd]+args,
904 output = check_output([self.job_cmd]+args,
896 env=os.environ,
905 env=os.environ,
897 cwd=self.work_dir,
906 cwd=self.work_dir,
898 stderr=STDOUT
907 stderr=STDOUT
899 )
908 )
900 except:
909 except:
901 output = 'The job already appears to be stoppped: %r' % self.job_id
910 output = 'The job already appears to be stoppped: %r' % self.job_id
902 self.notify_stop(dict(job_id=self.job_id, output=output)) # Pass the output of the kill cmd
911 self.notify_stop(dict(job_id=self.job_id, output=output)) # Pass the output of the kill cmd
903 return output
912 return output
904
913
905
914
906 class WindowsHPCControllerLauncher(WindowsHPCLauncher, ClusterAppMixin):
915 class WindowsHPCControllerLauncher(WindowsHPCLauncher, ClusterAppMixin):
907
916
908 job_file_name = Unicode(u'ipcontroller_job.xml', config=True,
917 job_file_name = Unicode(u'ipcontroller_job.xml', config=True,
909 help="WinHPC xml job file.")
918 help="WinHPC xml job file.")
910 controller_args = List([], config=False,
919 controller_args = List([], config=False,
911 help="extra args to pass to ipcontroller")
920 help="extra args to pass to ipcontroller")
912
921
913 def write_job_file(self, n):
922 def write_job_file(self, n):
914 job = IPControllerJob(config=self.config)
923 job = IPControllerJob(config=self.config)
915
924
916 t = IPControllerTask(config=self.config)
925 t = IPControllerTask(config=self.config)
917 # The tasks work directory is *not* the actual work directory of
926 # The tasks work directory is *not* the actual work directory of
918 # the controller. It is used as the base path for the stdout/stderr
927 # the controller. It is used as the base path for the stdout/stderr
919 # files that the scheduler redirects to.
928 # files that the scheduler redirects to.
920 t.work_directory = self.profile_dir
929 t.work_directory = self.profile_dir
921 # Add the profile_dir and from self.start().
930 # Add the profile_dir and from self.start().
922 t.controller_args.extend(self.cluster_args)
931 t.controller_args.extend(self.cluster_args)
923 t.controller_args.extend(self.controller_args)
932 t.controller_args.extend(self.controller_args)
924 job.add_task(t)
933 job.add_task(t)
925
934
926 self.log.debug("Writing job description file: %s", self.job_file)
935 self.log.debug("Writing job description file: %s", self.job_file)
927 job.write(self.job_file)
936 job.write(self.job_file)
928
937
929 @property
938 @property
930 def job_file(self):
939 def job_file(self):
931 return os.path.join(self.profile_dir, self.job_file_name)
940 return os.path.join(self.profile_dir, self.job_file_name)
932
941
933 def start(self):
942 def start(self):
934 """Start the controller by profile_dir."""
943 """Start the controller by profile_dir."""
935 return super(WindowsHPCControllerLauncher, self).start(1)
944 return super(WindowsHPCControllerLauncher, self).start(1)
936
945
937
946
938 class WindowsHPCEngineSetLauncher(WindowsHPCLauncher, ClusterAppMixin):
947 class WindowsHPCEngineSetLauncher(WindowsHPCLauncher, ClusterAppMixin):
939
948
940 job_file_name = Unicode(u'ipengineset_job.xml', config=True,
949 job_file_name = Unicode(u'ipengineset_job.xml', config=True,
941 help="jobfile for ipengines job")
950 help="jobfile for ipengines job")
942 engine_args = List([], config=False,
951 engine_args = List([], config=False,
943 help="extra args to pas to ipengine")
952 help="extra args to pas to ipengine")
944
953
945 def write_job_file(self, n):
954 def write_job_file(self, n):
946 job = IPEngineSetJob(config=self.config)
955 job = IPEngineSetJob(config=self.config)
947
956
948 for i in range(n):
957 for i in range(n):
949 t = IPEngineTask(config=self.config)
958 t = IPEngineTask(config=self.config)
950 # The tasks work directory is *not* the actual work directory of
959 # The tasks work directory is *not* the actual work directory of
951 # the engine. It is used as the base path for the stdout/stderr
960 # the engine. It is used as the base path for the stdout/stderr
952 # files that the scheduler redirects to.
961 # files that the scheduler redirects to.
953 t.work_directory = self.profile_dir
962 t.work_directory = self.profile_dir
954 # Add the profile_dir and from self.start().
963 # Add the profile_dir and from self.start().
955 t.engine_args.extend(self.cluster_args)
964 t.engine_args.extend(self.cluster_args)
956 t.engine_args.extend(self.engine_args)
965 t.engine_args.extend(self.engine_args)
957 job.add_task(t)
966 job.add_task(t)
958
967
959 self.log.debug("Writing job description file: %s", self.job_file)
968 self.log.debug("Writing job description file: %s", self.job_file)
960 job.write(self.job_file)
969 job.write(self.job_file)
961
970
962 @property
971 @property
963 def job_file(self):
972 def job_file(self):
964 return os.path.join(self.profile_dir, self.job_file_name)
973 return os.path.join(self.profile_dir, self.job_file_name)
965
974
966 def start(self, n):
975 def start(self, n):
967 """Start the controller by profile_dir."""
976 """Start the controller by profile_dir."""
968 return super(WindowsHPCEngineSetLauncher, self).start(n)
977 return super(WindowsHPCEngineSetLauncher, self).start(n)
969
978
970
979
971 #-----------------------------------------------------------------------------
980 #-----------------------------------------------------------------------------
972 # Batch (PBS) system launchers
981 # Batch (PBS) system launchers
973 #-----------------------------------------------------------------------------
982 #-----------------------------------------------------------------------------
974
983
975 class BatchClusterAppMixin(ClusterAppMixin):
984 class BatchClusterAppMixin(ClusterAppMixin):
976 """ClusterApp mixin that updates the self.context dict, rather than cl-args."""
985 """ClusterApp mixin that updates the self.context dict, rather than cl-args."""
977 def _profile_dir_changed(self, name, old, new):
986 def _profile_dir_changed(self, name, old, new):
978 self.context[name] = new
987 self.context[name] = new
979 _cluster_id_changed = _profile_dir_changed
988 _cluster_id_changed = _profile_dir_changed
980
989
981 def _profile_dir_default(self):
990 def _profile_dir_default(self):
982 self.context['profile_dir'] = ''
991 self.context['profile_dir'] = ''
983 return ''
992 return ''
984 def _cluster_id_default(self):
993 def _cluster_id_default(self):
985 self.context['cluster_id'] = ''
994 self.context['cluster_id'] = ''
986 return ''
995 return ''
987
996
988
997
989 class BatchSystemLauncher(BaseLauncher):
998 class BatchSystemLauncher(BaseLauncher):
990 """Launch an external process using a batch system.
999 """Launch an external process using a batch system.
991
1000
992 This class is designed to work with UNIX batch systems like PBS, LSF,
1001 This class is designed to work with UNIX batch systems like PBS, LSF,
993 GridEngine, etc. The overall model is that there are different commands
1002 GridEngine, etc. The overall model is that there are different commands
994 like qsub, qdel, etc. that handle the starting and stopping of the process.
1003 like qsub, qdel, etc. that handle the starting and stopping of the process.
995
1004
996 This class also has the notion of a batch script. The ``batch_template``
1005 This class also has the notion of a batch script. The ``batch_template``
997 attribute can be set to a string that is a template for the batch script.
1006 attribute can be set to a string that is a template for the batch script.
998 This template is instantiated using string formatting. Thus the template can
1007 This template is instantiated using string formatting. Thus the template can
999 use {n} fot the number of instances. Subclasses can add additional variables
1008 use {n} fot the number of instances. Subclasses can add additional variables
1000 to the template dict.
1009 to the template dict.
1001 """
1010 """
1002
1011
1003 # Subclasses must fill these in. See PBSEngineSet
1012 # Subclasses must fill these in. See PBSEngineSet
1004 submit_command = List([''], config=True,
1013 submit_command = List([''], config=True,
1005 help="The name of the command line program used to submit jobs.")
1014 help="The name of the command line program used to submit jobs.")
1006 delete_command = List([''], config=True,
1015 delete_command = List([''], config=True,
1007 help="The name of the command line program used to delete jobs.")
1016 help="The name of the command line program used to delete jobs.")
1008 job_id_regexp = CRegExp('', config=True,
1017 job_id_regexp = CRegExp('', config=True,
1009 help="""A regular expression used to get the job id from the output of the
1018 help="""A regular expression used to get the job id from the output of the
1010 submit_command.""")
1019 submit_command.""")
1011 batch_template = Unicode('', config=True,
1020 batch_template = Unicode('', config=True,
1012 help="The string that is the batch script template itself.")
1021 help="The string that is the batch script template itself.")
1013 batch_template_file = Unicode(u'', config=True,
1022 batch_template_file = Unicode(u'', config=True,
1014 help="The file that contains the batch template.")
1023 help="The file that contains the batch template.")
1015 batch_file_name = Unicode(u'batch_script', config=True,
1024 batch_file_name = Unicode(u'batch_script', config=True,
1016 help="The filename of the instantiated batch script.")
1025 help="The filename of the instantiated batch script.")
1017 queue = Unicode(u'', config=True,
1026 queue = Unicode(u'', config=True,
1018 help="The PBS Queue.")
1027 help="The PBS Queue.")
1019
1028
1020 def _queue_changed(self, name, old, new):
1029 def _queue_changed(self, name, old, new):
1021 self.context[name] = new
1030 self.context[name] = new
1022
1031
1023 n = Integer(1)
1032 n = Integer(1)
1024 _n_changed = _queue_changed
1033 _n_changed = _queue_changed
1025
1034
1026 # not configurable, override in subclasses
1035 # not configurable, override in subclasses
1027 # PBS Job Array regex
1036 # PBS Job Array regex
1028 job_array_regexp = CRegExp('')
1037 job_array_regexp = CRegExp('')
1029 job_array_template = Unicode('')
1038 job_array_template = Unicode('')
1030 # PBS Queue regex
1039 # PBS Queue regex
1031 queue_regexp = CRegExp('')
1040 queue_regexp = CRegExp('')
1032 queue_template = Unicode('')
1041 queue_template = Unicode('')
1033 # The default batch template, override in subclasses
1042 # The default batch template, override in subclasses
1034 default_template = Unicode('')
1043 default_template = Unicode('')
1035 # The full path to the instantiated batch script.
1044 # The full path to the instantiated batch script.
1036 batch_file = Unicode(u'')
1045 batch_file = Unicode(u'')
1037 # the format dict used with batch_template:
1046 # the format dict used with batch_template:
1038 context = Dict()
1047 context = Dict()
1039 def _context_default(self):
1048 def _context_default(self):
1040 """load the default context with the default values for the basic keys
1049 """load the default context with the default values for the basic keys
1041
1050
1042 because the _trait_changed methods only load the context if they
1051 because the _trait_changed methods only load the context if they
1043 are set to something other than the default value.
1052 are set to something other than the default value.
1044 """
1053 """
1045 return dict(n=1, queue=u'', profile_dir=u'', cluster_id=u'')
1054 return dict(n=1, queue=u'', profile_dir=u'', cluster_id=u'')
1046
1055
1047 # the Formatter instance for rendering the templates:
1056 # the Formatter instance for rendering the templates:
1048 formatter = Instance(EvalFormatter, (), {})
1057 formatter = Instance(EvalFormatter, (), {})
1049
1058
1050
1059
1051 def find_args(self):
1060 def find_args(self):
1052 return self.submit_command + [self.batch_file]
1061 return self.submit_command + [self.batch_file]
1053
1062
1054 def __init__(self, work_dir=u'.', config=None, **kwargs):
1063 def __init__(self, work_dir=u'.', config=None, **kwargs):
1055 super(BatchSystemLauncher, self).__init__(
1064 super(BatchSystemLauncher, self).__init__(
1056 work_dir=work_dir, config=config, **kwargs
1065 work_dir=work_dir, config=config, **kwargs
1057 )
1066 )
1058 self.batch_file = os.path.join(self.work_dir, self.batch_file_name)
1067 self.batch_file = os.path.join(self.work_dir, self.batch_file_name)
1059
1068
1060 def parse_job_id(self, output):
1069 def parse_job_id(self, output):
1061 """Take the output of the submit command and return the job id."""
1070 """Take the output of the submit command and return the job id."""
1062 m = self.job_id_regexp.search(output)
1071 m = self.job_id_regexp.search(output)
1063 if m is not None:
1072 if m is not None:
1064 job_id = m.group()
1073 job_id = m.group()
1065 else:
1074 else:
1066 raise LauncherError("Job id couldn't be determined: %s" % output)
1075 raise LauncherError("Job id couldn't be determined: %s" % output)
1067 self.job_id = job_id
1076 self.job_id = job_id
1068 self.log.info('Job submitted with job id: %r', job_id)
1077 self.log.info('Job submitted with job id: %r', job_id)
1069 return job_id
1078 return job_id
1070
1079
1071 def write_batch_script(self, n):
1080 def write_batch_script(self, n):
1072 """Instantiate and write the batch script to the work_dir."""
1081 """Instantiate and write the batch script to the work_dir."""
1073 self.n = n
1082 self.n = n
1074 # first priority is batch_template if set
1083 # first priority is batch_template if set
1075 if self.batch_template_file and not self.batch_template:
1084 if self.batch_template_file and not self.batch_template:
1076 # second priority is batch_template_file
1085 # second priority is batch_template_file
1077 with open(self.batch_template_file) as f:
1086 with open(self.batch_template_file) as f:
1078 self.batch_template = f.read()
1087 self.batch_template = f.read()
1079 if not self.batch_template:
1088 if not self.batch_template:
1080 # third (last) priority is default_template
1089 # third (last) priority is default_template
1081 self.batch_template = self.default_template
1090 self.batch_template = self.default_template
1082
1091
1083 # add jobarray or queue lines to user-specified template
1092 # add jobarray or queue lines to user-specified template
1084 # note that this is *only* when user did not specify a template.
1093 # note that this is *only* when user did not specify a template.
1085 # print self.job_array_regexp.search(self.batch_template)
1094 # print self.job_array_regexp.search(self.batch_template)
1086 if not self.job_array_regexp.search(self.batch_template):
1095 if not self.job_array_regexp.search(self.batch_template):
1087 self.log.debug("adding job array settings to batch script")
1096 self.log.debug("adding job array settings to batch script")
1088 firstline, rest = self.batch_template.split('\n',1)
1097 firstline, rest = self.batch_template.split('\n',1)
1089 self.batch_template = u'\n'.join([firstline, self.job_array_template, rest])
1098 self.batch_template = u'\n'.join([firstline, self.job_array_template, rest])
1090
1099
1091 # print self.queue_regexp.search(self.batch_template)
1100 # print self.queue_regexp.search(self.batch_template)
1092 if self.queue and not self.queue_regexp.search(self.batch_template):
1101 if self.queue and not self.queue_regexp.search(self.batch_template):
1093 self.log.debug("adding PBS queue settings to batch script")
1102 self.log.debug("adding PBS queue settings to batch script")
1094 firstline, rest = self.batch_template.split('\n',1)
1103 firstline, rest = self.batch_template.split('\n',1)
1095 self.batch_template = u'\n'.join([firstline, self.queue_template, rest])
1104 self.batch_template = u'\n'.join([firstline, self.queue_template, rest])
1096
1105
1097 script_as_string = self.formatter.format(self.batch_template, **self.context)
1106 script_as_string = self.formatter.format(self.batch_template, **self.context)
1098 self.log.debug('Writing batch script: %s', self.batch_file)
1107 self.log.debug('Writing batch script: %s', self.batch_file)
1099
1108
1100 with open(self.batch_file, 'w') as f:
1109 with open(self.batch_file, 'w') as f:
1101 f.write(script_as_string)
1110 f.write(script_as_string)
1102 os.chmod(self.batch_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
1111 os.chmod(self.batch_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
1103
1112
1104 def start(self, n):
1113 def start(self, n):
1105 """Start n copies of the process using a batch system."""
1114 """Start n copies of the process using a batch system."""
1106 self.log.debug("Starting %s: %r", self.__class__.__name__, self.args)
1115 self.log.debug("Starting %s: %r", self.__class__.__name__, self.args)
1107 # Here we save profile_dir in the context so they
1116 # Here we save profile_dir in the context so they
1108 # can be used in the batch script template as {profile_dir}
1117 # can be used in the batch script template as {profile_dir}
1109 self.write_batch_script(n)
1118 self.write_batch_script(n)
1110 output = check_output(self.args, env=os.environ)
1119 output = check_output(self.args, env=os.environ)
1111
1120
1112 job_id = self.parse_job_id(output)
1121 job_id = self.parse_job_id(output)
1113 self.notify_start(job_id)
1122 self.notify_start(job_id)
1114 return job_id
1123 return job_id
1115
1124
1116 def stop(self):
1125 def stop(self):
1117 output = check_output(self.delete_command+[self.job_id], env=os.environ)
1126 output = check_output(self.delete_command+[self.job_id], env=os.environ)
1118 self.notify_stop(dict(job_id=self.job_id, output=output)) # Pass the output of the kill cmd
1127 self.notify_stop(dict(job_id=self.job_id, output=output)) # Pass the output of the kill cmd
1119 return output
1128 return output
1120
1129
1121
1130
1122 class PBSLauncher(BatchSystemLauncher):
1131 class PBSLauncher(BatchSystemLauncher):
1123 """A BatchSystemLauncher subclass for PBS."""
1132 """A BatchSystemLauncher subclass for PBS."""
1124
1133
1125 submit_command = List(['qsub'], config=True,
1134 submit_command = List(['qsub'], config=True,
1126 help="The PBS submit command ['qsub']")
1135 help="The PBS submit command ['qsub']")
1127 delete_command = List(['qdel'], config=True,
1136 delete_command = List(['qdel'], config=True,
1128 help="The PBS delete command ['qsub']")
1137 help="The PBS delete command ['qsub']")
1129 job_id_regexp = CRegExp(r'\d+', config=True,
1138 job_id_regexp = CRegExp(r'\d+', config=True,
1130 help="Regular expresion for identifying the job ID [r'\d+']")
1139 help="Regular expresion for identifying the job ID [r'\d+']")
1131
1140
1132 batch_file = Unicode(u'')
1141 batch_file = Unicode(u'')
1133 job_array_regexp = CRegExp('#PBS\W+-t\W+[\w\d\-\$]+')
1142 job_array_regexp = CRegExp('#PBS\W+-t\W+[\w\d\-\$]+')
1134 job_array_template = Unicode('#PBS -t 1-{n}')
1143 job_array_template = Unicode('#PBS -t 1-{n}')
1135 queue_regexp = CRegExp('#PBS\W+-q\W+\$?\w+')
1144 queue_regexp = CRegExp('#PBS\W+-q\W+\$?\w+')
1136 queue_template = Unicode('#PBS -q {queue}')
1145 queue_template = Unicode('#PBS -q {queue}')
1137
1146
1138
1147
1139 class PBSControllerLauncher(PBSLauncher, BatchClusterAppMixin):
1148 class PBSControllerLauncher(PBSLauncher, BatchClusterAppMixin):
1140 """Launch a controller using PBS."""
1149 """Launch a controller using PBS."""
1141
1150
1142 batch_file_name = Unicode(u'pbs_controller', config=True,
1151 batch_file_name = Unicode(u'pbs_controller', config=True,
1143 help="batch file name for the controller job.")
1152 help="batch file name for the controller job.")
1144 default_template= Unicode("""#!/bin/sh
1153 default_template= Unicode("""#!/bin/sh
1145 #PBS -V
1154 #PBS -V
1146 #PBS -N ipcontroller
1155 #PBS -N ipcontroller
1147 %s --log-to-file --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1156 %s --log-to-file --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1148 """%(' '.join(map(pipes.quote, ipcontroller_cmd_argv))))
1157 """%(' '.join(map(pipes.quote, ipcontroller_cmd_argv))))
1149
1158
1150
1159
1151 def start(self):
1160 def start(self):
1152 """Start the controller by profile or profile_dir."""
1161 """Start the controller by profile or profile_dir."""
1153 return super(PBSControllerLauncher, self).start(1)
1162 return super(PBSControllerLauncher, self).start(1)
1154
1163
1155
1164
1156 class PBSEngineSetLauncher(PBSLauncher, BatchClusterAppMixin):
1165 class PBSEngineSetLauncher(PBSLauncher, BatchClusterAppMixin):
1157 """Launch Engines using PBS"""
1166 """Launch Engines using PBS"""
1158 batch_file_name = Unicode(u'pbs_engines', config=True,
1167 batch_file_name = Unicode(u'pbs_engines', config=True,
1159 help="batch file name for the engine(s) job.")
1168 help="batch file name for the engine(s) job.")
1160 default_template= Unicode(u"""#!/bin/sh
1169 default_template= Unicode(u"""#!/bin/sh
1161 #PBS -V
1170 #PBS -V
1162 #PBS -N ipengine
1171 #PBS -N ipengine
1163 %s --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1172 %s --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1164 """%(' '.join(map(pipes.quote,ipengine_cmd_argv))))
1173 """%(' '.join(map(pipes.quote,ipengine_cmd_argv))))
1165
1174
1166 def start(self, n):
1175 def start(self, n):
1167 """Start n engines by profile or profile_dir."""
1176 """Start n engines by profile or profile_dir."""
1168 return super(PBSEngineSetLauncher, self).start(n)
1177 return super(PBSEngineSetLauncher, self).start(n)
1169
1178
1170 #SGE is very similar to PBS
1179 #SGE is very similar to PBS
1171
1180
1172 class SGELauncher(PBSLauncher):
1181 class SGELauncher(PBSLauncher):
1173 """Sun GridEngine is a PBS clone with slightly different syntax"""
1182 """Sun GridEngine is a PBS clone with slightly different syntax"""
1174 job_array_regexp = CRegExp('#\$\W+\-t')
1183 job_array_regexp = CRegExp('#\$\W+\-t')
1175 job_array_template = Unicode('#$ -t 1-{n}')
1184 job_array_template = Unicode('#$ -t 1-{n}')
1176 queue_regexp = CRegExp('#\$\W+-q\W+\$?\w+')
1185 queue_regexp = CRegExp('#\$\W+-q\W+\$?\w+')
1177 queue_template = Unicode('#$ -q {queue}')
1186 queue_template = Unicode('#$ -q {queue}')
1178
1187
1179 class SGEControllerLauncher(SGELauncher, BatchClusterAppMixin):
1188 class SGEControllerLauncher(SGELauncher, BatchClusterAppMixin):
1180 """Launch a controller using SGE."""
1189 """Launch a controller using SGE."""
1181
1190
1182 batch_file_name = Unicode(u'sge_controller', config=True,
1191 batch_file_name = Unicode(u'sge_controller', config=True,
1183 help="batch file name for the ipontroller job.")
1192 help="batch file name for the ipontroller job.")
1184 default_template= Unicode(u"""#$ -V
1193 default_template= Unicode(u"""#$ -V
1185 #$ -S /bin/sh
1194 #$ -S /bin/sh
1186 #$ -N ipcontroller
1195 #$ -N ipcontroller
1187 %s --log-to-file --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1196 %s --log-to-file --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1188 """%(' '.join(map(pipes.quote, ipcontroller_cmd_argv))))
1197 """%(' '.join(map(pipes.quote, ipcontroller_cmd_argv))))
1189
1198
1190 def start(self):
1199 def start(self):
1191 """Start the controller by profile or profile_dir."""
1200 """Start the controller by profile or profile_dir."""
1192 return super(SGEControllerLauncher, self).start(1)
1201 return super(SGEControllerLauncher, self).start(1)
1193
1202
1194 class SGEEngineSetLauncher(SGELauncher, BatchClusterAppMixin):
1203 class SGEEngineSetLauncher(SGELauncher, BatchClusterAppMixin):
1195 """Launch Engines with SGE"""
1204 """Launch Engines with SGE"""
1196 batch_file_name = Unicode(u'sge_engines', config=True,
1205 batch_file_name = Unicode(u'sge_engines', config=True,
1197 help="batch file name for the engine(s) job.")
1206 help="batch file name for the engine(s) job.")
1198 default_template = Unicode("""#$ -V
1207 default_template = Unicode("""#$ -V
1199 #$ -S /bin/sh
1208 #$ -S /bin/sh
1200 #$ -N ipengine
1209 #$ -N ipengine
1201 %s --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1210 %s --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1202 """%(' '.join(map(pipes.quote, ipengine_cmd_argv))))
1211 """%(' '.join(map(pipes.quote, ipengine_cmd_argv))))
1203
1212
1204 def start(self, n):
1213 def start(self, n):
1205 """Start n engines by profile or profile_dir."""
1214 """Start n engines by profile or profile_dir."""
1206 return super(SGEEngineSetLauncher, self).start(n)
1215 return super(SGEEngineSetLauncher, self).start(n)
1207
1216
1208
1217
1209 # LSF launchers
1218 # LSF launchers
1210
1219
1211 class LSFLauncher(BatchSystemLauncher):
1220 class LSFLauncher(BatchSystemLauncher):
1212 """A BatchSystemLauncher subclass for LSF."""
1221 """A BatchSystemLauncher subclass for LSF."""
1213
1222
1214 submit_command = List(['bsub'], config=True,
1223 submit_command = List(['bsub'], config=True,
1215 help="The PBS submit command ['bsub']")
1224 help="The PBS submit command ['bsub']")
1216 delete_command = List(['bkill'], config=True,
1225 delete_command = List(['bkill'], config=True,
1217 help="The PBS delete command ['bkill']")
1226 help="The PBS delete command ['bkill']")
1218 job_id_regexp = CRegExp(r'\d+', config=True,
1227 job_id_regexp = CRegExp(r'\d+', config=True,
1219 help="Regular expresion for identifying the job ID [r'\d+']")
1228 help="Regular expresion for identifying the job ID [r'\d+']")
1220
1229
1221 batch_file = Unicode(u'')
1230 batch_file = Unicode(u'')
1222 job_array_regexp = CRegExp('#BSUB[ \t]-J+\w+\[\d+-\d+\]')
1231 job_array_regexp = CRegExp('#BSUB[ \t]-J+\w+\[\d+-\d+\]')
1223 job_array_template = Unicode('#BSUB -J ipengine[1-{n}]')
1232 job_array_template = Unicode('#BSUB -J ipengine[1-{n}]')
1224 queue_regexp = CRegExp('#BSUB[ \t]+-q[ \t]+\w+')
1233 queue_regexp = CRegExp('#BSUB[ \t]+-q[ \t]+\w+')
1225 queue_template = Unicode('#BSUB -q {queue}')
1234 queue_template = Unicode('#BSUB -q {queue}')
1226
1235
1227 def start(self, n):
1236 def start(self, n):
1228 """Start n copies of the process using LSF batch system.
1237 """Start n copies of the process using LSF batch system.
1229 This cant inherit from the base class because bsub expects
1238 This cant inherit from the base class because bsub expects
1230 to be piped a shell script in order to honor the #BSUB directives :
1239 to be piped a shell script in order to honor the #BSUB directives :
1231 bsub < script
1240 bsub < script
1232 """
1241 """
1233 # Here we save profile_dir in the context so they
1242 # Here we save profile_dir in the context so they
1234 # can be used in the batch script template as {profile_dir}
1243 # can be used in the batch script template as {profile_dir}
1235 self.write_batch_script(n)
1244 self.write_batch_script(n)
1236 #output = check_output(self.args, env=os.environ)
1245 #output = check_output(self.args, env=os.environ)
1237 piped_cmd = self.args[0]+'<\"'+self.args[1]+'\"'
1246 piped_cmd = self.args[0]+'<\"'+self.args[1]+'\"'
1238 self.log.debug("Starting %s: %s", self.__class__.__name__, piped_cmd)
1247 self.log.debug("Starting %s: %s", self.__class__.__name__, piped_cmd)
1239 p = Popen(piped_cmd, shell=True,env=os.environ,stdout=PIPE)
1248 p = Popen(piped_cmd, shell=True,env=os.environ,stdout=PIPE)
1240 output,err = p.communicate()
1249 output,err = p.communicate()
1241 job_id = self.parse_job_id(output)
1250 job_id = self.parse_job_id(output)
1242 self.notify_start(job_id)
1251 self.notify_start(job_id)
1243 return job_id
1252 return job_id
1244
1253
1245
1254
1246 class LSFControllerLauncher(LSFLauncher, BatchClusterAppMixin):
1255 class LSFControllerLauncher(LSFLauncher, BatchClusterAppMixin):
1247 """Launch a controller using LSF."""
1256 """Launch a controller using LSF."""
1248
1257
1249 batch_file_name = Unicode(u'lsf_controller', config=True,
1258 batch_file_name = Unicode(u'lsf_controller', config=True,
1250 help="batch file name for the controller job.")
1259 help="batch file name for the controller job.")
1251 default_template= Unicode("""#!/bin/sh
1260 default_template= Unicode("""#!/bin/sh
1252 #BSUB -J ipcontroller
1261 #BSUB -J ipcontroller
1253 #BSUB -oo ipcontroller.o.%%J
1262 #BSUB -oo ipcontroller.o.%%J
1254 #BSUB -eo ipcontroller.e.%%J
1263 #BSUB -eo ipcontroller.e.%%J
1255 %s --log-to-file --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1264 %s --log-to-file --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1256 """%(' '.join(map(pipes.quote,ipcontroller_cmd_argv))))
1265 """%(' '.join(map(pipes.quote,ipcontroller_cmd_argv))))
1257
1266
1258 def start(self):
1267 def start(self):
1259 """Start the controller by profile or profile_dir."""
1268 """Start the controller by profile or profile_dir."""
1260 return super(LSFControllerLauncher, self).start(1)
1269 return super(LSFControllerLauncher, self).start(1)
1261
1270
1262
1271
1263 class LSFEngineSetLauncher(LSFLauncher, BatchClusterAppMixin):
1272 class LSFEngineSetLauncher(LSFLauncher, BatchClusterAppMixin):
1264 """Launch Engines using LSF"""
1273 """Launch Engines using LSF"""
1265 batch_file_name = Unicode(u'lsf_engines', config=True,
1274 batch_file_name = Unicode(u'lsf_engines', config=True,
1266 help="batch file name for the engine(s) job.")
1275 help="batch file name for the engine(s) job.")
1267 default_template= Unicode(u"""#!/bin/sh
1276 default_template= Unicode(u"""#!/bin/sh
1268 #BSUB -oo ipengine.o.%%J
1277 #BSUB -oo ipengine.o.%%J
1269 #BSUB -eo ipengine.e.%%J
1278 #BSUB -eo ipengine.e.%%J
1270 %s --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1279 %s --profile-dir="{profile_dir}" --cluster-id="{cluster_id}"
1271 """%(' '.join(map(pipes.quote, ipengine_cmd_argv))))
1280 """%(' '.join(map(pipes.quote, ipengine_cmd_argv))))
1272
1281
1273 def start(self, n):
1282 def start(self, n):
1274 """Start n engines by profile or profile_dir."""
1283 """Start n engines by profile or profile_dir."""
1275 return super(LSFEngineSetLauncher, self).start(n)
1284 return super(LSFEngineSetLauncher, self).start(n)
1276
1285
1277
1286
1278 #-----------------------------------------------------------------------------
1287 #-----------------------------------------------------------------------------
1279 # A launcher for ipcluster itself!
1288 # A launcher for ipcluster itself!
1280 #-----------------------------------------------------------------------------
1289 #-----------------------------------------------------------------------------
1281
1290
1282
1291
1283 class IPClusterLauncher(LocalProcessLauncher):
1292 class IPClusterLauncher(LocalProcessLauncher):
1284 """Launch the ipcluster program in an external process."""
1293 """Launch the ipcluster program in an external process."""
1285
1294
1286 ipcluster_cmd = List(ipcluster_cmd_argv, config=True,
1295 ipcluster_cmd = List(ipcluster_cmd_argv, config=True,
1287 help="Popen command for ipcluster")
1296 help="Popen command for ipcluster")
1288 ipcluster_args = List(
1297 ipcluster_args = List(
1289 ['--clean-logs=True', '--log-to-file', '--log-level=%i'%logging.INFO], config=True,
1298 ['--clean-logs=True', '--log-to-file', '--log-level=%i'%logging.INFO], config=True,
1290 help="Command line arguments to pass to ipcluster.")
1299 help="Command line arguments to pass to ipcluster.")
1291 ipcluster_subcommand = Unicode('start')
1300 ipcluster_subcommand = Unicode('start')
1292 profile = Unicode('default')
1301 profile = Unicode('default')
1293 n = Integer(2)
1302 n = Integer(2)
1294
1303
1295 def find_args(self):
1304 def find_args(self):
1296 return self.ipcluster_cmd + [self.ipcluster_subcommand] + \
1305 return self.ipcluster_cmd + [self.ipcluster_subcommand] + \
1297 ['--n=%i'%self.n, '--profile=%s'%self.profile] + \
1306 ['--n=%i'%self.n, '--profile=%s'%self.profile] + \
1298 self.ipcluster_args
1307 self.ipcluster_args
1299
1308
1300 def start(self):
1309 def start(self):
1301 return super(IPClusterLauncher, self).start()
1310 return super(IPClusterLauncher, self).start()
1302
1311
1303 #-----------------------------------------------------------------------------
1312 #-----------------------------------------------------------------------------
1304 # Collections of launchers
1313 # Collections of launchers
1305 #-----------------------------------------------------------------------------
1314 #-----------------------------------------------------------------------------
1306
1315
1307 local_launchers = [
1316 local_launchers = [
1308 LocalControllerLauncher,
1317 LocalControllerLauncher,
1309 LocalEngineLauncher,
1318 LocalEngineLauncher,
1310 LocalEngineSetLauncher,
1319 LocalEngineSetLauncher,
1311 ]
1320 ]
1312 mpi_launchers = [
1321 mpi_launchers = [
1313 MPILauncher,
1322 MPILauncher,
1314 MPIControllerLauncher,
1323 MPIControllerLauncher,
1315 MPIEngineSetLauncher,
1324 MPIEngineSetLauncher,
1316 ]
1325 ]
1317 ssh_launchers = [
1326 ssh_launchers = [
1318 SSHLauncher,
1327 SSHLauncher,
1319 SSHControllerLauncher,
1328 SSHControllerLauncher,
1320 SSHEngineLauncher,
1329 SSHEngineLauncher,
1321 SSHEngineSetLauncher,
1330 SSHEngineSetLauncher,
1322 ]
1331 ]
1323 winhpc_launchers = [
1332 winhpc_launchers = [
1324 WindowsHPCLauncher,
1333 WindowsHPCLauncher,
1325 WindowsHPCControllerLauncher,
1334 WindowsHPCControllerLauncher,
1326 WindowsHPCEngineSetLauncher,
1335 WindowsHPCEngineSetLauncher,
1327 ]
1336 ]
1328 pbs_launchers = [
1337 pbs_launchers = [
1329 PBSLauncher,
1338 PBSLauncher,
1330 PBSControllerLauncher,
1339 PBSControllerLauncher,
1331 PBSEngineSetLauncher,
1340 PBSEngineSetLauncher,
1332 ]
1341 ]
1333 sge_launchers = [
1342 sge_launchers = [
1334 SGELauncher,
1343 SGELauncher,
1335 SGEControllerLauncher,
1344 SGEControllerLauncher,
1336 SGEEngineSetLauncher,
1345 SGEEngineSetLauncher,
1337 ]
1346 ]
1338 lsf_launchers = [
1347 lsf_launchers = [
1339 LSFLauncher,
1348 LSFLauncher,
1340 LSFControllerLauncher,
1349 LSFControllerLauncher,
1341 LSFEngineSetLauncher,
1350 LSFEngineSetLauncher,
1342 ]
1351 ]
1343 all_launchers = local_launchers + mpi_launchers + ssh_launchers + winhpc_launchers\
1352 all_launchers = local_launchers + mpi_launchers + ssh_launchers + winhpc_launchers\
1344 + pbs_launchers + sge_launchers + lsf_launchers
1353 + pbs_launchers + sge_launchers + lsf_launchers
1345
1354
General Comments 0
You need to be logged in to leave comments. Login now