cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Marek 'marx' Grac <mgrac@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH] Autodetect EOL in fence agents
Date: Wed,  9 May 2012 10:37:55 +0200	[thread overview]
Message-ID: <1336552675-30291-1-git-send-email-mgrac@redhat.com> (raw)

Fence agents diffes in EOL they accept. EOL \r\n is quite universal but in some cases
it is translated to \n\n what makes login process impossible to complete as it ends like
(capitals are fence agent response)

Login: USER\n
Password: \n

Login: PASSWORD\n

Until now there was a device option that handles that login_eol_lf but we found out that there are few
APC devices which does not work correctly with \r\n and needs \n. So we have to autodetect it, this feature was
added to standard library. As it can also helps with common problem when you have to write exactly
conn.send("message" + "\r\n"). Method conn.sendline("") uses EOL according to platform, so we have decided to create
new method conn.sendxline (need a better name) which uses correct EOL. We need conn.sendline in communication with other local
application so we should not reuse it for this purpose.
---
 fence/agents/lib/fencing.py.py |   37 +++++++++++++++++++++++--------------
 1 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
index 5748391..a61c45f 100644
--- a/fence/agents/lib/fencing.py.py
+++ b/fence/agents/lib/fencing.py.py
@@ -175,11 +175,6 @@ all_opt = {
 		"required" : "0",
 		"shortdesc" : "Force ribcl version to use",
 		"order" : 1 },
-	"login_eol_lf" : {
-		"getopt" : "",
-		"help" : "",
-		"order" : 1
-		},
 	"cmd_prompt" : {
 		"getopt" : "c:",
 		"longopt" : "command-prompt",
@@ -407,12 +402,20 @@ all_opt = {
 common_opt = [ "retry_on", "delay" ]
 
 class fspawn(pexpect.spawn):
+	def __init__(self, options, command):
+		pexpect.spawn.__init__(self, command)
+		self.opt = options
+		
 	def log_expect(self, options, pattern, timeout):
 		result = self.expect(pattern, timeout)
 		if options["log"] >= LOG_MODE_VERBOSE:
 			options["debug_fh"].write(self.before + self.after)
 		return result
 
+	# send EOL according to what was detected in login process (telnet)
+	def sendxline(self, message):
+		self.send(message + self.opt["eol"])
+
 def atexit_handler():
 	try:
 		sys.stdout.close()
@@ -864,10 +867,7 @@ def fence_login(options):
 	if (options.has_key("-4")):
 		force_ipvx="-4 "
 
-	if (options["device_opt"].count("login_eol_lf")):
-		login_eol = "\n"
-	else:
-		login_eol = "\r\n"
+	options["eol"] = "\r\n"
 
 	## Do the delay of the fence device before logging in
 	## Delay is important for two-node clusters fencing but we do not need to delay 'status' operations
@@ -939,7 +939,7 @@ def fence_login(options):
 					fail_usage("Failed: You have to enter passphrase (-p) for identity file")
 		else:
 			try:
-				conn = fspawn(TELNET_PATH)
+				conn = fspawn(options, TELNET_PATH)
 				conn.send("set binary\n")
 				conn.send("open %s -%s\n"%(options["-a"], options["-u"]))
 			except pexpect.ExceptionPexpect, ex:
@@ -948,11 +948,20 @@ def fence_login(options):
 				"are not in the spec file and must be installed separately." + "\n")
 				sys.exit(EC_GENERIC_ERROR)
 
-			conn.log_expect(options, re_login, int(options["-y"]))
-			conn.send(options["-l"] + login_eol)
-			conn.log_expect(options, re_pass, int(options["-Y"]))
+			result = conn.log_expect(options, re_login, int(options["-y"]))
+			conn.sendxline(options["-l"])
+
+			## automatically change end of line separator
+			screen = conn.read_nonblocking(size=100, timeout=int(options["-Y"]))
+			if (re_login.search(screen) != None):
+				options["eol"] = "\n"
+				conn.sendxline(options["-l"])
+				result = conn.log_expect(options, re_pass, int(options["-y"]))
+			elif (re_pass.search(screen) == None):
+				conn.log_expect(options, re_pass, int(options["-Y"]))
+
 			try:
-				conn.send(options["-p"] + login_eol)
+				conn.sendxline(options["-p"])
 				conn.log_expect(options, options["-c"], int(options["-Y"]))
 			except KeyError:
 				fail(EC_PASSWORD_MISSING)
-- 
1.7.7.6



                 reply	other threads:[~2012-05-09  8:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1336552675-30291-1-git-send-email-mgrac@redhat.com \
    --to=mgrac@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).