-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
280 lines (239 loc) · 10 KB
/
fabfile.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import time
from datetime import datetime
from fabric.api import env, local, lcd, roles, run, parallel, cd, put, execute, settings, shell_env, abort, hide, task, prefix
#------------------------------------------------------------------------------
# Author: Daniel Porto - [email protected]
# Last Update: may, 2 2018
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#environment variables
#------------------------------------------------------------------------------
# force read configuration in .ssh/config
env.use_ssh_config=True
# create a new ssh configuration with proxyjump support
ssh_config_file = "~/.ssh/cloudtm"
ssh_head_node = "inesc-head" #ip or ssh config name for the headnode
ssh_identity_file = "~/.ssh/key_openstack.pem"
# cloud properties ------------------------------------------------------------
# | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | extra_specs |
# | 141 | m4.large | 8192 | 20 | 0 | | 2 | 1.0 | True | {} |
# | 134 | m4.xlarge | 16384 | 30 | 0 | | 4 | 1.0 | True | {} |
# | 135 | m4.2xlarge | 32768 | 40 | 0 | | 8 | 1.0 | True | {} |
cloud_flavor="141"
# cloud images: centos7, kaioken-kernel-3.10 , kaioken-kernel-4.14 , kaioken-kernel-4.15
cloud_image="kaioken-kernel-4.15"
cloud_keyname="key_openstack"
cloud_availability_zones=["node01","node02","node03","node04","node05","node06","node07","node08","node09","node10"]
# cloud credentials
cloud_url="http://10.100.0.21:5000/v2.0"
tenant_id="XXXX"
tenant_name="YYY"
tenant_user="YYYY"
tenant_pw="PPPP"
# define the number of instances to create
clients = 5
# nodes are from ssh config file or you can place the IPS directly
env.roledefs={
'headnode': ['inesc-head'],
'manager' : ['172.10.0.12'],
'replicas' : ['172.10.0.20','172.10.0.21','172.10.0.22'],
}
#------------------------------------------------------------------------------
# internal tasks
#------------------------------------------------------------------------------
@task
@parallel
@roles('headnode', 'replicas', 'manager')
def check_storage():
with hide('commands'):
str1 = run("df -h ")
time.sleep(5)
print"node "+env.host_string+" storage: "+str1
@task
@parallel
@roles('headnode', 'replicas', 'manager')
def check_load():
with hide('commands'):
str1 = run("uptime")
time.sleep(5)
print"node "+env.host_string+" load:"+str1
#------------------------------------------------------------------------------
# cluster management tasks
#------------------------------------------------------------------------------
@task
@roles('headnode')
def list_clients():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
str1 = run("nova list")
if (len(str1)==0):
print "There is no client running at openstack!"
else:
print str1
@task
@roles('headnode')
def launch_client_instances():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
for i in range(clients):
command = "nova boot"
command += " --image "+cloud_image
command += " --flavor="+cloud_flavor
command += " --key-name "+cloud_keyname
command += " client"+str(i) # instance name
command += " --availability-zone nova:"+cloud_availability_zones[i]
print command
str1 = run(command)
print str1
@task
@roles('headnode')
def list_failed_clients():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
str1 = run("nova list | grep ERROR")
if (len(str1)==0):
print "There is no client running at openstack!"
else:
print str1
@task
@roles('headnode')
def terminate_failed_clients():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
try:
str1 = run("nova list | grep ERROR")
except:
print "There is no failed client running at openstack!"
return
if len(str1)> 0:
clients = str1.split("\n")
for client in clients:
client_id = client.split(" ")[1]
print client_id
command = "nova delete "+ client_id
run(command)
@task
@roles('headnode')
def terminate_all_clients():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
try:
str1 = run("nova list | grep '|' | grep -v Status")
except:
print "There is no client running at openstack!"
return
if len(str1)> 0:
clients = str1.split("\n")
for client in clients:
client_id = client.split(" ")[1]
print client_id
command = "nova delete "+ client_id
run(command)
@task
@roles('headnode')
def list_client_ips():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
try:
str1 = run("nova list | grep net")
except:
print "There is no failed client running at openstack!"
return
if len(str1)> 0:
clients = str1.split("\n")
for client in clients:
output = client.replace("|"," ")
output = " ".join(output.split()) # remove white spaces within the string
output = output.split(" ") # properly separate fields of the string
#print output
client_id = output[0]
client_name = output[1]
client_ip = output[3].split("=")[1]
print client_id, client_name, client_ip
# command = "nova delete "+ client_id
# run(command)
@task
@roles('headnode')
def generate_client_sshconfig():
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
try:
str1 = run("nova list | grep net")
except:
print "There is no failed client running at openstack!"
return
if len(str1)> 0:
clients = str1.split("\n")
print "Writing this output to "+ssh_config_file+" for local ssh connection and generating ansible inventory"
# initialize ssh configuration
local("echo '' > "+ssh_config_file)
entry=list()
client_list=list()
client_list_ip=list()
for client in clients:
output = client.replace("|"," ")
output = " ".join(output.split()) # remove white spaces within the string
output = output.split(" ") # properly separate fields of the string
print output
client_name = output[1]
client_ip = output[3].split("=")[1]
client_list.append(client_name)
client_list_ip.append(client_ip)
print "Ping node",client_ip,"to activate..."
run("ping -c3 "+client_ip) # just to ativate the node
entry.append("Host "+ client_name)
entry.append(" proxyjump "+ssh_head_node)
entry.append(" hostname "+ client_ip)
entry.append(' identityfile "'+ssh_identity_file+'"')
entry.append(" user centos")
entry.append(" port 22")
entry.append(" StrictHostKeyChecking no")
for line in entry:
local("echo '"+line+"' >> "+ssh_config_file)
print "Update this file with the following client list:"
print client_list_ip
print "you can ssh directly to the nodes from your machine:"
print "ssh ",client_list[0]
@roles('headnode')
def get_client_ip_list():
client_ips = list()
with hide('commands'), shell_env(OS_AUTH_URL=cloud_url,OS_TENANT_ID=tenant_id,\
OS_TENANT_NAME=tenant_name,OS_USERNAME=tenant_user,OS_PASSWORD=tenant_pw,\
OS_NO_CACHE="1"):
try:
str1 = run("nova list | grep net")
except:
print "There is no failed client running at openstack!"
return
if len(str1)> 0:
clients = str1.split("\n")
for client in clients:
output = client.replace("|"," ")
output = " ".join(output.split()) # remove white spaces within the string
output = output.split(" ") # properly separate fields of the string
client_ip = output[3].split("=")[1] # get client ip
client_ips.append(client_ip)
return client_ips
@task
@roles('headnode')
def ping_clients():
clients = get_client_ip_list()
for client_ip in clients:
out = run("ping -c2 "+client_ip) # just to ativate the node
print "Ping node",client_ip,"to activate:", out.split("\n")[-1]
#------------------------------------------------------------------------------
# public tasks
#------------------------------------------------------------------------------
def __public_tasks():
pass
@task
@roles('headnode')
def check_headnode():
run("uptime")