<s> """<STR_LIT>""" <EOL> import datetime <EOL> import json <EOL> import logging <EOL> import os <EOL> import re <EOL> import urllib <EOL> from simian . auth import gaeserver <EOL> from simian . mac import common as main_common <EOL> from simian . mac import models <EOL> from simian . mac . common import gae_util <EOL> from simian . mac . common import util <EOL> from simian . mac . munki import common <EOL> from simian . mac . munki import handlers <EOL> FORCE_CONTINUE_POSTFLIGHT_DAYS = <NUM_LIT:5> <EOL> REPAIR_CLIENT_PREFLIGHT_COUNT_SINCE_POSTFLIGHT = <NUM_LIT:5> <EOL> INSTALL_RESULT_FAILED = '<STR_LIT>' <EOL> INSTALL_RESULT_SUCCESSFUL = '<STR_LIT>' <EOL> JSON_PREFIX = '<STR_LIT>' <EOL> LEGACY_INSTALL_RESULTS_STRING_REGEX = re . compile ( <EOL> r'<STR_LIT>' % ( <EOL> INSTALL_RESULT_SUCCESSFUL , INSTALL_RESULT_FAILED ) ) <EOL> DOWNLOAD_FAILED_STRING_REGEX = re . compile ( <EOL> r'<STR_LIT>' ) <EOL> LEGACY_FEEDBACK_LIST = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def IsExitFeedbackIpAddress ( ip_address ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> ip_address and <EOL> models . KeyValueCache . IpInList ( '<STR_LIT>' , ip_address ) ) <EOL> class Reports ( handlers . AuthenticationHandler ) : <EOL> """<STR_LIT>""" <EOL> def GetReportFeedback ( self , uuid , report_type , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> feedback = { } <EOL> if '<STR_LIT>' in kwargs : <EOL> c = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> c = models . Computer . get_by_key_name ( uuid ) <EOL> ip_address = kwargs . get ( '<STR_LIT>' , None ) <EOL> client_exit = kwargs . get ( '<STR_LIT>' , None ) <EOL> if client_exit and report_type == '<STR_LIT>' : <EOL> if c is None or c . postflight_datetime is None : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> else : <EOL> now = datetime . datetime . utcnow ( ) <EOL> postflight_stale_datetime = now - datetime . timedelta ( <EOL> days = FORCE_CONTINUE_POSTFLIGHT_DAYS ) <EOL> if c . postflight_datetime < postflight_stale_datetime : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> else : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> elif report_type == '<STR_LIT>' : <EOL> if IsExitFeedbackIpAddress ( ip_address ) : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> elif common . IsPanicModeNoPackages ( ) : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> elif not c or c . preflight_datetime is None : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> elif getattr ( c , '<STR_LIT>' , None ) is not None : <EOL> feedback [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> feedback [ '<STR_LIT>' ] = True <EOL> else : <EOL> if ( c . preflight_count_since_postflight >= <EOL> REPAIR_CLIENT_PREFLIGHT_COUNT_SINCE_POSTFLIGHT ) : <EOL> feedback [ '<STR_LIT>' ] = True <EOL> feedback [ '<STR_LIT>' ] = True <EOL> feedback [ '<STR_LIT>' ] = True <EOL> feedback [ '<STR_LIT>' ] = <NUM_LIT:3> <EOL> feedback [ '<STR_LIT>' ] = True <EOL> return feedback <EOL> def _LogInstalls ( self , installs , computer ) : <EOL> """<STR_LIT>""" <EOL> if not installs : <EOL> return <EOL> on_corp = self . request . get ( '<STR_LIT>' ) <EOL> if on_corp == '<STR_LIT:1>' : <EOL> on_corp = True <EOL> elif on_corp == '<STR_LIT:0>' : <EOL> on_corp = False <EOL> else : <EOL> on_corp = None <EOL> to_put = [ ] <EOL> for install in installs : <EOL> if install . startswith ( '<STR_LIT>' ) : <EOL> d = { <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:name>' : install , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> } <EOL> try : <EOL> m = LEGACY_INSTALL_RESULTS_STRING_REGEX . search ( install ) <EOL> if not m : <EOL> raise ValueError <EOL> elif m . group ( <NUM_LIT:3> ) == INSTALL_RESULT_SUCCESSFUL : <EOL> d [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> else : <EOL> d [ '<STR_LIT:status>' ] = m . group ( <NUM_LIT:4> ) <EOL> d [ '<STR_LIT:name>' ] = m . group ( <NUM_LIT:1> ) <EOL> d [ '<STR_LIT:version>' ] = m . group ( <NUM_LIT:2> ) <EOL> except ( IndexError , AttributeError , ValueError ) : <EOL> logging . warning ( '<STR_LIT>' , install ) <EOL> else : <EOL> d = common . KeyValueStringToDict ( install ) <EOL> name = d . get ( '<STR_LIT>' , '<STR_LIT>' ) or d . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> version = d . get ( '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> status = str ( d . get ( '<STR_LIT:status>' , '<STR_LIT>' ) ) <EOL> applesus = common . GetBoolValueFromString ( d . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> unattended = common . GetBoolValueFromString ( d . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> try : <EOL> duration_seconds = int ( d . get ( '<STR_LIT>' , None ) ) <EOL> except ( TypeError , ValueError ) : <EOL> duration_seconds = None <EOL> try : <EOL> dl_kbytes_per_sec = int ( d . get ( '<STR_LIT>' , None ) ) <EOL> if dl_kbytes_per_sec == <NUM_LIT:0> : <EOL> dl_kbytes_per_sec = None <EOL> except ( TypeError , ValueError ) : <EOL> dl_kbytes_per_sec = None <EOL> try : <EOL> install_datetime = util . Datetime . utcfromtimestamp ( d . get ( '<STR_LIT:time>' , None ) ) <EOL> except ValueError as e : <EOL> logging . info ( '<STR_LIT>' , str ( e ) ) <EOL> install_datetime = datetime . datetime . utcnow ( ) <EOL> except util . EpochExtremeFutureValueError as e : <EOL> logging . info ( '<STR_LIT>' , str ( e ) ) <EOL> install_datetime = datetime . datetime . utcnow ( ) <EOL> except util . EpochFutureValueError : <EOL> install_datetime = datetime . datetime . utcnow ( ) <EOL> pkg = '<STR_LIT>' % ( name , version ) <EOL> entity = models . InstallLog ( <EOL> uuid = computer . uuid , computer = computer , package = pkg , status = status , <EOL> on_corp = on_corp , applesus = applesus , unattended = unattended , <EOL> duration_seconds = duration_seconds , mtime = install_datetime , <EOL> dl_kbytes_per_sec = dl_kbytes_per_sec ) <EOL> entity . success = entity . IsSuccess ( ) <EOL> to_put . append ( entity ) <EOL> gae_util . BatchDatastoreOp ( models . db . put , to_put ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> session = gaeserver . DoMunkiAuth ( ) <EOL> uuid = main_common . SanitizeUUID ( session . uuid ) <EOL> report_type = self . request . get ( '<STR_LIT>' ) <EOL> report_feedback = { } <EOL> message = None <EOL> details = None <EOL> client_id = None <EOL> computer = None <EOL> if report_type == '<STR_LIT>' or report_type == '<STR_LIT>' : <EOL> client_id_str = urllib . unquote ( self . request . get ( '<STR_LIT>' ) ) <EOL> client_id = common . ParseClientId ( client_id_str , uuid = uuid ) <EOL> user_settings_str = self . request . get ( '<STR_LIT>' ) <EOL> user_settings = None <EOL> try : <EOL> if user_settings_str : <EOL> user_settings = util . Deserialize ( <EOL> urllib . unquote ( str ( user_settings_str ) ) ) <EOL> except util . DeserializeError : <EOL> logging . warning ( <EOL> '<STR_LIT>' , <EOL> client_id_str , user_settings_str ) <EOL> pkgs_to_install = self . request . get_all ( '<STR_LIT>' ) <EOL> apple_updates_to_install = self . request . get_all ( <EOL> '<STR_LIT>' ) <EOL> computer = models . Computer . get_by_key_name ( uuid ) <EOL> ip_address = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if report_type == '<STR_LIT>' : <EOL> client_exit = self . request . get ( '<STR_LIT>' , None ) <EOL> report_feedback = self . GetReportFeedback ( <EOL> uuid , report_type , computer = computer , ip_address = ip_address , <EOL> client_exit = client_exit ) <EOL> if self . request . get ( '<STR_LIT>' ) == '<STR_LIT:1>' : <EOL> self . response . out . write ( JSON_PREFIX + json . dumps ( report_feedback ) ) <EOL> else : <EOL> feedback_to_send = '<STR_LIT:OK>' <EOL> for feedback in LEGACY_FEEDBACK_LIST : <EOL> if report_feedback . get ( feedback . lower ( ) ) : <EOL> feedback_to_send = feedback <EOL> self . response . out . write ( feedback_to_send ) <EOL> if report_feedback . get ( '<STR_LIT>' ) : <EOL> if not client_exit : <EOL> client_exit = '<STR_LIT>' <EOL> common . WriteClientLog ( <EOL> models . PreflightExitLog , uuid , computer = computer , <EOL> exit_reason = client_exit ) <EOL> common . LogClientConnection ( <EOL> report_type , client_id , user_settings , pkgs_to_install , <EOL> apple_updates_to_install , computer = computer , ip_address = ip_address , <EOL> report_feedback = report_feedback ) <EOL> elif report_type == '<STR_LIT>' : <EOL> computer = models . Computer . get_by_key_name ( uuid ) <EOL> self . _LogInstalls ( self . request . get_all ( '<STR_LIT>' ) , computer ) <EOL> for removal in self . request . get_all ( '<STR_LIT>' ) : <EOL> common . WriteClientLog ( <EOL> models . ClientLog , uuid , computer = computer , action = '<STR_LIT>' , <EOL> details = removal ) <EOL> for problem in self . request . get_all ( '<STR_LIT>' ) : <EOL> common . WriteClientLog ( <EOL> models . ClientLog , uuid , computer = computer , <EOL> action = '<STR_LIT>' , details = problem ) <EOL> elif report_type == '<STR_LIT>' : <EOL> reason = self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> details = self . request . get ( '<STR_LIT>' ) <EOL> logging . warning ( '<STR_LIT>' , reason , details ) <EOL> common . WriteBrokenClient ( uuid , reason , details ) <EOL> elif report_type == '<STR_LIT>' : <EOL> details = { } <EOL> for k in [ '<STR_LIT:time>' , '<STR_LIT:user>' , '<STR_LIT:source>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> details [ k ] = self . request . get ( k , None ) <EOL> common . WriteComputerMSULog ( uuid , details ) <EOL> else : <EOL> params = [ ] <EOL> for param in self . request . arguments ( ) : <EOL> params . append ( '<STR_LIT>' % ( param , self . request . get_all ( param ) ) ) <EOL> common . WriteClientLog ( <EOL> models . ClientLog , uuid , action = '<STR_LIT>' , details = str ( params ) ) </s>
