Пример #1
0
def load_plugins(plugin_path: str, from_list: list,
                 base_class: object) -> DictObj:
    """Load plugins.

    Args:
        plugin_path: Python path to the plugins directory
        from_list: same as __import__ fromlist
        base_class: Base class that plugins are inheriting from, to be used for discovery

    Returns:
        DictObj: plugin name => plugin
    """
    plugins = dict()
    imported_package = __import__(plugin_path, fromlist=from_list)
    for i in pkgutil.iter_modules(imported_package.__path__,
                                  imported_package.__name__ + '.'):
        if not i.ispkg:
            plugin_module = __import__(i.name, fromlist=from_list)
            class_members = inspect.getmembers(plugin_module, inspect.isclass)
            for (_, class_member) in class_members:
                if issubclass(class_member, base_class) & (class_member
                                                           is not base_class):
                    plugins[class_member().name] = class_member

    return DictObj(plugins)
Пример #2
0
def do_print_license_types(licenses: DictObj) -> None:
    """Print the license types supported by file_create.

    Args:
        licenses: The licenses DictOjb returned from load_files_and_licenses
    """
    LOGGER.info('\nSupported License Types:')
    for key, value in licenses.items():
        LOGGER.info('\t%s: %s', key, value().description)
    raise SystemExit(0)
Пример #3
0
def do_print_file_types(files: DictObj) -> None:
    """Print the file types supported by file_create.

    Args:
        files: The files DictObj returned from load_files_and_licenses
    """
    LOGGER.info('\nSupported File Types:')
    for key in files.keys():
        LOGGER.info("\t%s", key)
    raise SystemExit(0)
Пример #4
0
    def _setup(self, paths: Paths, plugins: DictObj, info: DictObj) -> None:
        for op_name, op_settings in info.items():
            plugin = op_settings.get('plugin')

            if not plugin:
                raise SyntaxError(
                    "no plugin defined for '{0!s}'".format(op_name))
            if plugin not in plugins:
                raise ValueError("plugin not found: {0!s}".format(plugin))

            self.map[op_name] = plugins[plugin]().setup(
                paths, self.project, op_settings)
Пример #5
0
class Projects:
    """Projects holder class

    paths: paths and backup name
    plugins: loaded plugins
    info: projects settings
    """
    def __init__(self, paths: Paths, plugins: DictObj, info: DictObj) -> None:
        self.map = DictObj(dict())

        self._setup(paths, plugins, info)

    def _setup(self, paths: Paths, plugins: DictObj, info: DictObj) -> None:
        projects = info.get('projects')
        if not projects:
            raise SyntaxError("no projects defined")

        for project_name, project_settings in projects.items():
            name = project_settings.pop('name', '')
            if not name:
                name = project_name

            subdir = project_settings.pop('backup_dir', '')
            if subdir:
                new_paths = paths.copy()
                new_paths.subdir = subdir
                self.map[project_name] = Project(new_paths, name, plugins,
                                                 project_settings)
            else:
                self.map[project_name] = Project(paths, name, plugins,
                                                 project_settings)

    def run(self) -> Tuple[bool, str]:
        """Run all defined projects

        Returns:
            bool: operations completed successfully
            str: if operations failed, the error message explaining what failed
        """
        for pos in sorted(list(self.map.keys())):
            finished, error_msg = self.map[pos].run()
            if not finished:
                return finished, error_msg

        return True, ''
Пример #6
0
class Project:
    """Project holder class

    Args:
        paths: paths and backup name
        project: name of project
        plugins: loaded plugins
        info: project settings
    """
    def __init__(self, paths: Paths, project: str, plugins: DictObj,
                 info: DictObj):
        self.project = project
        self.map = DictObj(dict())

        self._setup(paths, plugins, info)

    def _setup(self, paths: Paths, plugins: DictObj, info: DictObj) -> None:
        for op_name, op_settings in info.items():
            plugin = op_settings.get('plugin')

            if not plugin:
                raise SyntaxError(
                    "no plugin defined for '{0!s}'".format(op_name))
            if plugin not in plugins:
                raise ValueError("plugin not found: {0!s}".format(plugin))

            self.map[op_name] = plugins[plugin]().setup(
                paths, self.project, op_settings)

    def run(self) -> Tuple[bool, str]:
        """Run operations for this project

        Returns:
            bool: operations completed successfully
            str: if operations failed, the error message explaining what failed
        """
        LOGGER.info("Project: %s", self.project)

        for pos in sorted(list(self.map.keys())):
            finished, error_msg = self.map[pos].run()
            if not finished:
                return finished, error_msg

        return True, ''
Пример #7
0
def load_plugins() -> DictObj:
    """Loads plugins

    Returns:
        DictObj: plugin name => Setup Plugin object
    """
    plugins = dict()
    imported_package = __import__('eljef.backup.plugins', fromlist=['plugin'])
    for i in pkgutil.iter_modules(imported_package.__path__,
                                  imported_package.__name__ + '.'):
        if not i.ispkg:
            plugin_module = __import__(i.name, fromlist=['plugin'])
            class_members = inspect.getmembers(plugin_module, inspect.isclass)
            for (_, class_member) in class_members:
                if issubclass(class_member,
                              SetupPlugin) & (class_member is not SetupPlugin):
                    plugins[class_member().name] = class_member

    return DictObj(plugins)
Пример #8
0
    def _setup(self, paths: Paths, plugins: DictObj, info: DictObj) -> None:
        projects = info.get('projects')
        if not projects:
            raise SyntaxError("no projects defined")

        for project_name, project_settings in projects.items():
            name = project_settings.pop('name', '')
            if not name:
                name = project_name

            subdir = project_settings.pop('backup_dir', '')
            if subdir:
                new_paths = paths.copy()
                new_paths.subdir = subdir
                self.map[project_name] = Project(new_paths, name, plugins,
                                                 project_settings)
            else:
                self.map[project_name] = Project(paths, name, plugins,
                                                 project_settings)
Пример #9
0
def load_config(path: str, defaults: dict) -> DictObj:
    """Loads the configuration file and any project files loaded in projects_folder if defined

    Args:
        path: full path to config file
        defaults: dictionary filled with default settings

    Returns:
        DictObj filled with settings from config files
    """
    try:
        settings = Settings(defaults, path, '').get_all()
    except FileNotFoundError as exception_object:
        raise FileNotFoundError(
            "{0!s}: not found".format(path)) from exception_object
    except IOError as exception_object:
        raise IOError("{0!s}: not a file".format(path)) from exception_object

    return DictObj(
        merge_dictionaries(
            settings,
            load_projects_folder(
                path,
                settings.get('backup', dict()).get('projects_folder'))))
Пример #10
0
 def __init__(self, config_path) -> None:
     fops.mkdir(os.path.abspath(config_path))
     self.__config = os.path.join(os.path.abspath(config_path),
                                  'groups.yaml')
     self.__groups = DictObj()
     self.__read()
Пример #11
0
class DockerGroups(object):
    """Information class for groups of docker containers.

    config_path: Path to base configuration directory.
    """
    def __init__(self, config_path) -> None:
        fops.mkdir(os.path.abspath(config_path))
        self.__config = os.path.join(os.path.abspath(config_path),
                                     'groups.yaml')
        self.__groups = DictObj()
        self.__read()

    def __read(self) -> None:
        LOGGER.debug('Building list of currently defined groups.')
        groups_yaml = fops.file_read_convert(self.__config, 'YAML', True)
        for key, value in groups_yaml.items():
            self.add(key, value, False)

    def add(self, group: str, group_data: dict = None,
            save: bool = True) -> None:
        """Define a new group by name.

        Args:
            group: Name of group to define and add to the list of currently defined groups.
            group_data: `master` and `member` data for a group.
            save: Save group data to file after completion of adding.
        """
        if group not in self.__groups:
            self.__groups[group] = DockerGroup(group_data)
            if save:
                self.save()
            LOGGER.debug("Group '%s' successfully added.", group)
        else:
            LOGGER.debug("Group '%s' already exists.", group)

    def get(self, group) -> DockerGroup:
        """Get a group object by name.

        Args:
            group: Name of group to return group object for.

        Returns:
            Filled DockerGroup information class.

        Raises:
            DockerError: If group is not defined.
        """
        if group not in self.__groups:
            raise DockerError("Group '{0!s}' not defined.".format(group))

        return self.__groups[group]

    def list(self) -> list:
        """List defined groups.

        Returns:
            A list of defined groups. If there are no groups, am empty list is returned.
        """
        ret = []
        if self.__groups:
            ret += [*self.__groups]
        return ret

    def save(self) -> None:
        """Save group information to file."""
        LOGGER.debug('Saving groups information.')
        fops.file_write_convert(self.__config, 'YAML', self.__groups.to_dict())
Пример #12
0
    def __init__(self, paths: Paths, project: str, plugins: DictObj,
                 info: DictObj):
        self.project = project
        self.map = DictObj(dict())

        self._setup(paths, plugins, info)
Пример #13
0
    def __init__(self, paths: Paths, plugins: DictObj, info: DictObj) -> None:
        self.map = DictObj(dict())

        self._setup(paths, plugins, info)