Created
November 3, 2013 12:10
-
-
Save ahmedsbytes/7289565 to your computer and use it in GitHub Desktop.
A python script that restarts other node on ovh when fencing occurs
Original post http://forum.proxmox.com/threads/11066-Proxmox-HA-Cluster-at-OVH-Fencing?p=75152
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assemled by Dennis Busch, secofor GmbH, | |
# Germany | |
# This work is licensed under a | |
# Creative Commons Attribution-ShareAlike 3.0 Unported License. | |
# Manual call parametres example | |
# | |
# login=ab12345-ovh | |
# passwd=MYSECRET | |
# email=admin@myadmin | |
# ipaddr=ns12345 | |
# action=off | |
# # where ipaddr is your server's OVH name | |
import sys, re, pexpect | |
sys.path.append("/usr/share/fence") | |
from fencing import * | |
import sys | |
from suds.client import Client | |
from suds.xsd.doctor import ImportDoctor, Import | |
import time | |
from datetime import datetime | |
OVH_RESCUE_PRO_NETBOOT_ID='28' | |
OVH_HARD_DISK_NETBOOT_ID='1' | |
STATUS_HARD_DISK_SLEEP=240 # Wait 4 minutes to SO to boot | |
STATUS_RESCUE_PRO_SLEEP=150 # Wait 2 minutes 30 seconds to Rescue-Pro to run | |
OVH_FENCE_DEBUG=False # True or False for debug | |
def netboot_reboot(nodeovh,login,passwd,email,mode): | |
imp = Import('http://schemas.xmlsoap.org/soap/encoding/') | |
url='https://www.ovh.com/soapi/soapi-re-1.59.wsdl' | |
imp.filter.add('http://soapi.ovh.com/manager') | |
d = ImportDoctor(imp) | |
soap = Client(url, doctor=d) | |
session = soap.service.login(login, passwd, 'es', 0) | |
#dedicatedNetbootModifyById changes the mode of the next reboot | |
result = soap.service.dedicatedNetbootModifyById(session, nodeovh, mode, '', email) | |
#dedicatedHardRebootDo initiates a hard reboot on the given node | |
soap.service.dedicatedHardRebootDo(session, nodeovh, 'Fencing initiated by cluster', '', 'es') | |
soap.service.logout(session) | |
def reboot_status(nodeovh,login,passwd): | |
imp = Import('http://schemas.xmlsoap.org/soap/encoding/') | |
url='https://www.ovh.com/soapi/soapi-re-1.59.wsdl' | |
imp.filter.add('http://soapi.ovh.com/manager') | |
d = ImportDoctor(imp) | |
soap = Client(url, doctor=d) | |
session = soap.service.login(login, passwd, 'es', 0) | |
result = soap.service.dedicatedHardRebootStatus(session, nodeovh) | |
tmpstart = datetime.strptime(result.start,'%Y-%m-%d %H:%M:%S') | |
tmpend = datetime.strptime(result.end,'%Y-%m-%d %H:%M:%S') | |
result.start = tmpstart | |
result.end = tmpend | |
soap.service.logout(session) | |
return result | |
#print stderr to file | |
save_stderr = sys.stderr | |
errlog = open("/var/log/fence_ovh_error.log","a") | |
sys.stderr = errlog | |
global all_opt | |
device_opt = [ "email", "ipaddr", "action" , "login" , "passwd" , "nodename" ] | |
ovh_fence_opt = { | |
"email" : { | |
"getopt" : "Z:", | |
"longopt" : "email", | |
"help" : "-Z, --email=<email> email for reboot message: admin@domain.com", | |
"required" : "1", | |
"shortdesc" : "Reboot email", | |
"default" : "", | |
"order" : 1 }, | |
} | |
all_opt.update(ovh_fence_opt) | |
all_opt["ipaddr"]["shortdesc"] = "OVH node name" | |
atexit.register(atexit_handler) | |
options=check_input(device_opt,process_input(device_opt)) | |
# Not sure if I need this old notation | |
## Support for -n [switch]:[plug] notation that was used before | |
if ((options.has_key("-n")) and (-1 != options["-n"].find(":"))): | |
(switch, plug) = options["-n"].split(":", 1) | |
if ((switch.isdigit()) and (plug.isdigit())): | |
options["-s"] = switch | |
options["-n"] = plug | |
if (not (options.has_key("-s"))): | |
options["-s"]="1" | |
docs = { } | |
docs["shortdesc"] = "Fence agent for OVH" | |
docs["longdesc"] = "fence_ovh is an Power Fencing agent \ | |
which can be used within OVH datecentre. \ | |
Poweroff is simulated with a reboot into rescue-pro \ | |
mode. \ | |
/usr/local/etc/ovhsecret example: \ | |
\ | |
[OVH] \ | |
Login = ab12345-ovh \ | |
Passwd = MYSECRET \ | |
" | |
docs["vendorurl"] = "http://www.ovh.net" | |
show_docs(options, docs) | |
#I use a own logfile for debugging purpose | |
if OVH_FENCE_DEBUG: | |
logfile=open("/var/log/fence_ovh.log", "a"); | |
logfile.write(time.strftime("\n%d.%m.%Y %H:%M:%S \n")) | |
logfile.write("Parameter:\t") | |
for val in sys.argv: | |
logfile.write(val + " ") | |
logfile.write("\n") | |
print options | |
action=options['--action'] | |
email=options['--email'] | |
login=options['--username'] | |
passwd=options['--password'] | |
nodeovh=options['--ip'] | |
if nodeovh[-8:] != '.ovh.net': | |
nodeovh += '.ovh.net' | |
# Save datetime just before changing netboot | |
before_netboot_reboot = datetime.now() | |
if action == 'off': | |
netboot_reboot(nodeovh,login,passwd,email,OVH_RESCUE_PRO_NETBOOT_ID) #Reboot in Rescue-pro | |
elif action == 'on': | |
netboot_reboot(nodeovh,login,passwd,email,OVH_HARD_DISK_NETBOOT_ID) #Reboot from HD | |
elif action == 'reboot': | |
netboot_reboot(nodeovh,login,passwd,email,OVH_HARD_DISK_NETBOOT_ID) #Reboot from HD | |
else: | |
if OVH_FENCE_DEBUG: | |
logfile.write("nothing to do\n") | |
logfile.close() | |
errlog.close() | |
sys.exit() | |
if action == 'off': | |
time.sleep(STATUS_RESCUE_PRO_SLEEP) #Reboot in vKVM | |
elif action == 'on': | |
time.sleep(STATUS_HARD_DISK_SLEEP) #Reboot from HD | |
elif action == 'reboot': | |
time.sleep(STATUS_HARD_DISK_SLEEP) #Reboot from HD | |
else: | |
if OVH_FENCE_DEBUG: | |
logfile.write("No sense! Check script please!\n") | |
logfile.close() | |
errlog.close() | |
sys.exit() | |
after_netboot_reboot = datetime.now() | |
# Verification of success | |
reboot_start_end=reboot_status(nodeovh,login,passwd) | |
if OVH_FENCE_DEBUG: | |
logfile.write("reboot_start_end.start: " +reboot_start_end.start.strftime('%Y-%m-%d %H:%M:%S')+"\n") | |
logfile.write("before_netboot_reboot: " +before_netboot_reboot.strftime('%Y-%m-%d %H:%M:%S')+"\n") | |
logfile.write("reboot_start_end.end: " +reboot_start_end.end.strftime('%Y-%m-%d %H:%M:%S')+"\n") | |
logfile.write("after_netboot_reboot: " +after_netboot_reboot.strftime('%Y-%m-%d %H:%M:%S')+"\n") | |
if ((reboot_start_end.start > before_netboot_reboot) and (reboot_start_end.end < after_netboot_reboot)): | |
if OVH_FENCE_DEBUG: | |
logfile.write("Netboot reboot went OK.\n") | |
else: | |
if OVH_FENCE_DEBUG: | |
logfile.write("ERROR: Netboot reboot wasn't OK.\n") | |
logfile.close() | |
errlog.close() | |
sys.exit(1) | |
if OVH_FENCE_DEBUG: | |
logfile.close() | |
errlog.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment