-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdh_api_mgmt_services.py
142 lines (117 loc) · 5.03 KB
/
cdh_api_mgmt_services.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
#
# Importing required modules.
#
import time, sys, yaml, logging, argparse
from cm_api.api_client import ApiResource, ApiException
from cm_api.endpoints.services import ApiServiceSetupInfo
from subprocess import call
def enable_license_for_cm(cloudera_manager):
try:
cloudera_license = cloudera_manager.get_license()
print cloudera_license
except ApiException:
cloudera_manager.begin_trial()
def host_installation(cloudera_manager, config):
"""
Host installation.
https://cloudera.github.io/cm_api/epydoc/5.10.0/cm_api.endpoints.cms.ClouderaManager-class.html#host_install
"""
logging.info("Installing HOSTs.")
cmd = cloudera_manager.host_install(config['cm_host_installation']['host_username'],
config['cluster']['hosts'],
ssh_port=config['cm_host_installation']['ssh_port'],
password=config['cm_host_installation']['host_password'],
parallel_install_count=10,
cm_repo_url=config['cm_host_installation']['host_cm_repo_url'],
gpg_key_custom_url=config['cm_host_installation']['host_cm_repo_gpg_key_custom_url'],
java_install_strategy=config['cm_host_installation']['host_java_install_strategy'],
unlimited_jce=config['cm_host_installation']['host_unlimited_jce_policy'])
#
# Check command to complete.
#
if not cmd.wait().success:
logging.info("Command `host_install` Failed. {0}".format(cmd.resultMessage))
if (cmd.resultMessage is not None and
'There is already a pending command on this entity' in cmd.resultMessage):
raise Exception("HOST INSTALLATION FAILED.")
def deploy_management_server(cloudera_manager, config):
"""
Deploy management services, below are the list of services created.
- Activity Monitor
- Alert Publisher
- Event Server
- Host Monitor
- Reports Manager
- Service Monitor
:return:
"""
logging.info("Deploying Management Server Services")
try:
#
# try to get the current service if already present and running.
#
mgmt_service = cloudera_manager.get_service()
if mgmt_service.serviceState == "STARTED":
logging.info("MGMT service already created on the server")
return
except ApiException:
#
# If not then we create a mgmt service.
#
logging.info("Creating MGMT server")
mgmt_service = cloudera_manager.create_mgmt_service(ApiServiceSetupInfo())
#
# Now add all services to the if not already present
#
for role in config['services']['MGMT']['roles']:
if not len(mgmt_service.get_roles_by_type(role['group'])) > 0:
logging.info("Creating role for {0}".format(role['group']))
mgmt_service.create_role('{0}-1'.format(role['group']), role['group'], role['hosts'][0])
#
# Update configuration for each service.
#
for role in config['services']['MGMT']['roles']:
role_group = mgmt_service.get_role_config_group('mgmt-{0}-BASE'.format(role['group']))
logging.info(role_group)
#
# Update the group's configuration.
# [https://cloudera.github.io/cm_api/epydoc/5.10.0/cm_api.endpoints.role_config_groups.ApiRoleConfigGroup-class.html#update_config]
#
role_group.update_config(role.get('config', {}))
#
# Start mgmt services.
#
mgmt_service.start().wait()
#
# Wait and restart mgmt service just to make sure.
#
logging.info("Waiting for MGMT service to restart. !!!")
#
# Check for mgmt service, if started, else bail out.
#
if cloudera_manager.get_service().serviceState == 'STARTED':
logging.info("Management Services started")
else:
logging.ERROR("[MGMT] Cloudera Management services didn't start up properly")
if __name__ == '__main__':
# setting logging to DEBUG
logging.basicConfig(level=logging.DEBUG)
try:
with open('cloudera_mgmt.yaml', 'r') as cluster_yaml:
config = yaml.load(cluster_yaml)
api_handle = ApiResource(config['cm']['host'],
config['cm']['port'],
config['cm']['username'],
config['cm']['password'],
config['cm']['tls'],
version=config['cm']['api-version'])
# Checking CM services
cloudera_manager = api_handle.get_cloudera_manager()
# Enable License
enable_license_for_cm(cloudera_manager)
# Install Hosts
host_installation(cloudera_manager, config)
# Deploy Management Services
deploy_management_server(cloudera_manager, config)
except IOError as e:
logging.error("ERROR {0}. EXIT NOW :( !!!!".format(e))