def get_media(id_media): r_conn = new_rethink_connection() d = r.table('media').get(id_media).run(r_conn) close_rethink_connection(r_conn) return d
def update_origin_and_parents_to_new_template(id_domain, template_id): r_conn = new_rethink_connection() rtable = r.table('domains') new_create_dict_origin = {'create_dict': {'origin': template_id}} results = rtable.get(id_domain).update(new_create_dict_origin).run(r_conn) close_rethink_connection(r_conn) return results
def set_unknown_domains_not_in_hyps(hyps): # find domains in status Started,Paused,Unknown # that are not in hypervisors r_conn = new_rethink_connection() rtable = r.table('domains') status_to_unknown = ['Started', 'Paused', 'Unknown'] l = list( rtable.filter(lambda d: r.expr(status_to_unknown).contains(d['status']) ).filter(lambda d: r.not_( r.expr(hyps).contains(d['hyp_started']))).update({ 'status': 'Unknown' }).run(r_conn)) status_to_stopped = ['Starting', 'CreatingTemplate'] l = list( rtable.filter(lambda d: r.expr(status_to_stopped).contains(d['status']) ).filter(lambda d: r.not_( r.expr(hyps).contains(d['hyp_started']))).update({ 'status': 'Stopped' }).run(r_conn)) close_rethink_connection(r_conn) return l
def remove_dict_new_template_from_domain(id_domain): r_conn = new_rethink_connection() rtable = r.table('domains') results = rtable.get(id_domain).replace( r.row.without({'create_dict': 'template_dict'})).run(r_conn) close_rethink_connection(r_conn) return results
def table_config_created_and_populated(): try: r_conn_test = r.connect(RETHINK_HOST, RETHINK_PORT) if not r.db_list().contains(RETHINK_DB).run(r_conn_test): return False else: r_conn = new_rethink_connection() out = False if r.table_list().contains('config').run(r_conn): rtable = r.table('config') out = rtable.get(1).run(r_conn) close_rethink_connection(r_conn) if out is not False: if out is not None: return True else: return False else: return False except Exception as e: log.info('rethink db connectin failed') log.info(e) return False
def get_pool_from_domain(domain_id): r_conn = new_rethink_connection() rtable = r.table('domains') try: d = rtable.get(domain_id).pluck('hypervisors_pools').run(r_conn) if len(d) > 0: if len(d['hypervisors_pools']) > 0: pool = d['hypervisors_pools'][0] else: log.error( f'domain: {domain_id} with not hypervisors_pools in list. Pool default forced.' ) pool = 'default' else: log.error( f'domain: {domain_id} withouth hypervisors_pools key defined. Pool default forced.' ) pool = 'default' except r.ReqlNonExistenceError: log.error( 'domain_id {} does not exist in domains table'.format(domain_id)) log.debug('function: {}'.format(sys._getframe().f_code.co_name)) pool = 'default' close_rethink_connection(r_conn) return pool
def table_config_created_and_populated(): try: r_conn_test = r.connect(RETHINK_HOST, RETHINK_PORT) if not r.db_list().contains(RETHINK_DB).run(r_conn_test): print( f'rethink host {RETHINK_HOST} and port {RETHINK_PORT} has connected but rethink database {RETHINK_DB} is not created' ) return False else: r_conn = new_rethink_connection() out = False if r.table_list().contains('config').run(r_conn): rtable = r.table('config') out = rtable.get(1).run(r_conn) close_rethink_connection(r_conn) if out is not False: if out is not None: #print('table config populated in database') return True else: print('table config not populated in database') return False else: return False except Exception as e: print( f'rethink db connectin failed with hostname {RETHINK_HOST} and port {RETHINK_PORT}' ) print(e) print('Traceback: \n .{}'.format(traceback.format_exc())) return False
def remove_domain_viewer_values(id): r_conn = new_rethink_connection() rtable = r.table('domains') results = rtable.get(id).replace(r.row.without('viewer')).run(r_conn) close_rethink_connection(r_conn) return results
def table_config_created_and_populated(): try: r_conn_test = r.connect(RETHINK_HOST, RETHINK_PORT) if not r.db_list().contains(RETHINK_DB).run(r_conn_test): print(f'rethink host {RETHINK_HOST} and port {RETHINK_PORT} has connected but rethink database {RETHINK_DB} is not created') return False else: r_conn = new_rethink_connection() out = False if r.table_list().contains('config').run(r_conn): rtable = r.table('config') out = rtable.get(1).run(r_conn) close_rethink_connection(r_conn) if out is not False: if out is not None: #print('table config populated in database') return True else: print('table config not populated in database') return False else: return False except Exception as e: print(f'rethink db connectin failed with hostname {RETHINK_HOST} and port {RETHINK_PORT}') print(e) print('Traceback: \n .{}'.format(traceback.format_exc())) return False
def get_domain(id): r_conn = new_rethink_connection() rtable = r.table('domains') dict_domain = rtable.get(id).run(r_conn) close_rethink_connection(r_conn) return dict_domain
def insert_domain(dict_domain): r_conn = new_rethink_connection() rtable = r.table('domains') result = rtable.insert(dict_domain).run(r_conn) close_rethink_connection(r_conn) return result
def remove_domain(id): r_conn = new_rethink_connection() rtable = r.table('domains') result = rtable.get(id).delete().run(r_conn) close_rethink_connection(r_conn) return result
def update_domain_history_from_id_domain(domain_id, new_status, new_detail, date_now): r_conn = new_rethink_connection() rtable = r.table('domains') # domain_fields = rtable.get(domain_id).pluck('status','history_domain','detail','hyp_started').run(r_conn) domain_fields = rtable.get(domain_id).pluck('history_domain', 'hyp_started').run(r_conn) close_rethink_connection(r_conn) if 'history_domain' in domain_fields: history_domain = domain_fields['history_domain'] else: history_domain = [] if new_detail is None: new_detail = '' if 'hyp_started' in domain_fields: hyp_started = domain_fields['hyp_started'] else: hyp_started = '' #now = date_now.strftime("%Y-%b-%d %H:%M:%S.%f") now = time.time() update_domain_history_status(domain_id=domain_id, new_status=new_status, when=now, history_domain=history_domain, detail=new_detail, hyp_id=hyp_started)
def update_domain_progress(id_domain, percent): r_conn = new_rethink_connection() rtable = r.table('domains') results = rtable.get(id_domain).update({'progress': {'percent': percent, 'when': time.time()}}).run(r_conn) close_rethink_connection(r_conn) return results
def update_all_hyps_status(reset_status='Offline'): r_conn = new_rethink_connection() results = r.table('hypervisors').update({ 'status': reset_status }).run(r_conn) close_rethink_connection(r_conn) return results
def remove_disk_template_created_list_in_domain(id_domain): r_conn = new_rethink_connection() rtable = r.table('domains') results = rtable.get(id_domain).replace( r.row.without('disk_template_created')).run(r_conn) close_rethink_connection(r_conn) return results
def get_hyps_with_status(list_status, not_=False, empty=False): r_conn = new_rethink_connection() rtable = r.table('hypervisors') if not_ == True: l = list( rtable.filter({ 'enabled': True }).filter(lambda d: r.not_( r.expr(list_status).contains(d['status']))).run(r_conn)) else: l = list( rtable.filter({ 'enabled': True }).filter(lambda d: r.expr(list_status).contains(d['status'])).run( r_conn)) if empty == True: nostatus = list( rtable.filter({ 'enabled': True }).filter(lambda n: ~n.has_fields('status')).run(r_conn)) l = l + nostatus close_rethink_connection(r_conn) return l
def insert_db_hyp_status(dict_hyp_status): r_conn = new_rethink_connection() rtable = r.table('hypervisors_status') rtable.insert(dict_hyp_status). \ run(r_conn, durability="soft", noreply=True) close_rethink_connection(r_conn)
def insert_db_domain_status(dict_domain_status): r_conn = new_rethink_connection() rtable = r.table('domains_status') rtable.insert(dict_domain_status). \ run(r_conn, durability="soft", noreply=True) close_rethink_connection(r_conn)
def update_disk_operation(id, dict_fields_update): r_conn = new_rethink_connection() rtable = r.table('disk_operations') results = rtable.get(id).update(dict_fields_update).run(r_conn) close_rethink_connection(r_conn) return results
def update_disk_backing_chain(id_domain, index_disk, path_disk, list_backing_chain, new_template=False, list_backing_chain_template=[]): r_conn = new_rethink_connection() rtable = r.table('domains') domain = rtable.get(id_domain).run(r_conn) if new_template == True: domain['create_dict']['template_dict']['disks_info'] = list_backing_chain_template domain['disks_info'] = list_backing_chain results = rtable.replace(domain).run(r_conn) # # if new_template == True: # dict_disks = rtable.get(id_domain).pluck({'create_dict':{'template_dict':{'hardware':{'disks':{'file':True}}}}}).run(r_conn)['create_dict']['template_dict]'] # else: # dict_disks = rtable.get(id_domain).pluck({'hardware':{'disks':{'file':True}}}).run(r_conn) # # # if path_disk == dict_disks['hardware']['disks'][index_disk]['file']: # # INFO TO DEVELOPER: BACKING CHAIN NOT IN HARDWARE DICT # # dict_disks['template_json']['hardware']['disks'][index_disk]['backing_chain'] = list_backing_chain # if 'disks_info' not in dict_disks.keys(): # dict_disks['disks_info']={} # dict_disks['disks_info']['path_disk'] = list_backing_chain # # if new_template == True: # results = rtable.get(id_domain).update({'template_json':dict_disks}).run(r_conn) # else: # results = rtable.get(id_domain).update(dict_disks).run(r_conn) close_rethink_connection(r_conn) return results
def get_history_domain(domain_id): r_conn = new_rethink_connection() rtable = r.table('domains') result = rtable.get(domain_id).pluck('history_domain').run(r_conn) results = list(result['history_domain']) close_rethink_connection(r_conn) return results
def get_pool_from_domain(domain_id): r_conn = new_rethink_connection() rtable = r.table('domains') try: d = rtable.get(domain_id).pluck('pool', 'kind').run(r_conn) if d['kind'] == 'desktop': if 'pool' not in d.keys(): pool = 'default' else: if type(d['pool']) is unicode or type(d['pool']) is str: rtable = r.table('hypervisors_pools') d_pool = rtable.get(d['pool']).run(r_conn) if len(d_pool.keys()) > 0: pool = d['pool'] else: pool = 'default' else: pool = False except r.ReqlNonExistenceError: log.error('domain_id {} does not exist in domains table'.format(domain_id)) log.debug('function: {}'.format(sys._getframe().f_code.co_name)) pool = False close_rethink_connection(r_conn) return pool
def get_if_all_disk_template_created(id_domain): r_conn = new_rethink_connection() rtable = r.table('domains') dict_disk_templates = rtable.get(id_domain).pluck('disk_template_created').run(r_conn) created = len(dict_disk_templates['disk_template_created']) == sum(dict_disk_templates['disk_template_created']) close_rethink_connection(r_conn) return created
def update_domain_delete_after_stopped(id_domain,do_delete=True): r_conn = new_rethink_connection() rtable = r.table('domains') rtable.get(id_domain).update( {'delete_after_stopped': do_delete}).run(r_conn) close_rethink_connection(r_conn)
def update_domain_status(status, id_domain, hyp_id=None, detail='', keep_hyp_id=False): r_conn = new_rethink_connection() rtable = r.table('domains') # INFO TO DEVELOPER TODO: verificar que el estado que te ponen es realmente un estado válido # INFO TO DEVELOPER TODO: si es stopped puede interesar forzar resetear hyp_started no?? # INFO TO DEVELOPER TODO: MOLARÍA GUARDAR UN HISTÓRICO DE LOS ESTADOS COMO EN HYPERVISORES # INFO TO DEVELOPER: OJO CON hyp_started a None... peligro si alguien lo chafa, por eso estos if/else if keep_hyp_id == True: hyp_id = rtable.get(id_domain).pluck('hyp_started').run(r_conn)['hyp_started'] if hyp_id is None: # print('ojojojo') results = rtable.get_all(id_domain, index='id').update({ 'status': status, 'hyp_started': '', 'detail': json.dumps(detail)}).run(r_conn) else: results = rtable.get_all(id_domain, index='id').update({'hyp_started': hyp_id, 'status': status, 'detail': json.dumps(detail)}).run(r_conn) if status == 'Stopped': stop_last_domain_status(id_domain) close_rethink_connection(r_conn) # if results_zero(results): # # log.debug('id_domain {} in hyperviros {} does not exist in domain table'.format(id_domain,hyp_id)) return results
def update_domain_start_after_created(id_domain,do_create=True): r_conn = new_rethink_connection() rtable = r.table('domains') rtable.get(id_domain).update( {'start_after_created': do_create}).run(r_conn) close_rethink_connection(r_conn)
def update_domain_createing_template(id_domain, template_field, status='CreatingTemplate'): r_conn = new_rethink_connection() rtable = r.table('domains') rtable.get(id_domain).update( {'template_json': template_field, 'status': status}).run(r_conn) close_rethink_connection(r_conn)
def get_pool_hypers_conf(id_pool='default'): r_conn = new_rethink_connection() rtable = r.table('hypervisors_pools') result = rtable.get(id_pool).run(r_conn) close_rethink_connection(r_conn) return result
def update_uri_hyp(hyp_id, uri): r_conn = new_rethink_connection() rtable = r.table('hypervisors') out = rtable.get(hyp_id). \ update({'uri': uri}). \ run(r_conn) close_rethink_connection(r_conn) return out
def update_db_hyp_info(id, hyp_info): r_conn = new_rethink_connection() rtable = r.table('hypervisors') rtable.filter({'id': id}). \ update({'info': hyp_info}). \ run(r_conn) close_rethink_connection(r_conn)
def update_disk_template_created(id_domain, index_disk): r_conn = new_rethink_connection() rtable = r.table('domains') dict_disk_templates = rtable.get(id_domain).pluck('disk_template_created').run(r_conn) dict_disk_templates['disk_template_created'][index_disk] = 1 results = rtable.get(id_domain).update(dict_disk_templates).run(r_conn) close_rethink_connection(r_conn) return results
def get_domains_from_user(user, kind='desktop'): r_conn = new_rethink_connection() rtable = r.table('domains') l = list(rtable.filter({'user': user, 'kind': kind}).pluck('status', 'id', 'kind', {'hardware': [{'disks': ['file']}]}).run(r_conn)) close_rethink_connection(r_conn) return [{'kind': s['kind'], 'id': s['id'], 'status': s['status'], 'disk': s['hardware']['disks'][0]['file']} for s in l]
def update_status_table(table,status,id_table,detail=""): r_conn = new_rethink_connection() d={'status':status, 'detail':detail} try: r.table(table).get(id_table).update(d).run(r_conn) except: logs.main.error(f'Error when updated status in table: {table}, status: {status}, id: {id_table}, detail: {detail}') close_rethink_connection(r_conn)
def insert_eval_initial_ux(obj): r_conn = new_rethink_connection() rtable = r.table('eval_initial_ux') x = rtable.get(obj.get('id')).run(r_conn) if x: rtable.get(obj.get('id')).update(obj).run(r_conn) else: rtable.insert(obj).run(r_conn, durability="soft", noreply=True) close_rethink_connection(r_conn)
def update_hypervisor_failed_connection(id, fail_connected_reason=''): r_conn = new_rethink_connection() rtable = r.table('hypervisors') rtable.get(id).update({'detail': str(fail_connected_reason)}).run(r_conn) # if len(fail_connected_reason) > 0: # rtable.get(id).update({'detail':fail_connected_reason}).run(r_conn) # else: # rtable.get(id).replace(r.row.without('fail_connected_reason')).run(r_conn) close_rethink_connection(r_conn)
def get_config_branch(key): r_conn = new_rethink_connection() rtable = r.table('config') try: d_config = rtable.get(1)[key].run(r_conn) except r.ReqlNonExistenceError: d_config = False close_rethink_connection(r_conn) return d_config
def update_actual_stats_hyp(id_hyp, hyp_stats, means = {}): d={'id':id_hyp, 'now': hyp_stats, 'means': means } r_conn = new_rethink_connection() rtable = r.table('hypervisors_status') rtable.insert(d,conflict='update').run(r_conn, durability="soft", noreply=True) close_rethink_connection(r_conn)
def insert_disk_operation(dict_disk_operation): r_conn = new_rethink_connection() rtable = r.table('disk_operations') result = rtable.insert(dict_disk_operation). \ run(r_conn) close_rethink_connection(r_conn) id = result['generated_keys'] return id
def get_id_hyp_from_uri(uri): r_conn = new_rethink_connection() rtable = r.table('hypervisors') l = list(rtable. \ filter({'uri': uri}). \ pluck('id'). \ run(r_conn)) close_rethink_connection(r_conn) if len(l) > 0: return l[0]['id'] else: log.error('function: {} uri {} not defined in hypervisors table'.format(str(__name__), uri))
def get_hypers_disk_operations(): r_conn = new_rethink_connection() rtable = r.table('hypervisors') hypers_ids = [d['id'] for d in list( rtable.filter({'enabled': True, 'capabilities': {'disk_operations': True}}).pluck('id').run(r_conn))] close_rethink_connection(r_conn) return hypers_ids pass
def get_hyp_hostname_from_id(id): r_conn = new_rethink_connection() l = r.table('hypervisors').get(id).pluck('hostname', 'port', 'user').run(r_conn) close_rethink_connection(r_conn) if len(l) > 0: if l.__contains__('user') and l.__contains__('port'): return l['hostname'], l['port'], l['user'] else: log.error('hypervisor {} does not contain user or port in database'.format(id)) return False else: return False
def get_downloads_in_progress(): r_conn = new_rethink_connection() try: d = r.table('media').get_all(r.args(['DownloadStarting', 'Downloading']), index='status'). \ pluck('id', 'path', 'isard-web', 'status').run(r_conn) except r.ReqlNonExistenceError: d = [] close_rethink_connection(r_conn) return d
def get_hypers_info(id_pool='default', pluck=None): r_conn = new_rethink_connection() rtable = r.table('hypervisors') return_operations = [] if not pluck: results = list(rtable.filter(r.row['hypervisors_pools'].contains(id_pool)). \ filter({'status': 'Online'}).run(r_conn)) else: results = list(rtable.filter(r.row['hypervisors_pools'].contains(id_pool)). \ filter({'status': 'Online'}).pluck(pluck).run(r_conn)) close_rethink_connection(r_conn) return results
def get_hyps_ready_to_start(): r_conn = new_rethink_connection() rtable = r.table('hypervisors') l = list(rtable. \ filter({'enabled': True, 'status': 'ReadyToStart'}). \ pluck('id', 'hostname', 'hypervisors_pools'). \ run(r_conn)) close_rethink_connection(r_conn) hyps_hostnames = {d['id']: d['hostname'] for d in l} return hyps_hostnames
def stop_last_domain_status(name): r_conn = new_rethink_connection() rtable = r.table('domains_status') try: return rtable. \ get_all(name, index='name'). \ nth(-1).update({'state': 'Stopped', 'state_reason': 'not running'}). \ run(r_conn) close_rethink_connection(r_conn) except: close_rethink_connection(r_conn) return None
def update_status_media_from_path(path,status,detail=''): r_conn = new_rethink_connection() table = 'media' d_status ={'status':status, 'detail':detail} result = [] l = list(r.table(table).filter({'path_downloaded': path}).run(r_conn)) if len(l) > 0: for d in l: result.append(r.table(table).get(d['id']).update(d_status).run(r_conn)) else: result = False close_rethink_connection(r_conn) return result
def get_last_hyp_status(id): r_conn = new_rethink_connection() rtable = r.table('hypervisors_status') l = list(rtable. \ filter({'hyp_id': id}). \ order_by(r.desc('when')). \ limit(1). \ run(r_conn)) close_rethink_connection(r_conn) if len(l) == 0: return None else: return l[0]
def get_last_domain_status(name): r_conn = new_rethink_connection() rtable = r.table('domains_status') try: return rtable. \ get_all(name, index='name'). \ nth(-1). \ run(r_conn) # ~ filter({'name':name}).\ # ~ order_by(r.desc('when')).\ # ~ limit(1).\ close_rethink_connection(r_conn) except: close_rethink_connection(r_conn) return None
def get_hyp_hostnames_online(): r_conn = new_rethink_connection() rtable = r.table('hypervisors') l = list(rtable. \ filter({'enabled': True, 'status': 'Online'}). \ pluck('id', 'hostname'). \ run(r_conn)) close_rethink_connection(r_conn) log.debug(l) if len(l) > 0: hyps_hostnames = {d['id']: d['hostname'] for d in l} return hyps_hostnames else: return dict()
def update_actual_stats_domain(id_domain, domain_stats, means): d={'id':id_domain, 'now': domain_stats, 'means': means } r_conn = new_rethink_connection() rtable = r.table('domains_status') try: rtable.insert(d,conflict='update').run(r_conn, durability="soft", noreply=True) except Exception as e: log.debug('Error inserting domain_stats: {}'.format(id_domain)) log.debug(e) close_rethink_connection(r_conn)
def get_hypers_in_pool(id_pool='default', only_online=True): r_conn = new_rethink_connection() rtable = r.table('hypervisors') return_operations = [] if only_online: l = list(rtable.filter(r.row['hypervisors_pools'].contains(id_pool)). \ filter({'status': 'Online'}).pluck('id').run(r_conn)) else: l = list(rtable.filter(r.row['hypervisors_pools'].contains(id_pool)). \ pluck('id').run(r_conn)) hyp_ids = [a['id'] for a in l] close_rethink_connection(r_conn) return hyp_ids
def get_all_hypervisor_status(hyp_id, start=None, end=None): r_conn = new_rethink_connection() rtable = r.table('hypervisors_status') if start and end: results = rtable.filter({'hyp_id': hyp_id}).filter(lambda s: start <= s['when'] and s['when'] <= end).order_by( r.desc('when')).run(r_conn) elif start: results = rtable.filter({'hyp_id': hyp_id}).filter(lambda s: start <= s['when']).order_by(r.desc('when')).run( r_conn) elif end: results = rtable.filter({'hyp_id': hyp_id}).filter(lambda s: s['when'] <= end).order_by(r.desc('when')).run( r_conn) else: results = rtable.filter({'hyp_id': hyp_id}).order_by(r.desc('when')).run(r_conn) results = list(results) close_rethink_connection(r_conn) return results
def get_all_domain_status(name, start=None, stop=None, history=False): r_conn = new_rethink_connection() table = "domains_status" if not history else "domains_status_history" rtable = r.table(table) obj = {'name': name} if start and stop: results = rtable.filter(obj).filter(lambda s: start <= s['when'] and s['when'] <= stop).order_by( r.desc('when')).run(r_conn) elif start: results = rtable.filter(obj).filter(lambda s: start <= s['when']).order_by(r.desc('when')).run( r_conn) elif stop: results = rtable.filter(obj).filter(lambda s: s['when'] <= stop).order_by(r.desc('when')).run(r_conn) else: results = rtable.filter(obj).order_by(r.desc('when')).run(r_conn) results = list(results) close_rethink_connection(r_conn) return results
def get_hyps_with_status(list_status, not_=False, empty=False): r_conn = new_rethink_connection() rtable = r.table('hypervisors') if not_ == True: l = list(rtable.filter({'enabled': True}).filter(lambda d: r.not_(r.expr(list_status). contains(d['status']))). run(r_conn)) else: l = list(rtable.filter({'enabled': True}).filter(lambda d: r.expr(list_status). contains(d['status'])). run(r_conn)) if empty == True: nostatus = list(rtable.filter({'enabled': True}).filter(lambda n: ~n.has_fields('status')).run(r_conn)) l = l + nostatus close_rethink_connection(r_conn) return l
def __init__(self): self.conn = new_rethink_connection()