1    	#!/usr/bin/python -tt
2    	
3    	#####
4    	##
5    	## The Following Agent Has Been Tested On:
6    	##
7    	##  Version
8    	## +---------------------------------------------+
9    	##  Tested on HMC
10   	##
11   	#####
12   	
13   	import sys, re
14   	import atexit
15   	sys.path.append("@FENCEAGENTSLIBDIR@")
16   	from fencing import *
17   	from fencing import fail, fail_usage, EC_STATUS_HMC
18   	
19   	#BEGIN_VERSION_GENERATION
20   	RELEASE_VERSION=""
21   	REDHAT_COPYRIGHT=""
22   	BUILD_DATE=""
23   	#END_VERSION_GENERATION
24   	
25   	def get_power_status(conn, options):
26   		if options["--hmc-version"] == "3":
27   			conn.send("lssyscfg -r lpar -m " + options["--managed"] + " -n " + options["--plug"] + " -F name,state\n")
28   			conn.log_expect(options["--command-prompt"], int(options["--power-timeout"]))
29   	
30   			try:
31   				status = re.compile("^" + options["--plug"] + ",(.*?),.*$",
32   						re.IGNORECASE | re.MULTILINE).search(conn.before).group(1)
33   			except AttributeError:
34   				fail(EC_STATUS_HMC)
35   		elif options["--hmc-version"] == "4":
36   			conn.send("lssyscfg -r lpar -m "+ options["--managed"] +
37   					" --filter 'lpar_names=" + options["--plug"] + "'\n")
38   			conn.log_expect(options["--command-prompt"], int(options["--power-timeout"]))
39   	
40   			try:
41   				status = re.compile(",state=(.*?),", re.IGNORECASE).search(conn.before).group(1)
42   			except AttributeError:
43   				fail(EC_STATUS_HMC)
44   	
45   		##
46   		## Transformation to standard ON/OFF status if possible
47   		if status in ["Running", "Open Firmware", "Shutting Down", "Starting"]:
48   			status = "on"
49   		else:
50   			status = "off"
51   	
52   		return status
53   	
54   	def set_power_status(conn, options):
55   		if options["--hmc-version"] == "3":
56   			conn.send("chsysstate -o " + options["--action"] + " -r lpar -m " + options["--managed"]
57   				+ " -n " + options["--plug"] + "\n")
58   			conn.log_expect(options["--command-prompt"], int(options["--power-timeout"]))
59   		elif options["--hmc-version"] == "4":
60   			if options["--action"] == "on":
61   				conn.send("chsysstate -o on -r lpar -m " + options["--managed"] +
62   					" -n " + options["--plug"] +
63   					" -f `lssyscfg -r lpar -F curr_profile " +
64   					" -m " + options["--managed"] +
65   					" --filter \"lpar_names=" + options["--plug"] + "\"`\n")
66   			else:
67   				conn.send("chsysstate -o shutdown -r lpar --immed" +
68   					" -m " + options["--managed"] + " -n " + options["--plug"] + "\n")
69   			conn.log_expect(options["--command-prompt"], int(options["--power-timeout"]))
70   	
71   	def get_lpar_list(conn, options):
72   		outlets = {}
(1) Event cond_false: Condition "options["--hmc-version"] == "3"", taking false branch.
73   		if options["--hmc-version"] == "3":
74   			conn.send("query_partition_names -m " + options["--managed"] + "\n")
75   			conn.log_expect(options["--command-prompt"], int(options["--power-timeout"]))
76   	
77   			## We have to remove first 3 lines (command + header) and last line (part of new prompt)
78   			####
79   			res = re.search("^.+?\n(.+?\n){2}(.*)\n.*$", conn.before, re.S)
80   	
81   			if res == None:
82   				fail_usage("Unable to parse output of list command")
83   	
84   			lines = res.group(2).split("\n")
85   			for outlet_line in lines:
86   				outlets[outlet_line.rstrip()] = ("", "")
(2) Event else_branch: Reached else branch.
(3) Event cond_true: Condition "options["--hmc-version"] == "4"", taking true branch.
87   		elif options["--hmc-version"] == "4":
88   			conn.send("lssyscfg -r lpar -m " + options["--managed"] +
89   				" -F name:state\n")
90   			conn.log_expect(options["--command-prompt"], int(options["--power-timeout"]))
91   	
92   			## We have to remove first line (command) and last line (part of new prompt)
93   			####
94   			res = re.search("^.+?\n(.*)\n.*$", conn.before, re.S)
95   	
(4) Event cond_true: Condition "res == None", taking true branch.
(5) Event null_check: Comparing "res" to a null-like value implies that "res" might be null-like.
Also see events: [property_access]
96   			if res == None:
97   				fail_usage("Unable to parse output of list command")
98   	
(6) Event property_access: Accessing a property of null-like value "res".
Also see events: [null_check]
99   			lines = res.group(1).split("\n")
100  			for outlet_line in lines:
101  				(port, status) = outlet_line.split(":")
102  				outlets[port] = ("", status)
103  	
104  		return outlets
105  	
106  	def define_new_opts():
107  		all_opt["managed"] = {
108  			"getopt" : "s:",
109  			"longopt" : "managed",
110  			"help" : "-s, --managed=[id]             Name of the managed system",
111  			"required" : "0",
112  			"shortdesc" : "Managed system name",
113  			"order" : 1}
114  		all_opt["hmc_version"] = {
115  			"getopt" : "H:",
116  			"longopt" : "hmc-version",
117  			"help" : "-H, --hmc-version=[version]    Force HMC version to use: (3|4) (default: 4)",
118  			"required" : "0",
119  			"shortdesc" : "Force HMC version to use",
120  			"default" : "4",
121  			"choices" : ["3", "4"],
122  			"order" : 1}
123  	
124  	def main():
125  		device_opt = ["ipaddr", "login", "passwd", "secure", "cmd_prompt", \
126  		                "port", "managed", "hmc_version"]
127  	
128  		atexit.register(atexit_handler)
129  	
130  		define_new_opts()
131  	
132  		all_opt["login_timeout"]["default"] = "15"
133  		all_opt["secure"]["default"] = "1"
134  		all_opt["cmd_prompt"]["default"] = [r":~>", r"]\$", r"\$ "]
135  	
136  		options = check_input(device_opt, process_input(device_opt), other_conditions = True)
137  	
138  		docs = {}
139  		docs["shortdesc"] = "Fence agent for IBM LPAR"
140  		docs["longdesc"] = ""
141  		docs["vendorurl"] = "http://www.ibm.com"
142  		show_docs(options, docs)
143  	
144  		if not options.has_key("--managed"):
145  			fail_usage("Failed: You have to enter name of managed system")
146  	
147  		if options["--action"] == "validate-all":
148  			sys.exit(0)
149  	
150  		##
151  		## Operate the fencing device
152  		####
153  		conn = fence_login(options)
154  		result = fence_action(conn, options, set_power_status, get_power_status, get_lpar_list)
155  		fence_logout(conn, "quit\r\n")
156  		sys.exit(result)
157  	
158  	if __name__ == "__main__":
159  		main()
160