summaryrefslogtreecommitdiffstats
path: root/agents/bladecenter/fence_bladecenter.py
blob: d670367f2ac5601b38b030dcfcd0ee968a204fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!@PYTHON@ -tt

#####
##
## The Following Agent Has Been Tested On:
##
##  Model                 Firmware
## +--------------------+---------------------------+
## (1) Main application	  BRET85K, rev 16
##     Boot ROM           BRBR67D, rev 16
##     Remote Control     BRRG67D, rev 16
##
#####

import sys, re
import atexit
sys.path.append("@FENCEAGENTSLIBDIR@")
from fencing import *
from fencing import fail, EC_STATUS, EC_GENERIC_ERROR

def get_power_status(conn, options):
	node_cmd = r"system:blade\[" + options["--plug"] + r"\]>"

	conn.send_eol("env -T system:blade[" + options["--plug"] + "]")
	i = conn.log_expect([node_cmd, "system>"], int(options["--shell-timeout"]))
	if i == 1:
		## Given blade number does not exist
		if "--missing-as-off" in options:
			return "off"
		else:
			fail(EC_STATUS)
	conn.send_eol("power -state")
	conn.log_expect(node_cmd, int(options["--shell-timeout"]))
	status = conn.before.splitlines()[-1]
	conn.send_eol("env -T system")
	conn.log_expect(options["--command-prompt"], int(options["--shell-timeout"]))

	return status.lower().strip()

def set_power_status(conn, options):
	node_cmd = r"system:blade\[" + options["--plug"] + r"\]>"

	conn.send_eol("env -T system:blade[" + options["--plug"] + "]")
	i = conn.log_expect([node_cmd, "system>"], int(options["--shell-timeout"]))
	if i == 1:
		## Given blade number does not exist
		if "--missing-as-off" in options:
			return
		else:
			fail(EC_GENERIC_ERROR)

	conn.send_eol("power -"+options["--action"])
	conn.log_expect(node_cmd, int(options["--shell-timeout"]))
	conn.send_eol("env -T system")
	conn.log_expect(options["--command-prompt"], int(options["--shell-timeout"]))

def get_blades_list(conn, options):
	outlets = {}

	node_cmd = "system>"

	conn.send_eol("env -T system")
	conn.log_expect(node_cmd, int(options["--shell-timeout"]))
	conn.send_eol("list -l 2")
	conn.log_expect(node_cmd, int(options["--shell-timeout"]))

	lines = conn.before.split("\r\n")
	filter_re = re.compile(r"^\s*blade\[(\d+)\]\s+(.*?)\s*$")
	for blade_line in lines:
		res = filter_re.search(blade_line)
		if res != None:
			outlets[res.group(1)] = (res.group(2), "")

	return outlets

def main():
	device_opt = ["ipaddr", "login", "passwd", "cmd_prompt", "secure", \
			"port", "missing_as_off", "telnet"]

	atexit.register(atexit_handler)

	all_opt["power_wait"]["default"] = "10"
	all_opt["cmd_prompt"]["default"] = ["system>"]

	options = check_input(device_opt, process_input(device_opt))

	docs = {}
	docs["shortdesc"] = "Fence agent for IBM BladeCenter"
	docs["longdesc"] = "fence_bladecenter is an I/O Fencing agent \
which can be used with IBM Bladecenters with recent enough firmware that \
includes telnet support. It logs into a Brocade chasis via telnet or ssh \
and uses the command line interface to power on and off blades."
	docs["vendorurl"] = "http://www.ibm.com"
	show_docs(options, docs)

	##
	## Operate the fencing device
	######
	conn = fence_login(options, "(username\s*:\s*)")
	result = fence_action(conn, options, set_power_status, get_power_status, get_blades_list)
	fence_logout(conn, "exit")
	sys.exit(result)

if __name__ == "__main__":
	main()