def cmd_mysql_exec(*argv, **kwargs): """ Syntax mysql.exec[hostname],[user],[password],[database],[query] """ try: _mysql = __import__("MySQLdb") except ImportError: # Try to install package and try again ecm.install_package('python-mysqldb') try: _mysql = __import__("MySQLdb") except: raise Exception("Unsupported MySQLdb") user = kwargs.get('user', 'root') password = kwargs.get('password', '') database = kwargs.get('database', '') host = kwargs.get('host', 'localhost') query = kwargs.get('query', 'SELECT VERSION()') default_file = kwargs.get('default_file', '') if default_file == '/etc/mysql/debian.cnf': user = '******' try: if default_file: conn = _mysql.connect(host=host, user=user, db=database, read_default_file=default_file) else: conn = _mysql.connect(host=host, user=user, passwd=password, db=database) except Exception as e: raise Exception("Unable to connect: %s" % e[1]) try: cursor = conn.cursor() cursor.execute(query) retval = cursor.fetchall() cursor.close() conn.close() return retval except _mysql.Error, e: print "Error %d: %s" % (e.args[0], e.args[1])
def _extract_alternative(self, filename): """ extractor helper: Try to extract file using system commands """ from shutil import move from os import path file_type = self._get_file_type(filename) # Move file before decompress move(filename, self.working_dir) filename = path.join(self.working_dir, path.basename(filename)) if file_type == 'zip': package = 'unzip' command = 'unzip' args = [filename] elif file_type == 'gz': package = 'gzip' command = 'gunzip' args = [filename] elif file_type == 'bz2': package = 'bzip2' command = 'bzip2' args = ['-d', filename] else: raise Exception("Unsupported file compression") exists = ecm.which(command) if not exists: # Try to install package ecm.install_package(package) exists = ecm.which(command) if exists and command: # Decompress out, stdout, stderr = ecm.run_command(command, args, workdir=self.working_dir) ret = {'out': out, 'stderr': stderr, 'stdout': stdout} return ret raise Exception("Could not extract file")
def cmd_packages_install(self, *argv, **kwargs): """ Install packages received in csv or in debian packages "Depends" format Syntax: packages.install[packages] """ packages_b64 = kwargs.get('packages', None) if not packages_b64: raise ecm.InvalidParameters(self.cmd_packages_install.__doc__) try: str_packages = b64decode(packages_b64) except: raise Exception("Invalid b64 received") packages = self._parse_package_string(str_packages) # Invalid package list if not packages: return False # apt-get update or yum clean on first time refresh_db = True ret = { 'out': 0, 'stdout': '', 'stderr': ''} for i in range(0, 100): packages_pending = False for pkg in packages: if not pkg[0]['installed']: packages_pending = True try: package_name = pkg[i]['name'] except KeyError: continue out, stdout, stderr = ecm.install_package(package_name, refresh_db) ret['stdout'] += stdout ret['stderr'] += stderr ret['out'] = out if not out: pkg[0]['installed'] = 1 refresh_db = False if not packages_pending: break return ret
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
def _install(self): """ try to install subversion """ ecm.install_package('subversion') return self._is_available()
def _install(self): """ Try to install git """ ecm.install_package('git') return bool(self._is_available())