Commit e884e98b authored by Antoine Millet's avatar Antoine Millet
Browse files

Implemented plugins management

parent de7cbf63
Loading
Loading
Loading
Loading
+129 −0
Original line number Diff line number Diff line
@@ -684,6 +684,135 @@ class CliHandler(RegisteredCCHandler):
                    errs.success(obj['id'], 'ok.', jobs=job_id)
        return errs.get_dict()

#
# Plugins management:
#

    @listed
    def loadplugin(self, name):
        """ Get the content of a plugin stored ont he server.

        :param name: name of the plugin to load
        """
        self.client.check('plugin')
        return self.server.plugins.load(name, empty_if_missing=True)[1]

    @listed
    def saveplugin(self, name, content):
        """ Save a plugin on the server.

        :param name: name of the plugin to save
        :param content: content of the plugin to save
        """
        self.client.check('plugin')
        return self.server.plugins.save(name, content)

    @listed
    def delplugin(self, query):
        """ Delete a plugin on the server.

        :param query: the tql query used to select plugins to delete
        """
        objects = self.client.list(query, show=('r', 'name'), method='plugin')
        errs = Reporter()
        for obj in objects:
            if obj['r'] != 'plugin':
                errs.error(obj['id'], 'not a plugin')
            else:
                try:
                    self.server.plugins.delete(obj['name'])
                except RepositoryOperationError as err:
                    errs.error(obj['id'], 'error: %s' % err)
                else:
                    errs.success(obj['id'], 'plugin deleted')

        return errs.get_dict()

    @listed
    def installplugin(self, query, plugin):
        """ Install a plugin on matching nodes.
        """

        # Load the plugin:
        sha1_hash, _ = self.server.plugins.load(plugin)

        objects = self.client.list(query, show=('r', ), method='plugin')
        errs = Reporter()
        for obj in objects:
            if obj['r'] not in ('host', 'hv'):
                errs.error(obj['id'], 'not a host')
            else:
                try:
                    node = self.server.get_client(obj['id'])
                except KeyError:
                    errs.error(obj['id'], 'node not connected')
                    continue

                try:
                    node.plugin_install(sha1_hash, plugin)
                except RpcError as err:
                    errs.error(obj['id'], '%s (exc: %s)' % (err.message,
                                                            err.exception))
                else:
                    errs.success(obj['id'], 'plugin installed')
        return errs.get_dict()

    @listed
    def uninstallplugin(self, query, plugin):
        """ Install a plugin on matching nodes.
        """

        objects = self.client.list(query, show=('r', ), method='plugin')
        errs = Reporter()
        for obj in objects:
            if obj['r'] not in ('host', 'hv'):
                errs.error(obj['id'], 'not a host')
            else:
                try:
                    node = self.server.get_client(obj['id'])
                except KeyError:
                    errs.error(obj['id'], 'node not connected')
                    continue

                try:
                    node.plugin_uninstall(plugin)
                except RpcError as err:
                    errs.error(obj['id'], '%s (exc: %s)' % (err.message,
                                                            err.exception))
                else:
                    errs.success(obj['id'], 'plugin uninstalled')
        return errs.get_dict()

    @listed
    def runplugin(self, query, plugin, method, **kwargs):
        """ Execute a plugin method on matching nodes.
        """

        # Load the plugin:
        sha1_hash, _ = self.server.plugins.load(plugin)

        objects = self.client.list(query, show=('r', ), method='plugin')
        errs = Reporter()
        for obj in objects:
            if obj['r'] not in ('host', 'hv'):
                errs.error(obj['id'], 'not a host')
            else:
                try:
                    node = self.server.get_client(obj['id'])
                except KeyError:
                    errs.error(obj['id'], 'node not connected')
                    continue

                try:
                    job_id = node.plugin_run(plugin, method, self.client.login,
                                             **kwargs)
                except RpcError as err:
                    errs.error(obj['id'], '%s (exc: %s)' % (err.message,
                                                            err.exception))
                else:
                    job_id = '.'.join((obj['id'], job_id))
                    errs.success(obj['id'], 'ok.', jobs=job_id)
        return errs.get_dict()

    #
    # Election / Migration / Cloning:
+23 −0
Original line number Diff line number Diff line
@@ -13,6 +13,14 @@ class HostHandler(RegisteredCCHandler):
        """
        return self.server.scripts.load(name)

    def plugin_get(self, name):
        """ Get a plugin by its name.

        :param name: name of the plugin to get
        """
        return self.server.plugins.load(name)


class HostClient(Client):

    """ A host client connected to the cc-server.
@@ -66,5 +74,20 @@ class HostClient(Client):
        """
        return self.conn.call('script_run', sha1_hash, script, *args)

    def plugin_run(self, sha1_hash, plugin, method, **kwargs):
        """ Run a plugin method on the host.
        """
        return self.conn.call('plugin_run', sha1_hash, plugin, method, **kwargs)

    def plugin_install(self, sha1_hash, plugin):
        """ Install a plugin on the host.
        """
        return self.conn.call('plugin_install', sha1_hash, plugin)

    def plugin_uninstall(self, plugin):
        """ Uninstall a plugin on the host.
        """
        return self.conn.call('plugin_uninstall', plugin)


Client.register_client_class(HostClient)
+7 −0
Original line number Diff line number Diff line
@@ -101,6 +101,13 @@ class CCServer(object):
        self.scripts = Repository(self.logger.getChild('scripts'), self,
                                  scripts_directory, role='script')

        # Plugin repository:
        plugins_directory = os.path.join(conf_dir, 'plugins')
        if not os.path.isdir(plugins_directory):
            os.mkdir(plugins_directory)
        self.plugins = Repository(self.logger.getChild('plugins'), self,
                                  plugins_directory, role='plugin')

    def _update_accounts(self):
        """ Update the database with accounts.
        """