forked from eedgar/stonith-fences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfence_fusion
executable file
·138 lines (114 loc) · 4.91 KB
/
fence_fusion
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
#!/usr/bin/python
# The following agent has adapted from Eric Edgar's original fence agent by Ramon Acedo and has been tested on:
#
# VMware Fusion 6.0.5 on Mac OS X 10.10.1
import sys
import pexpect
import exceptions
import atexit
sys.path.append("/usr/share/fence")
from fencing import *
#BEGIN_VERSION_GENERATION
RELEASE_VERSION = "0.0.1"
BUILD_DATE = "(built Fri Aug 10 11:20:18 UTC 2012)"
REDHAT_COPYRIGHT = "Copyright (C) Eric Edgar"
#END_VERSION_GENERATION
# Returns an array with all the MACs of all the VMs that are powered on
def get_outlets_status(conn, options):
results = []
mac_results = {}
result = {}
try:
conn.sendline('''IFS=$\'\\n\';for x in $(mdfind vmwarevm |grep ".vmwarevm$"); do IFS=$\'\\n\';find $x -name \*vmx -exec grep -H "Address " {} \; ;done|sort|uniq''')
conn.log_expect(options, options["--command-prompt"], int(options["--shell-timeout"]))
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
else:
results = conn.before.splitlines()[3:-1]
for line in results:
if "generatedAddress" in line:
file, data = line.split(':', 1)
mac = data.split("= ")[1].strip("\"")
mac_results.setdefault(file, []).append(mac)
try:
conn.sendline("/Applications/VMware\ Fusion.app/Contents/Library/vmrun list")
conn.log_expect(options, options["--command-prompt"], int(options["--shell-timeout"]))
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
else:
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["--plug"].upper() in outlets) and "on" or "off")
def set_power_status(conn, options):
try:
data = '''IFS=$\'\\n\';for x in $(mdfind vmwarevm |grep ".vmwarevm$"); do IFS=$\'\\n\';find $x -name \*vmx -exec grep -H "Address " {} \; ;done|grep -i %s|uniq'''
conn.sendline(data.replace('%s', options['--plug'].replace("_", ":").upper()))
conn.log_expect(options, options["--command-prompt"], int(options["--shell-timeout"]))
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
else:
results = conn.before.splitlines()[3:]
for line in results:
if "generatedAddress" in line:
file, data = line.split(':', 1)
if options['--plug'].replace("_", ":").upper() in data.upper():
try:
conn.sendline("/Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion %s" % (options["--action"] == "on" and "start '%s' gui" % file or "stop '%s' hard" % file))
conn.log_expect(options, options["--command-prompt"], int(options["--shell-timeout"]))
time.sleep(1)
except pexpect.EOF:
fail(EC_CONNECTION_LOST)
except pexpect.TIMEOUT:
fail(EC_TIMED_OUT)
def main():
device_opt = ["help", "version", "agent", "verbose", "debug",
"action", "ipaddr", "login", "passwd", "passwd_script",
"secure", "identity_file", "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 "--command-prompt" in options:
options["--command-prompt"] = "\$\ "
if not "--action" in options:
options["--action"] = "reboot"
options["ssh_options"] = "-t '/bin/bash -c \"PS1=\[EXPECT\]$\ /bin/bash --noprofile --norc\"'"
docs = {}
docs["shortdesc"] = "Fence agent for vmware fusion"
docs["longdesc"] = "fence_fusion is an vmware fusionFencing agent \
which can be used with the virtual machines managed by vmware fusion. \
It logs via ssh to a vmware fusion 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()