def __init__(self, app, enc_vol, user_id, data_dir, data_path, setup=True): self.app = app self.enc_vol = enc_vol self.user_id = user_id self.data_dir = data_dir self.data_path = data_path self.api_disabled = False # Start the task runner thread from container_exec import ContainerExec self.container_exec = ContainerExec(self.user_id, self.data_path) self.container_exec.begin() if self.exec_cmd('init'): print 'Database loaded' else: raise Exception('Database not initialized') if setup: self.setup()
class Container(object): def __init__(self, app, enc_vol, user_id, data_dir, data_path, setup=True): self.app = app self.enc_vol = enc_vol self.user_id = user_id self.data_dir = data_dir self.data_path = data_path self.api_disabled = False # Start the task runner thread from container_exec import ContainerExec self.container_exec = ContainerExec(self.user_id, self.data_path) self.container_exec.begin() if self.exec_cmd('init'): print 'Database loaded' else: raise Exception('Database not initialized') if setup: self.setup() def setup(self): self.app.add_url_rule('/ping', 'ping', self.ping, methods=['GET']) self.app.add_url_rule('/sync', 'sync', self.sync, methods=['GET']) self.app.add_url_rule('/exit', 'exit', self.exit, methods=['GET']) self.app.add_url_rule('/auth', 'auth', self.auth, methods=['GET']) self.app.add_url_rule('/unauth', 'unauth', self.unauth, methods=['GET']) import atexit atexit.register(self.onexit) if self.exec_cmd('inituser'): print 'User initialized' else: print 'User exists' def onexit(self): print 'Flushing command queue' self.container_exec.flush(save=True) print 'Done' if self.enc_vol: print self.enc_vol.close() print 'Encrypted volume unmounted' def add_exec_bundle(self, cls): self.container_exec.add_exec_bundle(cls) def exec_cmd(self, cmd, params=None, timeout=30, disable=False): if self.api_disabled: raise Exception('Interface is not available') if disable: self.api_disabled = True if params is None: params = [] q = self.container_exec.add(cmd, *params) try: success, result = q.get(True, timeout) except Empty: raise Exception('Result timeout for "%s" after %ds' % (cmd, timeout)) else: if not success: raise result return result @staticmethod def is_error(err): return type(err) is dict and err.get('Error') def validate_auth(self): return if not current_user or not current_user.is_authenticated(): raise BenomeAuthError('Unauthorized') if self.enc_vol and not self.enc_vol.is_open(quick=True): raise BenomeDataError('Unavailable') def ping(self): if self.api_disabled: raise Exception('Interface is not available') return json_response({ 'Success': True }) def sync(self): self.exec_cmd('shutdown', disable=True) return json_response({ 'Success': True }) def exit(self): exit(0) def auth(self): if current_user and current_user.is_authenticated() and self.enc_vol and self.enc_vol.is_open(quick=True): raise BenomeAuthError('Already authenticated') password = request.args.get('Password', None) if not password: raise BenomeAuthError('Passphrase required') if self.enc_vol: if self.enc_vol.is_initialized(): self.enc_vol.open(password) elif not self.enc_vol.is_open(): init_success = self.enc_vol.init(password) if not self.enc_vol or self.enc_vol.is_open(): if self.exec_cmd('init'): print 'Database loaded' if self.exec_cmd('inituser'): print 'User initialized' user = init_user('ContainerUser') login_user(user, remember=True) return json_response('Success'), 200 else: raise BenomeDataError('Unavailable') raise BenomeAuthError('Unauthorized') def unauth(self): if not current_user or not current_user.is_authenticated(): raise BenomeAuthError('Unauthorized') logout_user() if self.enc_vol: self.enc_vol.close() return json_response('Closed'), 200