Esempio n. 1
0
    def __init__(self):
        """Function for intializing the class.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        self.username = getpass.getuser()
        valid = True
        major = 3
        minor = 5
        major_installed = sys.version_info[0]
        minor_installed = sys.version_info[1]

        # Exit if python version is too low
        if major_installed < major:
            valid = False
        elif major_installed == major and minor_installed < minor:
            valid = False
        if valid is False:
            log_message = ('Required python version must be >= {}.{}. '
                           'Python version {}.{} installed'
                           ''.format(major, minor, major_installed,
                                     minor_installed))
            log.log2die_safe(1018, log_message)
Esempio n. 2
0
def run():
    """Process agent data.

    Args:
        None

    Returns:
        None

    """
    # Prevent running as sudo user
    if 'SUDO_UID' in os.environ:
        MESSAGE = ('Cannot run setup using "sudo". Run as a regular user to '
                   'install in this directory or as user "root".')
        log.log2die_safe(1078, MESSAGE)

    # Initialize key variables
    username = getpass.getuser()

    # Determine whether version of python is valid
    _Python().setup()

    # Do specific setups for root user
    _Daemon().setup()

    # Update configuration if required
    _Configuration().setup()

    # Run server setup
    _Database().setup()
    _DBForeignTables().setup()
    _DBNonForeignTables().setup()

    # Give suggestions as to what to do
    if username == 'root':
        suggestions = """\

You can start mdl daemons with these commands:

    # systemctl start mdl-api.service

You can enable mdl daemons to start on system boot with these commands:

    # systemctl enable mdl-api.service

"""
        print(suggestions)

    # All done
    print_ok('Installation successful.')
Esempio n. 3
0
    def setup(self):
        """Update the configuration with good defaults.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        valid = True
        updated_list = []
        config = copy.deepcopy(self.config)
        directory = self.directories[0]

        # Update log_directory and ingest_cache_directory
        if isinstance(config, dict) is True:
            if 'main' in config:
                # Setup the log_directory to a known good default
                (updated, config) = self._create_directory_entries(
                    'log_directory', config)
                updated_list.append(updated)

            else:
                valid = False
        else:
            valid = False

        # Gracefully exit if things are not OK
        if valid is False:
            log_message = (
                'Configuration files found in {} is invalid'
                ''.format(self.directories))
            log.log2die_safe(1069, log_message)

        # Update configuration file if required
        if len(updated_list) == updated_list.count(True):
            for next_directory in self.directories:
                # Delete all YAML files in the directory
                general.delete_yaml_files(next_directory)

            # Write config back to directory
            filepath = ('%s/config.yaml') % (directory)
            with open(filepath, 'w') as outfile:
                yaml.dump(config, outfile, default_flow_style=False)
Esempio n. 4
0
def delete_files(directory, extension='.yaml'):
    """Delete all files of a specfic extension in a directory.

    Args:
        directory: Directory name
        extension: File extension

    Returns:
        None

    """
    # Determine whether directory is valid
    if os.path.isdir(directory) is False:
        log_message = ('Directory %s does not exist') % (directory)
        log.log2die_safe(1023, log_message)

    # Get list of files
    filelist = [
        next_file for next_file in os.listdir(directory)
        if next_file.endswith(extension)
    ]

    # Delete files
    for delete_file in filelist:
        file_path = ('%s/%s') % (directory, delete_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as exception_error:
            log_message = ('Error: deleting files in %s. Error: %s') % (
                directory, exception_error)
            log.log2die_safe(1014, log_message)
        except:
            log_message = ('Unexpected error')
            log.log2die_safe(1015, log_message)
Esempio n. 5
0
    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.mdl_user_exists = True
        self.mdl_user = None
        self.running_as_root = False

        # If running as the root user, then the mdl user needs to exist
        if username == 'root':
            self.running_as_root = True
            try:
                self.mdl_user = input(
                    'Please enter the username under which '
                    'mdl will run: ')

                # Get GID and UID for user
                self.gid = getpwnam(self.mdl_user).pw_gid
                self.uid = getpwnam(self.mdl_user).pw_uid
            except KeyError:
                self.mdl_user_exists = False
            return

        # Die if user doesn't exist
        if self.mdl_user_exists is False:
            log_message = (
                'User {} not found. Please try again.'
                ''.format(self.mdl_user))
            log.log2die_safe(1049, log_message)
Esempio n. 6
0
    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
        print('Installing required pip3 packages')
        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)
Esempio n. 7
0
    def _file_permissions(self):
        """Set file permissions.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        mdl_user = self.mdl_user
        root_directory = self.root_directory

        # Prompt to change ownership of root_directory
        groupname = grp.getgrgid(self.gid).gr_name
        response = input(
            'Change ownership of {} directory to user:{} group:{} (y,N) ?: '
            ''.format(root_directory, mdl_user, groupname))

        # Abort if necessary
        if response.lower() != 'y':
            log_message = ('Aborting as per user request.')
            log.log2die_safe(1050, log_message)

        # Change ownership of files under root_directory
        for parent_directory, directories, files in os.walk(root_directory):
            for directory in directories:
                os.chown(os.path.join(parent_directory, directory), self.uid,
                         self.gid)
            for next_file in files:
                os.chown(os.path.join(parent_directory, next_file), self.uid,
                         self.gid)

        # Change ownership of root_directory
        os.chown(root_directory, self.uid, self.gid)
Esempio n. 8
0
def read_yaml_files(directories):
    """Read the contents of all yaml files in a directory.

    Args:
        directories: List of directory names with configuration files

    Returns:
        config_dict: Dict of yaml read

    """
    # Initialize key variables
    yaml_found = False
    yaml_from_file = ''
    all_yaml_read = ''

    # Check each directory in sequence
    for config_directory in directories:
        # Check if config_directory exists
        if os.path.isdir(config_directory) is False:
            log_message = ('Configuration directory "%s" '
                           'doesn\'t exist!' % config_directory)
            log.log2die_safe(1009, log_message)

        # Cycle through list of files in directory
        for filename in os.listdir(config_directory):
            # Examine all the '.yaml' files in directory
            if filename.endswith('.yaml'):
                # YAML files found
                yaml_found = True

                # Read file and add to string
                file_path = ('%s/%s') % (config_directory, filename)
                try:
                    with open(file_path, 'r') as file_handle:
                        yaml_from_file = file_handle.read()
                except:
                    log_message = ('Error reading file %s. Check permissions, '
                                   'existence and file syntax.'
                                   '') % (file_path)
                    log.log2die_safe(1065, log_message)

                # Append yaml from file to all yaml previously read
                all_yaml_read = ('%s\n%s') % (all_yaml_read, yaml_from_file)

        # Verify YAML files found in directory
        if yaml_found is False:
            log_message = ('No files found in directory "%s" with ".yaml" '
                           'extension.') % (config_directory)
            log.log2die_safe(1010, log_message)

    # Return
    config_dict = yaml.load(all_yaml_read)
    return config_dict
Esempio n. 9
0
def _key_sub_key(key, sub_key, config_dict, die=True):
    """Get config parameter from YAML.

    Args:
        key: Primary key
        sub_key: Secondary key
        config_dict: Dictionary to explore
        die: Die if true and the result encountered is None

    Returns:
        result: result

    """
    # Get result
    result = None

    # Verify config_dict is indeed a dict.
    # Die safely as log_directory is not defined
    if isinstance(config_dict, dict) is False:
        log.log2die_safe(1021, 'Invalid configuration file. YAML not found')

    # Get new result
    if key in config_dict:
        # Make sure we don't have a None value
        if config_dict[key] is None:
            log_message = ('Configuration value {}: is blank. Please fix.'
                           ''.format(key))
            log.log2die_safe(1022, log_message)

        # Get value we need
        if sub_key in config_dict[key]:
            result = config_dict[key][sub_key]

    # Error if not configured
    if result is None and die is True:
        log_message = ('%s:%s not defined in configuration') % (key, sub_key)
        log.log2die_safe(1016, log_message)

    # Return
    return result
Esempio n. 10
0
"""
        print(suggestions)

    # Outline the versions of MySQL and MariaDB that are required
    suggestions = """\

mdl requires:

    MySQL >= 5.5
    MariaDB >= 10

Please verify.

"""
    print(suggestions)

    # All done
    print('\nOK\n')


if __name__ == '__main__':
    # Prevent running as sudo user
    if 'SUDO_UID' in os.environ:
        MESSAGE = (
            'Cannot run setup using "sudo". Run as a regular user to '
            'install in this directory or as user "root".')
        log.log2die_safe(1078, MESSAGE)

    # Run main
    main()