108
106
class Console(object):
109
107
""" Provides a nice python interface to the console
111
def __init__(self, config, uid, jail_path, working_dir):
109
def __init__(self, config, user, jail_path, working_dir):
112
110
"""Starts up a console service for user uid, inside chroot jail
113
111
jail_path with work directory of working_dir
115
113
super(Console, self).__init__()
117
115
self.config = config
119
117
self.jail_path = jail_path
120
118
self.working_dir = working_dir
152
150
# is (k / N) ** t (e.g. 3.2*10e-9).
156
155
self.port = int(random.uniform(3000, 8000))
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'),
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)
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.
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
189
raise ConsoleError("Unable to start console service!")
174
raise ConsoleError('Unable to start console service: %s'%error)
191
176
def __chat(self, cmd, args):
192
177
""" A wrapper around chat.chat to comunicate directly with the
204
189
# Some other error - probably serious
205
190
raise socket.error, (enumber, estring)
206
except cjson.DecodeError:
207
192
# Couldn't decode the JSON
208
193
raise ConsoleError(
209
194
"Could not understand the python console response")