<s> """<STR_LIT>""" <EOL> import re <EOL> import xml . etree . ElementTree as ET <EOL> import logging <EOL> import inspect <EOL> import time <EOL> import pandevice <EOL> import pan . xapi <EOL> from pan . config import PanConfig <EOL> import errors as err <EOL> import updater <EOL> logger = logging . getLogger ( __name__ ) <EOL> Root = pandevice . enum ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> class PanObject ( object ) : <EOL> XPATH = "<STR_LIT>" <EOL> ENTRY = "<STR_LIT>" <EOL> MEMBER = "<STR_LIT>" <EOL> SUFFIX = None <EOL> ROOT = Root . DEVICE <EOL> def __init__ ( self , name = None ) : <EOL> self . name = name <EOL> self . parent = None <EOL> self . children = [ ] <EOL> def add ( self , child ) : <EOL> child . parent = self <EOL> self . children . append ( child ) <EOL> return child <EOL> def pop ( self , index ) : <EOL> child = self . children . pop ( index ) <EOL> child . parent = None <EOL> return child <EOL> def remove_by_name ( self , name , cls = None ) : <EOL> index = PanObject . find ( self . children , name , cls ) <EOL> if index is None : <EOL> return None <EOL> return self . pop ( index ) <EOL> def xpath ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . parent is None : <EOL> parent_xpath = "<STR_LIT>" <EOL> elif issubclass ( type ( self . parent ) , PanDevice ) : <EOL> if self . ROOT == Root . DEVICE : <EOL> parent_xpath = self . parent . xpath_device ( ) <EOL> elif self . ROOT == Root . VSYS : <EOL> parent_xpath = self . parent . xpath_vsys ( ) <EOL> elif self . ROOT == Root . MGTCONFIG : <EOL> parent_xpath = self . parent . xpath_mgtconfig ( ) <EOL> else : <EOL> parent_xpath = self . parent . xpath ( ) <EOL> else : <EOL> parent_xpath = self . parent . xpath ( ) <EOL> suffix = "<STR_LIT>" if self . SUFFIX is None else self . SUFFIX % self . name <EOL> result = str ( parent_xpath + self . XPATH + suffix ) <EOL> return result <EOL> def element ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def apply ( self ) : <EOL> self . pandevice ( ) . xapi . edit ( self . xpath ( ) , self . element ( ) ) <EOL> def create ( self ) : <EOL> xpath = self . xpath ( ) . rsplit ( "<STR_LIT:/>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> self . pandevice ( ) . xapi . set ( xpath , self . element ( ) ) <EOL> def delete ( self ) : <EOL> self . pandevice ( ) . xapi . delete ( self . xpath ( ) ) <EOL> if self . parent is not None : <EOL> self . parent . remove_by_name ( self . name , type ( self ) ) <EOL> def pandevice ( self ) : <EOL> if issubclass ( self . __class__ , PanDevice ) : <EOL> return self <EOL> else : <EOL> if self . parent is None : <EOL> raise err . PanDeviceNotSet ( "<STR_LIT>" ) <EOL> else : <EOL> return self . parent . pandevice ( ) <EOL> @ classmethod <EOL> def find ( cls , list_of_panobjects , name , class_type = None ) : <EOL> if class_type is None : <EOL> class_type = cls <EOL> indexes = [ i for i , child in enumerate ( list_of_panobjects ) if <EOL> child . name == name and issubclass ( type ( child ) , class_type ) ] <EOL> for index in indexes : <EOL> return index <EOL> return None <EOL> class VsysImportMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( VsysImportMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> def create ( self , * args , ** kwargs ) : <EOL> super ( VsysImportMixin , self ) . create ( * args , ** kwargs ) <EOL> pandevice = super ( VsysImportMixin , self ) . pandevice ( ) <EOL> if pandevice . vsys != "<STR_LIT>" : <EOL> xpath_import = pandevice . xpath_vsys ( ) + "<STR_LIT>" + self . XPATH <EOL> pandevice . xapi . set ( xpath_import , "<STR_LIT>" % self . name ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> pandevice = super ( VsysImportMixin , self ) . pandevice ( ) <EOL> xpath_import = pandevice . xpath_vsys ( ) + "<STR_LIT>" + self . XPATH <EOL> if pandevice . vsys != "<STR_LIT>" : <EOL> pandevice . xapi . delete ( xpath_import + "<STR_LIT>" % self . name ) <EOL> super ( VsysImportMixin , self ) . delete ( * args , ** kwargs ) <EOL> class PanDevice ( PanObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> hostname , <EOL> api_username = None , <EOL> api_password = None , <EOL> api_key = None , <EOL> serial = None , <EOL> port = <NUM_LIT> , <EOL> is_virtual = None , <EOL> timeout = <NUM_LIT> , <EOL> interval = <NUM_LIT> , <EOL> classify_exceptions = False ) : <EOL> """<STR_LIT>""" <EOL> super ( PanDevice , self ) . __init__ ( ) <EOL> self . _logger = logging . getLogger ( __name__ + "<STR_LIT:.>" + self . __class__ . __name__ ) <EOL> self . hostname = hostname <EOL> self . port = port <EOL> self . _api_username = api_username <EOL> self . _api_password = api_password <EOL> self . _api_key = api_key <EOL> self . is_virtual = is_virtual <EOL> self . serial = serial <EOL> self . timeout = timeout <EOL> self . interval = interval <EOL> self . interfaces = { } <EOL> self . _xapi_private = None <EOL> self . _classify_exceptions = classify_exceptions <EOL> self . config_locked = False <EOL> self . commit_locked = False <EOL> self . lock_before_change = False <EOL> self . config_changed = False <EOL> self . connected_to_panorama = None <EOL> self . dg_in_sync = None <EOL> self . software = updater . SoftwareUpdater ( self ) <EOL> self . content = updater . ContentUpdater ( self ) <EOL> self . version = None <EOL> self . content_version = None <EOL> @ classmethod <EOL> def create_from_device ( cls , <EOL> hostname , <EOL> api_username = None , <EOL> api_password = None , <EOL> api_key = None , <EOL> serial = None , <EOL> port = <NUM_LIT> , <EOL> classify_exceptions = False ) : <EOL> """<STR_LIT>""" <EOL> import firewall <EOL> import panorama <EOL> device = PanDevice ( hostname , <EOL> api_username , <EOL> api_password , <EOL> api_key , <EOL> serial , <EOL> port , <EOL> classify_exceptions = classify_exceptions ) <EOL> version , model , serial = device . system_info ( ) <EOL> if model == "<STR_LIT>" : <EOL> subclass = panorama . Panorama <EOL> else : <EOL> subclass = firewall . Firewall <EOL> instance = subclass ( hostname , <EOL> api_username , <EOL> api_password , <EOL> api_key , <EOL> serial , <EOL> port , <EOL> classify_exceptions = classify_exceptions ) <EOL> instance . version = version <EOL> return instance <EOL> class XapiWrapper ( pan . xapi . PanXapi ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . pan_device = kwargs . pop ( '<STR_LIT>' , None ) <EOL> pan . xapi . PanXapi . __init__ ( self , * args , ** kwargs ) <EOL> for name , method in inspect . getmembers ( <EOL> pan . xapi . PanXapi , <EOL> inspect . ismethod ) : <EOL> if name [ <NUM_LIT:0> ] == "<STR_LIT:_>" : <EOL> continue <EOL> wrapper_method = self . make_method ( method ) <EOL> setattr ( PanDevice . XapiWrapper , name , wrapper_method ) <EOL> def make_method ( self , super_method ) : <EOL> def method ( * args , ** kwargs ) : <EOL> try : <EOL> return super_method ( * args , ** kwargs ) <EOL> except pan . xapi . PanXapiError as e : <EOL> if e . msg == "<STR_LIT>" : <EOL> raise err . PanInvalidCredentials ( <EOL> e . msg , <EOL> pan_device = self . pan_device , <EOL> ) <EOL> elif e . msg . startswith ( "<STR_LIT>" ) : <EOL> if e . msg . endswith ( "<STR_LIT>" ) : <EOL> raise err . PanConnectionTimeout ( <EOL> e . msg , <EOL> pan_device = self . pan_device , <EOL> ) <EOL> else : <EOL> raise err . PanURLError ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> elif e . msg . startswith ( "<STR_LIT>" ) : <EOL> raise err . PanJobTimeout ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> elif e . msg . startswith ( "<STR_LIT>" <EOL> "<STR_LIT>" ) : <EOL> raise err . PanCommitInProgress ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> elif e . msg . startswith ( "<STR_LIT>" ) : <EOL> raise err . PanCommitInProgress ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> elif e . msg . startswith ( "<STR_LIT>" ) : <EOL> raise err . PanInstallInProgress ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> elif e . msg . startswith ( "<STR_LIT>" ) : <EOL> raise err . PanSessionTimedOut ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> else : <EOL> raise err . PanDeviceXapiError ( e . msg , <EOL> pan_device = self . pan_device ) <EOL> return method <EOL> @ property <EOL> def api_key ( self ) : <EOL> if self . _api_key is None : <EOL> self . _api_key = self . _retrieve_api_key ( ) <EOL> return self . _api_key <EOL> @ property <EOL> def xapi ( self ) : <EOL> if self . _xapi_private is None : <EOL> self . _xapi_private = self . generate_xapi ( ) <EOL> return self . _xapi_private <EOL> def update_connection_method ( self ) : <EOL> self . _xapi_private = self . generate_xapi ( ) <EOL> return self . _xapi_private <EOL> def generate_xapi ( self ) : <EOL> kwargs = { '<STR_LIT>' : self . api_key , <EOL> '<STR_LIT>' : self . hostname , <EOL> '<STR_LIT:port>' : self . port , <EOL> '<STR_LIT>' : self . timeout , <EOL> } <EOL> if self . _classify_exceptions : <EOL> xapi_constructor = PanDevice . XapiWrapper <EOL> kwargs [ '<STR_LIT>' ] = self , <EOL> else : <EOL> xapi_constructor = pan . xapi . PanXapi <EOL> return xapi_constructor ( ** kwargs ) <EOL> def set_config_changed ( self ) : <EOL> if self . lock_before_change : <EOL> if not self . config_locked : <EOL> self . add_config_lock ( exception = True ) <EOL> """<STR_LIT>""" <EOL> self . config_changed = True <EOL> def __get_xpath_scope ( self ) : <EOL> """<STR_LIT>""" <EOL> xpath_device = "<STR_LIT>" <EOL> xpath_vsys = xpath_device + "<STR_LIT>" <EOL> xpath_devicegroup = xpath_device + "<STR_LIT>" <EOL> xpath_shared = "<STR_LIT>" <EOL> if self . devicegroup : <EOL> return xpath_devicegroup % self . devicegroup <EOL> elif self . is_panorama : <EOL> return xpath_shared <EOL> else : <EOL> return xpath_device <EOL> def _retrieve_api_key ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . debug ( "<STR_LIT>" % <EOL> ( self . hostname , self . _api_username ) ) <EOL> if self . _classify_exceptions : <EOL> xapi = PanDevice . XapiWrapper ( <EOL> pan_device = self , <EOL> api_username = self . _api_username , <EOL> api_password = self . _api_password , <EOL> hostname = self . hostname , <EOL> port = self . port , <EOL> timeout = self . timeout <EOL> ) <EOL> else : <EOL> xapi = pan . xapi . PanXapi ( <EOL> api_username = self . _api_username , <EOL> api_password = self . _api_password , <EOL> hostname = self . hostname , <EOL> port = self . port , <EOL> timeout = self . timeout <EOL> ) <EOL> xapi . keygen ( ) <EOL> return xapi . api_key <EOL> def system_info ( self , all_info = False ) : <EOL> """<STR_LIT>""" <EOL> self . xapi . op ( cmd = "<STR_LIT>" ) <EOL> pconf = PanConfig ( self . xapi . element_result ) <EOL> system_info = pconf . python ( ) <EOL> self . _logger . debug ( "<STR_LIT>" % system_info ) <EOL> if not system_info : <EOL> error_msg = '<STR_LIT>' <EOL> self . _logger . error ( error_msg ) <EOL> raise err . PanDeviceError ( error_msg , pan_device = self ) <EOL> if not all_info : <EOL> version = system_info [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> model = system_info [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> serial = system_info [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return version , model , serial <EOL> else : <EOL> return system_info [ '<STR_LIT:result>' ] <EOL> def refresh_version ( self ) : <EOL> """<STR_LIT>""" <EOL> system_info = self . system_info ( ) <EOL> self . version = system_info [ <NUM_LIT:0> ] <EOL> return self . version <EOL> def set_hostname ( self , hostname ) : <EOL> self . _logger . debug ( "<STR_LIT>" % ( hostname , ) ) <EOL> self . set_config_changed ( ) <EOL> xpath = pandevice . XPATH_DEVICECONFIG_SYSTEM <EOL> self . xapi . set ( xpath , "<STR_LIT>" % ( hostname , ) ) <EOL> def set_dns_servers ( self , primary , secondary = None ) : <EOL> self . _logger . debug ( "<STR_LIT>" % ( primary , secondary ) ) <EOL> xpath = pandevice . XPATH_DEVICECONFIG_SYSTEM + "<STR_LIT>" <EOL> element = ET . Element ( "<STR_LIT>" ) <EOL> if primary : <EOL> element_primary = ET . SubElement ( element , "<STR_LIT>" ) <EOL> element_primary . text = primary <EOL> if secondary : <EOL> element_secondary = ET . SubElement ( element , "<STR_LIT>" ) <EOL> element_secondary . text = secondary <EOL> self . xapi . edit ( xpath , ET . tostring ( element ) ) <EOL> def set_ntp_servers ( self , primary , secondary = None ) : <EOL> self . _logger . debug ( "<STR_LIT>" % ( primary , secondary ) ) <EOL> self . set_config_changed ( ) <EOL> xpath = pandevice . XPATH_DEVICECONFIG_SYSTEM <EOL> xpath61 = pandevice . XPATH_DEVICECONFIG_SYSTEM + "<STR_LIT>" <EOL> element61 = "<STR_LIT>" <EOL> if primary is None : <EOL> self . xapi . delete ( xpath61 ) <EOL> self . xapi . delete ( xpath + "<STR_LIT>" ) <EOL> self . xapi . delete ( xpath + "<STR_LIT>" ) <EOL> return <EOL> if primary : <EOL> element61 += "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % ( primary , ) <EOL> if secondary : <EOL> element61 += "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % ( secondary , ) <EOL> element61 += "<STR_LIT>" <EOL> try : <EOL> self . xapi . edit ( xpath61 , element61 ) <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> except ( pan . xapi . PanXapiError , err . PanDeviceXapiError ) as e : <EOL> try : <EOL> message = e . msg <EOL> except AttributeError : <EOL> message = e . message <EOL> if message . startswith ( "<STR_LIT>" ) : <EOL> self . _set_ntp_servers_60 ( primary , secondary = secondary ) <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> else : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> raise e <EOL> def _set_ntp_servers_60 ( self , primary , secondary = None ) : <EOL> """<STR_LIT>""" <EOL> xpath60 = pandevice . XPATH_DEVICECONFIG_SYSTEM <EOL> xpath60_pri = xpath60 + "<STR_LIT>" <EOL> xpath60_sec = xpath60 + "<STR_LIT>" <EOL> element60_pri = "<STR_LIT>" <EOL> element60_sec = "<STR_LIT>" <EOL> if primary : <EOL> element60_pri += "<STR_LIT>" % ( primary , ) <EOL> if secondary : <EOL> element60_sec += "<STR_LIT>" % ( secondary , ) <EOL> self . xapi . edit ( xpath60_pri , element60_pri ) <EOL> self . xapi . edit ( xpath60_sec , element60_sec ) <EOL> def show_interface ( self , interface ) : <EOL> self . set_config_changed ( ) <EOL> interface_name = self . _interface_name ( interface ) <EOL> self . xapi . op ( "<STR_LIT>" % ( interface_name , ) ) <EOL> pconf = PanConfig ( self . xapi . element_result ) <EOL> response = pconf . python ( ) <EOL> return response [ '<STR_LIT:result>' ] <EOL> def pending_changes ( self ) : <EOL> self . xapi . op ( cmd = "<STR_LIT>" , cmd_xml = True ) <EOL> pconf = PanConfig ( self . xapi . element_result ) <EOL> response = pconf . python ( ) <EOL> return response [ '<STR_LIT:result>' ] <EOL> def add_commit_lock ( self , comment = None , exception = False ) : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> cmd = ET . Element ( "<STR_LIT>" ) <EOL> subel = ET . SubElement ( cmd , "<STR_LIT>" ) <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> if comment is not None : <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> subel . text = comment <EOL> try : <EOL> self . xapi . op ( ET . tostring ( cmd ) ) <EOL> except ( pan . xapi . PanXapiError , err . PanDeviceXapiError ) as e : <EOL> if not re . match ( r"<STR_LIT>" , str ( e ) ) : <EOL> raise <EOL> else : <EOL> if exception : <EOL> raise err . PanLockError ( str ( e ) , pan_device = self ) <EOL> else : <EOL> self . _logger . debug ( str ( e ) ) <EOL> return False <EOL> self . commit_locked = True <EOL> return True <EOL> def remove_commit_lock ( self , admin = None , exception = False ) : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> cmd = ET . Element ( "<STR_LIT>" ) <EOL> subel = ET . SubElement ( cmd , "<STR_LIT>" ) <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> if admin is not None : <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> subel . text = admin <EOL> try : <EOL> self . xapi . op ( ET . tostring ( cmd ) ) <EOL> except ( pan . xapi . PanXapiError , err . PanDeviceXapiError ) as e : <EOL> if not re . match ( r"<STR_LIT>" , str ( e ) ) : <EOL> raise <EOL> else : <EOL> if exception : <EOL> raise err . PanLockError ( str ( e ) , pan_device = self ) <EOL> else : <EOL> self . _logger . debug ( str ( e ) ) <EOL> return False <EOL> self . commit_locked = False <EOL> return True <EOL> def add_config_lock ( self , comment = None , exception = False ) : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> cmd = ET . Element ( "<STR_LIT>" ) <EOL> subel = ET . SubElement ( cmd , "<STR_LIT>" ) <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> if comment is not None : <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> subel . text = comment <EOL> try : <EOL> self . xapi . op ( ET . tostring ( cmd ) ) <EOL> except ( pan . xapi . PanXapiError , err . PanDeviceXapiError ) as e : <EOL> if not re . match ( r"<STR_LIT>" , <EOL> str ( e ) ) : <EOL> raise <EOL> else : <EOL> if exception : <EOL> raise err . PanLockError ( str ( e ) , pan_device = self ) <EOL> else : <EOL> self . _logger . debug ( str ( e ) ) <EOL> return False <EOL> self . config_locked = True <EOL> return True <EOL> def remove_config_lock ( self , exception = False ) : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> cmd = ET . Element ( "<STR_LIT>" ) <EOL> subel = ET . SubElement ( cmd , "<STR_LIT>" ) <EOL> subel = ET . SubElement ( subel , "<STR_LIT>" ) <EOL> try : <EOL> self . xapi . op ( ET . tostring ( cmd ) ) <EOL> except ( pan . xapi . PanXapiError , err . PanDeviceXapiError ) as e : <EOL> if not re . match ( r"<STR_LIT>" , <EOL> str ( e ) ) : <EOL> raise <EOL> else : <EOL> if exception : <EOL> raise err . PanLockError ( str ( e ) , pan_device = self ) <EOL> else : <EOL> self . _logger . debug ( str ( e ) ) <EOL> return False <EOL> self . config_locked = False <EOL> return True <EOL> def check_commit_locks ( self ) : <EOL> self . xapi . op ( "<STR_LIT>" , cmd_xml = True ) <EOL> response = self . xapi . element_result . find ( "<STR_LIT>" ) <EOL> return True if response is not None else False <EOL> def revert_to_running_configuration ( self ) : <EOL> self . _logger . debug ( "<STR_LIT>" % ( self . hostname , ) ) <EOL> self . xapi . op ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def restart ( self ) : <EOL> self . _logger . debug ( "<STR_LIT>" % ( self . hostname , ) ) <EOL> try : <EOL> self . xapi . op ( "<STR_LIT>" , cmd_xml = True ) <EOL> except pan . xapi . PanXapiError as e : <EOL> if not e . msg . startswith ( "<STR_LIT>" ) : <EOL> raise e <EOL> def refresh_devices_from_panorama ( self , devices = ( ) ) : <EOL> try : <EOL> test_iterable = iter ( devices ) <EOL> except TypeError : <EOL> devices = ( devices , ) <EOL> stats_by_ip = { } <EOL> stats_by_host = { } <EOL> devicegroup_stats_by_serial = { } <EOL> template_stats_by_serial = { } <EOL> self . xapi . op ( "<STR_LIT>" , cmd_xml = True ) <EOL> pconf = PanConfig ( self . xapi . element_root ) <EOL> response = pconf . python ( ) <EOL> try : <EOL> for device in response [ '<STR_LIT>' ] [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> stats_by_ip [ device [ '<STR_LIT>' ] ] = device <EOL> stats_by_host [ device [ '<STR_LIT>' ] ] = device <EOL> stats_by_host [ device [ '<STR_LIT>' ] ] = device <EOL> for device in devices : <EOL> try : <EOL> device . serial = stats_by_host [ device . hostname ] [ '<STR_LIT>' ] <EOL> device . connected_to_panorama = stats_by_host [ device . hostname ] [ '<STR_LIT>' ] <EOL> except KeyError as e : <EOL> raise err . PanDeviceError ( "<STR_LIT>" <EOL> "<STR_LIT>" , pan_device = device ) <EOL> except KeyError : <EOL> return { } <EOL> self . xapi . op ( "<STR_LIT>" , cmd_xml = True ) <EOL> dg_element = self . xapi . element_result <EOL> for dg in dg_element . findall ( "<STR_LIT>" ) : <EOL> for device in dg . findall ( "<STR_LIT>" ) : <EOL> pconf = PanConfig ( config = device ) <EOL> stats = pconf . python ( ) <EOL> stats = stats [ '<STR_LIT>' ] <EOL> serial = stats [ '<STR_LIT>' ] <EOL> ip = stats [ '<STR_LIT>' ] <EOL> dg_name = dg . get ( '<STR_LIT:name>' ) <EOL> stats [ '<STR_LIT>' ] = dg_name <EOL> devicegroup_stats_by_serial [ serial ] = stats <EOL> stats_by_ip [ ip ] [ '<STR_LIT>' ] = dg_name <EOL> for device in devices : <EOL> if device . serial is not None : <EOL> stats = devicegroup_stats_by_serial . get ( device . serial ) <EOL> if stats is not None : <EOL> device . devicegroup = stats [ '<STR_LIT>' ] <EOL> sync_status = stats [ '<STR_LIT>' ] <EOL> device . dg_in_sync = True if sync_status == "<STR_LIT>" else False <EOL> return stats_by_ip <EOL> def commit ( self , sync = False , exception = False , cmd = None ) : <EOL> self . _logger . debug ( "<STR_LIT>" % ( self . hostname , ) ) <EOL> return self . _commit ( sync = sync , exception = exception , cmd = cmd ) <EOL> def _commit ( self , cmd = None , exclude = None , commit_all = False , <EOL> sync = False , sync_all = True , exception = False ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( cmd . __class__ , pan . commit . PanCommit ) : <EOL> cmd = cmd . cmd ( ) <EOL> elif issubclass ( cmd . __class__ , ET . Element ) : <EOL> cmd = ET . tostring ( cmd ) <EOL> elif issubclass ( cmd . __class__ , basestring ) : <EOL> pass <EOL> else : <EOL> cmd = ET . Element ( "<STR_LIT>" ) <EOL> if exclude is not None : <EOL> excluded = ET . SubElement ( cmd , "<STR_LIT>" ) <EOL> excluded = ET . SubElement ( excluded , exclude ) <EOL> cmd = ET . tostring ( cmd ) <EOL> if commit_all : <EOL> action = "<STR_LIT:all>" <EOL> else : <EOL> action = None <EOL> if sync : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> self . xapi . commit ( cmd = cmd , <EOL> action = action , <EOL> sync = sync , <EOL> sync_all = sync_all , <EOL> interval = self . interval , <EOL> timeout = self . timeout ) <EOL> self . config_changed = False <EOL> self . config_locked = False <EOL> self . commit_locked = False <EOL> if sync : <EOL> pconf = PanConfig ( self . xapi . element_result ) <EOL> response = pconf . python ( ) <EOL> job = response [ '<STR_LIT:result>' ] <EOL> if job is None : <EOL> if exception : <EOL> raise err . PanCommitNotNeeded ( "<STR_LIT>" , <EOL> pan_device = self ) <EOL> else : <EOL> return <EOL> job = job [ '<STR_LIT>' ] <EOL> devices_results = { } <EOL> devices_success = True <EOL> if commit_all and sync_all : <EOL> devices = job [ '<STR_LIT>' ] <EOL> if devices is not None : <EOL> devices = devices [ '<STR_LIT>' ] <EOL> for device in devices : <EOL> success = True if device [ '<STR_LIT:result>' ] == "<STR_LIT:OK>" else False <EOL> if not success : <EOL> devices_success = False <EOL> devices_results [ device [ '<STR_LIT>' ] ] = { <EOL> '<STR_LIT:success>' : success , <EOL> '<STR_LIT>' : device [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : device [ '<STR_LIT>' ] , <EOL> '<STR_LIT:result>' : device [ '<STR_LIT:result>' ] , <EOL> '<STR_LIT>' : device [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : device [ '<STR_LIT>' ] , <EOL> } <EOL> try : <EOL> devices_results [ device [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = device [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except TypeError as e : <EOL> devices_results [ device [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> try : <EOL> devices_results [ device [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = device [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <EOL> '<STR_LIT>' ] <EOL> except TypeError as e : <EOL> devices_results [ device [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = device [ '<STR_LIT>' ] <EOL> success = True if job [ '<STR_LIT:result>' ] == "<STR_LIT:OK>" and devices_success else False <EOL> if commit_all : <EOL> messages = [ ] <EOL> else : <EOL> messages = job [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if issubclass ( messages . __class__ , basestring ) : <EOL> messages = [ messages ] <EOL> result = { <EOL> '<STR_LIT:success>' : success , <EOL> '<STR_LIT:result>' : job [ '<STR_LIT:result>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:user>' : job [ '<STR_LIT:user>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : messages , <EOL> '<STR_LIT>' : devices_results <EOL> } <EOL> if exception and not success : <EOL> self . _logger . debug ( "<STR_LIT>" % <EOL> ( self . hostname , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] ) ) <EOL> raise err . PanCommitFailed ( pan_device = self , result = result ) <EOL> else : <EOL> if success : <EOL> self . _logger . debug ( "<STR_LIT>" % <EOL> ( self . hostname , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> self . _logger . debug ( "<STR_LIT>" % <EOL> ( self . hostname , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] ) ) <EOL> return result <EOL> else : <EOL> jobid = self . xapi . element_root . find ( '<STR_LIT>' ) <EOL> if jobid is None : <EOL> if exception : <EOL> raise err . PanCommitNotNeeded ( "<STR_LIT>" , <EOL> pan_device = self ) <EOL> else : <EOL> return <EOL> self . _logger . debug ( "<STR_LIT>" % ( jobid , ) ) <EOL> return jobid <EOL> def syncjob ( self , response , interval = <NUM_LIT:0.5> ) : <EOL> """<STR_LIT>""" <EOL> if interval is not None : <EOL> try : <EOL> interval = float ( interval ) <EOL> if interval < <NUM_LIT:0> : <EOL> raise ValueError <EOL> except ValueError : <EOL> raise err . PanDeviceError ( '<STR_LIT>' % interval ) <EOL> job = response . find ( '<STR_LIT>' ) <EOL> if job is None : <EOL> return False <EOL> job = job . text <EOL> self . _logger . debug ( '<STR_LIT>' , job ) <EOL> cmd = '<STR_LIT>' % job <EOL> start_time = time . time ( ) <EOL> while True : <EOL> try : <EOL> self . xapi . op ( cmd = cmd , cmd_xml = True ) <EOL> except pan . xapi . PanXapiError as msg : <EOL> raise pan . xapi . PanXapiError ( '<STR_LIT>' % ( cmd , msg ) ) <EOL> path = '<STR_LIT>' <EOL> status = self . xapi . element_root . find ( path ) <EOL> if status is None : <EOL> raise pan . xapi . PanXapiError ( '<STR_LIT>' + <EOL> "<STR_LIT>" % cmd ) <EOL> if status . text == '<STR_LIT>' : <EOL> pconf = PanConfig ( self . xapi . element_result ) <EOL> response = pconf . python ( ) <EOL> job = response [ '<STR_LIT:result>' ] <EOL> if job is None : <EOL> return <EOL> job = job [ '<STR_LIT>' ] <EOL> success = True if job [ '<STR_LIT:result>' ] == "<STR_LIT:OK>" else False <EOL> messages = job [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if issubclass ( messages . __class__ , basestring ) : <EOL> messages = [ messages ] <EOL> result = { <EOL> '<STR_LIT:success>' : success , <EOL> '<STR_LIT:result>' : job [ '<STR_LIT:result>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:user>' : job [ '<STR_LIT:user>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : job [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : messages , <EOL> } <EOL> return result <EOL> self . _logger . debug ( '<STR_LIT>' , job , status . text ) <EOL> if ( self . timeout is not None and self . timeout != <NUM_LIT:0> and <EOL> time . time ( ) > start_time + self . timeout ) : <EOL> raise pan . xapi . PanXapiError ( '<STR_LIT>' + <EOL> '<STR_LIT>' % job ) <EOL> self . _logger . debug ( '<STR_LIT>' , interval ) <EOL> time . sleep ( interval ) <EOL> def syncreboot ( self , interval = <NUM_LIT> , timeout = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> import httplib <EOL> if interval is not None : <EOL> try : <EOL> interval = float ( interval ) <EOL> if interval < <NUM_LIT:0> : <EOL> raise ValueError <EOL> except ValueError : <EOL> raise err . PanDeviceError ( "<STR_LIT>" % interval ) <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> start_time = time . time ( ) <EOL> attempts = <NUM_LIT:0> <EOL> is_rebooting = False <EOL> time . sleep ( interval ) <EOL> while True : <EOL> try : <EOL> attempts += <NUM_LIT:1> <EOL> version = self . refresh_version ( ) <EOL> except ( pan . xapi . PanXapiError , err . PanDeviceXapiError ) as e : <EOL> if not e . msg . startswith ( "<STR_LIT>" ) and not e . msg . startswith ( "<STR_LIT>" ) : <EOL> raise e <EOL> else : <EOL> is_rebooting = True <EOL> self . _logger . debug ( "<STR_LIT>" % str ( e ) ) <EOL> self . _logger . debug ( "<STR_LIT>" % str ( attempts ) ) <EOL> except httplib . BadStatusLine as e : <EOL> is_rebooting = True <EOL> self . _logger . debug ( "<STR_LIT>" % str ( e ) ) <EOL> self . _logger . debug ( "<STR_LIT>" % str ( attempts ) ) <EOL> else : <EOL> if is_rebooting : <EOL> self . _logger . debug ( "<STR_LIT>" % version ) <EOL> return version <EOL> else : <EOL> self . _logger . debug ( "<STR_LIT>" ) <EOL> if ( self . timeout is not None and self . timeout != <NUM_LIT:0> and <EOL> time . time ( ) > start_time + self . timeout ) : <EOL> raise err . PanDeviceError ( "<STR_LIT>" ) <EOL> self . _logger . debug ( "<STR_LIT>" , interval ) <EOL> time . sleep ( interval ) </s>
