def get_pre_snapshots(self, hostname): try: return Utility.return_json( 1, 'get_pre_snapshots()', JSnapComparison().get_pre_snapshots(hostname)) except Exception as e: return Utility.return_json(0, e.msg)
def delete_snapshot(self, hostname, timestamp): try: if not Utility.is_file_path_safe(hostname) or str(timestamp).count('.') > 1: return Utility.return_json(0, 'Invalid hostname') os.remove(f"snapshots/compiled_snapshots/{hostname}_{timestamp}.xml") except: return Utility.return_json(0, 'Unable to delete snapshot, probably non-existent') return Utility.return_json(1, 'Snapshot deleted')
def snapshot(self, credentials): timestamp = time.time() hostname = credentials['host'] test_file = Utility.load_yaml( f"testfiles/{credentials['device_type']}.yaml") dev = ConnectHandler(**credentials) output = '' for test in test_file: if 'command' not in test_file[test][0]: continue raw_command_name = str(test).replace('-', '_') #command = test_file[test][0]['command'] module = import_module( f'drivers.snapshot.cisco.commands.{raw_command_name}') command_ref = getattr(module, raw_command_name) command_ref = command_ref() xml = command_ref.schemaify( command_ref.parse(dev.send_command(command_ref.command()))) output = output + f"{hostname}@@@@{timestamp}@@@@{raw_command_name}@@@@{xml}@@@@@@" with open(f'snapshots/compiled_snapshots/{hostname}_{timestamp}.xml', 'w') as fp: fp.write(output) return Utility.return_json(True, 'Snapshot Completed')
def compare(self, hostname, presnap_timestamp, postsnap_timestamp): try: device_info = Utility.get_device_info(hostname) # Breaking DI by instantiating JSnapComparison here, but I don't foresee making a custom comparison engine for awhile. # But for easier future proofing, I'm leaving this essentially useless class in the process, as it will serve # as an abstraction layer when I do design my own comparison engine return Utility.return_json( 1, "Compare()", JSnapComparison().compare( hostname, presnap_timestamp, postsnap_timestamp, Utility.get_vendor_models()[device_info['vendor']][ device_info['model']])) except Exception as e: return Utility.return_json( 0, 'Failed - ensure you can access sense.one.twcbiz.com APIs')
def snapshot(self, credentials): timestamp = time.time() hostname = credentials['host'] test_file = Utility.load_yaml('testfiles/juniper.yaml') dev = ConnectHandler(**credentials) output = '' for test in test_file: if 'command' not in test_file[test][0]: continue raw_command_name = str(test).replace('-', '_') command = test_file[test][0]['command'] if ' | display xml' not in command: command = command + ' | display xml' result = str(dev.send_command(command)).strip('{master}').strip() xml = etree.fromstring(result).xpath( '//rpc-reply/*[not(self::cli)]') if len(xml) <= 0: return Utility.return_json(False, 'Invalid rpc-reply from device') xml = etree.tostring(xml[0]).decode("utf-8") output = output + f"{hostname}@@@@{timestamp}@@@@{raw_command_name}@@@@{xml}@@@@@@" with open(f'snapshots/compiled_snapshots/{hostname}_{timestamp}.xml', 'w') as fp: fp.write(output) return Utility.return_json(True, 'Snapshot Completed')
def snapshot(self, hostname): device_info = Utility.get_device_info(hostname) if device_info == None: return Utility.return_json(0, 'Invalid hostname or IP') class_path_name = Utility.get_vendor_models()[device_info['vendor']][device_info['model']] class_name = class_path_name.capitalize() # Dynamically import and instantiate the correct snapshot class module_name = import_module(f'drivers.snapshot.{device_info["vendor_lower"]}.{class_path_name}') class_ref = getattr(module_name, class_name) snapshot = class_ref() credentials = { 'device_type': Utility.get_device_types()[device_info['vendor']], 'host': hostname, 'username': Utility.get_credentials()['username'], 'password': Utility.get_credentials()['password'] } return snapshot.snapshot(credentials)