Add support for option --method and new option --amttool-path

---
 fence/agents/amt/fence_amt.py | 72 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 57 insertions(+), 15 deletions(-)

diff --git a/fence/agents/amt/fence_amt.py b/fence/agents/amt/fence_amt.py
index 8fe2dbc..7077828 100755
--- a/fence/agents/amt/fence_amt.py
+++ b/fence/agents/amt/fence_amt.py
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-import sys, subprocess, re
+import sys, subprocess, re, os, stat
 from pipes import quote
 sys.path.append("@FENCEAGENTSLIBDIR@")
 from fencing import *
@@ -21,12 +21,11 @@ def get_power_status(_, options):
     try:
         process = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE, shell=True)
     except OSError:
-        fail(EC_TOOL_FAIL)
+        fail_usage("Amttool not found or not accessible")
 
     process.wait()
 
     output = process.communicate()
-
     process.stdout.close()
 
     match = re.search('Powerstate:[\\s]*(..)', str(output))
@@ -51,19 +50,44 @@ def set_power_status(_, options):
         process = subprocess.Popen(cmd, stdout=null, stderr=null, shell=True)
     except OSError:
         null.close()
-        fail(EC_TOOL_FAIL)
+        fail_usage("Amttool not found or not accessible")
 
     process.wait()
     null.close()
 
     return
 
+def reboot_cycle(_, options):
+    cmd = create_command(options, "cycle")
+
+    if options["log"] >= LOG_MODE_VERBOSE:
+        options["debug_fh"].write("executing: " + cmd + "\n")
+
+    null = open('/dev/null', 'w')
+    try:
+        process = subprocess.Popen(cmd, stdout=null, stderr=null, shell=True)
+    except OSError:
+        null.close()
+        fail_usage("Amttool not found or not accessible")
+
+    status = process.wait()
+    null.close()
+
+    return not bool(status)
+
+def is_executable(path):
+    if os.path.exists(path):
+        stats = os.stat(path)
+        if stat.S_ISREG(stats.st_mode) and os.access(path, os.X_OK):
+            return True
+    return False
+
 def create_command(options, action):
 
     # --password / -p
     cmd = "AMT_PASSWORD=" + quote(options["--password"])
 
-    cmd += " " + options["amttool_path"]
+    cmd += " " + options["--amttool-path"]
 
     # --ip / -a
     cmd += " " + options["--ip"]
@@ -77,7 +101,10 @@ def create_command(options, action):
     elif action == "off":
         cmd = "echo \"y\"|" + cmd
         cmd += " powerdown"
-    if action in ["on", "off"] and options.has_key("--boot-options"):
+    elif action == "cycle":
+        cmd = "echo \"y\"|" + cmd
+        cmd += " powercycle"
+    if action in ["on", "off", "cycle"] and options.has_key("--boot-options"):
         cmd += options["--boot-options"]
 
     # --use-sudo / -d
@@ -86,25 +113,40 @@ def create_command(options, action):
 
     return cmd
 
-def main():
-
-    atexit.register(atexit_handler)
-
-    device_opt = [ "ipaddr", "no_login", "passwd", "boot_option", "no_port", 
"sudo"]
-
+def define_new_opts():
     all_opt["boot_option"] = {
         "getopt" : "b:",
         "longopt" : "boot-option",
-        "help":"-b, --boot-option=[option] Change the default boot behavior of 
the machine. (pxe|hd|hdsafe|cd|diag)",
+        "help":"-b, --boot-option=[option]     Change the default boot 
behavior of the machine. (pxe|hd|hdsafe|cd|diag)",
         "required" : "0",
         "shortdesc" : "Change the default boot behavior of the machine.",
             "choices" : ["pxe", "hd", "hdsafe", "cd", "diag"],
           "order" : 1
         }
+    all_opt["amttool_path"] = {
+        "getopt" : "i:",
+        "longopt" : "amttool-path",
+        "help" : "--amttool-path=[path]          Path to amttool binary",
+        "required" : "0",
+        "shortdesc" : "Path to amttool binary",
+        "default" : "/usr/bin/amttool",
+        "order": 200
+        }
+
+
+def main():
+
+    atexit.register(atexit_handler)
+
+    device_opt = [ "ipaddr", "no_login", "passwd", "boot_option", "no_port",
+         "sudo", "amttool_path", "method" ]
+
+    define_new_opts()
 
     options = check_input(device_opt, process_input(device_opt))
 
-    options["amttool_path"] = "/usr/bin/amttool"
+    if not is_executable(options["--amttool-path"]):
+        fail_usage("Amttool not found or not accessible")
 
     docs = { }
     docs["shortdesc"] = "Fence agent for AMT"
@@ -112,7 +154,7 @@ def main():
     docs["vendorurl"] = "http://www.intel.com/";
     show_docs(options, docs)
 
-    result = fence_action(None, options, set_power_status, get_power_status, 
None)
+    result = fence_action(None, options, set_power_status, get_power_status, 
None, reboot_cycle)
 
     sys.exit(result)
 
-- 
1.8.3.1

Reply via email to