106
108
class Console(object):
107
109
""" Provides a nice python interface to the console
109
def __init__(self, config, user, jail_path, working_dir):
111
def __init__(self, config, uid, jail_path, working_dir):
110
112
"""Starts up a console service for user uid, inside chroot jail
111
113
jail_path with work directory of working_dir
113
115
super(Console, self).__init__()
115
117
self.config = config
117
119
self.jail_path = jail_path
118
120
self.working_dir = working_dir
150
152
# is (k / N) ** t (e.g. 3.2*10e-9).
155
156
self.port = int(random.uniform(3000, 8000))
157
python_console = os.path.join(self.config['paths']['share'],
158
'services/python-console')
159
args = [python_console, str(self.port), str(self.magic)]
162
interpret.execute_raw(self.config, self.user, self.jail_path,
163
self.working_dir, "/usr/bin/python", args)
158
trampoline_path = os.path.join(self.config['paths']['lib'],
161
# Start the console server (port, magic)
162
# trampoline usage: tramp uid jail_dir working_dir script_path args
163
# console usage: python-console port magic
164
res = os.spawnv(os.P_WAIT, trampoline_path, [
167
self.config['paths']['jails']['mounts'],
168
self.config['paths']['jails']['src'],
169
self.config['paths']['jails']['template'],
171
os.path.join(self.config['paths']['share'], 'services'),
173
os.path.join(self.config['paths']['share'],
174
'services/python-console'),
166
except interpret.ExecutionError, e:
170
# If we can't start the console after 5 attemps (can't find a free
171
# port during random probing, syntax errors, segfaults) throw an
186
# If we can't start the console after 5 attemps (can't find a free port
187
# during random probing, syntax errors, segfaults) throw an exception.
174
raise ConsoleError('Unable to start console service: %s'%error)
189
raise ConsoleError("Unable to start console service!")
176
191
def __chat(self, cmd, args):
177
192
""" A wrapper around chat.chat to comunicate directly with the
189
204
# Some other error - probably serious
190
205
raise socket.error, (enumber, estring)
206
except cjson.DecodeError:
192
207
# Couldn't decode the JSON
193
208
raise ConsoleError(
194
209
"Could not understand the python console response")