<s> import json <EOL> import logging <EOL> import re <EOL> from collections import defaultdict , OrderedDict <EOL> import configparser <EOL> import xmlrpc . client <EOL> import concurrent . futures <EOL> from django . core . context_processors import csrf <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import redirect , render_to_response <EOL> from django . views . decorators . csrf import ensure_csrf_cookie <EOL> from django . conf import settings <EOL> from pathlib import Path <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def _get_supervisor ( hostname ) : <EOL> supervisor = xmlrpc . client . ServerProxy ( "<STR_LIT>" . format ( hostname ) , <EOL> verbose = False ) <EOL> return supervisor <EOL> def _get_monhelper ( hostname ) : <EOL> monhelper = xmlrpc . client . ServerProxy ( <EOL> "<STR_LIT>" . format ( hostname ) , verbose = False ) <EOL> return monhelper <EOL> def _get_server_data ( hostname , resource_pids , metadata ) : <EOL> supervisor = _get_supervisor ( hostname ) <EOL> monhelper = _get_monhelper ( hostname ) <EOL> try : <EOL> processes = supervisor . supervisor . getAllProcessInfo ( ) <EOL> server = { } <EOL> pids = [ ] <EOL> for process in processes : <EOL> if process [ "<STR_LIT>" ] : <EOL> pids . append ( process [ "<STR_LIT>" ] ) <EOL> group_name = process [ '<STR_LIT>' ] <EOL> group = server . setdefault ( group_name , <EOL> { "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> process [ "<STR_LIT>" ] = process [ "<STR_LIT>" ] in { "<STR_LIT>" } <EOL> process [ "<STR_LIT>" ] = process [ "<STR_LIT>" ] in { "<STR_LIT>" } <EOL> process [ "<STR_LIT>" ] = process [ "<STR_LIT>" ] in { "<STR_LIT>" , "<STR_LIT>" } <EOL> if process [ "<STR_LIT>" ] in { "<STR_LIT>" } : <EOL> group [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> else : <EOL> group [ '<STR_LIT>' ] = True <EOL> group [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> group [ '<STR_LIT>' ] . append ( process ) <EOL> if group [ '<STR_LIT>' ] == <NUM_LIT:1> : <EOL> group_tags = set ( ) <EOL> for server_regex , group_regex , tags in metadata : <EOL> group_match = group_regex . match ( group_name ) <EOL> server_match = server_regex . match ( hostname ) <EOL> if group_match and server_match : <EOL> group_tags . update ( tags ) <EOL> group [ '<STR_LIT>' ] = list ( sorted ( group_tags ) ) <EOL> if resource_pids : <EOL> try : <EOL> resources_dict = monhelper . getProcessResourceUsage ( <EOL> list ( int ( x ) for x in resource_pids ) ) <EOL> except ConnectionRefusedError : <EOL> LOG . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , hostname ) <EOL> else : <EOL> for pid , resources in resources_dict . items ( ) : <EOL> for process in processes : <EOL> if str ( process [ '<STR_LIT>' ] ) == pid : <EOL> process [ '<STR_LIT>' ] = resources <EOL> break <EOL> finally : <EOL> supervisor ( "<STR_LIT>" ) ( ) <EOL> monhelper ( "<STR_LIT>" ) ( ) <EOL> return server <EOL> def _get_data ( server_pids , metadata ) : <EOL> services_by_host = OrderedDict ( ) <EOL> servers = sorted ( settings . SUPERVISORS ) <EOL> with concurrent . futures . ThreadPoolExecutor ( max_workers = <NUM_LIT:15> ) as executor : <EOL> for hostname in servers : <EOL> server_data = executor . submit ( _get_server_data , hostname , <EOL> server_pids . get ( hostname ) , metadata ) <EOL> services_by_host [ hostname ] = server_data <EOL> for hostname in servers : <EOL> services_by_host [ hostname ] = services_by_host [ hostname ] . result ( ) <EOL> return services_by_host <EOL> def get_data ( request ) : <EOL> data = json . loads ( request . body . decode ( "<STR_LIT:ascii>" ) ) <EOL> data = _get_data ( data [ '<STR_LIT>' ] , [ ] ) <EOL> data_json = json . dumps ( data ) <EOL> return HttpResponse ( data_json , content_type = '<STR_LIT:application/json>' ) <EOL> def get_index_template_data ( ) : <EOL> metadata , tags_config , taggroups_dict = _get_metadata_conf ( ) <EOL> services_by_host = _get_data ( { } , metadata ) <EOL> all_tags = set ( ) <EOL> for server_data in services_by_host . values ( ) : <EOL> for group in server_data . values ( ) : <EOL> all_tags . update ( group [ '<STR_LIT>' ] ) <EOL> tags_by_group = defaultdict ( set ) <EOL> for tag_name in all_tags : <EOL> tag = tags_config [ tag_name ] <EOL> tags_by_group [ tag . taggroup ] . add ( tag_name ) <EOL> taggroups = [ ] <EOL> for name , tags in sorted ( tags_by_group . items ( ) ) : <EOL> taggroups . append ( ( taggroups_dict [ name ] . label , sorted ( tags ) ) ) <EOL> data = [ ] <EOL> for server , groups in sorted ( services_by_host . items ( ) ) : <EOL> data . append ( ( server , sorted ( groups . items ( ) ) ) ) <EOL> context = { <EOL> "<STR_LIT:data>" : data , <EOL> "<STR_LIT>" : taggroups , <EOL> '<STR_LIT>' : tags_config , <EOL> "<STR_LIT>" : settings . SITE_ROOT , <EOL> } <EOL> return context <EOL> @ ensure_csrf_cookie <EOL> def home ( request , template_name = "<STR_LIT>" ) : <EOL> context = get_index_template_data ( ) <EOL> context . update ( csrf ( request ) ) <EOL> resp = render_to_response ( template_name , context ) <EOL> return resp <EOL> def action ( request ) : <EOL> server = request . POST [ '<STR_LIT>' ] <EOL> supervisor = _get_supervisor ( server ) <EOL> try : <EOL> if '<STR_LIT>' in request . POST : <EOL> supervisor . supervisor . startAllProcesses ( ) <EOL> return HttpResponse ( json . dumps ( "<STR_LIT>" ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> supervisor . supervisor . stopAllProcesses ( ) <EOL> return HttpResponse ( json . dumps ( "<STR_LIT>" ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> program = "<STR_LIT>" . format ( request . POST [ '<STR_LIT>' ] , request . POST [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in request . POST : <EOL> supervisor . supervisor . startProcess ( program ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> supervisor . supervisor . stopProcess ( program ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> supervisor . supervisor . stopProcess ( program ) <EOL> supervisor . supervisor . startProcess ( program ) <EOL> finally : <EOL> supervisor ( "<STR_LIT>" ) ( ) <EOL> return redirect ( settings . SITE_ROOT ) <EOL> def get_program_logs ( request ) : <EOL> logs = "<STR_LIT>" . format ( <EOL> request . GET [ '<STR_LIT>' ] , request . GET [ '<STR_LIT>' ] , request . GET [ '<STR_LIT>' ] ) <EOL> stream = request . GET [ '<STR_LIT>' ] <EOL> assert stream in { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> full_name = "<STR_LIT>" . format ( request . GET [ '<STR_LIT>' ] , <EOL> request . GET [ '<STR_LIT>' ] ) <EOL> if stream == '<STR_LIT>' : <EOL> supervisor = _get_supervisor ( request . GET [ '<STR_LIT>' ] ) <EOL> try : <EOL> logs , _offeset , _overflow = supervisor . supervisor . tailProcessStdoutLog ( <EOL> full_name , - <NUM_LIT> , <NUM_LIT> ) <EOL> finally : <EOL> supervisor ( "<STR_LIT>" ) ( ) <EOL> elif stream == '<STR_LIT>' : <EOL> supervisor = _get_supervisor ( request . GET [ '<STR_LIT>' ] ) <EOL> try : <EOL> logs , _offeset , _overflow = supervisor . supervisor . tailProcessStderrLog ( <EOL> full_name , - <NUM_LIT> , <NUM_LIT> ) <EOL> finally : <EOL> supervisor ( "<STR_LIT>" ) ( ) <EOL> elif stream == '<STR_LIT>' : <EOL> supervisor = _get_monhelper ( request . GET [ '<STR_LIT>' ] ) <EOL> try : <EOL> logs , _offeset , _overflow = supervisor . tailApplicationLog ( <EOL> int ( request . GET [ '<STR_LIT>' ] ) , - <NUM_LIT> , <NUM_LIT> ) <EOL> finally : <EOL> supervisor ( "<STR_LIT>" ) ( ) <EOL> else : <EOL> raise AssertionError <EOL> return HttpResponse ( logs , content_type = '<STR_LIT>' ) <EOL> class TagConfig : <EOL> enabled_by_default = True <EOL> taggroup = '<STR_LIT>' <EOL> class TagGroup : <EOL> label = '<STR_LIT>' <EOL> def _get_metadata_conf ( ) : <EOL> mappings = [ ] <EOL> tags_config = defaultdict ( TagConfig ) <EOL> taggroups = defaultdict ( TagGroup ) <EOL> metadata_dir = getattr ( settings , "<STR_LIT>" , None ) <EOL> if not metadata_dir : <EOL> return mappings , tags_config , taggroups <EOL> for fname in Path ( metadata_dir ) . iterdir ( ) : <EOL> config = configparser . ConfigParser ( ) <EOL> config . read ( str ( fname ) ) <EOL> for section in config . sections ( ) : <EOL> if section . startswith ( "<STR_LIT>" ) : <EOL> group_regex = re . compile ( config [ section ] . get ( "<STR_LIT>" , '<STR_LIT>' ) ) <EOL> server_regex = re . compile ( config [ section ] . get ( "<STR_LIT>" , '<STR_LIT>' ) ) <EOL> tags = config [ section ] . get ( "<STR_LIT>" ) <EOL> if tags is None : <EOL> tags = frozenset ( ) <EOL> else : <EOL> tags = { tag . strip ( ) for tag in tags . split ( '<STR_LIT:U+002C>' ) } <EOL> mappings . append ( ( server_regex , group_regex , tags ) ) <EOL> elif section . startswith ( "<STR_LIT>" ) : <EOL> _ , _ , tag_name = section . partition ( "<STR_LIT>" ) <EOL> enabled = config [ section ] . getboolean ( "<STR_LIT>" , True ) <EOL> tags_config [ tag_name ] . enabled_by_default = enabled <EOL> taggroup = config [ section ] . get ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> tags_config [ tag_name ] . taggroup = taggroup <EOL> elif section . startswith ( "<STR_LIT>" ) : <EOL> _ , _ , taggroup_name = section . partition ( "<STR_LIT>" ) <EOL> label = config [ section ] . get ( "<STR_LIT:label>" , '<STR_LIT>' ) <EOL> taggroups [ taggroup_name ] . label = label <EOL> return mappings , tags_config , taggroups </s>
