-
Notifications
You must be signed in to change notification settings - Fork 0
/
try.py
557 lines (441 loc) · 17.5 KB
/
try.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import ConfigParser
import socket
import sys
import time
from time import sleep
from cm_api.api_client import ApiResource
from cm_api.endpoints.clusters import ApiCluster
from cm_api.endpoints.clusters import create_cluster
from cm_api.endpoints.parcels import ApiParcel
from cm_api.endpoints.parcels import get_parcel
from cm_api.endpoints.cms import ClouderaManager
from cm_api.endpoints.services import ApiService, ApiServiceSetupInfo
from cm_api.endpoints.services import create_service
from cm_api.endpoints.types import ApiCommand, ApiRoleConfigGroupRef
from cm_api.endpoints.role_config_groups import get_role_config_group
from cm_api.endpoints.role_config_groups import ApiRoleConfigGroup
from cm_api.endpoints.roles import ApiRole
config = ConfigParser.ConfigParser()
config.read("/root/python-script/clouderaconfig.ini")
cm_api_host = config.get("CM", "cm.api.host")
cm_api_version = config.get("CM", "cm.api.version")
cm_api_user = config.get("CM", "admin.api.name")
cm_api_pass = config.get("CM", "admin.api.password")
cm_management_host = config.get("MANAGEMENT", "mgmt.host")
host_username = config.get("HOST", "host.username")
private_key_file = config.get("HOST", "host.privatekey")
cluster_name = config.get("CDH", "cluster.name")
cluster_hosts = config.get("CDH", "cluster.hosts").split(',')
cdh_version = config.get("CDH", "cdh.version")
parcel_version = config.get("CDH", "parcel.version")
management_service_name = config.get("MANAGEMENT", "service.name")
smon_role_name = config.get("MANAGEMENT", "servicemonitor.name")
hmon_role_name = config.get("MANAGEMENT", "hostmonitor.name")
eserv_role_name = config.get("MANAGEMENT", "eventserver.name")
apub_role_name = config.get("MANAGEMENT", "alertpublisher.name")
amon_role_name = config.get("MANAGEMENT", "activitymonitor.name")
yarn_servicename = config.get("YARN", "yarn.servicename")
yarn_resourcemanager_servicename = config.get("YARN", "yarn.resourcemanager.servicename")
yarn_jobhistory_servicename = config.get("YARN", "yarn.jobhistory.servicename")
yarn_nodemanager_servicename = config.get("YARN", "yarn.nodemanager.servicename")
yarn_resourcemanager = config.get("YARN", "yarn.resourcemanager")
yarn_jobhistory = config.get("YARN", "yarn.jobhistory")
yarn_nodemanager = config.get("YARN", "yarn.nodemanager").split(',')
hdfs_servicename = config.get("HDFS", "hdfs.servicename")
hdfs_namenode_servicename = config.get("HDFS", "hdfs.namenode.servicename")
hdfs_secondarynamenode_servicename = config.get("HDFS", "hdfs.secondarynamenode.servicename")
hdfs_balancer_servicename = config.get("HDFS", "hdfs.balancer.servicename")
hdfs_datanode_servicename = config.get("HDFS", "hdfs.datanode.servicename")
hdfs_namenode = config.get("HDFS", "hdfs.namenode")
hdfs_secondarynamenode = config.get("HDFS", "hdfs.secondarynamenode")
hdfs_balancer = config.get("HDFS", "hdfs.balancer")
hdfs_datanode = config.get("HDFS", "hdfs.datanode").split(',')
zookeeper_servicename = config.get("ZOOKEEPER", "zookeeper.servicename")
zookeeper_server_servicename = config.get("ZOOKEEPER", "zookeeper.server.servicename")
zookeeper_server = config.get("ZOOKEEPER", "zookeeper.server").split(',')
hive_servicename = config.get("HIVE", "hive.servicename")
hive_gateway_servicename = config.get("HIVE", "hive.gateway.servicename")
hive_hivemetastore_servicename = config.get("HIVE", "hive.hivemetastore.servicename")
hive_hiveserver2_servicename = config.get("HIVE", "hive.hiveserver2.servicename")
hive_gateway = config.get("HIVE", "hive.gateway")
hive_metastore = config.get("HIVE", "hive.metastore")
hive_hiveserver2 = config.get("HIVE", "hive.hiveserver2")
oozie_servicename = config.get("OOZIE", "oozie.servicename")
oozie_server_servicename = config.get("OOZIE", "oozie.server.servicename")
oozie_server = config.get("OOZIE", "oozie.server")
hue_servicename = config.get("HUE", "hue.servicename")
hue_server_servicename = config.get("HUE", "hue.server.servicename")
hue_server = config.get("HUE", "hue.server")
with open(private_key_file,'r') as f:
private_key_contents = f.read()
api = ApiResource(cm_api_host, version=cm_api_version, username=cm_api_user, password=cm_api_pass)
manager = api.get_cloudera_manager()
cmd = manager.host_install(host_username,cluster_hosts, private_key=private_key_contents)
print "checking if host_install finished"
while cmd.active == True:
sleep(5)
print " ..."
cmd = cmd.fetch()
if cmd.success != True:
print "host_install failed: " + cmd.resultMessage
exit(0)
print "host_install successful: " + cmd.resultMessage
cluster = api.create_cluster(cluster_name, cdh_version)
all_hosts = api.get_all_hosts()
hostrefs = []
yarn_nodemanager_hostrefs = []
hdfs_datanode_hostrefs = []
zookeeper_server_hostrefs=[]
hive_gateway_hostrefs = []
for host in all_hosts:
if host.hostname == cm_management_host:
cm_management_host_hostref = host.hostId
if host.hostname == yarn_resourcemanager:
yarn_resourcemanager_hostref = host.hostId
if host.hostname == yarn_jobhistory:
yarn_jobhistory_hostref = host.hostId
if host.hostname in yarn_nodemanager:
yarn_nodemanager_hostrefs.append(host.hostId)
if host.hostname == hdfs_namenode:
hdfs_namenode_hostref = host.hostId
if host.hostname == hdfs_secondarynamenode:
hdfs_secondarynamenode_hostref = host.hostId
if host.hostname == hdfs_balancer:
hdfs_balancer_hostref = host.hostId
if host.hostname in hdfs_datanode:
hdfs_datanode_hostrefs.append(host.hostId)
if host.hostname in zookeeper_server:
zookeeper_server_hostrefs.append(host.hostId)
if host.hostname in hive_gateway:
hive_gateway_hostrefs.append(host.hostId)
if host.hostname == hive_metastore:
hive_metastore_hostref = host.hostId
if host.hostname == hive_hiveserver2:
hive_hiveserver2_hostref = host.hostId
if host.hostname == oozie_server:
oozie_server_hostref = host.hostId
if host.hostname == hue_server:
hue_server_hostref = host.hostId
hostrefs.append(host.hostId)
cluster.add_hosts(hostrefs)
parcel = cluster.get_parcel('CDH', parcel_version)
parcel.start_download()
while True:
parcel = cluster.get_parcel('CDH', parcel_version)
if parcel.stage == 'DOWNLOADED':
break
if parcel.state.errors:
raise Exception(str(parcel.state.errors))
print "progress: %s / %s" % (parcel.state.progress, parcel.state.totalProgress)
sleep(5)
print "downloaded CDH parcel version %s on cluster %s" % (parcel_version, cluster_name)
parcel.start_distribution()
while True:
parcel = cluster.get_parcel('CDH', parcel_version)
if parcel.stage == 'DISTRIBUTED':
break
if parcel.state.errors:
raise Exception(str(parcel.state.errors))
print "progress: %s / %s" % (parcel.state.progress, parcel.state.totalProgress)
sleep(5)
print "distributed CDH parcel version %s on cluster %s" % (parcel_version, cluster_name)
parcel.activate()
#arcels_list = []
# get and list all available parcels
#print "Available parcels:"
#for p in cluster.get_all_parcels():
# print '\t' + p.product + ' ' + p.version
# if p.version.startswith(parcel_version) and p.product == "CDH":
# parcels_list.append(p)
#
#if len(parcels_list) == 0:
# print "No " + cdh_version + " parcel found!"
# exit(0)
#
#cdh_parcel = parcels_list[0]
#for p in parcels_list:
# if p.version > cdh_parcel.version:
# cdh_parcel = p
#
#print cdh_parcel
#
#p = cluster.get_parcel(cdh_parcel.product, cdh_parcel.version)
#cmd = p.start_download()
#
#while True:
# p = cluster.get_parcel(cdh_parcel.product, cdh_parcel.version)
# if p.stage == "DOWNLOADED":
# break
# if p.state.errors:
# raise Exception(str(p.state.errors))
# print "Downloading %s: %s / %s" % (cdh_parcel.product, p.state.progress, p.state.totalProgress)
# time.sleep(10)
#
#print cdh_parcel.product + ' ' + cdh_parcel.version + " downloaded"
#
#p.start_distribution()
#while True:
# p = cluster.get_parcel(cdh_parcel.product, cdh_parcel.version)
# if p.stage == "DISTRIBUTED":
# break
# if p.state.errors:
# raise Exception(str(p.state.errors))
# print "Distributing %s: %s / %s" % (cdh_parcel.product, p.state.progress, p.state.totalProgress)
# time.sleep(15)
#
#print "Distributed %s" % (parcel['name'])
#
#p.activate()
#if cmd.success != True:
# print "Parcel activation failed!"
# exit(0)
#
## make sure the activation finishes
#while cdh_parcel.stage != "ACTIVATED":
# cdh_parcel = get_parcel(api, cdh_parcel.product, cdh_parcel.version, cluster_name)
#print cdh_parcel.product + ' ' + cdh_parcel.version + " activated"
mgmt = manager.create_mgmt_service(ApiServiceSetupInfo(name = management_service_name, type = "MGMT"))
mgmt.create_role(smon_role_name + "-1", "SERVICEMONITOR",cm_management_host_hostref)
mgmt.create_role(hmon_role_name + "-1", "HOSTMONITOR", cm_management_host_hostref)
mgmt.create_role(eserv_role_name + "-1", "EVENTSERVER", cm_management_host_hostref)
mgmt.create_role(apub_role_name + "-1", "ALERTPUBLISHER", cm_management_host_hostref)
mgmt.start().wait()
print "Deployed and started Cloudera Management Services"
print "Inspecting hosts. This might take a few minutes."
cmd = manager.inspect_hosts()
while cmd.active == True:
cmd = cmd.fetch()
sleep(5)
if cmd.success != True:
print " failed: inspect_hosts" + cmd.resultMessage
exit(0)
print "inspect_hostsl successful: " + cmd.resultMessage
print "Deploying ZooKeeper"
zookeeper_service = cluster.create_service(zookeeper_servicename, "ZOOKEEPER")
i = 1
for hostref in zookeeper_server_hostrefs:
zookeeper_service.create_role(zookeeper_server_servicename + "-" + str(i),"SERVER",hostref)
i = i+1
print "configuraring ZooKeeper"
zookeeper_config = {
'zookeeper_datadir_autocreate': 'true'
}
zookeeper_server_config = {
'dataLogDir':'/var/lib/zookeeper',
'dataDir':'/var/lib/zookeeper',
'zookeeper_server_java_heapsize':'52428800'
}
zookeeper_server_groups = []
for group in zookeeper_service.get_all_role_config_groups():
if group.roleType == 'SERVER':
zookeeper_server_groups.append(group)
zookeeper_service.update_config(zookeeper_config)
for group in zookeeper_server_groups:
group.update_config(zookeeper_server_config)
print "Initiating First Run command after ZooKeeper"
cmd = cluster.first_run()
while cmd.active == True:
sleep(5)
print " ..."
cmd = cmd.fetch()
if cmd.success != True:
print "First run failed after ZooKeeper: " + cmd.resultMessage
exit(0)
print "First run successful after ZooKeeper: " + cmd.resultMessage
print "Restarting cluster"
cluster.stop().wait()
cluster.start().wait()
print "Deploying HDFS"
hdfs_service = cluster.create_service(hdfs_servicename, "HDFS")
hdfs_service.create_role(hdfs_namenode_servicename + "-1","NAMENODE",hdfs_namenode_hostref)
hdfs_service.create_role(hdfs_secondarynamenode_servicename + "-1","SECONDARYNAMENODE",hdfs_secondarynamenode_hostref)
hdfs_service.create_role(hdfs_balancer_servicename + "-1","BALANCER",hdfs_balancer_hostref)
i = 1
for hostref in hdfs_datanode_hostrefs:
hdfs_service.create_role(hdfs_datanode_servicename + "-" + str(i),"DATANODE",hostref)
i = i+1
print "configuraring HDFS"
namenode_groups = []
secondarynamenode_groups = []
datanode_groups = []
hdfs_gateway_groups = []
for group in hdfs_service.get_all_role_config_groups():
if group.roleType == 'NAMENODE':
namenode_groups.append(group)
if group.roleType == 'SECONDARYNAMENODE':
secondarynamenode_groups.append(group)
if group.roleType == 'DATANODE':
datanode_groups.append(group)
if group.roleType == 'GATEWAY':
hdfs_gateway_groups.append(group)
namenode_config = {
'dfs_name_dir_list':'/dfs/nn'
}
datanode_config = {
'dfs_data_dir_list':'/dfs/dn',
'dfs_datanode_failed_volumes_tolerated': 0
}
secondarynamenode_config = {
'fs_checkpoint_dir_list': '/dfs/snn'
}
hdfs_gateway_config = {
'dfs_client_use_trash':'true'
}
for group in namenode_groups:
group.update_config(namenode_config)
for group in secondarynamenode_groups:
group.update_config(secondarynamenode_config)
for group in datanode_groups:
group.update_config(datanode_config)
for group in hdfs_gateway_groups:
group.update_config(hdfs_gateway_config)
print "Deploying YARN"
yarn_service = cluster.create_service(yarn_servicename, "YARN")
yarn_service.create_role(yarn_resourcemanager_servicename + "-1","RESOURCEMANAGER",yarn_resourcemanager_hostref)
yarn_service.create_role(yarn_jobhistory_servicename + "-1","JOBHISTORY",yarn_jobhistory_hostref)
i = 1
for hostref in yarn_nodemanager_hostrefs:
yarn_service.create_role(yarn_nodemanager_servicename + "-" + str(i),"NODEMANAGER",hostref)
i = i+1
print "configuraring YARN"
nodemanager_groups = []
yarn_gateway_groups = []
for group in yarn_service.get_all_role_config_groups():
if group.roleType == 'NODEMANAGER':
nodemanager_groups.append(group)
if group.roleType == 'GATEWAY':
yarn_gateway_groups.append(group)
yarn_config = {
'hdfs_service': hdfs_servicename
}
nodemanager_config = {
'yarn_nodemanager_local_dirs':'/yarn/nm'
}
yarn_gateway_config = {
'mapred_submit_replication':len(hdfs_datanode_hostrefs)
}
yarn_service.update_config(yarn_config)
for group in nodemanager_groups:
group.update_config(nodemanager_config)
for group in yarn_gateway_groups:
group.update_config(yarn_gateway_config)
print "Initiating First Run command after YARN and HDFS"
cmd = cluster.first_run()
while cmd.active == True:
sleep(5)
print " ..."
cmd = cmd.fetch()
if cmd.success != True:
print "First run failed after YARN and HDFS: " + cmd.resultMessage
exit(0)
print "First run successful after YARN and HDFS: " + cmd.resultMessage
print "Restarting cluster"
cluster.stop().wait()
cluster.start().wait()
print "Deploying Hive"
hive_service = cluster.create_service(hive_servicename, "HIVE")
i = 1
for hostref in hive_gateway_hostrefs:
hive_service.create_role(hive_gateway_servicename + "-" + str(i),"GATEWAY",hostref)
i = i+1
hive_service.create_role(hive_hivemetastore_servicename + "-1","HIVEMETASTORE",hive_metastore_hostref)
hive_service.create_role(hive_hiveserver2_servicename + "-1","HIVESERVER2",hive_hiveserver2_hostref)
print "configuraring HIVE"
hive_config = {
'hive_metastore_database_host': cm_management_host,
'hive_metastore_database_name': 'hive',
'hive_metastore_database_password': 'suTOXS7HzV',
'hive_metastore_database_port': 7432,
'hive_metastore_database_type': 'postgresql',
'mapreduce_yarn_service': yarn_servicename,
'zookeeper_service': zookeeper_servicename
}
hiveserver2_config = {
'hiveserver2_java_heapsize':'52428800'
}
hivemetastore_config = {
'hive_metastore_java_heapsize':'52428800'
}
hive_service.update_config(hive_config)
hiveserver2_groups = []
hivemetastore_groups = []
for group in hive_service.get_all_role_config_groups():
if group.roleType == 'HIVESERVER2':
hiveserver2_groups.append(group)
if group.roleType == 'HIVEMETASTORE':
hivemetastore_groups.append(group)
for group in hiveserver2_groups:
group.update_config(hiveserver2_config)
for group in hivemetastore_groups:
group.update_config(hivemetastore_config)
print "Initiating First Run command after HIVE"
cmd = cluster.first_run()
while cmd.active == True:
sleep(5)
print " ..."
cmd = cmd.fetch()
if cmd.success != True:
print "First run failed after HIVE: " + cmd.resultMessage
exit(0)
print "First run successful after HIVE: " + cmd.resultMessage
print "Restarting cluster"
cluster.stop().wait()
cluster.start().wait()
print "Deploying OOZIE"
oozie_service = cluster.create_service(oozie_servicename, "OOZIE")
oozie_service.create_role(oozie_server_servicename,"OOZIE_SERVER",oozie_server_hostref)
print "configuring OOZIE"
oozie_config = {
'hive_service':hive_servicename,
'mapreduce_yarn_service':yarn_servicename,
'zookeeper_service':zookeeper_servicename
}
oozie_server_config = {
'oozie_java_heapsize':'52428800',
'oozie_data_dir':'/var/lib/oozie/data'
}
oozieserver_groups = []
for group in oozie_service.get_all_role_config_groups():
if group.roleType == 'OOZIE_SERVER':
oozieserver_groups.append(group)
oozie_service.update_config(oozie_config)
for group in oozieserver_groups:
group.update_config(oozie_server_config)
print "Initiating First Run command after OOZIE"
cmd = cluster.first_run()
while cmd.active == True:
sleep(5)
print " ..."
cmd = cmd.fetch()
if cmd.success != True:
print "First run failed after OOZIE: " + cmd.resultMessage
exit(0)
print "First run successful after OOZIE: " + cmd.resultMessage
print "Restarting cluster"
cluster.stop().wait()
cluster.start().wait()
print "Deploying HUE"
hue_service = cluster.create_service(hue_servicename, "HUE")
hue_service.create_role(hue_server_servicename,"HUE_SERVER",hue_server_hostref)
print "configuring HUE"
hue_config = {
'hive_service':hive_servicename,
'zookeeper_service':zookeeper_servicename,
'oozie_service':oozie_servicename,
'hue_webhdfs':'namenode-1'
}
hue_service.update_config(hue_config)
print "Initiating First Run command after HUE"
cmd = cluster.first_run()
while cmd.active == True:
sleep(5)
print " ..."
cmd = cmd.fetch()
if cmd.success != True:
print "First run failed after HUE: " + cmd.resultMessage
exit(0)
print "First run successful after HUE: " + cmd.resultMessage
print "Restarting cluster"
cluster.stop().wait()
cluster.start().wait()
print "Cloudera CDH installation in done"