improved post server setup code in Monitor.spec
authorStephen Soltesz <soltesz@cs.princeton.edu>
Wed, 1 Jul 2009 00:24:24 +0000 (00:24 +0000)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Wed, 1 Jul 2009 00:24:24 +0000 (00:24 +0000)
improved blacklist.py
added module path to exception in plc.py
included correct functions file for monitor-server.init

Monitor.spec
blacklist.py
monitor-server.init
monitor/wrapper/plc.py

index 91590d2..056f587 100644 (file)
@@ -229,8 +229,8 @@ easy_install --build-directory /var/tmp -UZ Elixir
 
 # crazy openssl libs for racadm binary
 ln -s /lib/libssl.so.0.9.8b /usr/lib/libssl.so.2
-mkdir /usr/share/monitor/.ssh
-chmod 700 /usr/share/monitor/.ssh
+mkdir %{_datadir}/%{name}/.ssh
+chmod 700 %{_datadir}/%{name}/.ssh
 
 if grep 'pam_loginuid.so' /etc/pam.d/crond ; then
     sed -i -e 's/^session    required   pam_loginuid.so/#session    required   pam_loginuid.so/g' /etc/pam.d/crond
@@ -244,7 +244,7 @@ if ! grep '<category id="plc_zabbix">' /etc/planetlab/default_config.xml ; then
 fi
 
 # NOTE: enable monitor by default, since we're installing it.
-plc-config --save /etc/planetlab/default_config.xml \
+plc-config --save /etc/planetlab/configs/site.xml \
                        --category plc_monitor --variable enabled --value true
 
 %post server
index 8704b59..3ff769f 100755 (executable)
@@ -4,6 +4,7 @@ import os
 import sys
 import string
 import time
+from monitor import common
 from monitor import database
 from monitor.database.info.model import *
 import getopt
@@ -12,62 +13,77 @@ def usage():
        print "blacklist.py --delete=<i>" 
 
 def main():
+       from monitor import parser as parsermodule
+       parser = parsermodule.getParser(['nodesets'])
 
-       loginbase = False
+       parser.set_defaults( expires=0, delete=False, add=False, list=True, loginbase=False)
+       parser.add_option("", "--expires", dest="expires", 
+                                               help="Set expiration time for blacklisted objects (in seconds)" )
+       parser.add_option("", "--delete", dest="delete", action="store_true",
+                                               help="Remove objects from blacklist" )
+       parser.add_option("", "--list", dest="list", action="store_true",
+                                               help="List objects in blacklist" )
+       parser.add_option("", "--add", dest="add", action="store_true",
+                                               help="List objects in blacklist" )
+       parser.add_option("", "--loginbase", dest="loginbase", action="store_true",
+                                               help="List objects in blacklist" )
+
+       config = parsermodule.parse_args(parser)
+
+       l_nodes = common.get_nodeset(config)
+       if config.site is None:
+               loginbase = False
+               if config.loginbase: loginbase=True
+       else:
+               loginbase = True
+               print "Blacklisting site:", config.site
 
-       try:
-               longopts = ["delete=", "loginbase", "help"]
-               (opts, argv) = getopt.getopt(sys.argv[1:], "d:lh", longopts)
-       except getopt.GetoptError, err:
-               print "Error: " + err.msg
-               sys.exit(1)
 
        hostnames_q = BlacklistRecord.getHostnameBlacklist()
        loginbases_q = BlacklistRecord.getLoginbaseBlacklist()
        hostnames  = [ h.hostname for h in hostnames_q ]
        loginbases = [ h.loginbase for h in loginbases_q ]
+       hostnames_exp  = [ (h.hostname,h.date_created+timedelta(0,h.expires)) for h in hostnames_q ]
+       loginbases_exp = [ (h.loginbase,h.date_created+timedelta(0,h.expires)) for h in loginbases_q ]
 
-       for (opt, optval) in opts:
-               if opt in ["-d", "--delete"]:
-                       i = optval
-                       bl = BlacklistRecord.get_by(hostname=i)
-                       bl.delete()
-               elif opt in ["-l", "--loginbase"]:
-                       loginbase = True
-               else:
-                       usage()
-                       sys.exit(0)
+       if config.add:
+               print "Blacklisting nodes: ", l_nodes
+               for host in l_nodes:
+                       if host not in hostnames:
+                               print "adding to blacklist %s" % host
+                               bl = BlacklistRecord(hostname=host, expires=int(config.expires))
+                               bl.flush()
 
-       i_cnt = 0
-       if not loginbase:
-               for i in hostnames:
-                       print i
-                       i_cnt += 1
-       else:
-               for i in loginbases:
-                       print i
-                       i_cnt += 1
-               
+               if loginbase:
+                       print "Blacklisting site: ", config.site
+                       if config.site not in loginbases:
+                               print "adding to blacklist %s" % config.site
+                               bl = BlacklistRecord(loginbase=config.site, expires=int(config.expires))
+                               bl.flush()
 
+       elif config.delete:
+               print "Deleting nodes: %s" % l_nodes
+               for h in l_nodes:
+                       bl = BlacklistRecord.get_by(hostname=h)
+                       if bl: bl.delete()
+               if config.site:
+                       print "Deleting site: %s" % config.site
+                       bl = BlacklistRecord.get_by(loginbase=config.site)
+                       if bl: bl.delete()
+       else:
+               # default option is to list
+               if loginbase:
+                       objlist = loginbases_exp
+               else:
+                       objlist = hostnames_exp
 
-       while 1:
-               line = sys.stdin.readline()
-               if not line:
-                       break
-               line = line.strip()
-               if line not in hostnames and line not in loginbases:
-                       if loginbase:
-                               bl = BlacklistRecord(loginbase=line)
+               for i in objlist:
+                       if i[1] > datetime.now():
+                               print i[0], i[1]
                        else:
-                               bl = BlacklistRecord(hostname=line)
-                       bl.flush()
-                       i_cnt += 1
-
+                               print i[0]
+                       
        session.flush()
-       if loginbase:
-               print "Total %d loginbases in blacklist" % (i_cnt)
-       else:
-               print "Total %d nodes in blacklist" % (i_cnt)
        
 if __name__ == '__main__':
        import os
index cc7e143..ee7c469 100644 (file)
@@ -13,6 +13,7 @@
 
 # Source function library and configuration
 . /etc/plc.d/functions
+. /etc/plc.d/monitor.functions
 . /etc/planetlab/plc_config
 local_config=/etc/planetlab/configs/site.xml
 
index b681d55..1515396 100644 (file)
@@ -71,7 +71,7 @@ class PLC:
 
                try:
                        return lambda *params : method(self.auth, *params)
-               except ProtocolError:
+               except xmlrpclib.ProtocolError:
                        traceback.print_exc()
                        global_error_count += 1
                        if global_error_count >= 10: