-
Notifications
You must be signed in to change notification settings - Fork 2
/
fence_apc_vbox
executable file
·218 lines (187 loc) · 6.4 KB
/
fence_apc_vbox
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/python
#####
##
## APC Simulator with VirtualBox toolkit
##
## Host VirtualBox version
## +---------------------------------------------+
## Windows 7 4.3.12r93733
##
#####
import sys, os, re, pexpect, exceptions
import traceback
import zmq
import time
#sys.path.append("/home/roobot/src/fence_sim/")
sys.path.append("./")
from fencing_vbox import *
#BEGIN_VERSION_GENERATION
RELEASE_VERSION="1.0.2"
BUILD_DATE="(built Wed July 30 07:52:12 UTC 2014)"
GALAXYIO_COPYRIGHT="Copyright (C) GalaxyIO Technologies Co., Ltd. 2013-2014 All rights reserved."
#END_VERSION_GENERATION
class apc_vbox_conn():
def __init__(self):
# FIXME: will be dropped later
self.power_state = "off"
def start(self, ipaddr, port):
try:
self.ctxt = zmq.Context()
self.sock = self.ctxt.socket(zmq.REQ)
self.sock.connect("tcp://" + ipaddr + ":" + port);
except Exception, ex:
syslog.syslog(syslog.LOG_NOTICE, str(ex))
pass
def stop(self):
self.ctxt.destroy()
def set_power_status(self, ops_str, plug_num):
self.sock.send(ops_str + ' ' + str(plug_num), copy=True)
self.power_state = self.sock.recv()
def get_power_status(self, plug_num):
# send request and wait response
# return value: "on" | "off" | "NA"
self.sock.send('GetPowerState ' + str(plug_num), copy=True)
self.power_state = self.sock.recv()
return self.power_state
def fence_login(options):
try:
conn = apc_vbox_conn()
conn.start(options["--ip"], options["--ipport"]);
#s.send(target_box + ' ' + fence_ops, copy=True)
syslog.syslog(syslog.LOG_INFO, "APC Simulator login.")
return conn
except Exception, ex:
syslog.syslog(syslog.LOG_NOTICE, str(ex))
pass
def fence_logout(conn):
try:
conn.stop()
syslog.syslog(syslog.LOG_INFO, "APC Simulator logout.")
except Exception, ex:
syslog.syslog(syslog.LOG_NOTICE, str(ex))
pass
def get_power_status(conn, options):
return conn.get_power_status(options["--plug"])
def set_power_status(conn, options):
action = {
'on' : "1",
'off': "2"
}[options["--action"]]
if (action == "1"):
conn.set_power_status("on", options["--plug"])
return "on"
elif (action == "2"):
conn.set_power_status("off", options["--plug"])
return "off"
else:
syslog.syslog(syslog.LOG_NOTICE, "Unknown action: " + options["--action"])
return
def wait_power_status2(tn, options):
for dummy in xrange(int(options["--power-timeout"])):
if get_power_status(tn, options) != options["--action"]:
time.sleep(1)
else:
return 1
return 0
def fence_action(tn, options, set_power_fn, get_power_fn, get_outlet_list = None):
result = 0
#syslog.syslog(syslog.LOG_INFO, "Tgt Plug:" + options["--plug"])
try:
## Process options that manipulate fencing device
#####
if (options["--action"] == "list"):
print "NOTICE: List option is not working on this device yet"
return 0
status = get_power_status(tn, options)
if status != "on" and status != "off":
fail(EC_STATUS)
if options["--action"] == "on":
if status == "on":
print "Success: Already ON"
else:
power_on = False
set_power_status(tn, options)
time.sleep(int(options["--power-wait"]))
if wait_power_status2(tn, options):
power_on = True
if power_on:
print "Success: Powered ON"
else:
fail(EC_WAITING_ON)
elif options["--action"] == "off":
if status == "off":
print "Success: Already OFF"
else:
set_power_status(tn, options)
time.sleep(int(options["--power-wait"]))
if wait_power_status2(tn, options):
print "Success: Powered OFF"
else:
fail(EC_WAITING_OFF)
elif options["--action"] == "reboot":
syslog.syslog(syslog.LOG_INFO, "action: reboot")
if status != "off":
options["--action"] = "off"
set_power_status(tn, options)
time.sleep(int(options["--power-wait"]))
if wait_power_status2(tn, options) == 0:
fail(EC_WAITING_OFF)
options["--action"] = "on"
power_on = False
try:
for _ in range(1, 1 + int(options["--retry-on"])):
set_power_status(tn, options)
time.sleep(int(options["--power-wait"]))
if wait_power_status2(tn, options) == 1:
power_on = True
break
except Exception, ex:
# an error occured during power ON phase in reboot
# fence action was completed succesfully even in that case
sys.stderr.write(str(ex))
syslog.syslog(syslog.LOG_NOTICE, str(ex))
pass
if power_on == False:
# this should not fail as node was fenced succesfully
sys.stderr.write('Timed out waiting to power ON\n')
syslog.syslog(syslog.LOG_NOTICE, "Timed out waiting to power ON")
print "Success: Rebooted"
elif options["--action"] == "status":
print "Status: " + status.upper()
if status.upper() == "OFF":
result = 2
elif options["--action"] == "monitor":
result = 0
pass
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
except pycurl.error, ex:
sys.stderr.write(ex[1] + "\n")
syslog.syslog(syslog.LOG_ERR, ex[1])
fail(EC_TIMED_OUT)
return result
#########################################################################
def main():
device_opt = [ "ipaddr", "cmd_prompt", "port", "no_login", "no_password" ]
atexit.register(atexit_handler)
options = check_input(device_opt, process_input(device_opt))
docs = { }
docs["shortdesc"] = "Fence agent for VBox APC Simulator over telnet"
docs["longdesc"] = "fence_apc_vbox is an I/O Fencing agent\
which can be used with the VBox APC APU simulator. It logs into device \
via telnet and reboots a specified outlet. Lengthy telnet connections \
should be avoided while a GFS cluster is running because the connection \
will block any necessary fencing actions."
docs["vendorurl"] = "http://www.galaxyio.com"
show_docs(options, docs)
syslog.syslog(syslog.LOG_INFO, str(options))
conn = fence_login(options)
syslog.syslog(syslog.LOG_INFO, "login done")
result = -1
result = fence_action(conn, options, set_power_status, get_power_status, get_power_status)
fence_logout(conn)
sys.exit(result)
if __name__ == "__main__":
main()