-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfence_workstation
executable file
·128 lines (106 loc) · 4.28 KB
/
fence_workstation
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
#!/usr/bin/python
# The Following Agent Has Been Tested On:
#
# Vmware fusion 4.1.3 on MacOS X 10.7.4
#
import sys
import pexpect
import exceptions
sys.path.append("/usr/share/fence")
from fencing import *
#BEGIN_VERSION_GENERATION
RELEASE_VERSION = "0.0.1"
BUILD_DATE = "(built Wed Dec 5 11:20:18 UTC 2012)"
REDHAT_COPYRIGHT = "Copyright (C) Eric Edgar"
#END_VERSION_GENERATION
def get_outlets_status(conn, options):
results = []
mac_results = {}
result = {}
try:
conn.sendline('''IFS=$'\n';for x in $(cat ~/.vmware/inventory.vmls|grep vmx|grep config|awk '{FS=" = ";print $2}'|grep -v "="|sed \'s|"||g\');do grep -H \"Address \" $x;done|sort|uniq ''')
conn.log_expect(options, options["-c"],int(options["-g"]))
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
results = conn.before.splitlines()[3:]
for line in results:
file, data = line.split(':', 1)
mac = data.split("= ")[1].strip("\"")
mac_results.setdefault(file, []).append(mac)
try:
conn.sendline("/usr/bin/vmrun -T ws list")
conn.log_expect(options, options["-c"],int(options["-Y"]))
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
results = conn.before.splitlines()[2:]
for line in results:
if line in mac_results:
for mac in mac_results[line]:
result[mac.replace(":", "_").upper()] = ("", "on")
return result
def get_power_status(conn, options):
outlets=get_outlets_status(conn,options)
return ((options["-n"] in outlets) and "on" or "off")
def set_power_status(conn, options):
try:
data = '''IFS=$'\n';for x in $(cat ~/.vmware/inventory.vmls|grep vmx|grep config|awk '{FS=" = ";print $2}'|grep -v "="|sed \'s|"||g\');do grep -H \"Address \" $x;done|grep -i %s|uniq '''
conn.sendline(data.replace('%s', options['-n'].replace("_", ":").upper()))
conn.log_expect(options, options["-c"], int(options["-g"]))
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
results = conn.before.splitlines()[4:]
for line in results:
file, data = line.split(':', 1)
if options['-n'].replace("_", ":").upper() in data.upper():
try:
conn.sendline("/usr/bin/vmrun -T ws %s" % (options["-o"] == "on" and "start '%s' nogui" % file or "stop '%s' hard" % file))
conn.log_expect(options, options["-c"], int(options["-g"]))
time.sleep(1)
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
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"]
atexit.register(atexit_handler)
pinput = process_input(device_opt)
pinput["-x"] = 1
options = check_input(device_opt, pinput)
## Defaults for fence agent
if not "-c" in options:
options["-c"] = "\[EXPECT\]#\ "
options["ssh_options"] = "-t '/bin/bash -c \"PS1=\[EXPECT\]#\ /bin/bash --noprofile --norc\"'"
docs = {}
docs["shortdesc"] = "Fence agent for vmware workstation"
docs["longdesc"] = "fence_workstation is an vmware workstation Fencing agent \
which can be used with the virtual machines managed by vmware workstation. \
It logs via ssh to a vmware workstation host and there run vmrun command, which does \
all work. \
\n.P\n\
By default, vmrun needs root account to do properly work. So you \
must allow ssh login in your sshd_config."
show_docs(options, docs)
## Operate the fencing device
conn = fence_login(options)
result = fence_action(conn, options, set_power_status, get_power_status, get_outlets_status)
## Logout from system
try:
conn.sendline("quit")
conn.close()
except exceptions.OSError:
pass
except pexpect.ExceptionPexpect:
pass
sys.exit(result)
if __name__ == "__main__":
main()