-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinode
More file actions
200 lines (155 loc) · 5.89 KB
/
linode
File metadata and controls
200 lines (155 loc) · 5.89 KB
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
#!/usr/bin/env python
"""
Linux-HA STONITH module for the Linode API
==========================================
This script implements STONITH using the Linode API -- if a node in your
cluster becomes unavailable, it can be fenced by issuing shutdown/boot/reboot
commands to the API.
Dependencies
------------
python-pycurl (not absolutely necessary, but linode-python uses it for better
SSL certificate validation):
- apt-get install python-pycurl
The Python bindings to the Linode API:
- sudo pip install linode-python
Configuration
-------------
Copy this script to /usr/lib/stonith/plugins/external, and make it
executable.
**IMPORTANT!** Make sure your Linodes have labels that match their node names in
your HA configuration. This is crucial for obtaining their Linode IDs for the
API calls.
Example CRM configuration snippet:
primitive st-linode stonith:external/linode \
params hostlist="node1 node2 node3" api_key="YOUR_API_KEY"
clone fencing st-linode
Copyright (c) 2012 Fairview Computing, LLC.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import subprocess
import sys
from linode import api
class LinodeStonithPlugin(object):
linodes = {}
def __init__(self, api_key=None, hostlist=''):
self.api_key = api_key
self.hostlist = hostlist
def build_api(self):
"""
Set up the API, and map the node names as defined in the CRM
configuration to their Linode IDs. This requires that node names match
the labels given to the Linodes in the Linode Manager.
"""
self.api = api.Api(self.api_key)
linodes = {}
for host in hostlist.split():
linodes[host] = 1
node_list = self.api.linode_list()
for node in node_list:
if node['LABEL'] in linodes:
self.linodes[node['LABEL']] = node['LINODEID']
def execute(self, argv):
if len(argv) < 1:
self.log("err", 'At least one argument required.')
cmd = argv[0].lower().replace('-', '_')
m = getattr(self, cmd, None)
if m:
if cmd in ['off', 'on', 'reset']:
m(argv[1])
else:
m()
else:
self.not_implemented(cmd)
def getconfignames(self):
print 'hostlist api_key'
def gethosts(self):
print self.hostlist
def getinfo_devdescr(self):
print 'STONITH device for controlling Linodes via the Linode API'
def getinfo_devid(self):
print 'Linode API STONITH device'
def getinfo_devname(self):
print 'Linode API STONITH external device'
def getinfo_devurl(self):
print 'https://github.com/fairview/stonith-linode'
def getinfo_xml(self):
print """<parameters>
<parameter name="hostlist" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">List of host names</shortdesc>
<longdesc lang="en">
A list of CRM nodes to control. These names must be the same as their Linode labels.
</longdesc>
</parameter>
<parameter name="api_key" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">Linode API key</shortdesc>
<longdesc lang="en">
Your Linode API key.
</longdesc>
</parameter>
</parameters>
"""
def log(self, level, *args):
cmd = ["ha_log.sh", level]
cmd.extend(args)
subprocess.call(cmd, shell=False)
def not_implemented(self, cmd):
self.log('err', "Command '%s' not implemented." % (cmd,))
return(1)
def off(self, node):
self.build_api()
linode_id = self.linodes[node]
self.api.linode_shutdown(LinodeID=linode_id)
self.log(
'info',
"Sent shutdown request for Linode %s (%s)." % (node, linode_id)
)
def on(self, node):
self.build_api()
linode_id = self.linodes[node]
self.api.linode_boot(LinodeID=linode_id)
self.log(
'info',
"Sent boot request for Linode %s (%s)." % (node, linode_id)
)
def reset(self, node):
self.build_api()
linode_id = self.linodes[node]
self.api.linode_reboot(LinodeID=linode_id)
self.log(
'info',
"Sent reboot request for Linode %s (%s)." % (node, linode_id)
)
def status(self):
"""
If the Linode API is available, report that the fencing device is OK.
"""
self.build_api()
self.api.linode_list()
if __name__ == '__main__':
hostlist = os.environ.get('hostlist', '')
api_key = os.environ.get('api_key', '')
linode = LinodeStonithPlugin(api_key, hostlist)
if len(sys.argv) < 2:
linode.log("err", 'At least one argument required.')
sys.exit(1)
linode.execute(sys.argv[1:])