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

[Cluster-devel] cluster/fence/agents apc/fence_apc.py rsa/fenc ...



CVSROOT:	/cvs/cluster
Module name:	cluster
Changes by:	kupcevic sourceware org	2007-02-14 15:42:28

Modified files:
	fence/agents/apc: fence_apc.py 
	fence/agents/rsa: fence_rsa.py 
	fence/agents/rsb: fence_rsb.py 

Log message:
	Support "passwd_script" parameter in python fence agents.
	If both "passwd" and "passwd_script" parameters are specified, "passwd_script" will be used first (if it fails, fencing will be attempted using "passwd" parameter).

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/fence/agents/apc/fence_apc.py.diff?cvsroot=cluster&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/fence/agents/rsa/fence_rsa.py.diff?cvsroot=cluster&r1=1.3&r2=1.4
http://sourceware.org/cgi-bin/cvsweb.cgi/cluster/fence/agents/rsb/fence_rsb.py.diff?cvsroot=cluster&r1=1.1&r2=1.2

--- cluster/fence/agents/apc/fence_apc.py	2007/01/31 20:47:19	1.2
+++ cluster/fence/agents/apc/fence_apc.py	2007/02/14 15:42:26	1.3
@@ -47,6 +47,7 @@
 address = ""
 login = ""
 passwd = ""
+passwd_script = ""
 port = ""
 switchnum = ""
 action = POWER_REBOOT   #default action
@@ -88,6 +89,7 @@
   print "   -l [login]               login name"
   print "   -n [port]                switch port"
   print "   -p [password]            password"
+  print "   -S [path]                script to run to retrieve password"
   print "   -o [action]              Reboot (default), Off, On, or Status"
   print "   -v Verbose               Verbose mode - writes file to /tmp/apclog"
   print "   -V                       Print Version, then exit"
@@ -101,16 +103,16 @@
 
 def main():
 
-  global address, login, passwd, port, action, verbose, logfile, switchnum
+  global address, login, passwd, passwd_script, port, action, verbose, logfile, switchnum
 
   if len(sys.argv) > 1:
     try:
-      opts, args = getopt.getopt(sys.argv[1:], "a:hl:o:n:p:vV", ["help", "output="])
+      opts, args = getopt.getopt(sys.argv[1:], "a:hl:o:n:p:S:vV", ["help", "output="])
     except getopt.GetoptError:
       #print help info and quit
       usage()
       sys.exit(2)
-                                                                                
+      
     for o, a in opts:
       if o == "-v":
         verbose = True
@@ -123,6 +125,8 @@
         login = a
       if o == "-p":
         passwd = a
+      if o == "-S":
+        passwd_script = a
       if o == "-n":
         dex = a.find(":")
         if dex == (-1):
@@ -144,16 +148,17 @@
           sys.exit()
       if o == "-a":
         address = a
-    if address == "" or login == "" or passwd == "" or port == "":
+    if address == "" or login == "" or (passwd == "" and passwd_script == "") or port == "":
       usage()
       sys.exit()
-
+    
   else: #Take args from stdin...
     params = {}
     #place params in dict
     for line in sys.stdin:
       val = line.split("=")
-      params[val[0]] = val[1]
+      if len(val) == 2:
+        params[val[0].strip()] = val[1].strip()
 
     try:
       address = params["ipaddr"]
@@ -166,9 +171,14 @@
       sys.stderr.write("FENCE: Missing login param for fence_apc...exiting")
       sys.exit(1)
     try:
-      passwd = params["passwd"]
-    except KeyError, e:
-      sys.stderr.write("FENCE: Missing passwd param for fence_apc...exiting")
+      if 'passwd' in params:
+        passwd = params["passwd"]
+      if 'passwd_script' in params:
+        passwd_script = params['passwd_script']
+      if passwd == "" and passwd_script == "":
+        raise "missing password"
+    except:
+      sys.stderr.write("FENCE: Missing passwd for fence_apc...exiting")
       sys.exit(1)
     try:
       port = params["port"]
@@ -195,9 +205,67 @@
         action = POWER_REBOOT
     except KeyError, e:
       action = POWER_REBOOT
-
+    
     #### End of stdin section
-
+  
+  
+  # retrieve passwd from passwd_script (if specified)
+  passwd_scr = ''
+  if len(passwd_script):
+    try:
+      if not os.access(passwd_script, os.X_OK):
+        raise 'script not executable'
+      p = os.popen(passwd_script, 'r', 1024)
+      passwd_scr = p.readline().strip()
+      if p.close() != None:
+        raise 'script failed'
+    except:
+      sys.stderr.write('password-script "%s" failed\n' % passwd_script)
+      passwd_scr = ''
+    
+  if passwd == "" and passwd_scr == "":
+    sys.stderr.write('password not available, exiting...')
+    sys.exit(1)
+  elif passwd == passwd_scr:
+    pass
+  elif passwd and passwd_scr:
+    # execute self, with password_scr as passwd,
+    # if that fails, continue with "passwd" argument as password
+    if len(sys.argv) > 1:
+      comm = sys.argv[0]
+      skip_next = False
+      for w in sys.argv[1:]:
+        if skip_next:
+          skip_next = False
+        elif w in ['-p', '-S']:
+          skip_next = True
+        else:
+          comm += ' ' + w
+      comm += ' -p ' + passwd_scr
+      ret = os.system(comm)
+      if ret != -1 and os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == 0:
+        # success
+        sys.exit(0)
+      else:
+        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
+    else: # use stdin
+      p = os.popen(sys.argv[0], 'w', 1024)
+      for par in params:
+        if par not in ['passwd', 'passwd_script']:
+          p.write(par + '=' + params[par] + '\n')
+      p.write('passwd=' + passwd_scr + '\n')
+      p.flush()
+      if p.close() == None:
+        # success
+        sys.exit(0)
+      else:
+        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
+  elif passwd_scr:
+    passwd = passwd_scr
+  # passwd all set
+  
+  
+  
   ### Order of events
   # 0) If verbose, prepare log file handle
   # 1) Open socket
--- cluster/fence/agents/rsa/fence_rsa.py	2007/01/31 20:23:44	1.3
+++ cluster/fence/agents/rsa/fence_rsa.py	2007/02/14 15:42:28	1.4
@@ -32,17 +32,18 @@
 #END_VERSION_GENERATION
 
 def usage():
-  print "Usage:\n"
-  print "fence_rsa [options]\n"
-  print "Options:\n"
-  print "   -a <ipaddress>           ip or hostname of rsa II port\n"
-  print "   -h                       print out help\n"
-  print "   -l [login]               login name\n"
-  print "   -p [password]            password\n"
-  print "   -o [action]              Reboot (default), Off, On, or Status\n"
-  print "   -v Verbose               Verbose mode\n"
-  print "   -V                       Print Version, then exit\n"
-
+  print "Usage:"
+  print "fence_rsa [options]"
+  print "Options:"
+  print "   -a <ipaddress>           ip or hostname of rsa II port"
+  print "   -h                       print out help"
+  print "   -l [login]               login name"
+  print "   -p [password]            password"
+  print "   -S [path]                script to run to retrieve password"
+  print "   -o [action]              Reboot (default), Off, On, or Status"
+  print "   -v Verbose               Verbose mode"
+  print "   -V                       Print Version, then exit"
+  
   sys.exit (0)
 
 def version():
@@ -60,6 +61,7 @@
   address = ""
   login = ""
   passwd = ""
+  passwd_script = ""
   action = POWER_REBOOT   #default action
   verbose = False
 
@@ -80,7 +82,7 @@
 
   if len(sys.argv) > 1:
     try:
-      opts, args = getopt.getopt(sys.argv[1:], "a:hl:o:p:vV", ["help", "output="])
+      opts, args = getopt.getopt(sys.argv[1:], "a:hl:o:p:S:vV", ["help", "output="])
     except getopt.GetoptError:
       #print help info and quit
       usage()
@@ -94,11 +96,13 @@
         version()
       if o in ("-h", "--help"):
         usage()
-        sys.exit()
+        sys.exit(0)
       if o == "-l":
         login = a
       if o == "-p":
         passwd = a
+      if o == "-S":
+        passwd_script = a
       if o  == "-o":
         if a == "Off" or a == "OFF" or a == "off":
           action = POWER_OFF
@@ -110,35 +114,44 @@
           action = POWER_REBOOT
         else:
           usage()
-          sys.exit()
+          sys.exit(1)
       if o == "-a":
         address = a
-    if address == "" or login == "" or passwd == "":
+    if address == "" or login == "" or (passwd == "" and passwd_script == ""):
       usage()
-      sys.exit()
+      sys.exit(1)
 
   else: #Take args from stdin...
     params = {}
     #place params in dict
     for line in sys.stdin:
       val = line.split("=")
-      params[val[0]] = val[1]
-
+      if len(val) == 2:
+        params[val[0].strip()] = val[1].strip()
+    
     try:
       address = params["ipaddr"]
     except KeyError, e:
       os.write(standard_err, "FENCE: Missing ipaddr param for fence_rsa...exiting")
+      sys.exit(1)
+    
     try:
       login = params["login"]
     except KeyError, e:
       os.write(standard_err, "FENCE: Missing login param for fence_rsa...exiting")
-
+      sys.exit(1)
+    
     try:
-      passwd = params["passwd"]
+      if 'passwd' in params:
+        passwd = params["passwd"]
+      if 'passwd_script' in params:
+        passwd_script = params['passwd_script']
+      if passwd == "" and passwd_script == "":
+        raise "missing password"
     except KeyError, e:
       os.write(standard_err, "FENCE: Missing passwd param for fence_rsa...exiting")
-
-
+      sys.exit(1)
+    
     try:
       a = params["option"]
       if a == "Off" or a == "OFF" or a == "off":
@@ -149,9 +162,67 @@
         action = POWER_REBOOT
     except KeyError, e:
       action = POWER_REBOOT
-
+    
     ####End of stdin section
-
+  
+  
+  # retrieve passwd from passwd_script (if specified)
+  passwd_scr = ''
+  if len(passwd_script):
+    try:
+      if not os.access(passwd_script, os.X_OK):
+        raise 'script not executable'
+      p = os.popen(passwd_script, 'r', 1024)
+      passwd_scr = p.readline().strip()
+      if p.close() != None:
+        raise 'script failed'
+    except:
+      sys.stderr.write('password-script "%s" failed\n' % passwd_script)
+      passwd_scr = ''
+  
+  if passwd == "" and passwd_scr == "":
+    sys.stderr.write('password not available, exiting...')
+    sys.exit(1)
+  elif passwd == passwd_scr:
+    pass
+  elif passwd and passwd_scr:
+    # execute self, with password_scr as passwd,
+    # if that fails, continue with "passwd" argument as password
+    if len(sys.argv) > 1:
+      comm = sys.argv[0]
+      skip_next = False
+      for w in sys.argv[1:]:
+        if skip_next:
+          skip_next = False
+        elif w in ['-p', '-S']:
+          skip_next = True
+        else:
+          comm += ' ' + w
+      comm += ' -p ' + passwd_scr
+      ret = os.system(comm)
+      if ret != -1 and os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == 0:
+        # success
+        sys.exit(0)
+      else:
+        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
+    else: # use stdin
+      p = os.popen(sys.argv[0], 'w', 1024)
+      for par in params:
+        if par not in ['passwd', 'passwd_script']:
+          p.write(par + '=' + params[par] + '\n')
+      p.write('passwd=' + passwd_scr + '\n')
+      p.flush()
+      if p.close() == None:
+        # success
+        sys.exit(0)
+      else:
+        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
+  elif passwd_scr:
+    passwd = passwd_scr
+  # passwd all set
+  
+  
+  
   ##Time to open telnet session and log in. 
   try:
     sock = Telnet(address.strip())
--- cluster/fence/agents/rsb/fence_rsb.py	2006/04/19 19:39:01	1.1
+++ cluster/fence/agents/rsb/fence_rsb.py	2007/02/14 15:42:28	1.2
@@ -33,17 +33,18 @@
 #END_VERSION_GENERATION
 
 def usage():
-  print "Usage:\n"
-  print "fence_rsb [options]\n"
-  print "Options:\n"
-  print "   -a <ipaddress>           ip or hostname of rsb\n"
-  print "   -h                       print out help\n"
-  print "   -l [login]               login name\n"
-  print "   -n [telnet port]         telnet port\n"
-  print "   -p [password]            password\n"
-  print "   -o [action]              Reboot (default), Off, On, or Status\n"
-  print "   -v Verbose               Verbose mode\n"
-  print "   -V                       Print Version, then exit\n"
+  print "Usage:"
+  print "fence_rsb [options]"
+  print "Options:"
+  print "   -a <ipaddress>           ip or hostname of rsb"
+  print "   -h                       print out help"
+  print "   -l [login]               login name"
+  print "   -n [telnet port]         telnet port"
+  print "   -p [password]            password"
+  print "   -S [path]                script to run to retrieve password"
+  print "   -o [action]              Reboot (default), Off, On, or Status"
+  print "   -v Verbose               Verbose mode"
+  print "   -V                       Print Version, then exit"
 
   sys.exit (0)
 
@@ -64,6 +65,7 @@
   address = ""
   login = ""
   passwd = ""
+  passwd_script = ""
   action = POWER_REBOOT   #default action
   telnet_port = 3172
   verbose = False
@@ -93,7 +95,7 @@
 
   if len(sys.argv) > 1:
     try:
-      opts, args = getopt.getopt(sys.argv[1:], "a:hl:n:o:p:vV", ["help", "output="])
+      opts, args = getopt.getopt(sys.argv[1:], "a:hl:n:o:p:S:vV", ["help", "output="])
     except getopt.GetoptError:
       #print help info and quit
       usage()
@@ -106,13 +108,15 @@
         version()
       if o in ("-h", "--help"):
         usage()
-        sys.exit()
+        sys.exit(0)
       if o == "-l":
         login = a
       if o == "-n":
         telnet_port = a
       if o == "-p":
         passwd = a
+      if o == "-S":
+        passwd_script = a
       if o  == "-o":
         if a == "Off" or a == "OFF" or a == "off":
           action = POWER_OFF
@@ -124,34 +128,44 @@
           action = POWER_REBOOT
         else:
           usage()
-          sys.exit()
+          sys.exit(1)
       if o == "-a":
         address = a
-    if address == "" or login == "" or passwd == "":
+    if address == "" or login == "" or (passwd == "" and passwd_script == ""):
       usage()
-      sys.exit()
+      sys.exit(1)
 
   else: #Take args from stdin...
     params = {}
     #place params in dict
     for line in sys.stdin:
       val = line.split("=")
-      params[val[0]] = val[1]
+      if len(val) == 2:
+        params[val[0].strip()] = val[1].strip()
 
     try:
       address = params["ipaddr"]
     except KeyError, e:
       os.write(standard_err, "FENCE: Missing ipaddr param for fence_rsb...exiting")
+      sys.exit(1)
+    
     try:
       login = params["login"]
     except KeyError, e:
       os.write(standard_err, "FENCE: Missing login param for fence_rsb...exiting")
-
+      sys.exit(1)
+    
     try:
-      passwd = params["passwd"]
+      if 'passwd' in params:
+        passwd = params["passwd"]
+      if 'passwd_script' in params:
+        passwd_script = params['passwd_script']
+      if passwd == "" and passwd_script == "":
+        raise "missing password"
     except KeyError, e:
       os.write(standard_err, "FENCE: Missing passwd param for fence_rsb...exiting")
-
+      sys.exit(1)
+    
     try:
       telnet_port = params["telnet_port"]
     except KeyError, e:
@@ -169,7 +183,65 @@
       action = POWER_REBOOT
 
     ####End of stdin section
-
+  
+  
+  # retrieve passwd from passwd_script (if specified)
+  passwd_scr = ''
+  if len(passwd_script):
+    try:
+      if not os.access(passwd_script, os.X_OK):
+        raise 'script not executable'
+      p = os.popen(passwd_script, 'r', 1024)
+      passwd_scr = p.readline().strip()
+      if p.close() != None:
+        raise 'script failed'
+    except:
+      sys.stderr.write('password-script "%s" failed\n' % passwd_script)
+      passwd_scr = ''
+  
+  if passwd == "" and passwd_scr == "":
+    sys.stderr.write('password not available, exiting...')
+    sys.exit(1)
+  elif passwd == passwd_scr:
+    pass
+  elif passwd and passwd_scr:
+    # execute self, with password_scr as passwd,
+    # if that fails, continue with "passwd" argument as password
+    if len(sys.argv) > 1:
+      comm = sys.argv[0]
+      skip_next = False
+      for w in sys.argv[1:]:
+        if skip_next:
+          skip_next = False
+        elif w in ['-p', '-S']:
+          skip_next = True
+        else:
+          comm += ' ' + w
+      comm += ' -p ' + passwd_scr
+      ret = os.system(comm)
+      if ret != -1 and os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == 0:
+        # success
+        sys.exit(0)
+      else:
+        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
+    else: # use stdin
+      p = os.popen(sys.argv[0], 'w', 1024)
+      for par in params:
+        if par not in ['passwd', 'passwd_script']:
+          p.write(par + '=' + params[par] + '\n')
+      p.write('passwd=' + passwd_scr + '\n')
+      p.flush()
+      if p.close() == None:
+        # success
+        sys.exit(0)
+      else:
+        sys.stderr.write('Use of password from "passwd_script" failed, trying "passwd" argument\n')
+  elif passwd_scr:
+    passwd = passwd_scr
+  # passwd all set
+  
+  
+  
   try:
     telnet_port = int(telnet_port)
   except:


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