Advertisement
Guest User

Untitled

a guest
May 9th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from SimpleXMLRPCServer import SimpleXMLRPCServer as Server
  3. import os
  4. import sys
  5. import logger
  6. import pidder
  7. import time
  8. import signal
  9. import pgsql
  10. import ConfigParser
  11. #
  12. version="1.1"
  13. #
  14.  
  15. zeit=time.strftime("%H:%M:%S")
  16. datum=time.strftime("%d.%m.%Y")
  17.  
  18.  
  19. myhomepath=os.getenv("OFFICECRAWLER_HOME")
  20. logpath=myhomepath+"/logs/"
  21. tmppath=myhomepath+"/tmp/"
  22. configfile=myhomepath+"/conf/dbci.conf"
  23. pid=pidder.pidder(tmppath+"dbci.pid")
  24. log=logger.log(logpath+"dbci.log")
  25.  
  26. #
  27. def sig_hand(signum, frame):
  28.         log.add("SIGKILL - Stop DBCI with init-script!")
  29.     log.add("Shutting Down...")
  30.     os.kill(pid.mypid())
  31.     return 0
  32.  
  33. signal.signal(signal.SIGTERM, sig_hand)
  34.  
  35. #
  36.  
  37.  
  38. pid.mkpid()
  39. log.add("Starting DBCI")
  40.  
  41.  
  42. ###
  43. class db(object):
  44.  
  45.     def __init__(self, configfile):
  46.         try:
  47.             self.configfile = configfile
  48.             config=ConfigParser.ConfigParser()
  49.             config.read(self.configfile)
  50.             location = config.get("database", "location")
  51.             databasename = config.get("database", "databasename")
  52.             user = config.get("database", "user")
  53.             password = config.get("database", "password")
  54.             self.con = pgsql.connect(database=databasename, host=location, user=user, password=password)
  55.             self.cur = self.con.cursor()
  56.             log.add("Database init complete!")
  57.         except:
  58.             log.add("!CRITICAL! Can't connect to database. Exit")
  59.             os.remove(myhomepath+"/tmp/dbci.pid")
  60.             exit()
  61.  
  62.  
  63.     def about(self):
  64.         return "DBCI - DataBase Communication Interfacei - Version: %s" % version
  65.    
  66.  
  67.     def add_element(self, hash, inv, last_message, onoff, passphrase, memberofgroup, nocrawlerok, ip, nocrawler):
  68.         try:
  69.             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))
  70.             self.con.commit()
  71.             try:
  72.                 self.cur.execute("CREATE TABLE dict_%s ( id serial, key text, value text, topic text );" % (hash))
  73.                 self.con.commit()
  74.             except:
  75.                 log.add("TABLE %s is already there..." % (hash))
  76.            
  77.             log.add("Add element: " + hash+","+inv+","+last_message+","+onoff+","+passphrase+","+memberofgroup+","+nocrawlerok+","+ip+","+nocrawler)
  78.             return "ok"
  79.         except:
  80.             self.con.rollback()
  81.             log.add("Error while adding element: "  + hash+","+inv+","+last_message+","+onoff+","+passphrase+","+memberofgroup+","+nocrawlerok+","+ip+","+nocrawler)
  82. #           return "error"
  83.  
  84.  
  85.     def list_all_elements(self):
  86.         try:
  87.             self.cur.execute("select * from elements;")
  88.             list=self.cur.fetchall()
  89.             log.add("List of all elements requested")
  90.             return list
  91.         except:
  92.             self.con.rollback()
  93.             log.add("Error while list all elements!!")
  94.             return "error"
  95.  
  96.  
  97.     def list_all_keys_of_element(self, hash):
  98.         try:
  99.             self.cur.execute("SELECT * FROM dict_%s" % (hash))
  100.             list=self.cur.fetchall()
  101.             log.add("List of all Keys for %s requested" % (hash))
  102.             return list
  103.         except:
  104.             self.con.rollback()
  105.             log.add("Error while list Key's of %s!!!" % (hash))
  106.             return "error"
  107.  
  108.     def add_key_to_element(self, hash, key, value, topic):
  109.         try:
  110.             self.cur.execute("INSERT INTO %s (key, value, topic) VALUES ($1, $2, $3)" % (hash), (key, value, topic))
  111.             self.con.commit()
  112.             log.add("Add Key %s Value %s to %s. Topic: %s" % (key, value, hash, topic))
  113.             return "ok"
  114.         except:
  115.             self.con.rollback()
  116.             log.add("Error while adding Key to Hash: %s. (%s: %s)" % (hash, key, value))
  117.             return "error"
  118.  
  119.  
  120.     def delete_key_from_element(self, hash, key):
  121.         try:
  122.             self.cur.execute("DELETE FROM %s where key=$1" % (hash), (key,))
  123.             self.con.commit()
  124.             log.add("Key %s from Hash %s deleted" % (key, hash))
  125.             return "ok"
  126.         except:
  127.             self.con.rollback()
  128.             log.add("Error while deleting Key %s from Hash %s" % (key, hash))
  129.             return "error"
  130.  
  131.  
  132.     def delete_element_by_hash(self, hash):
  133.         try:
  134.             self.cur.execute("DELETE FROM elements where hash=$1", (hash,))
  135.             self.con.commit()
  136.             log.add("Hash %s has been deleted" % (hash))
  137.             return "ok"
  138.         except:
  139.             self.con.rollback()
  140.             log.add("Error while deleting Hash %s" % (hash))
  141.             return "error"
  142.  
  143.  
  144.     def help(self):
  145.         try:
  146.             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."
  147.             return help
  148.         except:
  149.             return "error"
  150.  
  151.  
  152. dbci=db(configfile)
  153. ###
  154. try:
  155.     log.add("Load RPC-Server config")
  156.     rpcconfig=ConfigParser.ConfigParser()
  157.     rpcconfig.read(configfile)
  158.     rpcserverip = rpcconfig.get("rpc", "serverip")
  159.     rpcserverport = rpcconfig.get("rpc", "serverport")
  160.     srv = Server((rpcserverip, int(rpcserverport)), allow_none=1)
  161.     log.add("RPC-Server initialization complete")
  162. except:
  163.     log.add("RPC-Server initialization Error")
  164.  
  165.  
  166. #register functions:
  167. try:
  168.     srv.register_function(dbci.about)
  169.     srv.register_function(dbci.add_element)
  170.     srv.register_function(dbci.list_all_elements)
  171.     srv.register_function(dbci.list_all_keys_of_element)
  172.     srv.register_function(dbci.add_key_to_element)
  173.     srv.register_function(dbci.delete_key_from_element)
  174.     srv.register_function(dbci.delete_element_by_hash)
  175.     srv.register_function(dbci.help)
  176.     log.add("Functions registered")
  177. except:
  178.     log.add("Error while registering functions!")
  179.  
  180.  
  181. log.add("Starting RPC-Server...")
  182. try:
  183.     srv.serve_forever()
  184. except:
  185.     log.add("CRITICAL: Can't start RPC-Server!!")
  186.  
  187. log.add("Stopping DBCI")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement