def __init__(self): """Method initializing the class. Args: config_dir: Configuration directory Returns: None """ # Initialize key variables self.agent_name = 'topology' # Get configuration self.agent_config = jm_configuration.ConfigAgent(self.agent_name) self.server_config = jm_configuration.Config() self.snmp_config = jm_configuration.ConfigSNMP() # Cleanup, move temporary files to clean permanent directory. # Delete temporary directory topology_directory = self.server_config.topology_directory() if os.path.isdir(topology_directory): jm_general.delete_files(topology_directory) else: os.makedirs(topology_directory, 0o755)
def api_make(config, host, verbose=False): """Process 'pagemaker' CLI option. Args: config: Configuration object verbose: Verbose output if True Returns: None """ # Initialize key variables threads_in_pool = 1 device_file_found = False # Create directory if needed perm_dir = config.web_directory() temp_dir = tempfile.mkdtemp() # Delete all files in temporary directory jm_general.delete_files(temp_dir) # Skip if device file not found if os.path.isfile(config.snmp_device_file(host)) is False: log_message = ( 'No YAML device file for host %s found in %s. ' 'Run toolbox.py with the "poll" option first.' '') % (host, config.snmp_directory()) log.log2quiet(1018, log_message) else: device_file_found = True #################################################################### # # Define variables that will be required for the database update # We have to initialize the dict during every loop to prevent # data corruption # #################################################################### data_dict = {} data_dict['host'] = host data_dict['config'] = config data_dict['verbose'] = verbose data_dict['temp_dir'] = temp_dir table = HTMLTable(config, host) # Create HTML output html = ('%s%s\n%s\n\n%s\n') % ( _html_header(host), host, table.device(), table.ethernet()) # Do the rest if device_file_found if device_file_found is True: # Wait on the queue until everything has been processed return html
def create(config, host): """Create topology page for host. Args: config: Configuration object host: Hostname to create pages for Returns: None """ # Initialize key variables device_file_found = False # Create directory if needed temp_dir = tempfile.mkdtemp() # Delete all files in temporary directory jm_general.delete_files(temp_dir) # Skip if device file not found if os.path.isfile(config.topology_device_file(host)) is False: log_message = ( 'No YAML device file for host %s found in %s. ' 'topoloy agent has not discovered it yet.' '') % (host, config.topology_directory()) log.log2quiet(1018, log_message) else: device_file_found = True #################################################################### # # Define variables that will be required for the database update # We have to initialize the dict during every loop to prevent # data corruption # #################################################################### table = HTMLTable(config, host) # Create HTML output html = ('%s%s\n%s\n\n%s\n') % ( _html_header(host), host, table.device(), table.ethernet()) # Do the rest if device_file_found if device_file_found is True: # Wait on the queue until everything has been processed return html
def test_delete_files(self): """Testing function delete_files.""" # Initialize key variables complete_filenames = {} ################################################# # Test with invalid directory ################################################# invalid_path = ('/tmp/%s.%s') % (self.random_string, self.random_string) with self.assertRaises(SystemExit): testimport.delete_files(invalid_path) ################################################# # Test with valid directory ################################################# # Create a test directory path = ('/tmp/%s') % (self.random_string) if os.path.exists(path) is False: os.makedirs(path) # Place files in the directory for count in range(0, 4): filename = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(15) ]) complete_filenames[count] = ('%s/%s') % (path, filename) open(complete_filenames[count], 'a').close() # Check files in directory self.assertEqual(os.path.isfile(complete_filenames[count]), True) # Delete files in directory testimport.delete_files(path) # Check that files were deleted for filename in complete_filenames.values(): self.assertEqual(os.path.isfile(filename), False) # Delete directory shutil.rmtree(path)
def test_delete_files(self): """Testing function delete_files.""" # Initialize key variables complete_filenames = {} ################################################# # Test with invalid directory ################################################# invalid_path = ("/tmp/%s.%s") % (self.random_string, self.random_string) with self.assertRaises(SystemExit): testimport.delete_files(invalid_path) ################################################# # Test with valid directory ################################################# # Create a test directory path = ("/tmp/%s") % (self.random_string) if os.path.exists(path) is False: os.makedirs(path) # Place files in the directory for count in range(0, 4): filename = "".join([random.choice(string.ascii_letters + string.digits) for n in range(15)]) complete_filenames[count] = ("%s/%s") % (path, filename) open(complete_filenames[count], "a").close() # Check files in directory self.assertEqual(os.path.isfile(complete_filenames[count]), True) # Delete files in directory testimport.delete_files(path) # Check that files were deleted for filename in complete_filenames.values(): self.assertEqual(os.path.isfile(filename), False) # Delete directory shutil.rmtree(path)
def make(config, verbose=False): """Process 'pagemaker' CLI option. Args: config: Configuration object verbose: Verbose output if True Returns: None """ # Initialize key variables threads_in_pool = 10 device_file_found = False # Create directory if needed perm_dir = config.web_directory() temp_dir = tempfile.mkdtemp() # Delete all files in temporary directory jm_general.delete_files(temp_dir) # Spawn a pool of threads, and pass them queue instance for _ in range(threads_in_pool): update_thread = PageMaker(THREAD_QUEUE) update_thread.daemon = True update_thread.start() # Get host data and write to file for host in config.hosts(): # Skip if device file not found if os.path.isfile(config.snmp_device_file(host)) is False: log_message = ( 'No YAML device file for host %s found in %s. ' 'Run toolbox.py with the "poll" option first.' '') % (host, config.snmp_directory()) log.log2quiet(1018, log_message) continue else: device_file_found = True #################################################################### # # Define variables that will be required for the database update # We have to initialize the dict during every loop to prevent # data corruption # #################################################################### data_dict = {} data_dict['host'] = host data_dict['config'] = config data_dict['verbose'] = verbose data_dict['temp_dir'] = temp_dir THREAD_QUEUE.put(data_dict) # Do the rest if device_file_found if device_file_found is True: # Wait on the queue until everything has been processed THREAD_QUEUE.join() # PYTHON BUG. Join can occur while threads are still shutting down. # This can create spurious "Exception in thread (most likely raised # during interpreter shutdown)" errors. # The "time.sleep(1)" adds a delay to make sure things really terminate # properly. This seems to be an issue on virtual machines in Dev only time.sleep(1) # Create index file write_file = ('%s/index.html') % (temp_dir) index_html = _index_html(config) with open(write_file, 'w') as file_handle: file_handle.write(index_html) # Cleanup, move temporary files to clean permanent directory. # Delete temporary directory if os.path.isdir(perm_dir): jm_general.delete_files(perm_dir) else: os.makedirs(perm_dir, 0o755) jm_general.move_files(temp_dir, perm_dir) # Clean up os.rmdir(temp_dir)
def snmp(config, verbose=False): """Process 'poll' CLI option. Args: config: Configuration object verbose: Verbose output if True Returns: None """ # Initialize key variables threads_in_pool = 10 # Create directory if needed perm_dir = config.snmp_directory() temp_dir = tempfile.mkdtemp() # Delete all files in temporary directory jm_general.delete_files(temp_dir) # Spawn a pool of threads, and pass them queue instance for _ in range(threads_in_pool): update_thread = PollAllSNMP(THREAD_QUEUE) update_thread.daemon = True update_thread.start() # Get host data and write to file for host in config.hosts(): #################################################################### # # Define variables that will be required for the database update # We have to initialize the dict during every loop to prevent # data corruption # #################################################################### data_dict = {} data_dict['host'] = host data_dict['config'] = config data_dict['verbose'] = verbose data_dict['temp_dir'] = temp_dir THREAD_QUEUE.put(data_dict) # Wait on the queue until everything has been processed THREAD_QUEUE.join() # PYTHON BUG. Join can occur while threads are still shutting down. # This can create spurious "Exception in thread (most likely raised # during interpreter shutdown)" errors. # The "time.sleep(1)" adds a delay to make sure things really terminate # properly. This seems to be an issue on virtual machines in Dev only time.sleep(1) # Cleanup, move temporary files to clean permanent directory. # Delete temporary directory if os.path.isdir(perm_dir): jm_general.delete_files(perm_dir) else: os.makedirs(perm_dir, 0o755) jm_general.move_files(temp_dir, perm_dir) os.rmdir(temp_dir)
def make(config, verbose=False): """Process 'pagemaker' CLI option. Args: config: Configuration object verbose: Verbose output if True Returns: None """ # Initialize key variables threads_in_pool = 10 device_file_found = False # Create directory if needed perm_dir = config.web_directory() temp_dir = tempfile.mkdtemp() # Delete all files in temporary directory jm_general.delete_files(temp_dir) # Spawn a pool of threads, and pass them queue instance for _ in range(threads_in_pool): update_thread = PageMaker(THREAD_QUEUE) update_thread.daemon = True update_thread.start() # Get host data and write to file for host in config.hosts(): # Skip if device file not found if os.path.isfile(config.snmp_device_file(host)) is False: log_message = ('No YAML device file for host %s found in %s. ' 'Run toolbox.py with the "poll" option first.' '') % (host, config.snmp_directory()) jm_general.logit(1018, log_message, False) continue else: device_file_found = True #################################################################### # # Define variables that will be required for the database update # We have to initialize the dict during every loop to prevent # data corruption # #################################################################### data_dict = {} data_dict['host'] = host data_dict['config'] = config data_dict['verbose'] = verbose data_dict['temp_dir'] = temp_dir THREAD_QUEUE.put(data_dict) # Do the rest if device_file_found if device_file_found is True: # Wait on the queue until everything has been processed THREAD_QUEUE.join() # PYTHON BUG. Join can occur while threads are still shutting down. # This can create spurious "Exception in thread (most likely raised # during interpreter shutdown)" errors. # The "time.sleep(1)" adds a delay to make sure things really terminate # properly. This seems to be an issue on virtual machines in Dev only time.sleep(1) # Create index file write_file = ('%s/index.html') % (temp_dir) index_html = _index_html(config) with open(write_file, 'w') as file_handle: file_handle.write(index_html) # Cleanup, move temporary files to clean permanent directory. # Delete temporary directory if os.path.isdir(perm_dir): jm_general.delete_files(perm_dir) else: os.makedirs(perm_dir, 0o755) jm_general.move_files(temp_dir, perm_dir) # Clean up os.rmdir(temp_dir)