-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenstack
executable file
·204 lines (181 loc) · 5.6 KB
/
openstack
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
#!/usr/bin/python
#
# Description: External STONITH module for openstack guests
#
# Author: Kim-Norman Sahm
# Email: kim-norman.sahm@t-online.de
# Licence: GPL
##################################################################
import sys
import os
import socket
from novaclient import client
global _debug
# Enable/disable debug mode
_debug = 0
if _debug:
fobj = open("/var/log/stonith-openstack.log", "a")
argv = sys.argv
try:
cmd = argv[1]
except IndexError:
my_err("Not enough arguments")
sys.exit(1)
if _debug:
fobj.write(cmd + "\n")
xmlinfo = '''<parameters>
<parameter name="openstack_instance" required="1">
<content type="string"/>
<shortdesc lang="en">openstack instance name</shortdesc>
<longdesc lang="en">
Contains the openstack instance (vm) name
</longdesc>
</parameter>
<parameter name="openstack_username" required="1">
<content type="string"/>
<shortdesc lang="en">openstack username</shortdesc>
<longdesc lang="en">
Contains the openstack username
</longdesc>
</parameter>
<parameter name="openstack_password" required="1">
<content type="string" default=""/>
<shortdesc lang="en">openstack password</shortdesc>
<longdesc lang="en">
The password for the openstack user
</longdesc>
</parameter>
<parameter name="openstack_tenant" required="1">
<content type="string" default=""/>
<shortdesc lang="en">openstack tenant (project) name</shortdesc>
<longdesc lang="en">
Contains the openstack tenant (project) name
</longdesc>
</parameter>
<parameter name="openstack_authurl" required="1">
<content type="string" default=""/>
<shortdesc lang="en">openstack authentication url (Keystone url)</shortdesc>
<longdesc lang="en">
Contains the openstack authentication url.
Example: https://mycloud.example.com:5000/v2.0/
</longdesc>
</parameter>
<parameter name="openstack_poweraction" unique="0" required="0">
<content type="string" default="reset"/>
<shortdesc lang="en">Openstack power action method</shortdesc>
<longdesc lang="en">
Options:
* reboot - soft reboot the instance
* reboot-force - hard reboot the instance
* shutdown - shutdown the instance
</longdesc>
</parameter>
</parameters>'''
info = {
'getinfo-xml': xmlinfo,
'getinfo-devdescr': 'OpenStack STONITH device',
'getinfo-devid': 'OpenStack instance ' + socket.gethostname()
}
if cmd in info:
print info[cmd]
if _debug:
fobj.write(info[cmd]+"\n")
sys.exit(0)
if cmd == 'getconfignames':
for arg in [ "openstack_instance", "openstack_username", "openstack_password", "openstack_tenant", "openstack_authurl", "openstack_poweraction"]:
print arg
sys.exit(0)
### PLACEHOLDER - function must be created
if cmd == 'status':
if _debug:
fobj.write("Status = 0\n")
sys.exit(0)
try:
instance = os.environ.get('openstack_instance').split("=")[1]
except:
try:
instance = os.environ.get('openstack_instance')
except:
if _debug:
for key in os.environ.keys():
fobj.write("%30s %s \n" % (key,os.environ[key]))
try:
username = os.environ.get('openstack_username').split("=")[1]
except:
username = os.environ.get('openstack_username')
try:
password = os.environ.get('openstack_password').split("=")[1]
except:
password = os.environ.get('openstack_password')
try:
tenant = os.environ.get('openstack_tenant').split("=")[1]
except:
tenant = os.environ.get('openstack_tenant')
try:
authurl = os.environ.get('openstack_authurl').split("=")[1]
except:
authurl = os.environ.get('openstack_authurl')
try:
action = os.environ.get('openstack_poweraction').split("=")[1]
except:
action = os.environ.get('openstack_poweraction')
if _debug:
fobj.write("instance = " + instance+"\n")
fobj.write("username = " + username+"\n")
fobj.write("password = " + password+"\n")
fobj.write("tenant = " + tenant+"\n")
fobj.write("authurl = " + authurl+"\n")
fobj.write("action = " + action+"\n")
if instance == "":
print "ERROR - you must set a instance name"
sys.exit(3)
elif username == "":
print "ERROR - you must set your openstack username"
sys.exit(3)
elif password == "":
print "ERROR - you must set your openstack password"
sys.exit(3)
elif tenant == "":
print "ERROR - you must set your openstack tenant (project) name"
sys.exit(3)
elif authurl == "":
sys.exit(3)
print "ERROR - you must set the authentication url (keystone url) from the openstack cloud"
sys.exit(3)
elif action != "reboot" and action != "reboot-force" and action != "shutdown":
print "ERROR - you must set a shutdown action (reboot, reboot-force or shutdown)"
sys.exit(3)
if cmd == 'gethosts':
print instance
if _debug:
fobj.write("gethosts = " + instance+"\n")
sys.exit(0)
nova = client.Client(2, username, password, tenant, authurl)
found = 0
instances = nova.servers.list()
for vm in instances:
if vm.name == instance:
found = 1
currentstatus = vm.status
id = vm.id
if _debug:
print "Found instance '" + instance +"' in tenant '" + tenant + "'"
break
if found == 1:
if _debug:
print "Current state of instance '" + instance + "': " + currentstatus
else:
print "ERROR - Cannot find any instance with name '" + instance + "'"
sys.exit(3)
try:
if action == "reboot":
nova.servers.reboot(id,reboot_type='SOFT')
elif action == "reboot-force":
nova.servers.reboot(id,reboot_type='HARD')
else:
nova.servers.stop(id)
except:
print "ERROR - Cannot shutdown instance '" + instance + "'"
exit(4)
if _debug:
fobj.close()