예제 #1
0
    def get_installed_plugins(self, plugins_type=None):
        """Returns a dict with all installed plugins

        The return dictionary contains all installed plugins with their
        descriptions & categorized by plugin type.

        :param plugins_type: A specific type of plugins to return
        """
        plugins_dict = OrderedDict()

        for supported_type in self.supported_plugin_types:
            if plugins_type:
                if plugins_type != supported_type:
                    continue
                elif plugins_type not in self.supported_plugin_types:
                    raise IRUnsupportedPluginType(plugins_type)

            if not self.config.has_section(supported_type):
                continue
            else:
                plugins_dict[supported_type] = {}

            for plugin_name in self.config.options(supported_type):
                plugin_desc = self.PLUGINS_DICT[plugin_name].description
                plugins_dict[supported_type][plugin_name] = plugin_desc

        if plugins_type:
            return plugins_dict.get(plugins_type, {})

        return plugins_dict
예제 #2
0
    def add_plugin(self,
                   plugin_source,
                   rev=None,
                   plugins_registry=None,
                   plugin_src_path=None):
        """Adds (install) a plugin

        :param plugin_source: Plugin source.
          Can be:
            1. Plugin name (from available in registry)
            2. Path to a local directory
            3. Git URL
        :param rev: git branch/tag/revision
        :param plugins_registry: content of plugin registry dictionary
        :param plugin_src_path: relative path to the plugin location inside the
               source
        """
        plugins_registry = plugins_registry or PLUGINS_REGISTRY
        plugin_data = {}
        # Check if a plugin is in the registry
        if plugin_source in plugins_registry:
            plugin_data = plugins_registry[plugin_source]
            plugin_source = plugins_registry[plugin_source]['src']

        if plugin_src_path is None:
            plugin_src_path = plugin_data.get('src_path', '')

        # Local dir plugin
        if os.path.exists(plugin_source):
            rm_source = False
        # Git Plugin
        else:
            if rev is None:
                rev = plugin_data.get('rev')
            plugin_source = \
                self._clone_git_plugin(plugin_source, rev)
            rm_source = True

        plugin = InfraredPlugin(os.path.join(plugin_source, plugin_src_path))
        plugin_type = plugin.type

        if plugin_type not in self.supported_plugin_types:
            raise IRUnsupportedPluginType(plugin_type)

        if not self.config.has_section(plugin_type):
            self.config.add_section(plugin_type)
        elif self.config.has_option(plugin_type, plugin.name):
            raise IRPluginExistsException(
                "Plugin with the same name & type already exists")

        dest = os.path.join(self.plugins_dir, plugin.name)
        if os.path.abspath(plugin_source) != os.path.abspath(dest):
            # copy only if plugin was added from a location which is
            # different from the location of the plugins dir
            if os.path.islink(dest):
                LOG.debug(
                    "%s found as symlink pointing to %s, "
                    "unlinking it, not touching the target.", dest,
                    os.path.realpath(dest))
                os.unlink(dest)
            elif os.path.exists(dest):
                shutil.rmtree(dest)

            shutil.copytree(plugin_source, dest)

        if rm_source:
            shutil.rmtree(plugin_source)

        self.config.set(plugin_type, plugin.name,
                        os.path.join(dest, plugin_src_path))

        with open(self.config_file, 'w') as fp:
            self.config.write(fp)

        self._install_requirements(dest)
        self._load_plugins()
예제 #3
0
    def add_plugin(self,
                   plugin_source,
                   rev=None,
                   plugins_registry=None,
                   plugin_src_path=None,
                   skip_roles=False,
                   link_roles=False):
        """Adds (install) a plugin

        :param plugin_source: Plugin source.
          Can be:
            1. Plugin name (from available in registry)
            2. Path to a local directory
            3. Git URL
        :param rev: git branch/tag/revision
        :param plugins_registry: content of plugin registry dictionary
        :param plugin_src_path: relative path to the plugin location inside the
               source
        :param skip_roles: Skip the from file roles installation
        :param link_roles: Auto create symlink from {plugin_src_path}/roles to
               the 'roles' directory inside the plugin root dir or to the
               plugin root dir itself
        """
        plugins_registry = plugins_registry or PLUGINS_REGISTRY
        plugin_data = {}
        # Check if a plugin is in the registry
        if plugin_source in plugins_registry:
            plugin_data = plugins_registry[plugin_source]
            plugin_source = plugins_registry[plugin_source]['src']

        if plugin_src_path is None:
            plugin_src_path = plugin_data.get('src_path', '')

        _link_roles = link_roles or bool(
            strtobool(plugin_data.get('link_roles', 'false')))

        # Local dir plugin
        if os.path.exists(plugin_source):
            rm_source = False
        # Git Plugin
        else:
            if rev is None:
                rev = plugin_data.get('rev')
            plugin_source = \
                self._clone_git_plugin(plugin_source, rev)
            rm_source = True

        plugin = InfraredPlugin(os.path.join(plugin_source, plugin_src_path))
        plugin_type = plugin.type

        if plugin_type not in self.supported_plugin_types:
            raise IRUnsupportedPluginType(plugin_type)

        if not self.config.has_section(plugin_type):
            self.config.add_section(plugin_type)
        elif self.config.has_option(plugin_type, plugin.name):
            raise IRPluginExistsException(
                "Plugin with the same name & type already exists")

        dest = os.path.join(self.plugins_dir, plugin.name)
        if os.path.abspath(plugin_source) != os.path.abspath(dest):
            # copy only if plugin was added from a location which is
            # different from the location of the plugins dir
            if os.path.islink(dest):
                LOG.debug(
                    "%s found as symlink pointing to %s, "
                    "unlinking it, not touching the target.", dest,
                    os.path.realpath(dest))
                os.unlink(dest)
            elif os.path.exists(dest):
                shutil.rmtree(dest)

            shutil.copytree(plugin_source, dest)

        if rm_source:
            shutil.rmtree(plugin_source)

        if _link_roles:
            if not plugin_src_path:
                raise IRFailedToAddPlugin(
                    "'--src-path' is required with '--link-roles'")

            roles_dir = os.path.join(dest, plugin_src_path, 'roles')
            if os.path.exists(roles_dir):
                raise IRFailedToAddPlugin(
                    "Can't create a symbolic link to a 'roles' directory in "
                    "'{roles_dir}', because one is already exists".format(
                        roles_dir=roles_dir))

            src_dir = dest
            if os.path.exists(dest + '/roles'):
                if not os.path.isdir(dest + '/roles'):
                    raise IRFailedToAddPlugin(
                        "The plugin directory ('{dest}') contains"
                        "a 'roles' file that can't be sym-linked".format(
                            dest=dest))
                src_dir += '/roles'

            os.mkdir(roles_dir)
            dest_dir = roles_dir + '/' + dest.split('/')[-1]
            os.symlink(src_dir, dest_dir)

        self.config.set(plugin_type, plugin.name,
                        os.path.join(dest, plugin_src_path))

        with open(self.config_file, 'w') as fp:
            self.config.write(fp)

        self._install_requirements(dest)
        if not skip_roles:
            # roles are skipped only in infrared's internal tests
            # param for this is not exposed in cli/spec files
            self._install_roles_from_file(dest)
        self._load_plugins()