def cmd_monitor_plugin_install(self, *argv, **kwargs):
        """
        Installs a plugin [url=plugin_url]
        """
        
        url = kwargs.get('url', None)
        content = None
        
        if not url:
            raise ecm.InvalidParameters(self.cmd_monitor_plugin_install.__doc__)
        
        try:
            content = ecm.get_url(url)
        except:
            pass
        
        if not content:
            raise Exception("Unable to get URL: %s" % url)
            
        try: 
            plugin = json.loads(content)
        except:
            raise Exception("Invalid data received")

        id = plugin.get('id')
        runas = plugin.get('runas')
        
        arg_config = plugin.get('config')
        arg_script_b64 = plugin.get('script')
        
        script = None
        try:
            script = b64decode(arg_script_b64)
        except:
            pass
        
        config = {
            'id': id,
            'runas': runas,
            'name': plugin.get('name'),
            'interval': plugin.get('interval'),
            'version': plugin.get('version'),
            'config': arg_config
        }
        
        if id and config and script:
            mplugin = MPlugin(MPLUGIN_PATH)
            if mplugin.install(id, config, script):
                # Installation ok, run it
                script_file = os.path.abspath(os.path.join(MPLUGIN_PATH, id, id))
                _run_background_file(script_file, runas)
            
                return True
            
        return False
Example #2
0
    def cmd_monitor_plugin_uninstall(self, *argv, **kwargs):
        """
        Uninstalls a plugin [id=plugin_id]
        """

        plugin_id = kwargs.get('id', None)
        
        if not plugin_id:
            raise ecm.InvalidParameters(self.cmd_monitor_plugin_uninstall.__doc__)
        
        mplugin = MPlugin(MPLUGIN_PATH)
        return mplugin.uninstall(plugin_id)
Example #3
0
    def cmd_monitor_get(self, *argv, **kwargs):
        """
        Runs monitor commands from monitor path
        """
        
        config = None
        b64_config = kwargs.get('config', None)
        
        try:
            config = json.loads(b64decode(b64_config))
        except:
            pass
        
        retval = []
        to_execute = []

        # Create dirs if necessary
        self._check_path(MPLUGIN_PATH)
        self._check_path(CPLUGIN_PATH)
        self._check_path(CACHE_PATH)
        
        # Clean old cache files
        self._cache_clean()

        if not os.path.isdir(MPLUGIN_PATH):
            return retval
            
        # Foreach plugin inside mplugins and custom
        for plugin_path in [MPLUGIN_PATH, CPLUGIN_PATH]:
            if not os.path.isdir(plugin_path):
                continue
                
            for p_path in os.listdir(plugin_path):
                p_path = os.path.join(plugin_path, p_path)

                # Skip disabled plugins
                if not os.path.isdir(p_path):
                    continue

                # Skip disabled plugins
                if os.path.basename(p_path).startswith('.'):
                    continue
                        
                runas = None
                scripts = []
                interval = COMMAND_INTERVAL

                # Search for plugin files
                if plugin_path == MPLUGIN_PATH:
                    mplugin = MPlugin(p_path)
                    
                    # Update config and re-read
                    if config:
                        mplugin.write_config(config.get(mplugin.id))
                        mplugin = MPlugin(p_path)
                        
                    runas = mplugin.data.get('runas', None)
                    interval = mplugin.data.get('interval', COMMAND_INTERVAL)

                    script = os.path.join(plugin_path, p_path, mplugin.id)
                        
                    if not os.path.exists(script):
                        script += '.py'
                        
                    if not os.path.exists(script):
                        continue

                    # Add as valid mplugin script
                    scripts = [script]
                                    
                    # Executable plugin base
                    if not os.access(script, os.X_OK):
                        os.chmod(script, 0755)
                                    
                # Custom plugins path
                else:
                    # Set default interval or read from path name
                    interval = self._interval_from_path(p_path)

                    for filename in os.listdir(p_path):
                        _tmp = os.path.join(plugin_path, p_path, filename)
                        if os.access(_tmp, os.X_OK):
                            scripts.append(_tmp)
                            
                for script in scripts:
                    # Read last result from cache (even if void)
                    from_cache = self._cache_read(script, interval + CACHE_SOFT_TIME)
                    retval.append(self._parse_script_name(script) + GLUE + str(interval) + GLUE + from_cache)
                    
                    # Execute script if cache wont be valid on next command_get execution
                    if not self._cache_read(script, interval - COMMAND_INTERVAL - CACHE_SOFT_TIME):
                        to_execute.append({'script': script, 'runas': runas})
                        
        for data in to_execute:
            _run_background_file(data['script'], data['runas'])

        return retval
Example #4
0
    def cmd_monitor_plugin_install(self, *argv, **kwargs):
        """
        Installs a plugin [url=plugin_url]
        """
        url = kwargs.get('url', None)
        content = None
        
        if not url:
            raise ecm.InvalidParameters(self.cmd_monitor_plugin_install.__doc__)
        
        try:
            content = ecm.get_url(url)
        except:
            pass
        
        if not content:
            raise Exception("Unable to get URL: %s" % url)
            
        try: 
            plugin = json.loads(content)
        except:
            raise Exception("Invalid data received")

        id = plugin.get('id')
        runas = plugin.get('runas')
        
        arg_config = plugin.get('config')
        arg_script_b64 = plugin.get('script')

        arg_requirements = plugin.get('requirements', None)

        if arg_requirements:
            pip_install = []
            system_install = []

            for req in arg_requirements.keys():
                if arg_requirements[req]['type'] == 'system':
                    system_install.append(arg_requirements[req]['name'])

                elif arg_requirements[req]['type'] == 'pip':
                    pip_install.append(arg_requirements[req]['name'])

            for item in system_install:
                result = ecm.install_package(item)

                if not result:
                    return False

            if not pip_import_error:
                for item in pip_install:
                    try:
                        result = pip_install_single_package(item)
                        if not result[0]:
                            return False
                    except:
                        pass

        script = None
        try:
            script = b64decode(arg_script_b64)
        except:
            pass
        
        config = {
            'id': id,
            'runas': runas,
            'name': plugin.get('name'),
            'interval': plugin.get('interval'),
            'version': plugin.get('version'),
            'config': arg_config
        }
        
        if id and config and script:
            mplugin = MPlugin(MPLUGIN_PATH)
            if mplugin.install(id, config, script):
                # Installation ok, run it
                script_file = os.path.abspath(os.path.join(MPLUGIN_PATH, id, id))
                _run_background_file(script_file, runas)
            
                return True
            
        return False