<s> import logging <EOL> import sys <EOL> from google . appengine . ext import ndb <EOL> from google . appengine . api import users <EOL> from app_dashboard_helper import AppDashboardHelper <EOL> from app_dashboard_helper import AppHelperException <EOL> class DashboardDataRoot ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> head_node_ip = ndb . StringProperty ( ) <EOL> table = ndb . StringProperty ( ) <EOL> replication = ndb . IntegerProperty ( ) <EOL> timestamp = ndb . DateTimeProperty ( auto_now = True , auto_now_add = True ) <EOL> class ServerStatus ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> cpu = ndb . StringProperty ( ) <EOL> memory = ndb . StringProperty ( ) <EOL> disk = ndb . StringProperty ( ) <EOL> roles = ndb . StringProperty ( repeated = True ) <EOL> timestamp = ndb . DateTimeProperty ( auto_now = True , auto_now_add = True ) <EOL> class RequestInfo ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> app_id = ndb . StringProperty ( required = True ) <EOL> num_of_requests = ndb . FloatProperty ( ) <EOL> timestamp = ndb . DateTimeProperty ( ) <EOL> class AppStatus ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> name = ndb . StringProperty ( ) <EOL> url = ndb . StringProperty ( repeated = True ) <EOL> timestamp = ndb . DateTimeProperty ( auto_now = True , auto_now_add = True ) <EOL> class UserInfo ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> is_user_cloud_admin = ndb . BooleanProperty ( ) <EOL> can_upload_apps = ndb . BooleanProperty ( ) <EOL> owned_apps = ndb . StringProperty ( repeated = True ) <EOL> timestamp = ndb . DateTimeProperty ( auto_now = True , auto_now_add = True ) <EOL> class InstanceInfo ( ndb . Model ) : <EOL> """<STR_LIT>""" <EOL> appid = ndb . StringProperty ( ) <EOL> host = ndb . StringProperty ( ) <EOL> port = ndb . IntegerProperty ( ) <EOL> language = ndb . StringProperty ( ) <EOL> timestamp = ndb . DateTimeProperty ( auto_now = True , auto_now_add = True ) <EOL> class AppDashboardData ( ) : <EOL> """<STR_LIT>""" <EOL> ROOT_KEYNAME = '<STR_LIT>' <EOL> MONITOR_PORT = <NUM_LIT> <EOL> FLOWER_PORT = <NUM_LIT> <EOL> MONIT_PORT = <NUM_LIT> <EOL> NO_APPS_RUNNING = "<STR_LIT:none>" <EOL> def __init__ ( self , helper = None ) : <EOL> """<STR_LIT>""" <EOL> self . helper = helper or AppDashboardHelper ( ) <EOL> def get_by_id ( self , model , key_name ) : <EOL> """<STR_LIT>""" <EOL> return model . get_by_id ( key_name ) <EOL> def get_all ( self , obj , keys_only = False ) : <EOL> """<STR_LIT>""" <EOL> return obj . query ( ) . fetch ( keys_only = keys_only ) <EOL> def update_all ( self ) : <EOL> """<STR_LIT>""" <EOL> self . update_head_node_ip ( ) <EOL> self . update_database_info ( ) <EOL> self . update_status_info ( ) <EOL> self . update_application_info ( ) <EOL> self . update_users ( ) <EOL> def get_monitoring_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( self . get_head_node_ip ( ) , self . MONITOR_PORT ) <EOL> def get_flower_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( self . get_head_node_ip ( ) , self . FLOWER_PORT ) <EOL> def get_monit_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . format ( self . get_head_node_ip ( ) , self . MONIT_PORT ) <EOL> def get_head_node_ip ( self ) : <EOL> """<STR_LIT>""" <EOL> dashboard_root = self . get_by_id ( DashboardDataRoot , self . ROOT_KEYNAME ) <EOL> if dashboard_root and dashboard_root . head_node_ip is not None : <EOL> return dashboard_root . head_node_ip <EOL> else : <EOL> return self . update_head_node_ip ( ) <EOL> def update_head_node_ip ( self ) : <EOL> """<STR_LIT>""" <EOL> dashboard_root = self . get_by_id ( DashboardDataRoot , self . ROOT_KEYNAME ) <EOL> if dashboard_root and dashboard_root . head_node_ip is not None : <EOL> return dashboard_root . head_node_ip <EOL> try : <EOL> if dashboard_root is None : <EOL> dashboard_root = DashboardDataRoot ( id = self . ROOT_KEYNAME ) <EOL> dashboard_root . head_node_ip = self . helper . get_host_with_role ( '<STR_LIT>' ) <EOL> dashboard_root . put ( ) <EOL> return dashboard_root . head_node_ip <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return None <EOL> def get_status_info ( self ) : <EOL> """<STR_LIT>""" <EOL> servers = self . get_all ( ServerStatus ) <EOL> return [ { '<STR_LIT>' : server . key . id ( ) , '<STR_LIT>' : server . cpu , <EOL> '<STR_LIT>' : server . memory , '<STR_LIT>' : server . disk , '<STR_LIT>' : server . roles , <EOL> '<STR_LIT:key>' : server . key . id ( ) . translate ( None , '<STR_LIT:.>' ) } for server in servers ] <EOL> def update_status_info ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> nodes = self . helper . get_appcontroller_client ( ) . get_stats ( ) <EOL> updated_statuses = [ ] <EOL> for node in nodes : <EOL> status = self . get_by_id ( ServerStatus , node [ '<STR_LIT>' ] ) <EOL> if status : <EOL> if status . cpu != str ( node [ '<STR_LIT>' ] ) or status . memory != str ( node [ '<STR_LIT>' ] ) or status . disk != str ( node [ '<STR_LIT>' ] ) or status . roles != node [ '<STR_LIT>' ] : <EOL> status . cpu = str ( node [ '<STR_LIT>' ] ) <EOL> status . memory = str ( node [ '<STR_LIT>' ] ) <EOL> status . disk = str ( node [ '<STR_LIT>' ] ) <EOL> status . roles = node [ '<STR_LIT>' ] <EOL> updated_statuses . append ( status ) <EOL> else : <EOL> status = ServerStatus ( id = node [ '<STR_LIT>' ] ) <EOL> status . cpu = str ( node [ '<STR_LIT>' ] ) <EOL> status . memory = str ( node [ '<STR_LIT>' ] ) <EOL> status . disk = str ( node [ '<STR_LIT>' ] ) <EOL> status . roles = node [ '<STR_LIT>' ] <EOL> updated_statuses . append ( status ) <EOL> ndb . put_multi ( updated_statuses ) <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> def get_database_info ( self ) : <EOL> """<STR_LIT>""" <EOL> dashboard_root = self . get_by_id ( DashboardDataRoot , self . ROOT_KEYNAME ) <EOL> if dashboard_root and dashboard_root . table is not None and dashboard_root . replication is not None : <EOL> return { <EOL> '<STR_LIT>' : dashboard_root . table , <EOL> '<STR_LIT>' : dashboard_root . replication <EOL> } <EOL> else : <EOL> return self . update_database_info ( ) <EOL> def update_database_info ( self ) : <EOL> """<STR_LIT>""" <EOL> dashboard_root = self . get_by_id ( DashboardDataRoot , self . ROOT_KEYNAME ) <EOL> if dashboard_root and dashboard_root . table is not None and dashboard_root . replication is not None : <EOL> return { <EOL> '<STR_LIT>' : dashboard_root . table , <EOL> '<STR_LIT>' : dashboard_root . replication <EOL> } <EOL> try : <EOL> acc = self . helper . get_appcontroller_client ( ) <EOL> db_info = acc . get_database_information ( ) <EOL> if dashboard_root is None : <EOL> dashboard_root = DashboardDataRoot ( id = self . ROOT_KEYNAME ) <EOL> dashboard_root . table = db_info [ '<STR_LIT>' ] <EOL> dashboard_root . replication = int ( db_info [ '<STR_LIT>' ] ) <EOL> dashboard_root . put ( ) <EOL> return { <EOL> '<STR_LIT>' : dashboard_root . table , <EOL> '<STR_LIT>' : dashboard_root . replication <EOL> } <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> def get_application_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( app . name , app . url ) for app in self . get_all ( AppStatus ) ) <EOL> def delete_app_from_datastore ( self , app , email = None ) : <EOL> """<STR_LIT>""" <EOL> if email is None : <EOL> user = users . get_current_user ( ) <EOL> if not user : <EOL> return None <EOL> email = user . email ( ) <EOL> try : <EOL> app_status = self . get_by_id ( AppStatus , app ) <EOL> if app_status : <EOL> app_status . key . delete ( ) <EOL> user_info = self . get_by_id ( UserInfo , email ) <EOL> if user_info : <EOL> if app in user_info . owned_apps : <EOL> user_info . owned_apps . remove ( app ) <EOL> user_info . put ( ) <EOL> return user_info <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return None <EOL> def update_application_info ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> status_on_all_nodes = self . helper . get_status_info ( ) <EOL> app_names_and_urls = { } <EOL> if not status_on_all_nodes : <EOL> return { } <EOL> for status in status_on_all_nodes : <EOL> for app , done_loading in status [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if app == self . NO_APPS_RUNNING : <EOL> continue <EOL> if done_loading : <EOL> try : <EOL> host_url = self . helper . get_login_host ( ) <EOL> ports = self . helper . get_app_ports ( app ) <EOL> app_names_and_urls [ app ] = [ <EOL> "<STR_LIT>" . format ( host_url , ports [ <NUM_LIT:0> ] ) , <EOL> "<STR_LIT>" . format ( host_url , ports [ <NUM_LIT:1> ] ) ] <EOL> except AppHelperException : <EOL> app_names_and_urls [ app ] = None <EOL> else : <EOL> app_names_and_urls [ app ] = None <EOL> all_apps = self . get_all ( AppStatus ) <EOL> all_app_names_were_running = [ app . key . id ( ) for app in all_apps ] <EOL> all_app_names_are_running = [ app for app in app_names_and_urls . keys ( ) ] <EOL> app_names_to_delete = [ ] <EOL> for app_name in all_app_names_were_running : <EOL> if app_name not in all_app_names_are_running : <EOL> app_names_to_delete . append ( app_name ) <EOL> elif not app_names_and_urls [ app_name ] : <EOL> app_names_to_delete . append ( app_name ) <EOL> if app_names_to_delete : <EOL> apps_to_delete = [ ] <EOL> for app in all_apps : <EOL> if app . name in app_names_to_delete : <EOL> apps_to_delete . append ( app . key ) <EOL> ndb . delete_multi ( apps_to_delete ) <EOL> app_names_to_add = [ ] <EOL> for app_name in all_app_names_are_running : <EOL> if app_name not in all_app_names_were_running : <EOL> app_names_to_add . append ( app_name ) <EOL> elif app_names_and_urls [ app_name ] : <EOL> app_names_to_add . append ( app_name ) <EOL> for app in all_apps : <EOL> if app . key . id ( ) in all_app_names_are_running and app . url != app_names_and_urls [ app . key . id ( ) ] : <EOL> app_names_to_add . append ( app_name ) <EOL> if app_names_to_add : <EOL> apps_to_add = [ AppStatus ( id = app , name = app , url = app_names_and_urls [ app ] ) <EOL> for app in app_names_to_add ] <EOL> ndb . put_multi ( apps_to_add ) <EOL> return app_names_and_urls <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return { } <EOL> def update_users ( self ) : <EOL> """<STR_LIT>""" <EOL> user_list = [ ] <EOL> try : <EOL> all_users_list = self . helper . list_all_users ( ) <EOL> users_to_update = [ ] <EOL> for email in all_users_list : <EOL> user_info = self . get_by_id ( UserInfo , email ) <EOL> if user_info : <EOL> is_user_cloud_admin = self . helper . is_user_cloud_admin ( email ) <EOL> can_upload_apps = self . helper . can_upload_apps ( email ) <EOL> owned_apps = self . helper . get_owned_apps ( email ) <EOL> if user_info . is_user_cloud_admin != is_user_cloud_admin or user_info . can_upload_apps != can_upload_apps or user_info . owned_apps != owned_apps : <EOL> user_info . is_user_cloud_admin = is_user_cloud_admin <EOL> user_info . can_upload_apps = can_upload_apps <EOL> user_info . owned_apps = owned_apps <EOL> users_to_update . append ( user_info ) <EOL> user_list . append ( user_info ) <EOL> else : <EOL> user_info = UserInfo ( id = email ) <EOL> user_info . is_user_cloud_admin = self . helper . is_user_cloud_admin ( email ) <EOL> user_info . can_upload_apps = self . helper . can_upload_apps ( email ) <EOL> user_info . owned_apps = self . helper . get_owned_apps ( email ) <EOL> users_to_update . append ( user_info ) <EOL> user_list . append ( user_info ) <EOL> ndb . put_multi ( users_to_update ) <EOL> return user_list <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return [ ] <EOL> def get_owned_apps ( self ) : <EOL> """<STR_LIT>""" <EOL> user = users . get_current_user ( ) <EOL> if not user : <EOL> return [ ] <EOL> email = user . email ( ) <EOL> try : <EOL> user_info = self . get_by_id ( UserInfo , email ) <EOL> if user_info : <EOL> return user_info . owned_apps <EOL> else : <EOL> return [ ] <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return [ ] <EOL> def is_user_cloud_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> user = users . get_current_user ( ) <EOL> if not user : <EOL> return False <EOL> try : <EOL> user_info = self . get_by_id ( UserInfo , user . email ( ) ) <EOL> if user_info : <EOL> return user_info . is_user_cloud_admin <EOL> else : <EOL> return False <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return False <EOL> def can_upload_apps ( self ) : <EOL> """<STR_LIT>""" <EOL> user = users . get_current_user ( ) <EOL> if not user : <EOL> return False <EOL> try : <EOL> user_info = self . get_by_id ( UserInfo , user . email ( ) ) <EOL> if user_info : <EOL> return user_info . can_upload_apps <EOL> else : <EOL> return False <EOL> except Exception as err : <EOL> logging . exception ( err ) <EOL> return False </s>
