Func MySQL Module

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

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

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 ->

Let's go to the code :
/usr/lib/python2.5/site-packages/func/minion/modules/mysql.py
///////////////////////////////////////////////////////////////////////////////////////////////

#
# Copyright 2009, Frederic Hornain
# Frederic Hornain <fhornain@xxxxxxxxx>
#
# 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 <fhornain@xxxxxxxxx>"
__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.

BR
Fred
 
-----------------------------------------------------
Fedora-ambassadors-list mailing list
Fedora-ambassadors-list@xxxxxxxxxx

Attachment: mysql.py
Description: Binary data

Attachment: Mysql.conf
Description: Binary data

_______________________________________________
Func-list mailing list
Func-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/func-list

[Index of Archives]     [Fedora Users]     [Linux Networking]     [Fedora Legacy List]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux