Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from SimpleXMLRPCServer import SimpleXMLRPCServer as Server
- import os
- import sys
- import logger
- import pidder
- import time
- import signal
- import pgsql
- import ConfigParser
- #
- version="1.1"
- #
- zeit=time.strftime("%H:%M:%S")
- datum=time.strftime("%d.%m.%Y")
- myhomepath=os.getenv("OFFICECRAWLER_HOME")
- logpath=myhomepath+"/logs/"
- tmppath=myhomepath+"/tmp/"
- configfile=myhomepath+"/conf/dbci.conf"
- pid=pidder.pidder(tmppath+"dbci.pid")
- log=logger.log(logpath+"dbci.log")
- #
- def sig_hand(signum, frame):
- log.add("SIGKILL - Stop DBCI with init-script!")
- log.add("Shutting Down...")
- os.kill(pid.mypid())
- return 0
- signal.signal(signal.SIGTERM, sig_hand)
- #
- pid.mkpid()
- log.add("Starting DBCI")
- ###
- class db(object):
- def __init__(self, configfile):
- try:
- self.configfile = configfile
- config=ConfigParser.ConfigParser()
- config.read(self.configfile)
- location = config.get("database", "location")
- databasename = config.get("database", "databasename")
- user = config.get("database", "user")
- password = config.get("database", "password")
- self.con = pgsql.connect(database=databasename, host=location, user=user, password=password)
- self.cur = self.con.cursor()
- log.add("Database init complete!")
- except:
- log.add("!CRITICAL! Can't connect to database. Exit")
- os.remove(myhomepath+"/tmp/dbci.pid")
- exit()
- def about(self):
- return "DBCI - DataBase Communication Interfacei - Version: %s" % version
- def add_element(self, hash, inv, last_message, onoff, passphrase, memberofgroup, nocrawlerok, ip, nocrawler):
- try:
- self.cur.execute("INSERT INTO elements (hash, inv, last_message, onoff, passphrase, memberofgroup, nocrawlerok, ip, nocrawler) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",(hash, inv, last_message, onoff, passphrase, memberofgroup, nocrawlerok, ip, nocrawler))
- self.con.commit()
- try:
- self.cur.execute("CREATE TABLE dict_%s ( id serial, key text, value text, topic text );" % (hash))
- self.con.commit()
- except:
- log.add("TABLE %s is already there..." % (hash))
- log.add("Add element: " + hash+","+inv+","+last_message+","+onoff+","+passphrase+","+memberofgroup+","+nocrawlerok+","+ip+","+nocrawler)
- return "ok"
- except:
- self.con.rollback()
- log.add("Error while adding element: " + hash+","+inv+","+last_message+","+onoff+","+passphrase+","+memberofgroup+","+nocrawlerok+","+ip+","+nocrawler)
- # return "error"
- def list_all_elements(self):
- try:
- self.cur.execute("select * from elements;")
- list=self.cur.fetchall()
- log.add("List of all elements requested")
- return list
- except:
- self.con.rollback()
- log.add("Error while list all elements!!")
- return "error"
- def list_all_keys_of_element(self, hash):
- try:
- self.cur.execute("SELECT * FROM dict_%s" % (hash))
- list=self.cur.fetchall()
- log.add("List of all Keys for %s requested" % (hash))
- return list
- except:
- self.con.rollback()
- log.add("Error while list Key's of %s!!!" % (hash))
- return "error"
- def add_key_to_element(self, hash, key, value, topic):
- try:
- self.cur.execute("INSERT INTO %s (key, value, topic) VALUES ($1, $2, $3)" % (hash), (key, value, topic))
- self.con.commit()
- log.add("Add Key %s Value %s to %s. Topic: %s" % (key, value, hash, topic))
- return "ok"
- except:
- self.con.rollback()
- log.add("Error while adding Key to Hash: %s. (%s: %s)" % (hash, key, value))
- return "error"
- def delete_key_from_element(self, hash, key):
- try:
- self.cur.execute("DELETE FROM %s where key=$1" % (hash), (key,))
- self.con.commit()
- log.add("Key %s from Hash %s deleted" % (key, hash))
- return "ok"
- except:
- self.con.rollback()
- log.add("Error while deleting Key %s from Hash %s" % (key, hash))
- return "error"
- def delete_element_by_hash(self, hash):
- try:
- self.cur.execute("DELETE FROM elements where hash=$1", (hash,))
- self.con.commit()
- log.add("Hash %s has been deleted" % (hash))
- return "ok"
- except:
- self.con.rollback()
- log.add("Error while deleting Hash %s" % (hash))
- return "error"
- def help(self):
- try:
- help="\n\nDBCI ----- HELP\n\n\nCommands:\n\n__init__(self, configfile)\t-\t INIT\nabout(self)\t-\tabout\nadd_element(self, hash, inv, last_message, onoff, passphrase, memberofgroup, nocrawlerok, ip, nocrawler)\t-\t Adds an Element\ndelete_element_by_hash(self, hash)\t-\tDelete Element\nlist_all_elements(self)\t-\tLists all Elements\nadd_key_to_element(self, hash, key, value, topic)\t-\tAdds a Key to Element\ndelete_key_from_element(self, hash, key)\t-\tDelete Key from Element\nlist_all_keys_of_element(self, hash)\t-\tLists all Keys of Element\n\n\n."
- return help
- except:
- return "error"
- dbci=db(configfile)
- ###
- try:
- log.add("Load RPC-Server config")
- rpcconfig=ConfigParser.ConfigParser()
- rpcconfig.read(configfile)
- rpcserverip = rpcconfig.get("rpc", "serverip")
- rpcserverport = rpcconfig.get("rpc", "serverport")
- srv = Server((rpcserverip, int(rpcserverport)), allow_none=1)
- log.add("RPC-Server initialization complete")
- except:
- log.add("RPC-Server initialization Error")
- #register functions:
- try:
- srv.register_function(dbci.about)
- srv.register_function(dbci.add_element)
- srv.register_function(dbci.list_all_elements)
- srv.register_function(dbci.list_all_keys_of_element)
- srv.register_function(dbci.add_key_to_element)
- srv.register_function(dbci.delete_key_from_element)
- srv.register_function(dbci.delete_element_by_hash)
- srv.register_function(dbci.help)
- log.add("Functions registered")
- except:
- log.add("Error while registering functions!")
- log.add("Starting RPC-Server...")
- try:
- srv.serve_forever()
- except:
- log.add("CRITICAL: Can't start RPC-Server!!")
- log.add("Stopping DBCI")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement