-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartSparkCluster.py
330 lines (299 loc) · 11.1 KB
/
StartSparkCluster.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sys
import getopt
import socket
import subprocess
import os
import stat
import time
csv_rows=[]
def CSVwrite(filename,csvarray,fieldnames,ioflag,indexrow):
ret=False
try:
import csv
csvfile = open(filename,ioflag)
csvwriter = csv.DictWriter(csvfile, delimiter=',', fieldnames=fieldnames)
if indexrow==True:
csvwriter.writerow(dict((fn,fn) for fn in fieldnames))
for row in csvarray:
csvwriter.writerow(row)
csvfile.close()
ret=True
except:
print 'CSVwrite except'
pass
return ret
def createHostsfile():
f = open("hosts", "w")
for row in csv_rows:
#scp -i "ai_tce.pem" scripts/instances.txt ubuntu@ec2-18-191-197-117.us-east-2.compute.amazonaws.com:/home/ubuntu/
# CSVFile
# AWS : need to use private ip instead of public ip for yarn cluster
host = row['PrivateIP'] + " " + row['NodeName']+"\n"
f.write(host)
f.close()
# /home/ubuntu/install_files/hadoop-2.7.7/etc/hadoop/slaves
def createSlavesfile():
f = open("slaves", "w")
for row in csv_rows:
slave = row['NodeName']+"\n"
f.write(slave)
f.close()
def createHostnamefile(row):
filename = 'hostname'
if os.path.exists(filename):
os.remove(filename)
f = open(filename, "w")
hostname = row['NodeName']+"\n"
f.write(hostname)
f.close()
def createInitScript(row,cluster,master_dns):
filename = 'init.sh'
if os.path.exists(filename):
os.remove(filename)
f = open(filename, "w")
# put commands into init.sh
# general one
initcmds = "sudo cp ~/Scripts/hostname /etc \n"
initcmds += "sudo cp ~/Scripts/hosts /etc \n"
initcmds += "source /home/ubuntu/env.sh\n"
if cluster == "standalone":
if row['NodeName'].find("master") != -1:
initcmds += "$SPARK_HOME/sbin/start-master.sh -h "+ row['DNS']+"\n"
elif row['NodeName'].find("slave") != -1:
initcmds += "$SPARK_HOME/sbin/start-slave.sh spark://"+master_dns+":7077\n"
elif cluster == "yarn":
if row['NodeName'].find("master") != -1:
initcmds+='sleep 1\n'
#/home/ubuntu/install_files/hadoop-2.7.7/bin/hadoop namenode -format
initcmds += "$HADOOP_HOME/bin/hadoop namenode -format\n"
#/home/ubuntu/install_files/hadoop-2.7.7/sbin/start-dfs.sh
initcmds += "$HADOOP_HOME/sbin/start-dfs.sh\n"
#/home/ubuntu/install_files/hadoop-2.7.7/sbin/start-yarn.sh
initcmds += "$HADOOP_HOME/sbin/start-yarn.sh\n"
elif row['NodeName'].find("slave") != -1:
initcmds+='\n'
f.write(initcmds)
f.close()
def createStopScript(row,cluster):
filename = 'stop.sh'
if os.path.exists(filename):
os.remove(filename)
f = open(filename, "w")
# put commands into init.sh
# general one
stopcmds = "source /home/ubuntu/env.sh\n"
if cluster == "standalone":
if row['NodeName'].find("master") != -1:
stopcmds += "$SPARK_HOME/sbin/stop-master.sh\n"
elif row['NodeName'].find("slave") != -1:
stopcmds += "$SPARK_HOME/sbin/stop-slave.sh\n"
elif cluster == "yarn":
stopcmds+=''
f.write(stopcmds)
f.close()
def Ops_SCP(dst,filename,aws_key):
noproxy_cmd = ["scp", "-i",aws_key,filename, dst]
proxy_cmd = ["scp","-o", "ProxyCommand='nc -x proxy-us.intel.com:1080 %h %p'" ,"-i",aws_key,filename, dst]
#print proxy_cmd
if Need_Intel_Proxy==True:
cmd = proxy_cmd
else:
cmd = noproxy_cmd
print cmd
p = subprocess.Popen(cmd)
sts = os.waitpid(p.pid, 0)
def Ops_generate_scp_script(dst,filename,action,aws_key):
if os.path.isfile("./tmp/scp.sh"):
os.remove("./tmp/scp.sh")
if Need_Intel_Proxy==True:
intel_proxy_cmd = "-o ProxyCommand='nc -x proxy-us.intel.com:1080 %h %p'"
else:
intel_proxy_cmd = ''
# Create connect scripts
if os.path.isdir('./tmp')==False:
os.mkdir("./tmp")
if action == 'push':
scp_cmd ="scp "+intel_proxy_cmd+" "+" -i "+aws_key+" "+filename+" "+dst
elif action == "pull":
scp_cmd ="scp "+intel_proxy_cmd+" "+" -i "+aws_key+" "+dst+" "+filename
filepath = "./tmp/scp.sh"
f = open(filepath, "w")
f.write(scp_cmd)
f.close()
os.chmod(filepath, stat.S_IRUSR |stat.S_IEXEC |stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH )
return filepath
def Ops_generate_ssh_script(row,cluster,aws_key):
# ssh -o ProxyCommand='nc -x proxy-us.intel.com:1080 %h %p' -L 8888:localhost:8888 -L 8080:localhost:8080 -i "ai_tce.pem" ubuntu@$1
if Need_Intel_Proxy==True:
intel_proxy_cmd = "-o ProxyCommand='nc -x proxy-us.intel.com:1080 %h %p'"
else:
intel_proxy_cmd = ''
if row['NodeName'] == "master":
if cluster == "standalone":
port_tunnel = "-L 8888:localhost:8888 -L 8080:localhost:8080 "
elif cluster == "yarn":
port_tunnel = "-L 8888:localhost:8888 -L 8088:localhost:8088"
else:
port_tunnel = ''
# Create connect scripts
if os.path.isdir('./tmp')==False:
os.mkdir("./tmp")
ssh_cmd ="ssh "+intel_proxy_cmd+" "+port_tunnel+" "+" -i "+aws_key+" "+" ubuntu@"+row['DNS']
filename = row['NodeName']+".sh"
filepath = "./tmp/"+filename
f = open(filepath, "aw")
f.write(ssh_cmd)
f.close()
os.chmod(filepath, stat.S_IRUSR |stat.S_IEXEC |stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH )
return filepath
def getPrivateAddr(dns,aws_key):
IP='0.0.0.0'
# get /etc/hostname
dst = "ubuntu@"+dns+":/etc/hostname"
filepath = "."
filepath = Ops_generate_scp_script(dst,filepath,"pull",aws_key)
try:
print(filepath)
os.system(filepath)
except:
print "ERROR : NO VALID DNS!"
return IP
# parse IP from hostname
with open("./hostname") as f:
lines = f.readlines()
for l in lines:
l=l.strip("\n")
print l
IP=l.split("-")[1]+'.'+l.split("-")[2]+'.'+l.split("-")[3]+'.'+l.split("-")[4]
print IP
return IP
def parseCSV(filename,csvfile,aws_key):
dns_list=[]
ip_list=[]
fieldnames = ['NodeName', 'IP','DNS','PrivateIP']
index=0
print(filename)
with open(filename) as f:
lines = f.readlines()
# get dns
for l in lines:
dns=l.split(' ')[1]
dns=dns.strip(' ')
dns=dns.strip('\n')
dns_list.append(dns)
# get ip and rows
for dns in dns_list:
print dns
addr = socket.getaddrinfo(dns,None)[0][4][0]
print addr
if index == 0:
nodename = 'master'
else:
nodename = 'slave'+str(index)
private_addr = getPrivateAddr(dns,aws_key)
index+=1
row={fieldnames[0]:nodename , fieldnames[1]:addr,fieldnames[2]:dns,fieldnames[3]:private_addr}
print row
csv_rows.append(row)
csvfile = "spark_cluster.csv";
fieldnames = ['NodeName', 'IP','DNS','PrivateIP']
CSVwrite(csvfile,'',fieldnames,'wb',True)
CSVwrite(csvfile,csv_rows,fieldnames,'ab',False)
def main(filename,cluster,csvfile,aws_key):
print 'main'
# read from CSV file if input is csv file
if filename == csvfile:
print "using exist CSV file"
import csv
with open(csvfile, 'rb') as f:
reader = csv.reader(f)
index = 0
fieldnames = []
csvrow={}
for row in reader:
if index == 0 :
fieldnames = row
else:
csvrow={fieldnames[0]:row[0] , fieldnames[1]:row[1],fieldnames[2]:row[2],fieldnames[3]:row[3]}
csv_rows.append(csvrow)
index+=1
else:
parseCSV(filename,csvfile,aws_key)
createHostsfile()
for row in csv_rows:
if row['NodeName']== 'master':
master_dns = row['DNS']
if cluster == "yarn":
# /home/ubuntu/install_files/hadoop-2.7.7/etc/hadoop/slaves
createSlavesfile()
dst ="ubuntu@"+row['DNS']+":/home/ubuntu/install_files/hadoop-2.7.7/etc/hadoop/"
filepath = Ops_generate_scp_script(dst,"slaves","push",aws_key)
os.system(filepath)
gnome_terminal_cmd = ["gnome-terminal"]
for row in csv_rows:
# SCP the config file
#scp -i "ai_tce.pem" scripts/instances.txt ubuntu@ec2-18-191-197-117.us-east-2.compute.amazonaws.com:/home/ubuntu/
# CSVFile
dst = "ubuntu@"+row['DNS']+":/home/ubuntu/Scripts"
filepath = Ops_generate_scp_script(dst,csvfile,"push",aws_key)
os.system(filepath)
# /etc/hosts
dst = "ubuntu@"+row['DNS']+":/home/ubuntu/Scripts"
filepath = Ops_generate_scp_script(dst,"hosts","push",aws_key)
os.system(filepath)
# /etc/hostname
createHostnamefile(row)
filepath = Ops_generate_scp_script(dst,"hostname","push",aws_key)
os.system(filepath)
# create init.sh
createInitScript(row,cluster,master_dns)
filepath = Ops_generate_scp_script(dst,"init.sh","push",aws_key)
os.system(filepath)
#~/.ssh/config
dst = "ubuntu@"+row['DNS']+":/home/ubuntu/.ssh"
filepath = Ops_generate_scp_script(dst,"config","push",aws_key)
os.system(filepath)
# compose gnome-terminal command
filepath = Ops_generate_ssh_script(row,cluster,aws_key)
#gnome-terminal --tab -e "bash -c 'ps -ef; bash'" --tab -e "bash -c 'ls;bash'" --tab -e "bash -c 'top -n 1; bash'"
gnome_terminal_cmd.append("--tab")
gnome_terminal_cmd.append("-e")
bashcmd = "bash -c '"+filepath+"';bash"
gnome_terminal_cmd.append(bashcmd)
#raw_input("Press Enter to continue...")
print gnome_terminal_cmd
p = subprocess.Popen(gnome_terminal_cmd)
sts = os.waitpid(p.pid, 0)
time.sleep( 5 )
# stop script
for row in csv_rows:
# create stop.sh
createStopScript(row,cluster)
filepath = Ops_generate_scp_script(dst,"stop.sh","push",aws_key)
os.system(filepath)
time.sleep( 1 )
if os.path.isdir('./tmp')==True:
os.system("rm -rf ./tmp")
if __name__ == "__main__":
import argparse
csvfile = "spark_cluster.csv";
Need_Intel_Proxy=False
# Instantiate the parser
parser = argparse.ArgumentParser(description='Optional app description')
parser.add_argument("-f", "--file", help="file name for instances",
dest="file", default="")
parser.add_argument("-c", "--cluster", help="cluster mode. standalone, yarn",
dest="cluster", default="standalone")
parser.add_argument("-p", "--proxy", help="proxy type",
dest="proxy", default="")
parser.add_argument("-k", "--key", help="AWS key",
dest="aws_key", default="ai_tce_or.pem")
# Required positional argument
args = parser.parse_args()
if args.file == '':
parser.print_help()
exit()
if args.proxy == 'intel':
Need_Intel_Proxy=True
print(args.aws_key)
main(args.file,args.cluster,csvfile,args.aws_key)