33
from ivle import chat, interpret
37
35
class ConsoleError(Exception):
38
36
""" The console failed in some way. This is bad. """
39
def __init__(self, value):
42
return repr(self.value)
44
39
class ConsoleException(Exception):
45
40
""" The code being exectuted on the console returned an exception.
47
def __init__(self, value):
50
return repr(self.value)
52
44
class TruncateStringIO(StringIO.StringIO):
53
45
""" A class that wraps around StringIO and truncates the buffer when the
114
106
class Console(object):
115
107
""" Provides a nice python interface to the console
117
def __init__(self, config, uid, jail_path, working_dir):
109
def __init__(self, config, user, jail_path, working_dir):
118
110
"""Starts up a console service for user uid, inside chroot jail
119
111
jail_path with work directory of working_dir
121
113
super(Console, self).__init__()
123
115
self.config = config
125
117
self.jail_path = jail_path
126
118
self.working_dir = working_dir
158
150
# is (k / N) ** t (e.g. 3.2*10e-9).
162
155
self.port = int(random.uniform(3000, 8000))
164
trampoline_path = os.path.join(self.config['paths']['lib'],
167
# Start the console server (port, magic)
168
# trampoline usage: tramp uid jail_dir working_dir script_path args
169
# console usage: python-console port magic
170
res = os.spawnv(os.P_WAIT, trampoline_path, [
173
self.config['paths']['jails']['mounts'],
174
self.config['paths']['jails']['src'],
175
self.config['paths']['jails']['template'],
177
os.path.join(self.config['paths']['share'], 'services'),
179
os.path.join(self.config['paths']['share'],
180
'services/python-console'),
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)
192
# If we can't start the console after 5 attemps (can't find a free port
193
# during random probing, syntax errors, segfaults) throw an exception.
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
195
raise ConsoleError("Unable to start console service!")
174
raise ConsoleError('Unable to start console service: %s'%error)
197
176
def __chat(self, cmd, args):
198
177
""" A wrapper around chat.chat to comunicate directly with the
210
189
# Some other error - probably serious
211
190
raise socket.error, (enumber, estring)
212
except cjson.DecodeError:
213
192
# Couldn't decode the JSON
214
193
raise ConsoleError(
215
194
"Could not understand the python console response")
195
except chat.ProtocolError, e:
196
raise ConsoleError(*e.args)