forked from jamesgao/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipcontroller.py
More file actions
366 lines (312 loc) · 12.8 KB
/
ipcontroller.py
File metadata and controls
366 lines (312 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
# encoding: utf-8
"""The IPython controller."""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
# Python looks for an empty string at the beginning of sys.path to enable
# importing from the cwd.
import sys
sys.path.insert(0, '')
import sys, time, os
from optparse import OptionParser
from twisted.application import internet, service
from twisted.internet import reactor, error, defer
from twisted.python import log
from IPython.kernel.fcutil import Tub, UnauthenticatedTub, have_crypto
# from IPython.tools import growl
# growl.start("IPython1 Controller")
from IPython.kernel.error import SecurityError
from IPython.kernel import controllerservice
from IPython.kernel.fcutil import check_furl_file_security
from IPython.kernel.config import config_manager as kernel_config_manager
from IPython.config.cutils import import_item
#-------------------------------------------------------------------------------
# Code
#-------------------------------------------------------------------------------
def make_tub(ip, port, secure, cert_file):
"""
Create a listening tub given an ip, port, and cert_file location.
:Parameters:
ip : str
The ip address that the tub should listen on. Empty means all
port : int
The port that the tub should listen on. A value of 0 means
pick a random port
secure: boolean
Will the connection be secure (in the foolscap sense)
cert_file:
A filename of a file to be used for theSSL certificate
"""
if secure:
if have_crypto:
tub = Tub(certFile=cert_file)
else:
raise SecurityError("OpenSSL is not available, so we can't run in secure mode, aborting")
else:
tub = UnauthenticatedTub()
# Set the strport based on the ip and port and start listening
if ip == '':
strport = "tcp:%i" % port
else:
strport = "tcp:%i:interface=%s" % (port, ip)
listener = tub.listenOn(strport)
return tub, listener
def make_client_service(controller_service, config):
"""
Create a service that will listen for clients.
This service is simply a `foolscap.Tub` instance that has a set of Referenceables
registered with it.
"""
# Now create the foolscap tub
ip = config['controller']['client_tub']['ip']
port = config['controller']['client_tub'].as_int('port')
location = config['controller']['client_tub']['location']
secure = config['controller']['client_tub']['secure']
cert_file = config['controller']['client_tub']['cert_file']
client_tub, client_listener = make_tub(ip, port, secure, cert_file)
# Set the location in the trivial case of localhost
if ip == 'localhost' or ip == '127.0.0.1':
location = "127.0.0.1"
if not secure:
log.msg("WARNING: you are running the controller with no client security")
def set_location_and_register():
"""Set the location for the tub and return a deferred."""
def register(empty, ref, furl_file):
client_tub.registerReference(ref, furlFile=furl_file)
if location == '':
d = client_tub.setLocationAutomatically()
else:
d = defer.maybeDeferred(client_tub.setLocation, "%s:%i" % (location, client_listener.getPortnum()))
for ciname, ci in config['controller']['controller_interfaces'].iteritems():
log.msg("Adapting Controller to interface: %s" % ciname)
furl_file = ci['furl_file']
log.msg("Saving furl for interface [%s] to file: %s" % (ciname, furl_file))
check_furl_file_security(furl_file, secure)
adapted_controller = import_item(ci['controller_interface'])(controller_service)
d.addCallback(register, import_item(ci['fc_interface'])(adapted_controller),
furl_file=ci['furl_file'])
reactor.callWhenRunning(set_location_and_register)
return client_tub
def make_engine_service(controller_service, config):
"""
Create a service that will listen for engines.
This service is simply a `foolscap.Tub` instance that has a set of Referenceables
registered with it.
"""
# Now create the foolscap tub
ip = config['controller']['engine_tub']['ip']
port = config['controller']['engine_tub'].as_int('port')
location = config['controller']['engine_tub']['location']
secure = config['controller']['engine_tub']['secure']
cert_file = config['controller']['engine_tub']['cert_file']
engine_tub, engine_listener = make_tub(ip, port, secure, cert_file)
# Set the location in the trivial case of localhost
if ip == 'localhost' or ip == '127.0.0.1':
location = "127.0.0.1"
if not secure:
log.msg("WARNING: you are running the controller with no engine security")
def set_location_and_register():
"""Set the location for the tub and return a deferred."""
def register(empty, ref, furl_file):
engine_tub.registerReference(ref, furlFile=furl_file)
if location == '':
d = engine_tub.setLocationAutomatically()
else:
d = defer.maybeDeferred(engine_tub.setLocation, "%s:%i" % (location, engine_listener.getPortnum()))
furl_file = config['controller']['engine_furl_file']
engine_fc_interface = import_item(config['controller']['engine_fc_interface'])
log.msg("Saving furl for the engine to file: %s" % furl_file)
check_furl_file_security(furl_file, secure)
fc_controller = engine_fc_interface(controller_service)
d.addCallback(register, fc_controller, furl_file=furl_file)
reactor.callWhenRunning(set_location_and_register)
return engine_tub
def start_controller():
"""
Start the controller by creating the service hierarchy and starting the reactor.
This method does the following:
* It starts the controller logging
* In execute an import statement for the controller
* It creates 2 `foolscap.Tub` instances for the client and the engines
and registers `foolscap.Referenceables` with the tubs to expose the
controller to engines and clients.
"""
config = kernel_config_manager.get_config_obj()
# Start logging
logfile = config['controller']['logfile']
if logfile:
logfile = logfile + str(os.getpid()) + '.log'
try:
openLogFile = open(logfile, 'w')
except:
openLogFile = sys.stdout
else:
openLogFile = sys.stdout
log.startLogging(openLogFile)
# Execute any user defined import statements
cis = config['controller']['import_statement']
if cis:
try:
exec cis in globals(), locals()
except:
log.msg("Error running import_statement: %s" % cis)
# Create the service hierarchy
main_service = service.MultiService()
# The controller service
controller_service = controllerservice.ControllerService()
controller_service.setServiceParent(main_service)
# The client tub and all its refereceables
client_service = make_client_service(controller_service, config)
client_service.setServiceParent(main_service)
# The engine tub
engine_service = make_engine_service(controller_service, config)
engine_service.setServiceParent(main_service)
# Start the controller service and set things running
main_service.startService()
reactor.run()
def init_config():
"""
Initialize the configuration using default and command line options.
"""
parser = OptionParser()
# Client related options
parser.add_option(
"--client-ip",
type="string",
dest="client_ip",
help="the IP address or hostname the controller will listen on for client connections"
)
parser.add_option(
"--client-port",
type="int",
dest="client_port",
help="the port the controller will listen on for client connections"
)
parser.add_option(
'--client-location',
type="string",
dest="client_location",
help="hostname or ip for clients to connect to"
)
parser.add_option(
"-x",
action="store_false",
dest="client_secure",
help="turn off all client security"
)
parser.add_option(
'--client-cert-file',
type="string",
dest="client_cert_file",
help="file to store the client SSL certificate"
)
parser.add_option(
'--task-furl-file',
type="string",
dest="task_furl_file",
help="file to store the FURL for task clients to connect with"
)
parser.add_option(
'--multiengine-furl-file',
type="string",
dest="multiengine_furl_file",
help="file to store the FURL for multiengine clients to connect with"
)
# Engine related options
parser.add_option(
"--engine-ip",
type="string",
dest="engine_ip",
help="the IP address or hostname the controller will listen on for engine connections"
)
parser.add_option(
"--engine-port",
type="int",
dest="engine_port",
help="the port the controller will listen on for engine connections"
)
parser.add_option(
'--engine-location',
type="string",
dest="engine_location",
help="hostname or ip for engines to connect to"
)
parser.add_option(
"-y",
action="store_false",
dest="engine_secure",
help="turn off all engine security"
)
parser.add_option(
'--engine-cert-file',
type="string",
dest="engine_cert_file",
help="file to store the engine SSL certificate"
)
parser.add_option(
'--engine-furl-file',
type="string",
dest="engine_furl_file",
help="file to store the FURL for engines to connect with"
)
parser.add_option(
"-l", "--logfile",
type="string",
dest="logfile",
help="log file name (default is stdout)"
)
parser.add_option(
"--ipythondir",
type="string",
dest="ipythondir",
help="look for config files and profiles in this directory"
)
(options, args) = parser.parse_args()
kernel_config_manager.update_config_obj_from_default_file(options.ipythondir)
config = kernel_config_manager.get_config_obj()
# Update with command line options
if options.client_ip is not None:
config['controller']['client_tub']['ip'] = options.client_ip
if options.client_port is not None:
config['controller']['client_tub']['port'] = options.client_port
if options.client_location is not None:
config['controller']['client_tub']['location'] = options.client_location
if options.client_secure is not None:
config['controller']['client_tub']['secure'] = options.client_secure
if options.client_cert_file is not None:
config['controller']['client_tub']['cert_file'] = options.client_cert_file
if options.task_furl_file is not None:
config['controller']['controller_interfaces']['task']['furl_file'] = options.task_furl_file
if options.multiengine_furl_file is not None:
config['controller']['controller_interfaces']['multiengine']['furl_file'] = options.multiengine_furl_file
if options.engine_ip is not None:
config['controller']['engine_tub']['ip'] = options.engine_ip
if options.engine_port is not None:
config['controller']['engine_tub']['port'] = options.engine_port
if options.engine_location is not None:
config['controller']['engine_tub']['location'] = options.engine_location
if options.engine_secure is not None:
config['controller']['engine_tub']['secure'] = options.engine_secure
if options.engine_cert_file is not None:
config['controller']['engine_tub']['cert_file'] = options.engine_cert_file
if options.engine_furl_file is not None:
config['controller']['engine_furl_file'] = options.engine_furl_file
if options.logfile is not None:
config['controller']['logfile'] = options.logfile
kernel_config_manager.update_config_obj(config)
def main():
"""
After creating the configuration information, start the controller.
"""
init_config()
start_controller()
if __name__ == "__main__":
main()