-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_cluster.py
92 lines (74 loc) · 3.01 KB
/
create_cluster.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
from __future__ import print_function
import cm_client
from cm_client.rest import ApiException
from collections import namedtuple
from pprint import pprint
import json
import time
import sys
def wait(cmd, timeout=None):
SYNCHRONOUS_COMMAND_ID = -1
if cmd.id == SYNCHRONOUS_COMMAND_ID:
return cmd
SLEEP_SECS = 5
if timeout is None:
deadline = None
else:
deadline = time.time() + timeout
try:
cmd_api_instance = cm_client.CommandsResourceApi(api_client)
while True:
cmd = cmd_api_instance.read_command(long(cmd.id))
pprint(cmd)
if not cmd.active:
return cmd
if deadline is not None:
now = time.time()
if deadline < now:
return cmd
else:
time.sleep(min(SLEEP_SECS, deadline - now))
else:
time.sleep(SLEEP_SECS)
except ApiException as e:
print("Exception when calling ClouderaManagerResourceApi->import_cluster_template: %s\n" % e)
cm_client.configuration.username = 'admin'
cm_client.configuration.password = 'admin'
api_client = cm_client.ApiClient("http://localhost:7180/api/v32")
cm_api = cm_client.ClouderaManagerResourceApi(api_client)
# accept trial licence
cm_api.begin_trial()
# Install CM Agent on host
with open ("/root/myRSAkey", "r") as f:
key = f.read()
instargs = cm_client.ApiHostInstallArguments(host_names=['YourHostname'],
user_name='root',
private_key=key,
cm_repo_url='https://archive.cloudera.com/cm6/6.2.0',
java_install_strategy='NONE',
ssh_port=22,
passphrase='')
cmd = cm_api.host_install_command(body=instargs)
wait(cmd)
# create MGMT/CMS
mgmt_api = cm_client.MgmtServiceResourceApi(api_client)
api_service = cm_client.ApiService()
api_service.roles = [cm_client.ApiRole(type='SERVICEMONITOR'),
cm_client.ApiRole(type='HOSTMONITOR'),
cm_client.ApiRole(type='EVENTSERVER'),
cm_client.ApiRole(type='ALERTPUBLISHER')]
mgmt_api.auto_assign_roles() # needed?
mgmt_api.auto_configure() # needed?
mgmt_api.setup_cms(body=api_service)
cmd = mgmt_api.start_command()
wait(cmd)
# create the cluster using the template
with open(sys.argv[1]) as f:
json_str = f.read()
Response = namedtuple("Response", "data")
dst_cluster_template=api_client.deserialize(response=Response(json_str),response_type=cm_client.ApiClusterTemplate)
cmd = cm_api.import_cluster_template(add_repositories=True, body=dst_cluster_template)
wait(cmd)
# API Docs for reference
# https://archive.cloudera.com/cm6/6.2.0/generic/jar/cm_api/swagger-html-sdk-docs/python/docs/ClouderaManagerResourceApi.html
# https://archive.cloudera.com/cm6/6.2.0/generic/jar/cm_api/swagger-html-sdk-docs/python/docs/MgmtServiceResourceApi.html