diff --git a/agents/vbox/fence_vbox.py b/agents/vbox/fence_vbox.py index 5491b9b3..9cebdd29 100644 --- a/agents/vbox/fence_vbox.py +++ b/agents/vbox/fence_vbox.py @@ -1,135 +1,135 @@ #!@PYTHON@ -tt # The Following Agent Has Been Tested On: # # VirtualBox 5.0.4 x64 on openSUSE 13.2 # import sys import re import time import atexit sys.path.append("@FENCEAGENTSLIBDIR@") from fencing import * from fencing import fail_usage def _invoke(conn, options, *cmd): prefix = options["--sudo-path"] + " " if "--use-sudo" in options else "" conn.send_eol(prefix + options["--vboxmanage-path"] + " " + " ".join(cmd)) conn.log_expect(options["--command-prompt"], int(options["--shell-timeout"])) def get_outlets_status(conn, options): _domain_re = re.compile(r'^\"(.*)\" \{(.*)\}$') result = {} _invoke(conn, options, "list", "vms") for line in conn.before.splitlines(): # format: "" {} domain = _domain_re.search(line.strip()) if domain is not None: result[domain.group(1)] = (domain.group(2), "off") _invoke(conn, options, "list", "runningvms") for line in conn.before.splitlines(): # format: "" {} domain = _domain_re.search(line.strip()) if domain is not None: result[domain.group(1)] = (domain.group(2), "on") return result def get_power_status(conn, options): outlets = get_outlets_status(conn, options) if options["--plug"] in outlets: return outlets[options["--plug"]][1] right_uuid_line = [outlets[o] for o in outlets.keys() if outlets[o][0] == options["--plug"]] if len(right_uuid_line): return right_uuid_line[0][1] if "--missing-as-off" in options: return "off" fail_usage("Failed: You have to enter existing name/UUID of virtual machine!") def set_power_status(conn, options): if options["--action"] == "on": _invoke(conn, options, "startvm", '"%s"' % options["--plug"], "--type", "headless") else: _invoke(conn, options, "controlvm", '"%s"' % options["--plug"], "poweroff") def define_new_opts(): all_opt["vboxmanage_path"] = { "getopt" : ":", "longopt" : "vboxmanage-path", "help" : "--vboxmanage-path=[path] Path to VBoxManage on the host", "required" : "0", "shortdesc" : "Path to VBoxManage on the host", "default" : "VBoxManage", "order" : 200 } all_opt["host_os"] = { "getopt" : ":", "longopt" : "host-os", "help" : "--host-os=[os] Operating system of the host", "required" : "0", "shortdesc" : "Operating system of the host", "choices" : ["linux", "macos", "windows"], "default" : "linux", "order" : 200 } def main(): device_opt = ["ipaddr", "login", "passwd", "cmd_prompt", "secure", "port", "sudo", "missing_as_off", "vboxmanage_path", "host_os"] define_new_opts() atexit.register(atexit_handler) all_opt["secure"]["default"] = "1" - all_opt["cmd_prompt"]["default"] = [r"\[EXPECT\]#\ "] - all_opt["ssh_options"]["default"] = "-t '/bin/bash -c \"" + r"PS1=\\[EXPECT\\]#\ " + "/bin/bash --noprofile --norc\"'" + all_opt["cmd_prompt"]["default"] = [r"\[EXPECT\]#"] + all_opt["ssh_options"]["default"] = "-t '/bin/bash -c \"" + r"PS1=\\[EXPECT\\]# " + "/bin/bash --noprofile --norc\"'" opt = process_input(device_opt) opt["logout_string"] = "quit" if "--host-os" in opt and "--vboxmanage-path" not in opt: if opt["--host-os"] == "linux": opt["--vboxmanage-path"] = "VBoxManage" elif opt["--host-os"] == "macos": opt["--vboxmanage-path"] = "/Applications/VirtualBox.app/Contents/MacOS/VBoxManage" opt["logout_string"] = "exit" elif opt["--host-os"] == "windows": opt["--vboxmanage-path"] = "\"/Program Files/Oracle/VirtualBox/VBoxManage.exe" opt["--command-prompt"] = "" opt["--ssh-options"] = "" options = check_input(device_opt, opt) options["eol"] = "\n" docs = {} docs["shortdesc"] = "Fence agent for VirtualBox" docs["longdesc"] = "fence_vbox is an I/O Fencing agent \ which can be used with the virtual machines managed by VirtualBox. \ It logs via ssh to a dom0 where it runs VBoxManage to do all of \ the work. \ \n.P\n\ By default, vbox needs to log in as a user that is a member of the \ vboxusers group. Also, you must allow ssh login in your sshd_config." docs["vendorurl"] = "https://www.virtualbox.org/" show_docs(options, docs) # Operate the fencing device conn = fence_login(options) result = fence_action(conn, options, set_power_status, get_power_status, get_outlets_status) fence_logout(conn, opt["logout_string"]) sys.exit(result) if __name__ == "__main__": main() diff --git a/agents/virsh/fence_virsh.py b/agents/virsh/fence_virsh.py index 7ccda2f8..88cee48d 100644 --- a/agents/virsh/fence_virsh.py +++ b/agents/virsh/fence_virsh.py @@ -1,96 +1,96 @@ #!@PYTHON@ -tt # The Following Agent Has Been Tested On: # # Virsh 0.3.3 on RHEL 5.2 with xen-3.0.3-51 # import sys, re import time import atexit sys.path.append("@FENCEAGENTSLIBDIR@") from fencing import * from fencing import fail_usage def get_name_or_uuid(options): return options["--uuid"] if "--uuid" in options else options["--plug"] def get_outlets_status(conn, options): if "--use-sudo" in options: prefix = options["--sudo-path"] + " " else: prefix = "" conn.sendline(prefix + "virsh list --all") conn.log_expect(options["--command-prompt"], int(options["--shell-timeout"])) result = {} #This is status of mini finite automata. 0 = we didn't found Id and Name, 1 = we did fa_status = 0 for line in conn.before.splitlines(): domain = re.search(r"^\s*(\S+)\s+(\S+)\s+(\S+).*$", line) if domain != None: if fa_status == 0 and domain.group(1).lower() == "id" and domain.group(2).lower() == "name": fa_status = 1 elif fa_status == 1: result[domain.group(2)] = ("", (domain.group(3).lower() in ["running", "blocked", "idle", "no state", "paused"] and "on" or "off")) return result def get_power_status(conn, options): prefix = options["--sudo-path"] + " " if "--use-sudo" in options else "" conn.sendline(prefix + "virsh domstate %s" % (get_name_or_uuid(options))) conn.log_expect(options["--command-prompt"], int(options["--shell-timeout"])) for line in conn.before.splitlines(): if line.strip() in ["running", "blocked", "idle", "no state", "paused"]: return "on" if "error: failed to get domain" in line.strip() and "--missing-as-off" in options: return "off" if "error:" in line.strip(): fail_usage("Failed: You have to enter existing name/UUID of virtual machine!") return "off" def set_power_status(conn, options): prefix = options["--sudo-path"] + " " if "--use-sudo" in options else "" conn.sendline(prefix + "virsh %s " % (options["--action"] == "on" and "start" or "destroy") + get_name_or_uuid(options)) conn.log_expect(options["--command-prompt"], int(options["--power-timeout"])) time.sleep(int(options["--power-wait"])) def main(): device_opt = ["ipaddr", "login", "passwd", "cmd_prompt", "secure", "port", "sudo", "missing_as_off"] atexit.register(atexit_handler) all_opt["secure"]["default"] = "1" - all_opt["cmd_prompt"]["default"] = [r"\[EXPECT\]#\ "] - all_opt["ssh_options"]["default"] = "-t '/bin/bash -c \"" + r"PS1=\\[EXPECT\\]#\ " + "/bin/bash --noprofile --norc\"'" + all_opt["cmd_prompt"]["default"] = [r"\[EXPECT\]#"] + all_opt["ssh_options"]["default"] = "-t '/bin/bash -c \"" + r"PS1=\\[EXPECT\\]# " + "/bin/bash --noprofile --norc\"'" options = check_input(device_opt, process_input(device_opt)) docs = {} docs["shortdesc"] = "Fence agent for virsh" docs["longdesc"] = "fence_virsh is an I/O Fencing agent \ which can be used with the virtual machines managed by libvirt. \ It logs via ssh to a dom0 and there run virsh command, which does \ all work. \ \n.P\n\ By default, virsh needs root account to do properly work. So you \ must allow ssh login in your sshd_config." docs["vendorurl"] = "http://libvirt.org" show_docs(options, docs) ## Operate the fencing device conn = fence_login(options) result = fence_action(conn, options, set_power_status, get_power_status, get_outlets_status) fence_logout(conn, "quit") sys.exit(result) if __name__ == "__main__": main() diff --git a/tests/data/metadata/fence_vbox.xml b/tests/data/metadata/fence_vbox.xml index b1eb25b4..3b53c7d2 100644 --- a/tests/data/metadata/fence_vbox.xml +++ b/tests/data/metadata/fence_vbox.xml @@ -1,222 +1,222 @@ fence_vbox is an I/O Fencing agent which can be used with the virtual machines managed by VirtualBox. It logs via ssh to a dom0 where it runs VBoxManage to do all of the work. By default, vbox needs to log in as a user that is a member of the vboxusers group. Also, you must allow ssh login in your sshd_config. https://www.virtualbox.org/ Fencing action - + Force Python regex for command prompt - + Force Python regex for command prompt Identity file (private key) for SSH Forces agent to use IPv4 addresses only Forces agent to use IPv6 addresses only IP address or hostname of fencing device IP address or hostname of fencing device TCP/UDP port to use for connection with device Login name Login password or passphrase Script to run to retrieve password Login password or passphrase Script to run to retrieve password Physical plug number on device, UUID or identification of machine Physical plug number on device, UUID or identification of machine Use SSH connection Use SSH connection - + SSH options to use Login name Disable logging to stderr. Does not affect --verbose or --debug-file or logging to syslog. Verbose mode Write debug information to given file Write debug information to given file Display version information and exit Display help and exit Separator for CSV created by 'list' operation Wait X seconds before fencing is started Operating system of the host Wait X seconds for cmd prompt after login Missing port returns OFF instead of failure Test X seconds for status change after ON/OFF Wait X seconds after issuing ON/OFF Wait X seconds for cmd prompt after issuing command Path to VBoxManage on the host Count of attempts to retry power on Use sudo (without password) when calling 3rd party software Use sudo (without password) when calling 3rd party software Path to ssh binary Path to sudo binary diff --git a/tests/data/metadata/fence_virsh.xml b/tests/data/metadata/fence_virsh.xml index a6bff164..864986d0 100644 --- a/tests/data/metadata/fence_virsh.xml +++ b/tests/data/metadata/fence_virsh.xml @@ -1,209 +1,209 @@ fence_virsh is an I/O Fencing agent which can be used with the virtual machines managed by libvirt. It logs via ssh to a dom0 and there run virsh command, which does all work. By default, virsh needs root account to do properly work. So you must allow ssh login in your sshd_config. http://libvirt.org Fencing action - + Force Python regex for command prompt - + Force Python regex for command prompt Identity file (private key) for SSH Forces agent to use IPv4 addresses only Forces agent to use IPv6 addresses only IP address or hostname of fencing device IP address or hostname of fencing device TCP/UDP port to use for connection with device Login name Login password or passphrase Script to run to retrieve password Login password or passphrase Script to run to retrieve password Physical plug number on device, UUID or identification of machine Physical plug number on device, UUID or identification of machine Use SSH connection Use SSH connection - + SSH options to use Login name Disable logging to stderr. Does not affect --verbose or --debug-file or logging to syslog. Verbose mode Write debug information to given file Write debug information to given file Display version information and exit Display help and exit Separator for CSV created by 'list' operation Wait X seconds before fencing is started Wait X seconds for cmd prompt after login Missing port returns OFF instead of failure Test X seconds for status change after ON/OFF Wait X seconds after issuing ON/OFF Wait X seconds for cmd prompt after issuing command Count of attempts to retry power on Use sudo (without password) when calling 3rd party software Use sudo (without password) when calling 3rd party software Path to ssh binary Path to sudo binary