def parser_check(parser, args): """Verify the conditions needed for the installation. Args: parser: The parser object args: The arguments passed into the CLI Returns: None """ # Perform installation check to ensure the environment is okay installation_checks() if args.action == 'install': if shared.root_check() is False and getpass.getuser() != 'travis': shared.log('Please run the script with sudo to continue.') print('Default Installation') elif args.action == 'developer': print('Unittesting mode') if shared.root_check() is True: shared.log('''\ You cannot run the developer installation as root. Please run without sudo privileges to continue''') else: parser.print_help(sys.stderr) sys.exit(1)
def get_pattoo_home(): """Retrieve home directory for pattoo user. Args: None Returns: The home directory for the pattoo user """ if shared.root_check() is True: try: # No exception will be thrown if the pattoo user exists pattoo_home = pwd.getpwnam('pattoo').pw_dir # Set defaults if pattoo user doesn't exist except KeyError: pattoo_home = '/home/pattoo' # Ensure that the pattoo home directory is not set to non-existent if pattoo_home == '/nonexistent': pattoo_home = '/home/pattoo' # Set up pattoo home for unittest user if the user is not root else: pattoo_home = shared.unittest_environment_setup() return pattoo_home
def install(pattoo_home): """Start configuration process. Args: pattoo_home: The home directory of the pattoo user Returns: None """ # Initialize key variables if os.environ.get('PATTOO_CONFIGDIR') is None: os.environ['PATTOO_CONFIGDIR'] = '{0}etc{0}pattoo'.format(os.sep) config_directory = os.environ.get('PATTOO_CONFIGDIR') shared_config = { 'pattoo': { 'language': 'en', 'log_directory': ( '/var/log/pattoo'), 'log_level': 'debug', 'cache_directory': ( '/opt/pattoo/cache'), 'daemon_directory': ( '/opt/pattoo/daemon'), 'system_daemon_directory': '/var/run/pattoo', } } # Defines how agents will communicate with the API daemons # Only required for travis-ci unittesting agent_config = { 'pattoo_agent_api': { 'ip_address': '127.0.0.1', 'ip_bind_port': 20201 }, } server_config = { 'pattoo_db': { 'db_pool_size': 10, 'db_max_overflow': 20, 'db_hostname': 'localhost', 'db_username': '******', 'db_password': '******', 'db_name': 'pattoo' }, 'pattoo_api_agentd': { 'ip_listen_address': '0.0.0.0', 'ip_bind_port': 20201, 'api_encryption_email': '*****@*****.**', }, 'pattoo_apid': { 'ip_listen_address': '0.0.0.0', 'ip_bind_port': 20202, 'jwt_secret_key': secrets.token_urlsafe(64), 'acesss_token_exp': '15_m', 'refresh_token_exp': '1_D' }, 'pattoo_ingesterd': { 'ingester_interval': 3600, 'batch_size': 500, 'graceful_timeout': 10 } } # Attempt to create configuration directory files.mkdir(config_directory) if _shared.root_check() is True: # Create the pattoo user and group configure.create_user('pattoo', pattoo_home, '/bin/false', True) # Attempt to change the ownership of the config and home directories shared.chown(config_directory) shared.chown(pattoo_home) # Create configuration configure.configure_component('pattoo', config_directory, shared_config) configure.configure_component( 'pattoo_server', config_directory, server_config) configure.configure_component( 'pattoo_agent', config_directory, agent_config)
def main(): """Pattoo CLI script. None Returns: None """ # Initialize key variables _help = 'This program is the CLI interface to configuring pattoo' daemon_list = ['pattoo_apid', 'pattoo_api_agentd', 'pattoo_ingesterd'] template_dir = os.path.join(ROOT_DIR, 'setup/systemd/system') # Process the CLI _parser = Parser(additional_help=_help) (args, parser) = _parser.args() # Perform checks checks.parser_check(_parser.args()[1], _parser.args()[0]) checks.pattoo_shared_check() checks.venv_check() # Import packages that depend on pattoo shared from _pattoo import configure from pattoo_shared.installation import packages, systemd, environment # Set up essentials for creating the virtualenv pattoo_home = get_pattoo_home() venv_dir = os.path.join(pattoo_home, 'pattoo-venv') if getpass.getuser() != 'travis': environment.environment_setup(venv_dir) venv_interpreter = os.path.join(venv_dir, 'bin/python3') installation_dir = '{} {}'.format(venv_interpreter, ROOT_DIR) # Installs all pattoo components if args.qualifier == 'all': print('Installing everything') configure.install(pattoo_home) packages.install(ROOT_DIR, venv_dir, args.verbose) # Import db after pip3 packages are installed from _pattoo import db db.install() if shared.root_check() is True and args.action != 'developer': systemd.install(daemon_list=daemon_list, template_dir=template_dir, installation_dir=installation_dir, verbose=args.verbose) # Configures pattoo and sets up database tables elif args.qualifier == 'database': print('Installing database tables') configure.install(pattoo_home) packages.install(ROOT_DIR, venv_dir) # Import db after pip3 packages are installed from _pattoo import db db.install() # Installs and starts system daemons elif args.qualifier == 'systemd': print('Installing systemd daemons') if shared.root_check() is True: systemd.install(daemon_list=daemon_list, template_dir=template_dir, installation_dir=installation_dir, verbose=True) else: shared.log('You need to be running as root to install the daemons') elif args.qualifier == 'pip': # Installs additionally required pip3 packages packages.install(ROOT_DIR, venv_dir, args.verbose) # Sets up the configuration for pattoo elif args.qualifier == 'configuration': configure.install(pattoo_home) # Print help if no argument options were triggered else: parser.print_help(sys.stderr) sys.exit(1) # Done print('Done')