Here you will find the Func mysql Module 0.0.2 : diff mysql.py mysql.py.17Mar2009.bkp 35a36 > from certmaster.config import read_config 37a39 > from func.minion import sub_process 42,43c44,45 < version = "0.0.2" < api_version = "0.0.2" --- > version = "0.0.1" > api_version = "0.0.1"
BR Fred On Mon, Mar 16, 2009 at 8:31 AM, Frederic Hornain <[email protected]>wrote: > Adrian, > > As soon as I have an hour today, I will create a script in order to have > validate test cases. > Keep you in touch. > > BR > Frederic > > > On Fri, Mar 13, 2009 at 2:59 AM, Adrian LIkins <[email protected]> wrote: > >> Frederic Hornain wrote: >> >>> Dear *, >>> >>> Here you will find enclosed my Func MySQL Module. OK it could be better >>> and smart coded but indeed I am not basically a developer. >>> So Thanks in advance for you indulgence. >>> >>> The aim of that module is to interact with MySQL databases on minions. >>> For my part I am going to use it to save retrieve data from minions and save >>> it on the database. >>> I am going to develop another module to do that. >>> >> Neat. >> >> >> BTW, some functions are not working correctly for the moment due to minor >>> problems mentionned in my previous post : "How to save options and options >>> in config" >>> FYI, Adian is currently working on it >>> >> Yup, that needs to be fixed. I got distracted today and didn't finish >> tracking it down. Somewhere, the module conf >> is properly loading the contents of the moudle config file, and using the >> defaults from the module instead. >> >> The function which are working for the moment. >>> func "localhost.localdomain" call mysql query "show tables;" >>> func "localhost.localdomain" call mysql query "create table t (col >>> char(1));" >>> func "localhost.localdomain" call mysql simple_query >>> func "*" call mysql show_config >>> >>> Let's hope it is enough as module description. >>> >>> Let's go to the configuration file : >>> /etc/func/modules/Mysql.conf >>> >>> ////////////////////////////////////////////////////////////////////////////// >>> [main] >>> password = >>> user = root >>> server = localhost >>> database = test >>> >>> Rem1 : it use default MySQL credential. Do not remember if the database >>> test is created at the package installation. If not use the "create database >>> test;" >>> Rem2 : For non MySQL DBA -> >>> >>> * yum install mysql >>> * service mysqld start >>> * mysql -u root >>> * mysql promt > show databases >>> * - if no test database >>> * mysql promt > create database test; >>> * mysql promt > quit >>> >>> >>> Let's go to the code : >>> /usr/lib/python2.5/site-packages/func/minion/modules/mysql.py >>> >>> /////////////////////////////////////////////////////////////////////////////////////////////// >>> >>> # >>> # Copyright 2009, Frederic Hornain >>> # Frederic Hornain <[email protected] <mailto:[email protected]>> >>> # >>> # This program is free software; you can redistribute it and/or modify >>> # it under the terms of the GNU General Public License as published by >>> # the Free Software Foundation; either version 2 of the License, or >>> # (at your option) any later version. >>> # >>> # This program is distributed in the hope that it will be useful, >>> # but WITHOUT ANY WARRANTY; without even the implied warranty of >>> # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >>> # GNU General Public License for more details. >>> # >>> # You should have received a copy of the GNU General Public License >>> # along with this program; if not, write to the Free Software >>> # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA >>> # 02110-1301 USA >>> >>> """ >>> Func MySQL SQL statement module >>> """ >>> >>> __author__ = "Frederic Hornain <[email protected] <mailto: >>> [email protected]>>" >>> >>> __version__ = "0.0.1" >>> __api_version__ = "0.0.1" >>> __description__ = "Func MySQL SQL statement module" >>> __creation_date__ = "03/12/2009" >>> >>> import func_module >>> import time >>> import sys >>> import service >>> import MySQLdb >>> import exceptions >>> from certmaster.config import read_config >>> from func.commonconfig import FuncdConfig >>> from certmaster.config import BaseConfig, Option, IntOption, FloatOption, >>> BoolOption >>> from func.minion import sub_process >>> >>> class Mysql(func_module.FuncModule): >>> >>> # Update these if need be. >>> version = "0.0.1" >>> api_version = "0.0.1" >>> description = "Func MySQL SQL statement module" >>> cursor = "" >>> connection = "" >>> >>> class Config(BaseConfig): >>> user = Option('') >>> password = Option('') >>> server = Option('') >>> database = Option('') >>> >>> def __mysql_error_code(self,rc): >>> if rc == 0: >>> result = "No Error" >>> elif rc == 1: >>> result = "No MySQL deamon is running on the default MySQL >>> port" >>> elif rc == 2: >>> result = "No MySQL user, password, server, database have been >>> set in /etc/func/modules/Mysql.conf" >>> elif rc == 3: >>> result = "Opening MySQL Connection Failed" >>> elif rc == 4: >>> result = "SQL Statement Failed" >>> elif rc == 5: >>> result = "Closing MySQL Cursor Failed" >>> elif rc == 6: >>> result = "Closing MySQL Connection Failed" >>> elif rc > 6: >>> result = "Unknown Error" >>> return result >>> >>> def __mysql_config(self): >>> if self.options.user == '' or self.options.server == '' or >>> self.options.database == '': >>> return 2 >>> else: >>> return 0 >>> >>> def __mysql_service_status(self): >>> mysql_service = service.Service() >>> deamon_status = mysql_service.status("mysqld") >>> if deamon_status == 0: >>> return 0 >>> else: >>> return 1 >>> >>> def __mysql_open_connection(self,rc): >>> if rc == 0: >>> rc = self.__mysql_service_status() >>> if rc == 0: >>> rc = self.__mysql_config() >>> if rc == 0: >>> try: >>> self.connection = MySQLdb.connect (host = >>> self.options.server, >>> user = self.options.user, >>> passwd = self.options.password, >>> db = self.options.database) >>> except MySQLdb.Error, e: >>> rc = 3 >>> return rc >>> >>> def __mysql_query(self,query,rc): >>> h = 0 >>> i = 0 >>> k = 0 >>> records = [] >>> try: >>> self.cursor = self.connection.cursor() >>> self.cursor.execute(query) >>> while (1): >>> row = self.cursor.fetchone() >>> if row == None: >>> break >>> elementnumber=len(row) >>> k = elementnumber-1 >>> for i in range(elementnumber): >>> if row[i] is None: >>> if i == h : >>> followingelements = row[:k] >>> row = ('none',) + followingelements >>> elif i > h and i < k: >>> previouselements = row[:i] >>> followingelements = row[i:k] >>> row = previouselements + ('none',) + >>> followingelements >>> elif i == k: >>> previouselements = row[:i] >>> row = previouselements + ('none',) >>> records.append(row) >>> except MySQLdb.Error, e: >>> rc = 4 >>> return (records,rc) >>> >>> def __mysql_close_cursor(self,rc): >>> try: >>> self.cursor.close() >>> except MySQLdb.Error, e: >>> rc = 5 return rc >>> >>> def __mysql_close_connection(self,rc): >>> try: >>> self.connection.close() >>> except MySQLdb.Error, e: >>> rc = 6 >>> return rc >>> >>> def show_config(self): >>> """ >>> Returns the options config >>> """ >>> return self.options >>> >>> def change_user(self,changeduser): >>> setattr(self.options,"user",changeduser) >>> self.save_config() >>> return self.options >>> >>> def change_password(self,changedpassword): >>> setattr(self.options,'password',changedpassword) >>> self.save_config() >>> return self.options >>> >>> def change_server(self,changedserver): >>> setattr(self.options,'server',changedserver) >>> self.save_config() >>> return self.options >>> >>> def change_database(self,changeddatabase): >>> setattr(self.options,'database',changeddatabase) >>> self.options.database = changeddatabase >>> self.database = self.options.database >>> self.Config.database = Option(changeddatabase) >>> self.save_config() >>> return self.options >>> >>> def return_database(self): >>> return getattr(self.options, 'database') >>> >>> def clear_config(self): >>> self.options.user = "root" >>> self.options.password = "" >>> self.options.server = "localhost" >>> self.options.database = "test" >>> self.save_config() >>> return self.options >>> >>> def simple_query(self): >>> rc = 0 >>> query="SELECT VERSION();" >>> rc = self.__mysql_open_connection(rc) >>> if rc == 0: >>> result = self.__mysql_query(query,rc) >>> if result[1] == 0: >>> rc = self.__mysql_close_cursor(rc) >>> else: >>> simplequeryresult = self.__mysql_error_code(rc) >>> if rc == 0: >>> rc = self.__mysql_close_connection(rc) if >>> rc == 0: >>> simplequeryresult=result[0] >>> else: >>> simplequeryresult = self.__mysql_error_code(rc) >>> else: >>> simplequeryresult = self.__mysql_error_code(rc) >>> else: >>> simplequeryresult = self.__mysql_error_code(rc) >>> return simplequeryresult >>> def query(self,query): >>> rc = 0 >>> rc = self.__mysql_open_connection(rc) >>> if rc == 0: >>> results = self.__mysql_query(query,rc) >>> if results[1] == 0: >>> rc = self.__mysql_close_cursor(rc) >>> else: >>> queryresults = self.__mysql_error_code(rc) >>> if rc == 0: >>> rc = self.__mysql_close_connection(rc) >>> if rc == 0: >>> queryresults=results[0] >>> else: >>> queryresults = self.__mysql_error_code(rc) >>> else: >>> queryresults = self.__mysql_error_code(rc) >>> else: >>> queryresults = self.__mysql_error_code(rc) >>> return queryresults >>> >>> >>> //////////////////////////////////////////////////////////////////// >>> EOF >>> >>> Feel free to send me your comment. >>> >> >> Module looks good, though I haven't tested it yet. Do you have any test >> cases or unit tests for it? (I know it's kind >> of a pain to test since it needs test mysql running...). But I wouldn't >> mind something to put in the unittests, even if >> it is kind of a "run at your own risk, this will create/destroy >> dbs/tables" thing. >> >> Adrian >> >> > > > -- > ----------------------------------------------------- > Fedora-ambassadors-list mailing list > [email protected] > Olpc mailing list > [email protected] > -- ----------------------------------------------------- Fedora-ambassadors-list mailing list [email protected] Olpc mailing list [email protected]
mysql.py
Description: Binary data
_______________________________________________ Func-list mailing list [email protected] https://www.redhat.com/mailman/listinfo/func-list
