def is_cluster_leader(): self_id = Compute.get_instance_id() cluster = Compute.get_install_id() instances = Compute.get_all_instances() leader = JBoxDynConfig.get_cluster_leader(cluster) img_recentness = Compute.get_image_recentness() JBoxDB.log_debug("cluster: %s. instances: %s. leader: %s. image recentness: %d", cluster, repr(instances), repr(leader), img_recentness) # if none set, or set instance is dead elect self as leader, but wait till next cycle to prevent conflicts if (leader is None) or (leader not in instances) and (img_recentness >= 0): JBoxDB.log_info("setting self (%s) as cluster leader", self_id) try: JBoxDynConfig.set_cluster_leader(cluster, self_id) except: JBoxDB.log_info("error setting self (%s) as cluster leader, will retry", self_id) return False is_leader = (leader == self_id) # if running an older ami, step down from cluster leader if (img_recentness < 0) and is_leader: JBoxDB.log_info("unmarking self (%s) as cluster leader", self_id) JBoxDynConfig.unset_cluster_leader(cluster) return False return is_leader
def is_cluster_leader(): self_id = Compute.get_instance_id() cluster = Compute.get_install_id() instances = Compute.get_all_instances() leader = JBoxDynConfig.get_cluster_leader(cluster) img_recentness = Compute.get_image_recentness() JBoxDB.log_debug( "cluster: %s. instances: %s. leader: %s. image recentness: %d", cluster, repr(instances), repr(leader), img_recentness) # if none set, or set instance is dead elect self as leader, but wait till next cycle to prevent conflicts if (leader is None) or (leader not in instances) and (img_recentness >= 0): JBoxDB.log_info("setting self (%s) as cluster leader", self_id) try: JBoxDynConfig.set_cluster_leader(cluster, self_id) except: JBoxDB.log_info( "error setting self (%s) as cluster leader, will retry", self_id) return False is_leader = (leader == self_id) # if running an older ami, step down from cluster leader if (img_recentness < 0) and is_leader: JBoxDB.log_info("unmarking self (%s) as cluster leader", self_id) JBoxDynConfig.unset_cluster_leader(cluster) return False return is_leader
def get_cluster_api_status(): result = dict() for inst in Compute.get_all_instances(): try: api_status = JBoxAsyncJob.sync_api_status(inst) if api_status['code'] == 0: result[inst] = api_status['data'] else: APIContainer.log_error("error fetching api status from %s", inst) except: APIContainer.log_error("exception fetching api status from %s", inst) APIContainer.log_debug("api status: %r", result) return result
def find_logged_in_instance(user_id): container_id = "/" + unique_sessname(user_id) instances = Compute.get_all_instances() for inst in instances: try: sessions = JBoxAsyncJob.sync_session_status(inst)['data'] if len(sessions) > 0: if container_id in sessions: return inst except: JBoxHandler.log_error("Error receiving sessions list from %r", inst) pass return None
def get_active_sessions(): instances = Compute.get_all_instances() active_sessions = set() for inst in instances: try: sessions = JBoxAsyncJob.sync_session_status(inst)['data'] if len(sessions) > 0: for sess_id in sessions.keys(): active_sessions.add(sess_id) except: SessContainer.log_error("Error receiving sessions list from %r", inst) return active_sessions
def find_logged_in_instance(user_id): container_id = "/" + unique_sessname(user_id) instances = Compute.get_all_instances() for inst in instances: try: sessions = JBoxAsyncJob.sync_session_status(inst)['data'] if len(sessions) > 0: if container_id in sessions: return inst except: JBoxHandler.log_error("Error receiving sessions list from %r", inst) pass return None
def get_cluster_api_status(): result = dict() for inst in Compute.get_all_instances(): try: api_status = JBoxAsyncJob.sync_api_status(inst) if api_status['code'] == 0: result[inst] = api_status['data'] else: APIContainer.log_error("error fetching api status from %s", inst) except: APIContainer.log_error("exception fetching api status from %s", inst) APIContainer.log_debug("api status: %r", result) return result
def get_active_sessions(): instances = Compute.get_all_instances() active_sessions = set() for inst in instances: try: sessions = JBoxAsyncJob.sync_session_status(inst)['data'] if len(sessions) > 0: for sess_id in sessions.keys(): active_sessions.add(sess_id) except: SessContainer.log_error( "Error receiving sessions list from %r", inst) return active_sessions
def handle_if_instance_info(self, is_allowed): stats = self.get_argument('instance_info', None) if stats is None: return False if not is_allowed: AdminHandler.log_error("Show instance info not allowed for user") response = {'code': -1, 'data': 'You do not have permissions to view these stats'} else: try: if stats == 'load': result = {} # get cluster loads average_load = Compute.get_cluster_average_stats('Load') if None != average_load: result['Average Load'] = average_load machine_loads = Compute.get_cluster_stats('Load') if None != machine_loads: for n, v in machine_loads.iteritems(): result['Instance ' + n] = v elif stats == 'sessions': result = dict() instances = Compute.get_all_instances() for idx in range(0, len(instances)): try: inst = instances[idx] result[inst] = JBoxAsyncJob.sync_session_status(inst)['data'] except: JBoxHandler.log_error("Error receiving sessions list from %r", inst) elif stats == 'apis': result = APIContainer.get_cluster_api_status() else: raise Exception("unknown command %s" % (stats,)) response = {'code': 0, 'data': result} except: AdminHandler.log_error("exception while getting stats") AdminHandler._get_logger().exception("exception while getting stats") response = {'code': -1, 'data': 'error getting stats'} self.write(response) return True