def authentication_fakeadmin(self,fakeuser,admin_password): with app.app_context(): admin_dbuser=r.table('users').get('admin').run(db.conn) if admin_dbuser is None: return False pw=Password() if pw.valid(admin_password,admin_dbuser['password']): with app.app_context(): dbuser=r.table('users').get(fakeuser).run(db.conn) if dbuser is None: return False else: dbuser['name']='FAKEUSER' #~ quota = admin_dbuser['quota'] #~ { 'domains':{ 'desktops': 99, #~ 'templates':99, #~ 'running': 99}, #~ 'hardware':{'vcpus': 8, #~ 'ram': 1000000}} # 10GB dbuser['quota']=admin_dbuser['quota'] dbuser['role']='admin' ram_users[fakeuser]=dbuser return User(dbuser) else: return False
def getNewKindId(self, kind, username, id): if kind == 'domains' or kind == 'media': web = [ d.copy() for d in self.web[kind] if '_' + username + '_' + d['id'] == id ] else: web = [d.copy() for d in self.web[kind] if d['id'] == id] if len(web) == 0: return False w = web[0].copy() if kind == 'domains' or kind == 'media': with app.app_context(): dbb = r.table(kind).get('_' + username + '_' + w['id']).run( db.conn) if dbb is None: w['id'] = '_' + username + '_' + w['id'] return w else: with app.app_context(): dbb = r.table(kind).get(w['id']).run(db.conn) if dbb is None: return w return False
def authentication_fakeadmin(self, fakeuser, admin_password): with app.app_context(): admin_dbuser = r.table('users').get('admin').run(db.conn) if admin_dbuser is None: return False pw = Password() if pw.valid(admin_password, admin_dbuser['password']): with app.app_context(): dbuser = r.table('users').get(fakeuser).run(db.conn) if dbuser is None: return False else: dbuser['name'] = 'FAKEUSER' #~ quota = admin_dbuser['quota'] #~ { 'domains':{ 'desktops': 99, #~ 'templates':99, #~ 'running': 99}, #~ 'hardware':{'vcpus': 8, #~ 'ram': 1000000}} # 10GB dbuser['quota'] = admin_dbuser['quota'] dbuser['role'] = 'admin' ram_users[fakeuser] = dbuser return User(dbuser) else: return False
def delete_old_stats(reduce_interval=300,delete_interval=86400): # 24h with app.app_context(): # domains_status r.table('domains_status_history').filter(r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn) reduced=[] cursor = r.table('domains_status').filter(r.row['when'] < int(time.time()) - reduce_interval).order_by('when').run(db.conn) r.table('domains_status').filter(r.row['when'] < int(time.time()) - reduce_interval).delete().run(db.conn) i=0 for c in cursor: if i % 50 == 0: reduced.append(c) i+=1 r.table('domains_status_history').insert(reduced).run(db.conn) # Hypervisors_status r.table('hypervisors_status_history').filter(r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn) reduced=[] cursor = r.table('hypervisors_status').filter(r.row['when'] < int(time.time()) - reduce_interval).order_by('when').run(db.conn) r.table('hypervisors_status').filter(r.row['when'] < int(time.time()) - reduce_interval).delete().run(db.conn) i=0 for c in cursor: if i % 50 == 0: reduced.append(c) i+=1 r.table('hypervisors_status_history').insert(reduced).run(db.conn) # Hypervisors_events (does not grow at the same speed) r.table('hypervisors_events').filter(r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn)
def create_user(username, password1, role='user'): with app.app_context(): new_user = User(username=username, role=role) new_user.set_password(password1) db.session.add(new_user) db.session.commit() print(f"Пользователь с именем {new_user.username} создан")
def create_container(dbtype, name, mem_limit, pm): adapter = _getAdapter(dbtype) container = adapter.create_container(mem_limit=int(mem_limit)*1024*1024, pm=pm) with app.app_context(): get_db().execute('insert into databases (docker_id, name, memory_limit, port_mapping, type) \ values (?, ?, ?, ?, ?)', [container['Id'], name, mem_limit, pm, dbtype]) get_db().commit() start_container(container['Id']) return container
def remove_container(c_id): with app.app_context(): get_db().execute('delete from databases where docker_id = ?', [c_id]) get_db().commit() try: dc().remove_container(c_id) except RuntimeError: pass return True
def stop_domains_without_viewer(): with app.app_context(): r.table('domains').get_all('Started', index='status').filter({ 'viewer': { 'client_since': False } }).update({ 'status': 'Stopping' }).run(db.conn)
def test_container(c_id): with app.app_context(): cdb = _get_container_from_db(c_id) adapter = _getAdapter(cdb['type']) info = inspect_container(c_id) if not info: raise Exception('No container by id') elif not info['running']: return False return adapter.test_container(info)
def send_email(subject, recipients, html=None, text=None): with app.app_context(), mail.connect() as conn: for recipient in recipients: msg = Message(subject, sender=app.config['EMAIL_FROM'], recipients=[recipient, ]) msg.html = html msg.text = text conn.send(msg)
def setUp(self): super(TestWebapp, self).setUp() self.p = mock.patch("webapp.ads") self.p.return_value = [] self.p.start() self.test_fd, app.config["DB"] = tempfile.mkstemp() with app.app_context(): get_db().executescript("create table user(id integer primary key autoincrement, name varchar)") get_db().executescript("insert into user (name) values ('ura'), ('masha')")
def authentication_local(self, username, password): with app.app_context(): dbuser = r.table('users').get(username).run(db.conn) log.info('USER:'******'active'] is not True: return False pw = Password() if pw.valid(password, dbuser['password']): #~ TODO: Check active or not user return User(dbuser) else: return False
def authentication_local(self,username,password): with app.app_context(): dbuser=r.table('users').get(username).run(db.conn) log.info('USER:'******'active'] is not True: return False pw=Password() if pw.valid(password,dbuser['password']): #~ TODO: Check active or not user return User(dbuser) else: return False
def send_email(subject, recipients, html=None, text=None): with app.app_context(), mail.connect() as conn: for recipient in recipients: msg = Message(subject, sender=app.config['EMAIL_FROM'], recipients=[ recipient, ]) msg.html = html msg.text = text conn.send(msg)
def setUp(self): self.app = app.test_client() self.app_context = app.app_context() self.app_context.__enter__() db.drop_all() db.create_all() self.user1 = User('user1', '123456') self.user2 = User('user2', '123456') db.session.add(self.user1) db.session.add(self.user2) db.session.commit()
def getNewKindId(self,kind,username,id): if kind == 'domains' or kind == 'media': web=[d.copy() for d in self.web[kind] if '_'+username+'_'+d['id'] == id] else: web=[d.copy() for d in self.web[kind] if d['id'] == id] if len(web)==0: return False w=web[0].copy() if kind == 'domains' or kind == 'media': with app.app_context(): dbb=r.table(kind).get('_'+username+'_'+w['id']).run(db.conn) if dbb is None: w['id']='_'+username+'_'+w['id'] return w else: with app.app_context(): dbb=r.table(kind).get(w['id']).run(db.conn) if dbb is None: return w return False
def rig_test_client(): with testing.postgresql.Postgresql() as postgresql: with app.app_context(): dburl = postgresql.url() engine = create_engine(dburl) Base.metadata.create_all(engine) db_session.bind = engine user_datastore = SQLAlchemySessionUserDatastore( db_session, User, Role) app.config['SQLALCHEMY_DATABASE_URI'] = dburl app.config['WTF_CSRF_ENABLED'] = False init_app_with_options(user_datastore) yield app.test_client(), engine
def add_scheduler(self,kind,action,hour,minute): id=kind+'_'+action+'_'+str(hour)+str(minute) function=getattr(isardScheduler,action) if kind == 'cron': self.scheduler.add_job(function, kind, hour=int(hour), minute=int(minute), jobstore=self.rStore, replace_existing=True, id=id) if kind == 'interval': self.scheduler.add_job(function, kind, hours=int(hour), minutes=int(minute), jobstore=self.rStore, replace_existing=True, id=id) if kind == 'date': alarm_time = datetime.now() + timedelta(hours=int(hour),minutes=int(minute)) self.scheduler.add_job(function, kind, run_date=alarm_time, jobstore=self.rStore, replace_existing=True, id=id) with app.app_context(): r.table('scheduler_jobs').get(id).update({'kind':kind,'action':action,'name':action.replace('_',' '),'hour':hour,'minute':minute}).run(db.conn) return True
def check_ephimeral_status(): with app.app_context(): domains = r.table('domains').get_all( 'Started', index='status').has_fields('ephimeral').pluck( 'id', 'ephimeral', 'history_domain').run(db.conn) t = time.time() for d in domains: if d['history_domain'][0]['when'] + int( d['ephimeral']['minutes']) * 60 < t: r.table('domains').get(d['id']).update({ 'status': d['ephimeral']['action'] }).run(db.conn)
def updateFromWeb(self): self.web={} self.kinds=['media','domains','builders','virt_install','virt_builder','videos','viewers'] failed=0 for k in self.kinds: self.web[k]=self.getKind(kind=k) if self.web[k]==500: # The id is no longer in updates server. # We better reset it with app.app_context(): r.table('config').get(1).update({'resources':{'code':False}}).run(db.conn) self.code=False
def is_registered(self): if self.is_conected(): if self.working: return self.code else: # we have an invalid code. (changes in web database?) with app.app_context(): r.table('config').get(1).update({ 'resources': { 'code': False } }).run(db.conn) return False
def get_missing_resources(self, domain, username): missing_resources = {'videos': []} dom_videos = domain['create_dict']['hardware']['videos'] with app.app_context(): sys_videos = list(r.table('videos').pluck('id').run(db.conn)) sys_videos = [sv['id'] for sv in sys_videos] for v in dom_videos: if v not in sys_videos: resource = self.getNewKindId('videos', username, v) if resource is not False: missing_resources['videos'].append(resource) ## graphics and interfaces missing return missing_resources
def get_missing_resources(self,domain,username): missing_resources={'videos':[]} dom_videos=domain['create_dict']['hardware']['videos'] with app.app_context(): sys_videos=list(r.table('videos').pluck('id').run(db.conn)) sys_videos=[sv['id'] for sv in sys_videos] for v in dom_videos: if v not in sys_videos: resource=self.getNewKindId('videos',username,v) if resource is not False: missing_resources['videos'].append(resource) ## graphics and interfaces missing return missing_resources
def test_get_tasks(): with app.app_context(): response = get_tasks() assert response != None assert response != '' assert response.status_code == 200 assert response.data != b'{}' response_content = response.json assert 'data' in response_content assert 'tasks' in response_content['data']
def register(self): try: req= requests.post(self.url+'/register' ,allow_redirects=False, verify=False, timeout=3) if req.status_code==200: with app.app_context(): r.table('config').get(1).update({'resources':{'code':req.json()}}).run(db.conn) self.code=req.json() self.updateFromConfig() self.updateFromWeb() return True else: print('Error response code: '+str(req.status_code)+'\nDetail: '+r.json()) except Exception as e: print("Error contacting.\n"+str(e)) return False
def get_containers(details=False): cs = [] with app.app_context(): for c in query_db('select * from databases'): cs.append({'id': c['docker_id'], 'name': c['name'], 'type': c['type']}) if not details: return cs else: detail_containers = [] for c in cs: # app.logger.debug(c) details = inspect_container(c['id']) details['name'] = c['name'] details['type'] = c['type'] detail_containers.append(details) return detail_containers
def check(self,username,password): if username=='admin': user_validated=self.authentication_local(username,password) if user_validated: self.update_access(username) return user_validated with app.app_context(): cfg=r.table('config').get(1).run(db.conn) if cfg is None: return False ldap_auth=cfg['auth']['ldap'] local_auth=cfg['auth']['local'] local_user=r.table('users').get(username).run(db.conn) if local_user is not None: if local_user['kind']=='local' and local_auth['active']: user_validated = self.authentication_local(username,password) if user_validated: self.update_access(username) return user_validated if local_user['kind']=='ldap' and ldap_auth['active']: user_validated = self.authentication_ldap(username,password) if user_validated: self.update_access(username) return user_validated #~ if local_user['kind']=='google_oauth2': #~ return self.authentication_googleOauth2(username,password) else: if ldap_auth['active']: user_validated=self.authentication_ldap(username,password) if user_validated: user=self.authentication_ldap(username,password,returnObject=False) if r.table('categories').get(user['category']).run(db.conn) is None: r.table('categories').insert({ 'id':user['category'], 'name':user['category'], 'description':'', 'quota':r.table('roles').get(user['role']).run(db.conn)['quota']}).run(db.conn) if r.table('groups').get(user['group']).run(db.conn) is None: r.table('groups').insert({ 'id':user['group'], 'name':user['group'], 'description':'', 'quota':r.table('categories').get(user['category']).run(db.conn)['quota']}).run(db.conn) r.table('users').insert(user).run(db.conn) self.update_access(username) return User(user) else: return False return False
def instantiate_db(app): """Make sure the db is initialized.""" # initialize db with flask_migrate with app.app_context(): try: flask_migrate.init(webapp.config.ALEMBIC_PATH) except alembic.util.exc.CommandError as e: if 'already exists' in str(e): pass else: logger.debug('flask db init failed: %s', e) raise e flask_migrate.migrate(webapp.config.ALEMBIC_PATH) try: logger.debug('flask db upgrade') flask_migrate.upgrade(webapp.config.ALEMBIC_PATH) except Exception as e: logger.debug('flask db upgrade failed: %s', e) raise e
def add_scheduler(self, kind, action, hour, minute): id = kind + '_' + action + '_' + str(hour) + str(minute) function = getattr(isardScheduler, action) if kind == 'cron': self.scheduler.add_job(function, kind, hour=int(hour), minute=int(minute), jobstore=self.rStore, replace_existing=True, id=id) if kind == 'interval': self.scheduler.add_job(function, kind, hours=int(hour), minutes=int(minute), jobstore=self.rStore, replace_existing=True, id=id) if kind == 'date': alarm_time = datetime.now() + timedelta(hours=int(hour), minutes=int(minute)) self.scheduler.add_job(function, kind, run_date=alarm_time, jobstore=self.rStore, replace_existing=True, id=id) with app.app_context(): r.table('scheduler_jobs').get(id).update({ 'kind': kind, 'action': action, 'name': action.replace('_', ' '), 'hour': hour, 'minute': minute }).run(db.conn) return True
def get_containers(details=False): cs = [] with app.app_context(): for c in query_db('select * from databases'): cs.append({ 'id': c['docker_id'], 'name': c['name'], 'type': c['type'] }) if not details: return cs else: detail_containers = [] for c in cs: # app.logger.debug(c) details = inspect_container(c['id']) details['name'] = c['name'] details['type'] = c['type'] detail_containers.append(details) return detail_containers
def updateFromWeb(self): self.web = {} self.kinds = [ 'media', 'domains', 'builders', 'virt_install', 'virt_builder', 'videos', 'viewers' ] failed = 0 for k in self.kinds: self.web[k] = self.getKind(kind=k) if self.web[k] == 500: # The id is no longer in updates server. # We better reset it with app.app_context(): r.table('config').get(1).update({ 'resources': { 'code': False } }).run(db.conn) self.code = False
def send_html_mail(recipients, subject, obj, change, date, url, text): templateLoader = jinja2.FileSystemLoader(searchpath=template_dir) env = jinja2.Environment(autoescape=True, loader=templateLoader) env.filters['printdict'] = format_dict body=contextdiff.nesteddiff(obj, change, contextdiff.mail) template = env.get_template("notif_mail.html") outputText = template.render( changes=change, subject=subject, body=body, date=date, url=url ) msg = Message(subject, sender = "*****@*****.**", bcc = list(recipients)) msg.html = outputText msg.body = text with app.app_context(): mail.send(msg)
def delete_old_stats(reduce_interval=300, delete_interval=86400): # 24h with app.app_context(): # domains_status r.table('domains_status_history').filter( r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn) reduced = [] cursor = r.table('domains_status').filter( r.row['when'] < int(time.time()) - reduce_interval).order_by('when').run(db.conn) r.table('domains_status').filter(r.row['when'] < int(time.time()) - reduce_interval).delete().run( db.conn) i = 0 for c in cursor: if i % 50 == 0: reduced.append(c) i += 1 r.table('domains_status_history').insert(reduced).run(db.conn) # Hypervisors_status r.table('hypervisors_status_history').filter( r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn) reduced = [] cursor = r.table('hypervisors_status').filter( r.row['when'] < int(time.time()) - reduce_interval).order_by('when').run(db.conn) r.table('hypervisors_status').filter( r.row['when'] < int(time.time()) - reduce_interval).delete().run(db.conn) i = 0 for c in cursor: if i % 50 == 0: reduced.append(c) i += 1 r.table('hypervisors_status_history').insert(reduced).run(db.conn) # Hypervisors_events (does not grow at the same speed) r.table('hypervisors_events').filter( r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn)
def register(self): try: req = requests.post(self.url + '/register', allow_redirects=False, verify=False, timeout=3) if req.status_code == 200: with app.app_context(): r.table('config').get(1).update({ 'resources': { 'code': req.json() } }).run(db.conn) self.updateFromConfig() self.updateFromWeb() return True else: print('Error response code: ' + str(req.status_code) + '\nDetail: ' + r.json()) except Exception as e: print("Error contacting.\n" + str(e)) return False
def event_stream_templates(username): with app.app_context(): for c in r.table('domains').get_all( username, index='user').filter((r.row["kind"] == 'user_template') | ( r.row["kind"] == 'public_template')).pluck({ 'id', 'name', 'icon', 'kind', 'description' }).changes(include_initial=True).run(db.conn): if c['new_val'] is None: yield 'retry: 5000\nevent: %s\nid: %d\ndata: %s\n\n' % ( 'Deleted', time.time(), json.dumps(c['old_val']['id'])) continue if 'old_val' not in c: yield 'retry: 5000\nevent: %s\nid: %d\ndata: %s\n\n' % ( 'New', time.time(), json.dumps(c['new_val'])) continue if 'detail' not in c['new_val']: c['new_val']['detail'] = '' c['new_val']['derivates'] = app.isardapi.get_domain_derivates( c['new_val']['id']) yield 'retry: 2000\nevent: %s\nid: %d\ndata: %s\n\n' % ( 'Status', time.time(), json.dumps(c['new_val'])) yield 'retry: 2000\nevent: %s\nid: %d\ndata: %s\n\n' % ( 'Quota', time.time(), json.dumps(qdict))
def getNewKind(self,kind,username): if kind == 'viewers': return self.web[kind] web=self.web[kind] with app.app_context(): dbb=list(r.table(kind).run(db.conn)) result=[] for w in web: dict={} found=False for d in dbb: if kind == 'domains' or kind == 'media': if d['id']=='_'+username+'_'+w['id']: dict=w.copy() found=True dict['id']='_'+username+'_'+dict['id'] dict['new']=False dict['status']=d['status'] dict['progress']=d.get('progress',False) break else: if d['id']==w['id']: dict=w.copy() found=True dict['new']=False dict['status']='Downloaded' break if not found: dict=w.copy() if kind == 'domains' or kind == 'media': dict['id']='_'+username+'_'+dict['id'] dict['new']=True dict['status']='Available' result.append(dict) return result
def getNewKind(self, kind, username): if kind == 'viewers': return self.web[kind] web = self.web[kind] with app.app_context(): dbb = list(r.table(kind).run(db.conn)) result = [] for w in web: dict = {} found = False for d in dbb: if kind == 'domains' or kind == 'media': if d['id'] == '_' + username + '_' + w['id']: dict = w.copy() found = True dict['id'] = '_' + username + '_' + dict['id'] dict['new'] = False dict['status'] = d['status'] dict['progress'] = d.get('progress', False) break else: if d['id'] == w['id']: dict = w.copy() found = True dict['new'] = False dict['status'] = 'Downloaded' break if not found: dict = w.copy() if kind == 'domains' or kind == 'media': dict['id'] = '_' + username + '_' + dict['id'] dict['new'] = True dict['status'] = 'Available' result.append(dict) return result
def stop_domains(): with app.app_context(): r.table('domains').get_all('Started',index='status').update({'status':'Stopping'}).run(db.conn)
def bulk_action(self,table,tbl_filter,tbl_update): with app.app_context(): log.info('BULK ACTION: Table {}, Filter {}, Update {}'.format(table,filter, update)) r.table(table).filter(filter).update(update).run(db.conn)
def update_access(self,username): with app.app_context(): r.table('users').get(username).update({'accessed':time.time()}).run(db.conn)
def getUser(self,username): with app.app_context(): usr=r.table('users').get(username).run(db.conn) return usr
def update_access(self, username): with app.app_context(): r.table('users').get(username).update({ 'accessed': time.time() }).run(db.conn)
def set_async_var(app, var, val): with app.app_context(): try: redis_obj.set(var, val) except: print("Counter redis server unreachable!")
#!/usr/bin/env python # -*- coding: utf-8 -*- from celery import Celery from celery.schedules import crontab import refresh_stock_holder,refresh_stock_finance,refresh_stock_trade from refresh_stock_holder import urls_queue, data_queue, ThreadCrawl, ThreadWrite from webapp import celery, app, config_app from webapp.services.holder_service import getRefreshStocks config_app(app, 'scriptfan.cfg') ctx = app.app_context() @celery.task def stock_holder(): with app.app_context() as ctx: refresh_stock_holder.main(ctx) @celery.task def stock_finance(): with app.app_context() as ctx: refresh_stock_finance.main(ctx) @celery.task def stock_trade(): with app.app_context() as ctx: refresh_stock_trade.main(ctx) celery.conf.beat_schedule = {
def getUser(self, username): with app.app_context(): usr = r.table('users').get(username).run(db.conn) return usr
def stock_holder(): with app.app_context() as ctx: refresh_stock_holder.main(ctx)
def stock_finance(): with app.app_context() as ctx: refresh_stock_finance.main(ctx)
def stock_trade(): with app.app_context() as ctx: refresh_stock_trade.main(ctx)
def stop_domains_without_viewer(): with app.app_context(): r.table('domains').get_all('Started',index='status').filter({'viewer':{'client_since':False}}).update({'status':'Stopping'}).run(db.conn)
def updateFromConfig(self): with app.app_context(): cfg = r.table('config').get(1).pluck('resources').run( db.conn)['resources'] self.url = cfg['url'] self.code = cfg['code']
def remove_container(c_id): with app.app_context(): get_db().execute('delete from databases where docker_id = ?', [c_id]) get_db().commit() dc().remove_container(c_id) return True
def _setUserQuota(self,role): with app.app_context(): return r.table('roles').get(role).run(db.conn)['quota']
def send_async_email(app,msg): with app.app_context(): mail.send(msg)
def start_test_server(): with app.app_context(): db.drop_all() db.create_all() server.start()
def incr_async_var(app, var): with app.app_context(): try: redis_obj.incr(var) except: print("Counter redis server unreachable!")
def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs)