-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbootstrap_cdh.py
executable file
·111 lines (80 loc) · 3.6 KB
/
bootstrap_cdh.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
#! /usr/bin/env python
# Dependency modules pywebhdfs, cloudera-director-python-client
import sys
import ConfigParser
import argparse
import uuid
import time
from os.path import isfile
from urllib2 import HTTPError
from pywebhdfs.webhdfs import PyWebHdfsClient
from cloudera.director.latest.models import (Login, SshCredentials,
InstanceProviderConfig, Environment, InstanceTemplate,
VirtualInstance, DeploymentTemplate, ClusterTemplate,
VirtualInstanceGroup)
from cloudera.director.common.client import ApiClient
from cloudera.director.latest import (AuthenticationApi, EnvironmentsApi,
DeploymentsApi, ClustersApi)
def str2bool(value):
return {"True": True, "true": True}.get(value, False)
def get_authenticated_client(args):
client = ApiClient(args.server)
# Authenticate. This will start a session and store the cookie
auth = AuthenticationApi(client)
auth.login(Login(username=args.admin_username, password=args.admin_password))
return client
def get_cm_host(client, environment_name, deployment_name):
api = DeploymentsApi(client)
cm_host = api.getRedacted(environment_name, deployment_name)
return cm_host.managerInstance.properties['publicIpAddress']
def get_cluster_config(client, environment_name, deployment_name, cluster_name, private):
api = ClustersApi(client)
cluster_config = api.get(environment_name, deployment_name, cluster_name)
# Return private ip or public ip of master node
for instance in cluster_config.instances:
if instance.virtualInstance.template.tags['group'] == 'master':
if private:
return instance.ipAddress
else:
return instance.properties['publicIpAddress']
def create_hdfs_dir(server, dirs = []):
try:
hdfs = PyWebHdfsClient(host=server,port='50070', user_name='hdfs', timeout=5)
except Exception, e:
print >> sys.stderr, "Exception: %s" % str(e)
sys.exit(0)
for dir in dirs:
hdfs.make_dir(dir, permission=777)
def main():
parser = argparse.ArgumentParser(prog='bootstrap_cdh.py')
parser.add_argument('--admin-username', default="admin",
help='Name of an user with administrative access (defaults to %(default)s)')
parser.add_argument('--admin-password', default="admin",
help='Password for the administrative user (defaults to %(default)s)')
parser.add_argument('--server', default="http://localhost:7189",
help="Cloudera Director server URL (defaults to %(default)s)")
parser.add_argument('--cluster', default="bigdatalab-cluster",
help="Cloudera cluster (defaults to %(default)s)")
parser.add_argument('--deploy', default="False",
help="Deploy cluster (defaults to %(default)s)")
args = parser.parse_args()
cluster_name = args.cluster
environment_name = cluster_name + ' Environment'
deployment_name = cluster_name + ' Deployment'
if str2bool(args.deploy):
client = get_authenticated_client(args)
namenode_private_ip = get_cluster_config(client, environment_name, deployment_name, cluster_name, private=True)
# Directories to create on HDFS for Flume Collector
flume_dir = [ 'flume', 'flume/logs', 'flume/logs/access', 'flume/logs/error' ]
# Get Public IP address of Namenode
namenode_public_ip = get_cluster_config(client, environment_name, deployment_name, cluster_name, private=False)
# Create directories on HDFS for Flume Collector
create_hdfs_dir(namenode_public_ip, flume_dir)
print namenode_private_ip
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except HTTPError as e:
print e.read()
raise e