def expired_sessions(): logging.warning("expired_sessions: AuthSession.gc()") result = sessions.AuthSession.gc() output = ( "Found %s expired sessions to clear (more than %s day old). <a href='?'>Back</a>" % (result, sessions.EXPIRE_DAYS)) if result > 0: if result == 500: output += " <a href='?expired_sessions'>Delete more...</a>" api.get_stats(True) return output
def clear_datastore(): logging.warning("clear_datastore: data_fs.rmtree('/')") from data import fs as data_fs # cannot use rmtree("/"), because it prohibits '/' try: data_fs.rmtree("/") except Exception as e: logging.warning(e) # data_fs.getdir("/").delete(recursive=True) memcache3.reset() data_fs.initfs() api.get_stats(True) output = "Removed '/'. <a href='?'>Back</a>" return output
def delete_orphans(): output, dir_orphans, file_orphans, chunk_orphans = find_orphans() total = 0 if len(dir_orphans) > 0: total += len(dir_orphans) db.delete(dir_orphans) if len(file_orphans) > 0: total += len(file_orphans) db.delete(file_orphans) if len(chunk_orphans) > 0: total += len(chunk_orphans) db.delete(chunk_orphans) if total > 0: api.get_stats(True) output = "Deleted %s orphans. <a href='?'>Back</a>" % total return output
def admin_view(): session = sessions.get_current_session(request.environ) # if not session.has_role("admin"): # output = ( # "You need to login as administrator <a href='%s'>Login</a>" # % sessions.LOGIN_URL # ) # return output qs = request.query_string if not isinstance(qs, str): qs = qs.decode("utf-8") logging.warning("AdminHandler.get: %s" % qs) actions = { "run_tests": run_tests, "clear_cache": clear_cache, "reset_stats": reset_stats, "clear_datastore": clear_datastore, "expired_sessions": expired_sessions, "check_orphans": check_orphans, "delete_orphans": delete_orphans, } # Handle admin commands if qs in actions: return actions[qs]() elif qs != "": raise NotImplementedError("Invalid command: %s" % qs) # Show admin page if session.is_user(): url = sessions.LOGOUT_URL url_linktext = "Logout" else: url = sessions.LOGIN_URL url_linktext = "Login" env = [] for k, v in list(os.environ.items()): env.append("%s: '%s'" % (k, v)) stats = api.get_stats() nickname = "stranger" if session.is_user(): nickname = session.nickname template_values = { "nickname": nickname, "url": url, "url_linktext": url_linktext, "memcache_stats": pformat(memcache3.get_stats()), "datastore_stats": pformat(stats), "environment_dump": "\n".join(env), "request_env_dump": pformat(request.environ), } return render_template("admin_data.html", **template_values)
def reset_stats(): logging.warning("reset_stats: api.get_stats(True)") api.get_stats(True) output = "Stats reset! <a href='?'>Back</a>" return output