Gitweb:
http://git.fedorahosted.org/git/?p=fence-agents.git;a=commitdiff;h=880e13...
Commit: 880e1373d607f863692e2a0667cf24c74fbd0a2c
Parent: a2b07ebb3b2050bf04b4d31a7a613efccf1096ff
Author: Marek 'marx' Grac <mgrac(a)redhat.com>
AuthorDate: Mon Nov 5 15:32:42 2012 +0100
Committer: Marek 'marx' Grac <mgrac(a)redhat.com>
CommitterDate: Thu Nov 8 10:20:10 2012 +0100
fencing: Add support for usage of longopt keys in options
This way we can make code more readable options["--power-timeout"] instead
options["-g"].
This code was created manually to support both short and long options but fence agents
will
be moved to long-options automatically.
---
fence/agents/lib/fencing.py.py | 196 +++++++++++++++++++++-------------------
1 files changed, 102 insertions(+), 94 deletions(-)
diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
index 9612b40..0041846 100644
--- a/fence/agents/lib/fencing.py.py
+++ b/fence/agents/lib/fencing.py.py
@@ -129,7 +129,7 @@ all_opt = {
"order" : 1 },
"passwd_script" : {
"getopt" : "S:",
- "longopt" : "password-script=",
+ "longopt" : "password-script",
"help" : "-S, --password-script=<script> Script to run to retrieve
password",
"required" : "0",
"shortdesc" : "Script to retrieve password",
@@ -578,16 +578,21 @@ def process_input(avail_opt):
for x in all_opt.keys():
if all_opt[x].has_key("longopt") and "--" +
all_opt[x]["longopt"] == o:
opt["-" + all_opt[x]["getopt"].rstrip(":")] =
dict(old_opt)[o]
+ opt["--" + all_opt[x]["longopt"]] = dict(old_opt)[o]
else:
+ for x in all_opt.keys():
+ if all_opt[x].has_key("getopt") and
all_opt[x].has_key("longopt") and \
+ ("-" + all_opt[x]["getopt"] == o or "-" +
all_opt[x]["getopt"].rstrip(":") == o):
+ opt["--" + all_opt[x]["longopt"]] = dict(old_opt)[o]
opt[o] = dict(old_opt)[o]
## Compatibility Layer
#####
z = dict(opt)
if z.has_key("-T") == 1:
- z["-o"] = "status"
- if z.has_key("-n") == 1:
- z["-m"] = z["-n"]
+ z["--action"] = "status"
+ if z.has_key("--plug") == 1:
+ z["-m"] = z["--plug"]
opt = z
##
@@ -653,7 +658,7 @@ def check_input(device_opt, opt):
## In special cases (show help, metadata or version) we don't need to check
anything
#####
- if options.has_key("-h") or options.has_key("-V") or
(options.has_key("-o") and options["-o"].lower() ==
"metadata"):
+ if options.has_key("-h") or options.has_key("-V") or
(options.has_key("--action") and options["--action"].lower() ==
"metadata"):
return options
## Set default values
@@ -661,12 +666,16 @@ def check_input(device_opt, opt):
for opt in device_opt:
if all_opt[opt].has_key("default"):
getopt_short = "-" + all_opt[opt]["getopt"].rstrip(":")
+ getopt_long = "--" + all_opt[opt]["longopt"]
if 0 == options.has_key(getopt_short):
options[getopt_short] = all_opt[opt]["default"]
+ if 0 == options.has_key(getopt_long):
+ options[getopt_long] = all_opt[opt]["default"]
+ options["--action"] = options["--action"].lower()
options["-o"] = options["-o"].lower()
- if options.has_key("-v"):
+ if options.has_key("--verbose"):
options["log"] = LOG_MODE_VERBOSE
else:
options["log"] = LOG_MODE_QUIET
@@ -679,71 +688,71 @@ def check_input(device_opt, opt):
else:
acceptable_actions.extend(["reboot"])
- if 0 == acceptable_actions.count(options["-o"]):
- fail_usage("Failed: Unrecognised action '" + options["-o"] +
"'")
+ if 0 == acceptable_actions.count(options["--action"]):
+ fail_usage("Failed: Unrecognised action '" +
options["--action"] + "'")
## Compatibility layer
#####
- if options["-o"] == "enable":
- options["-o"] = "on"
- if options["-o"] == "disable":
- options["-o"] = "off"
+ if options["--action"] == "enable":
+ options["--action"] = "on"
+ if options["--action"] == "disable":
+ options["--action"] = "off"
- if (0 == options.has_key("-l")) and device_opt.count("login") and
(device_opt.count("no_login") == 0):
+ if (0 == options.has_key("--username")) and
device_opt.count("login") and (device_opt.count("no_login") == 0):
fail_usage("Failed: You have to set login name")
- if 0 == options.has_key("-a") and 0 == options.has_key("-s"):
+ if 0 == options.has_key("--ip") and 0 == options.has_key("-s"):
fail_usage("Failed: You have to enter fence address")
if (device_opt.count("no_password") == 0):
if 0 == device_opt.count("identity_file"):
- if 0 == (options.has_key("-p") or options.has_key("-S")):
+ if 0 == (options.has_key("--password") or
options.has_key("--password-script")):
fail_usage("Failed: You have to enter password or password script")
else:
- if 0 == (options.has_key("-p") or options.has_key("-S") or
options.has_key("-k")):
+ if 0 == (options.has_key("--password") or
options.has_key("--password-script") or
options.has_key("--identity-file")):
fail_usage("Failed: You have to enter password, password script or identity
file")
- if 0 == options.has_key("-x") and 1 == options.has_key("-k"):
+ if 0 == options.has_key("--ssh") and 1 ==
options.has_key("--identity-file"):
fail_usage("Failed: You have to use identity file together with ssh connection
(-x)")
- if 1 == options.has_key("-k"):
- if 0 == os.path.isfile(options["-k"]):
- fail_usage("Failed: Identity file " + options["-k"] + " does
not exist")
+ if 1 == options.has_key("--identity-file"):
+ if 0 == os.path.isfile(options["--identity-file"]):
+ fail_usage("Failed: Identity file " + options["--identity-file"] +
" does not exist")
- if (0 == ["list", "monitor"].count(options["-o"].lower()))
and \
- (0 == options.has_key("-n") and 0 == options.has_key("-U")) and
(device_opt.count("port")):
+ if (0 == ["list",
"monitor"].count(options["--action"].lower())) and \
+ (0 == options.has_key("--plug") and 0 == options.has_key("--uuid"))
and (device_opt.count("port")):
fail_usage("Failed: You have to enter plug number")
- if options.has_key("-S"):
- options["-p"] = os.popen(options["-S"]).read().rstrip()
+ if options.has_key("--password-script"):
+ options["--password"] =
os.popen(options["--password-script"]).read().rstrip()
- if options.has_key("-D"):
+ if options.has_key("--debug-file"):
try:
- options["debug_fh"] = file (options["-D"], "w")
+ options["debug_fh"] = file (options["--debug-file"],
"w")
except IOError:
- fail_usage("Failed: Unable to create file "+options["-D"])
+ fail_usage("Failed: Unable to create file " +
options["--debug-file"])
- if options.has_key("-v") and options.has_key("debug_fh") == 0:
+ if options.has_key("--verbose") and options.has_key("debug_fh") ==
0:
options["debug_fh"] = sys.stderr
- if options.has_key("-R"):
- options["-P"] = os.popen(options["-R"]).read().rstrip()
+ if options.has_key("--snmp-priv-passwd-script"):
+ options["--snmp-priv-passwd"] =
os.popen(options["--snmp-priv-passwd-script"]).read().rstrip()
- if options.has_key("-u") == False:
- if options.has_key("-x"):
- options["-u"] = 22
- elif options.has_key("-z"):
- options["-u"] = 443
+ if options.has_key("--ipport") == False:
+ if options.has_key("--ssh"):
+ options["--ipport"] = 22
+ elif options.has_key("--ssl"):
+ options["--ipport"] = 443
elif device_opt.count("web"):
- options["-u"] = 80
+ options["--ipport"] = 80
else:
- options["-u"] = 23
+ options["--ipport"] = 23
return options
def wait_power_status(tn, options, get_power_fn):
- for dummy in xrange(int(options["-g"])):
- if get_power_fn(tn, options) != options["-o"]:
+ for dummy in xrange(int(options["--power-timeout"])):
+ if get_power_fn(tn, options) != options["--action"]:
time.sleep(1)
else:
return 1
@@ -759,15 +768,15 @@ def show_docs(options, docs = None):
## Process special options (and exit)
#####
- if options.has_key("-h"):
+ if options.has_key("--help"):
usage(device_opt)
sys.exit(0)
- if options.has_key("-o") and options["-o"].lower() ==
"metadata":
+ if options.has_key("--action") and options["--action"].lower() ==
"metadata":
metadata(device_opt, options, docs)
sys.exit(0)
- if options.has_key("-V"):
+ if options.has_key("--version"):
print __main__.RELEASE_VERSION, __main__.BUILD_DATE
print __main__.REDHAT_COPYRIGHT
sys.exit(0)
@@ -778,24 +787,24 @@ def fence_action(tn, options, set_power_fn, get_power_fn,
get_outlet_list = None
try:
## Process options that manipulate fencing device
#####
- if (options["-o"] == "list") and \
+ if (options["--action"] == "list") and \
0 == options["device_opt"].count("port") and 0 ==
options["device_opt"].count("partition") and \
0 == options["device_opt"].count("uuid") and 0 ==
options["device_opt"].count("module_name"):
print "N/A"
return
- elif (options["-o"] == "list" and get_outlet_list == None):
+ elif (options["--action"] == "list" and get_outlet_list == None):
## @todo: exception?
## This is just temporal solution, we will remove default value
## None as soon as all existing agent will support this operation
print "NOTICE: List option is not working on this device yet"
return
- elif (options["-o"] == "list") or ((options["-o"] ==
"monitor") and 1 == options["device_opt"].count("port")):
+ elif (options["--action"] == "list") or
((options["--action"] == "monitor") and 1 ==
options["device_opt"].count("port")):
outlets = get_outlet_list(tn, options)
## keys can be numbers (port numbers) or strings (names of VM)
for o in outlets.keys():
(alias, status) = outlets[o]
- if options["-o"] != "monitor":
- print o + options["-C"] + alias
+ if options["--action"] != "monitor":
+ print o + options["--separator"] + alias
return
status = get_power_fn(tn, options)
@@ -803,15 +812,14 @@ def fence_action(tn, options, set_power_fn, get_power_fn,
get_outlet_list = None
if status != "on" and status != "off":
fail(EC_STATUS)
-
- if options["-o"] == "on":
+ if options["--action"] == "on":
if status == "on":
print "Success: Already ON"
else:
power_on = False
- for _ in range(1, 1 + int(options["-F"])):
+ for _ in range(1, 1 + int(options["--retry-on"])):
set_power_fn(tn, options)
- time.sleep(int(options["-G"]))
+ time.sleep(int(options["--power-wait"]))
if wait_power_status(tn, options, get_power_fn):
power_on = True
break
@@ -820,30 +828,30 @@ def fence_action(tn, options, set_power_fn, get_power_fn,
get_outlet_list = None
print "Success: Powered ON"
else:
fail(EC_WAITING_ON)
- elif options["-o"] == "off":
+ elif options["--action"] == "off":
if status == "off":
print "Success: Already OFF"
else:
set_power_fn(tn, options)
- time.sleep(int(options["-G"]))
+ time.sleep(int(options["--power-wait"]))
if wait_power_status(tn, options, get_power_fn):
print "Success: Powered OFF"
else:
fail(EC_WAITING_OFF)
- elif options["-o"] == "reboot":
+ elif options["--action"] == "reboot":
if status != "off":
- options["-o"] = "off"
+ options["--action"] = "off"
set_power_fn(tn, options)
- time.sleep(int(options["-G"]))
+ time.sleep(int(options["--power-wait"]))
if wait_power_status(tn, options, get_power_fn) == 0:
fail(EC_WAITING_OFF)
- options["-o"] = "on"
+ options["--action"] = "on"
power_on = False
try:
- for _ in range(1, 1 + int(options["-F"])):
+ for _ in range(1, 1 + int(options["--retry-on"])):
set_power_fn(tn, options)
- time.sleep(int(options["-G"]))
+ time.sleep(int(options["--power-wait"]))
if wait_power_status(tn, options, get_power_fn) == 1:
power_on = True
break
@@ -858,11 +866,11 @@ def fence_action(tn, options, set_power_fn, get_power_fn,
get_outlet_list = None
sys.stderr.write('Timed out waiting to power ON\n')
print "Success: Rebooted"
- elif options["-o"] == "status":
+ elif options["--action"] == "status":
print "Status: " + status.upper()
if status.upper() == "OFF":
result = 2
- elif options["-o"] == "monitor":
+ elif options["--action"] == "monitor":
pass
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
@@ -877,10 +885,10 @@ def fence_action(tn, options, set_power_fn, get_power_fn,
get_outlet_list = None
def fence_login(options):
force_ipvx = ""
- if (options.has_key("-6")):
+ if (options.has_key("--inet6-only")):
force_ipvx = "-6 "
- if (options.has_key("-4")):
+ if (options.has_key("--inet4-only")):
force_ipvx = "-4 "
if (options.has_key("eol") == False):
@@ -888,23 +896,23 @@ def fence_login(options):
## 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
- if options["-o"] in ["off", "reboot"]:
- time.sleep(int(options["-f"]))
+ if options["--action"] in ["off", "reboot"]:
+ time.sleep(int(options["--delay"]))
try:
re_login = re.compile("(login\s*: )|(Login Name: )|(username: )|(User Name
:)", re.IGNORECASE)
re_pass = re.compile("(password)|(pass phrase)", re.IGNORECASE)
- if options.has_key("-z"):
- command = '%s %s %s %s' % (SSL_PATH, force_ipvx, options["-a"],
options["-u"])
+ if options.has_key("--ssl"):
+ command = '%s %s %s %s' % (SSL_PATH, force_ipvx, options["--ip"],
options["--ipport"])
try:
conn = fspawn(options, command)
except pexpect.ExceptionPexpect, ex:
## SSL telnet is part of the fencing package
sys.stderr.write(str(ex) + "\n")
sys.exit(EC_GENERIC_ERROR)
- elif options.has_key("-x") and 0 == options.has_key("-k"):
- command = '%s %s %s@%s -p %s' % (SSH_PATH, force_ipvx,
options["-l"], options["-a"], options["-u"])
+ elif options.has_key("--ssh") and 0 ==
options.has_key("--identity-file"):
+ command = '%s %s %s@%s -p %s' % (SSH_PATH, force_ipvx,
options["--username"], options["--ip"],
options["--ipport"])
if options.has_key("ssh_options"):
command += ' ' + options["ssh_options"]
try:
@@ -917,23 +925,23 @@ def fence_login(options):
if options.has_key("telnet_over_ssh"):
#This is for stupid ssh servers (like ALOM) which behave more like telnet (ignore
name and display login prompt)
- result = conn.log_expect(options, [ re_login, "Are you sure you want to continue
connecting (yes/no)?" ], int(options["-y"]))
+ result = conn.log_expect(options, [ re_login, "Are you sure you want to continue
connecting (yes/no)?" ], int(options["--login-timeout"]))
if result == 1:
conn.sendline("yes") # Host identity confirm
- conn.log_expect(options, re_login, int(options["-y"]))
+ conn.log_expect(options, re_login, int(options["--login-timeout"]))
- conn.sendline(options["-l"])
- conn.log_expect(options, re_pass, int(options["-y"]))
+ conn.sendline(options["--username"])
+ conn.log_expect(options, re_pass, int(options["--login-timeout"]))
else:
- result = conn.log_expect(options, [ "ssword:", "Are you sure you want
to continue connecting (yes/no)?" ], int(options["-y"]))
+ result = conn.log_expect(options, [ "ssword:", "Are you sure you want
to continue connecting (yes/no)?" ], int(options["--login-timeout"]))
if result == 1:
conn.sendline("yes")
- conn.log_expect(options, "ssword:", int(options["-y"]))
+ conn.log_expect(options, "ssword:",
int(options["--login-timeout"]))
- conn.sendline(options["-p"])
- conn.log_expect(options, options["-c"], int(options["-y"]))
- elif options.has_key("-x") and 1 == options.has_key("-k"):
- command = '%s %s %s@%s -i %s -p %s' % (SSH_PATH, force_ipvx,
options["-l"], options["-a"], options["-k"],
options["-u"])
+ conn.sendline(options["--password"])
+ conn.log_expect(options, options["--command-prompt"],
int(options["--login-timeout"]))
+ elif options.has_key("--ssh") and
options.has_key("--identity-file"):
+ command = '%s %s %s@%s -i %s -p %s' % (SSH_PATH, force_ipvx,
options["--username"], options["--ip"],
options["--identity-file"], options["--ipport"])
if options.has_key("ssh_options"):
command += ' ' + options["ssh_options"]
try:
@@ -944,44 +952,44 @@ def fence_login(options):
"are not in the spec file and must be installed separately." +
"\n")
sys.exit(EC_GENERIC_ERROR)
- result = conn.log_expect(options, [ options["-c"], \
+ result = conn.log_expect(options, [ options["--command-prompt"], \
"Are you sure you want to continue connecting (yes/no)?", \
- "Enter passphrase for key '" + options["-k"] +
"':" ], int(options["-y"]))
+ "Enter passphrase for key '" + options["--identity-file"] +
"':" ], int(options["--login-timeout"]))
if result == 1:
conn.sendline("yes")
- conn.log_expect(options, [ options["-c"], "Enter passphrase for key
'"+options["-k"]+"':"] , int(options["-y"]))
+ conn.log_expect(options, [ options["--command-prompt"], "Enter
passphrase for key '"+options["--identity-file"]+"':"] ,
int(options["--login-timeout"]))
if result != 0:
- if options.has_key("-p"):
- conn.sendline(options["-p"])
- conn.log_expect(options, options["-c"], int(options["-y"]))
+ if options.has_key("--password"):
+ conn.sendline(options["--password"])
+ conn.log_expect(options, options["--command-prompt"],
int(options["--login-timeout"]))
else:
fail_usage("Failed: You have to enter passphrase (-p) for identity file")
else:
try:
conn = fspawn(options, TELNET_PATH)
conn.send("set binary\n")
- conn.send("open %s -%s\n"%(options["-a"],
options["-u"]))
+ conn.send("open %s -%s\n"%(options["--ip"],
options["--ipport"]))
except pexpect.ExceptionPexpect, ex:
sys.stderr.write(str(ex) + "\n")
sys.stderr.write("Due to limitations, binary dependencies on fence agents
"
"are not in the spec file and must be installed separately." +
"\n")
sys.exit(EC_GENERIC_ERROR)
- result = conn.log_expect(options, re_login, int(options["-y"]))
- conn.send_eol(options["-l"])
+ result = conn.log_expect(options, re_login,
int(options["--login-timeout"]))
+ conn.send_eol(options["--username"])
## automatically change end of line separator
- screen = conn.read_nonblocking(size=100, timeout=int(options["-Y"]))
+ screen = conn.read_nonblocking(size=100,
timeout=int(options["--shell-timeout"]))
if (re_login.search(screen) != None):
options["eol"] = "\n"
- conn.send_eol(options["-l"])
- result = conn.log_expect(options, re_pass, int(options["-y"]))
+ conn.send_eol(options["--username"])
+ result = conn.log_expect(options, re_pass,
int(options["--login-timeout"]))
elif (re_pass.search(screen) == None):
- conn.log_expect(options, re_pass, int(options["-Y"]))
+ conn.log_expect(options, re_pass, int(options["--shell-timeout"]))
try:
- conn.send_eol(options["-p"])
- conn.log_expect(options, options["-c"], int(options["-Y"]))
+ conn.send_eol(options["--password"])
+ conn.log_expect(options, options["--command-prompt"],
int(options["--shell-timeout"]))
except KeyError:
fail(EC_PASSWORD_MISSING)
except pexpect.EOF: