From eb2d15ceaf1dacc856811f701382e23c63a60ccb Mon Sep 17 00:00:00 2001 From: Dmitry Teselkin Date: Mon, 23 Nov 2015 18:55:10 +0300 Subject: [PATCH] Fix fence_ssh python script Import module atexit explicitly. Since a lot of packages were updated during migration to CentOS7 this script no more works without that. Probably it was imported implicitly, which is wrong anyway. Fix command line options changes according to updated fencing.py Remove test if needed shutdown slave because in any case all that we do is just restart the slave. Always. Blueprint: master-on-centos7 Compatible with CentOS6 & CentOS7 master node Change-Id: I0437a71ac59a46eac58714bb28d78882987e5938 --- .../cobbler/fence_ssh.centos6.py} | 43 +++++--- .../files/cobbler/fence_ssh.centos7.py | 102 ++++++++++++++++++ .../puppet/nailgun/manifests/cobbler.pp | 16 ++- 3 files changed, 147 insertions(+), 14 deletions(-) rename deployment/puppet/nailgun/{templates/cobbler/fence_ssh.erb => files/cobbler/fence_ssh.centos6.py} (61%) create mode 100644 deployment/puppet/nailgun/files/cobbler/fence_ssh.centos7.py diff --git a/deployment/puppet/nailgun/templates/cobbler/fence_ssh.erb b/deployment/puppet/nailgun/files/cobbler/fence_ssh.centos6.py similarity index 61% rename from deployment/puppet/nailgun/templates/cobbler/fence_ssh.erb rename to deployment/puppet/nailgun/files/cobbler/fence_ssh.centos6.py index ae1af53c4f..9976c11c45 100644 --- a/deployment/puppet/nailgun/templates/cobbler/fence_ssh.erb +++ b/deployment/puppet/nailgun/files/cobbler/fence_ssh.centos6.py @@ -1,8 +1,19 @@ #!/usr/bin/python -import sys, re, pexpect, exceptions +import atexit +import exceptions +import pexpect +import sys +import time + sys.path.append("/usr/share/fence") -from fencing import * +from fencing import all_opt +from fencing import atexit_handler +from fencing import check_input +from fencing import fence_action +from fencing import fence_login +from fencing import process_input +from fencing import show_docs # BEGIN_VERSION_GENERATION RELEASE_VERSION = "0.1.0" @@ -15,26 +26,28 @@ def get_power_status(conn, options): try: conn.sendline("/bin/echo 1") conn.log_expect(options, options["-c"], int(options["-Y"])) - except: + except Exception: return "off" return "on" + def set_power_status(conn, options): if options["-o"] == "off": try: conn.sendline("/sbin/reboot") conn.log_expect(options, options["-c"], int(options["-g"])) time.sleep(2) - except: + except Exception: pass + def main(): - device_opt = [ "help", "version", "agent", "quiet", "verbose", "debug", - "action", "ipaddr", "login", "passwd", "passwd_script", - "secure", "identity_file", "test", "port", "separator", - "inet4_only", "inet6_only", "ipport", - "power_timeout", "shell_timeout", - "login_timeout", "power_wait" ] + device_opt = ["help", "version", "agent", "quiet", "verbose", "debug", + "action", "ipaddr", "login", "passwd", "passwd_script", + "secure", "identity_file", "test", "port", "separator", + "inet4_only", "inet6_only", "ipport", + "power_timeout", "shell_timeout", + "login_timeout", "power_wait"] atexit.register(atexit_handler) @@ -53,10 +66,14 @@ def main(): options["-c"] = "\[EXPECT\]#\ " # this string will be appended to the end of ssh command - options["ssh_options"] = "-t -o 'StrictHostKeyChecking=no' '/bin/bash -c \"PS1=%s /bin/bash --noprofile --norc\"'" % options["-c"] - options["-X"] = "-t -o 'StrictHostKeyChecking=no' '/bin/bash -c \"PS1=%s /bin/bash --noprofile --norc\"'" % options["-c"] + strict = "-t -o 'StrictHostKeyChecking=no'" + bash = "/bin/bash --noprofile --norc" + options["ssh_options"] = "%s '/bin/bash -c \"PS1=%s %s\"'" % \ + (strict, options["-c"], bash) + options["-X"] = "%s '/bin/bash -c \"PS1=%s %s\"'" % \ + (strict, options["-c"], bash) - docs = { } + docs = {} docs["shortdesc"] = "Fence agent that can just reboot node via ssh" docs["longdesc"] = "fence_ssh is an I/O Fencing agent \ which can be used to reboot nodes via ssh." diff --git a/deployment/puppet/nailgun/files/cobbler/fence_ssh.centos7.py b/deployment/puppet/nailgun/files/cobbler/fence_ssh.centos7.py new file mode 100644 index 0000000000..1147dab593 --- /dev/null +++ b/deployment/puppet/nailgun/files/cobbler/fence_ssh.centos7.py @@ -0,0 +1,102 @@ +#!/usr/bin/python + +import atexit +import exceptions +import pexpect +import sys +import time + +sys.path.append("/usr/share/fence") +from fencing import all_opt +from fencing import atexit_handler +from fencing import check_input +from fencing import fence_action +from fencing import fence_login +from fencing import process_input +from fencing import show_docs + +# BEGIN_VERSION_GENERATION +RELEASE_VERSION = "0.1.0" +BUILD_DATE = "(built Wed Oct 31 11:20:18 UTC 2012)" +MIRANTIS_COPYRIGHT = "Copyright (C) Mirantis, Inc. 2012 All rights reserved." +# END_VERSION_GENERATION + + +def get_power_status(conn, options): + try: + conn.sendline("/bin/echo 1") + conn.log_expect(options, options["--command-prompt"], + int(options["--shell-timeout"])) + except Exception: + return "off" + return "on" + + +def set_power_status(conn, options): + if options["--action"] == "off": + try: + conn.sendline("/sbin/reboot") + conn.log_expect(options, options["--command-prompt"], + int(options["--power-timeout"])) + time.sleep(2) + except Exception: + pass + + +def main(): + device_opt = ["help", "version", "agent", "verbose", "debug", + "action", "ipaddr", "login", "passwd", "passwd_script", + "secure", "identity_file", "port", "separator", + "inet4_only", "inet6_only", "ipport", + "power_timeout", "shell_timeout", + "login_timeout", "power_wait"] + + atexit.register(atexit_handler) + + all_opt["login_timeout"]["default"] = 60 + + pinput = process_input(device_opt) + + # use ssh to manipulate node + pinput["--ssh"] = 1 + pinput["--command-prompt"] = ".*" + + options = check_input(device_opt, pinput) + + if options["--action"] != "off": + sys.exit(0) + + options["-c"] = "\[EXPECT\]#\ " + + # this string will be appended to the end of ssh command + options["ssh_options"] = "-t -o 'StrictHostKeyChecking=no' " \ + "'/bin/bash -c \"PS1=%s /bin/bash " \ + "--noprofile --norc\"'" % options["-c"] + options["-X"] = "-t -o 'StrictHostKeyChecking=no' " \ + "'/bin/bash -c \"PS1=%s /bin/bash " \ + "--noprofile --norc\"'" % options["-c"] + + docs = {} + docs["shortdesc"] = "Fence agent that can just reboot node via ssh" + docs["longdesc"] = "fence_ssh is an I/O Fencing agent " \ + "which can be used to reboot nodes via ssh." + show_docs(options, docs) + + # Operate the fencing device + + # this method will actually launch ssh command + conn = fence_login(options) + + result = fence_action(conn, options, set_power_status, + get_power_status, None) + + try: + conn.close() + except exceptions.OSError: + pass + except pexpect.ExceptionPexpect: + pass + + sys.exit(result) +if __name__ == "__main__": + main() diff --git a/deployment/puppet/nailgun/manifests/cobbler.pp b/deployment/puppet/nailgun/manifests/cobbler.pp index 4dfa29a5d7..a11498ce2a 100644 --- a/deployment/puppet/nailgun/manifests/cobbler.pp +++ b/deployment/puppet/nailgun/manifests/cobbler.pp @@ -46,6 +46,20 @@ class nailgun::cobbler( default => 'bootstrap', } + if $::osfamily == 'RedHat' { + case $operatingsystemmajrelease { + '6': { + $fence_ssh_source = 'puppet:///modules/nailgun/cobbler/fence_ssh.centos6.py' + } + '7': { + $fence_ssh_source = 'puppet:///modules/nailgun/cobbler/fence_ssh.centos7.py' + } + default: { + $fence_ssh_source = 'puppet:///modules/nailgun/cobbler/fence_ssh.centos6.py' + } + } + } + class { '::cobbler': server => $server, production => $production, @@ -88,7 +102,7 @@ class nailgun::cobbler( } file { '/usr/sbin/fence_ssh': - content => template('nailgun/cobbler/fence_ssh.erb'), + source => $fence_ssh_source, owner => 'root', group => 'root', mode => '0755',