def main(): """Test all the infoset-ng modules with unittests. Args: None Returns: None """ # Determine unittest directory root_dir = general.root_directory() test_dir = ('%s/infoset/test') % (root_dir) # Get list of test files test_files = os.listdir(test_dir) for filename in sorted(test_files): full_path = ('%s/%s') % (test_dir, filename) # Run the test if filename.startswith('test_'): run_script(full_path) # Print message = ('\nHooray - All Done OK!\n') print(message)
def test_root_directory(self): """Test function root_directory.""" # Determine root directory for infoset infoset_dir = infoset.__path__[0] components = infoset_dir.split(os.sep) # Determine root directory 2 levels above root_dir = os.sep.join(components[0:-2]) result = general.root_directory() self.assertEqual(result, root_dir)
def __init__(self): """Method for intializing the class. Args: None Returns: None """ # Initialize key variables self.root = ('%s/.status') % (general.root_directory())
def __init__(self): """Function for intializing the class. Args: None Returns: None """ # Read configuration into dictionary self.directory = ('%s/etc') % (general.root_directory()) self.config = general.read_yaml_files([self.directory])
def _start(self, daemon, restart=False): """Start or restart daemon. Args: daemon: Name of daemon restart: Restart if True Returns: None """ # Initialize key variables username = getpass.getuser() running = False if restart is True: attempt = 'restart' else: attempt = 'start' # Get status root_directory = general.root_directory() if restart is False: if username == 'root': script_name = ( '/bin/systemctl start {}.service'.format(daemon)) else: script_name = ('{}/bin/systemd/{} --start'.format( root_directory, daemon)) else: if username == 'root': script_name = ( '/bin/systemctl restart {}.service'.format(daemon)) else: script_name = ('{}/bin/systemd/{} --restart --force' ''.format(root_directory, daemon)) # Attempt to restart / start response = general.run_script(script_name, die=False) if bool(response['returncode']) is True: log_message = ('Could not {} daemon {}.'.format(attempt, daemon)) log.log2see_safe(1012, log_message) # Return after waiting for daemons to startup properly running = self._running(daemon) return running
def _agent_filepath(config): """Return the path to the agent. Args: config: Agent configuration object Returns: agent_filepath: Complete file path """ # Initialize key variables agent_filename = config.agent_filename() # Get agent status variables root_dir = general.root_directory() agent_filepath = ('%s/%s') % (root_dir, agent_filename) # Return return agent_filepath
def __init__(self): """Function for intializing the class. Args: None Returns: None """ # Initialize key variables running_username = getpass.getuser() daemon_username = configuration.Config().username() self.root_directory = general.root_directory() infoset_user_exists = True self.infoset_user = None self.running_as_root = False # Set the username we need to be running as if running_username == 'root': try: # Get GID and UID for user self.infoset_user = daemon_username self.gid = getpwnam(self.infoset_user).pw_gid self.uid = getpwnam(self.infoset_user).pw_uid except KeyError: infoset_user_exists = False # Die if user doesn't exist if infoset_user_exists is False: log_message = ('User {} not found. Please try again.' ''.format(self.infoset_user)) log.log2die_safe(1049, log_message) else: self.infoset_user = daemon_username # If running as the root user, then the infoset user needs to exist if running_username == 'root': self.running_as_root = True return
def _install_pip3_packages(self): """Install PIP3 packages. Args: None Returns: None """ # Initialize key variables username = self.username # Don't attempt to install packages if running in the Travis CI # environment if 'TRAVIS' in os.environ and 'CI' in os.environ: return # Determine whether PIP3 exists shared.print_ok( 'Installing required pip3 packages from requirements.txt file.') pip3 = general.search_file('pip3') if pip3 is None: log_message = ('Cannot find python "pip3". Please install.') log.log2die_safe(1052, log_message) # Install required PIP packages requirements_file = ('%s/requirements.txt') % ( general.root_directory()) if username == 'root': script_name = ('pip3 install --upgrade --requirement %s' '') % (requirements_file) else: script_name = ('pip3 install --user --upgrade --requirement %s' '') % (requirements_file) general.run_script(script_name) # Status message shared.print_ok('pip3 packages installation complete.')
def test_config_directories(self): """Test function config_directories.""" # Initialize key variables save_directory = None if 'INFOSET_CONFIGDIR' in os.environ: save_directory = os.environ['INFOSET_CONFIGDIR'] # Try with no INFOSET_CONFIGDIR os.environ.pop('INFOSET_CONFIGDIR', None) directory = '{}/etc'.format(general.root_directory()) result = general.config_directories() self.assertEqual(result, [directory]) # Test with INFOSET_CONFIGDIR set directory = tempfile.mkdtemp() os.environ['INFOSET_CONFIGDIR'] = directory result = general.config_directories() self.assertEqual(result, [directory]) # Restore state if save_directory is not None: os.environ['INFOSET_CONFIGDIR'] = save_directory
def _create_directory_entries(self, key, config): """Update the configuration with good defaults for directories. Args: key: Configuration key related to a directory. config: Configuration dictionary Returns: updated: True if we have to update a value """ # Initialize key variables updated = False dir_dict = { 'log_directory': 'log', 'ingest_cache_directory': 'cache', } directory = general.root_directory() # Setup the key value to a known good default if key in config['main']: # Verify whether key value is empty if config['main'][key] is not None: # Create if os.path.isdir(config['main'][key]) is False: config['main'][key] = ('%s/%s') % (directory, dir_dict[key]) updated = True else: config['main'][key] = ('%s/%s') % (directory, dir_dict[key]) updated = True else: config['main'][key] = ('%s/%s') % (directory, dir_dict[key]) updated = True # Return return (updated, config)
def __init__(self): """Function for intializing the class. Args: None Returns: None """ # Initialize key variables username = getpass.getuser() self.root_directory = general.root_directory() self.infoset_user_exists = True self.infoset_user = None self.running_as_root = False # If running as the root user, then the infoset user needs to exist if username == 'root': self.running_as_root = True try: self.infoset_user = input( 'Please enter the username under which ' 'infoset-ng will run: ') # Get GID and UID for user self.gid = getpwnam(self.infoset_user).pw_gid self.uid = getpwnam(self.infoset_user).pw_uid except KeyError: self.infoset_user_exists = False return # Die if user doesn't exist if self.infoset_user_exists is False: log_message = ('User {} not found. Please try again.' ''.format(self.infoset_user)) log.log2die_safe(1049, log_message)
def _blank_configuration(self): """Determine if systemd is installed. Args: None Returns: None """ # Verify found = False root_directory = general.root_directory() config_directory = '{}/etc'.format(root_directory) # Do nothing if running Travis CI if 'INFOSET_CONFIGDIR' in os.environ: return # Find yaml files in config_directory filenames = [ filename for filename in os.listdir(config_directory) if os.path.isfile(os.path.join(config_directory, filename)) ] for filename in filenames: if filename.lower().endswith('.yaml'): log_message = ('Configuration file found') shared.print_ok(log_message) found = True break # Create a blank config if none are found if found is False: config_yaml = ("""\ main: log_directory: log_level: ingest_cache_directory: ingest_pool_size: listen_address: bind_port: interval: memcached_hostname: memcached_port: sqlalchemy_pool_size: sqlalchemy_max_overflow: db_hostname: db_username: db_password: db_name: username: """) # Write config back to directory config_dict = yaml.safe_load(config_yaml) filepath = ('%s/config.yaml') % (config_directory) with open(filepath, 'w') as outfile: yaml.dump(config_dict, outfile, default_flow_style=False) # Message log_message = ('Created blank starter configuration') shared.print_ok(log_message)