Example #1
0
    def uninstallPlugin(self, plugin_name=None, plugin_pk=None, dev_mode=False):
        if plugin_pk:
            plugin_list = Plugin.objects(pk=plugin_pk)
        else:
            plugin_list = Plugin.objects(name=plugin_name)
        if not plugin_list:
            return {'status': 'fail', 'log': unicode(_('Plugin not installed'))}
        else:
            for plugin in plugin_list:
                if not dev_mode:
                    pip.main(['uninstall', '--quiet', 'lisa-plugin-' + plugin_name])
                plugin.delete()
                for cron in Cron.objects(plugin=plugin):
                    cron.delete()
                for rule in Rule.objects(plugin=plugin):
                    rule.delete()
                intent_list = Intent.objects(plugin=plugin)
                for oIntent in intent_list:
                    oIntent.delete()

            return {'status': 'success', 'log': unicode(_('Plugin uninstalled'))}
Example #2
0
    def enablePlugin(self, plugin_name=None, plugin_pk=None):
        if plugin_pk:
            plugin_list = Plugin.objects(pk=plugin_pk)
        else:
            plugin_list = Plugin.objects(name=plugin_name)
        for plugin in plugin_list:
            if plugin.enabled:
                return {'status': 'fail', 'log': unicode(_('Plugin already enabled'))}
            else:
                plugin.enabled = True
                plugin.save()
                for cron in Cron.objects(plugin=plugin):
                    cron.enabled = True
                    cron.save()
                for rule in Rule.objects(plugin=plugin):
                    rule.enabled = True
                    rule.save()

                intent_list = Intent.objects(plugin=plugin)
                for oIntent in intent_list:
                    oIntent.enabled = True
                    oIntent.save()
                return {'status': 'success', 'log': unicode(_('Plugin enabled'))}
Example #3
0
    def disablePlugin(self, plugin_name=None, plugin_pk=None):
        if plugin_pk:
            plugin_list = Plugin.objects(pk=plugin_pk)
        else:
            plugin_list = Plugin.objects(name=plugin_name)
        for plugin in plugin_list:
            if not plugin.enabled:
                return {'status': 'fail', 'log': 'Plugin already disabled'}
            else:
                plugin.enabled = False
                plugin.save()
                for cron in Cron.objects(plugin=plugin):
                    cron.enabled = False
                    cron.save()
                for rule in Rule.objects(plugin=plugin):
                    rule.enabled = False
                    rule.save()

                intent_list = Intent.objects(plugin=plugin)
                for oIntent in intent_list:
                    oIntent.enabled = False
                    oIntent.save()

                return {'status': 'success', 'log': 'Plugin disabled'}
Example #4
0
    def installPlugin(self, plugin_name=None, test_mode=False, dev_mode=False, version=None):
        version_str = ""
        if Plugin.objects(name=plugin_name):
            return {'status': 'fail', 'log': unicode(_('Plugin already installed'))}

        if version:
            version_str = ''.join(["==", version])
        if not dev_mode:
            # This test mode is here only for travis to allow installing plugin in a readable directory
            if test_mode:
                pip.main(['install', '--quiet', '--install-option=--install-platlib=' + os.getcwd() + '/../',
                          '--install-option=--install-purelib=' + os.getcwd() + '/../', 'lisa-plugin-' +
                                                                                        plugin_name + version_str])
            else:
                pip.main(['install', 'lisa-plugin-' + plugin_name + version_str])
        jsonfile = self.pkgpath + '/' + plugin_name + '/' + plugin_name.lower() + '.json'
        metadata = json.load(open(jsonfile))

        plugin = Plugin()
        description_list = []
        for item in metadata:
            if item != 'crons' and item != 'rules':
                if item == 'description':
                    for description in metadata[item]:
                        oDescription = Description()
                        for k,v in description.iteritems():
                            setattr(oDescription, k, v)
                        description_list.append(oDescription)
                    setattr(plugin, item, description_list)
                elif item == 'enabled':
                    if metadata[item] == 0:
                        setattr(plugin, item, False)
                    else:
                        setattr(plugin, item, True)
                else:
                    setattr(plugin, item, metadata[item])
        plugin.save()

        for item in metadata:
            if item == 'rules':
                for rule_item in metadata['rules']:
                    rule = Rule()
                    for parameter in rule_item:
                        if parameter == 'enabled':
                            if rule_item[parameter] == 0:
                                setattr(rule, parameter, False)
                            else:
                                setattr(rule, parameter, True)
                        else:
                            setattr(rule, parameter, rule_item[parameter])
                    rule.plugin = plugin
                    rule.save()
            if item == 'crons':
                for cron_item in metadata['crons']:
                    cron = Cron()
                    for parameter in cron_item:
                        if parameter == 'enabled':
                            if cron_item[parameter] == 0:
                                setattr(cron, parameter, False)
                            else:
                                setattr(cron, parameter, True)
                        else:
                            setattr(cron, parameter, cron_item[parameter])
                    cron.plugin = plugin
                    cron.save()

        for intent, value in metadata['configuration']['intents'].iteritems():
            oIntent = Intent()
            oIntent.name = intent
            oIntent.function = value['method']
            oIntent.module = '.'.join(['lisa.plugins', plugin_name, 'modules', plugin_name.lower(),
                                       plugin_name])
            oIntent.enabled = True
            oIntent.plugin = plugin
            oIntent.save()
        return {'status': 'success', 'log': unicode(_('Plugin installed'))}
Example #5
0
    def upgradePlugin(self, plugin_name=None, plugin_pk=None, test_mode=False, dev_mode=False):
        if plugin_pk:
            plugin_list = Plugin.objects(pk=plugin_pk)
        else:
            plugin_list = Plugin.objects(name=plugin_name)

        if not plugin_list:
            return {'status': 'fail', 'log': unicode(_('Plugin not installed'))}

        if not dev_mode:
            # This test mode is here only for travis to allow installing plugin in a readable directory
            if test_mode:
                pip.main(['install', '--quiet', '--install-option=--install-platlib=' + os.getcwd() + '/../',
                              '--install-option=--install-purelib=' + os.getcwd() + '/../', 'lisa-plugin-' +
                                                                                            plugin_name, '--upgrade'])
            else:
                pip.main(['install', 'lisa-plugin-' + plugin_name, '--upgrade'])

        jsonfile = self.pkgpath + '/' + plugin_name + '/' + plugin_name.lower() + '.json'
        try:
            metadata = json.load(open(jsonfile))
        except:
            return {'status': 'fail', 'log': unicode(_("The json of the plugin can't be loaded"))}

        for plugin in plugin_list:
            if self.versioncompare(version1=metadata['version'], version2=plugin.version) > 0:
                description_list = []
                for item in metadata:
                    if item != 'crons' and item != 'rules':
                        if item == 'description':
                            # Does we really need to update description object ?
                            pass
                        elif item == 'configuration':
                            #TODO Shouldn't override the configuration of the user
                            # But we should add only the missing entries (only adding)
                            pass
                        elif item == 'enabled':
                            # Shouldn't override the choice of the user
                            pass
                        else:
                            setattr(plugin, item, metadata[item])
                plugin.save()

                for item in metadata:
                    if item == 'rules':
                        for rule_item in metadata['rules']:
                            oRule_list = Rule.objects(name=rule_item['name'])
                            for rule in oRule_list:
                                for parameter in rule_item:
                                    if parameter == 'enabled':
                                        if rule_item[parameter] == 0:
                                            setattr(rule, parameter, False)
                                        else:
                                            setattr(rule, parameter, True)
                                    else:
                                        setattr(rule, parameter, rule_item[parameter])
                                rule.plugin = plugin
                                rule.save()

                    if item == 'crons':
                        for cron_item in metadata['crons']:
                            oCron_list = Cron.objects(name=cron_item['name'])
                            for cron in oCron_list:
                                for parameter in cron_item:
                                    if parameter == 'enabled':
                                        if cron_item[parameter] == 0:
                                            setattr(cron, parameter, False)
                                        else:
                                            setattr(cron, parameter, True)
                                    else:
                                        setattr(cron, parameter, cron_item[parameter])
                                cron.plugin = plugin
                                cron.save()

                for intent, value in metadata['configuration']['intents'].iteritems():
                    oIntent_list = Intent.objects(name=intent)
                    for oIntent in oIntent_list:
                        oIntent.name = intent
                        oIntent.function = value['method']
                        oIntent.module = '.'.join(['lisa.plugins', plugin_name, 'modules', plugin_name.lower(),
                                                   plugin_name])
                        oIntent.enabled = True
                        oIntent.plugin = plugin
                        oIntent.save()
            else:
                return {'status': 'fail', 'log': unicode(_('Plugin already up to date'))}
        return {'status': 'success', 'log': unicode(_('Plugin upgraded'))}