-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.py
207 lines (173 loc) · 6.77 KB
/
main.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import argparse
from pprint import pprint
from .auth import SimpleAuth
from .base import get_logger
from .constants import (YarnApplicationState, FinalApplicationStatus,
ApplicationState, JobStateInternal)
from . import ResourceManager, NodeManager, HistoryServer, ApplicationMaster
log = get_logger(__name__)
def get_parser():
parser = argparse.ArgumentParser(
description='Client for Hadoop® YARN API')
parser.add_argument('--endpoint', help='API endpoint (https://test.cluster.com:8090)')
#parser.add_argument('--api_class', help='Please provide api class - rm, hs, nm, am', required=True)
parser.add_argument('--timeout', help='Request timeout', default=30)
parser.add_argument('--auth', help='Authentication type', default=None, choices=['simple', None])
parser.add_argument('--verify', help='Verify cert or not', default=True)
subparsers = parser.add_subparsers()
populate_resource_manager_arguments(subparsers)
populate_node_manager_arguments(subparsers)
populate_application_master_arguments(subparsers)
populate_history_server_arguments(subparsers)
return parser
def create_parsers(subparsers_instance, module_class, module_name, listing_of_apis):
for api in listing_of_apis:
_help_message = module_name + " " + api.replace("_", " ").title() + " API"
_new_parser = subparsers_instance.add_parser(
api, help=_help_message
)
_new_parser.set_defaults(method=api)
_method = getattr(module_class, api)
for _arg in _method.__code__.co_varnames[:_method.__code__.co_argcount]:
if _arg != 'self':
_new_parser.add_argument(_arg)
def populate_resource_manager_arguments(subparsers):
rm_parser = subparsers.add_parser(
'rm', help='ResourceManager REST API\'s')
rm_parser.set_defaults(api_class=ResourceManager)
rm_subparsers = rm_parser.add_subparsers()
listing_of_apis = [
'cluster_information',
'cluster_metrics',
'cluster_scheduler',
'cluster_applications',
'cluster_application_statistics',
'cluster_application',
'cluster_application_attempts',
'cluster_application_attempt_info',
'cluster_application_attempt_containers',
'cluster_application_attempt_container_info',
'cluster_application_state',
'cluster_application_kill',
'cluster_nodes',
'cluster_node',
'cluster_node_update_resource',
'cluster_submit_application',
'cluster_new_application',
'cluster_get_application_queue',
'cluster_change_application_queue',
'cluster_get_application_priority',
'cluster_change_application_priority',
'cluster_node_container_memory',
'cluster_scheduler_queue',
'cluster_scheduler_queue_availability',
'cluster_queue_partition',
'cluster_reservations',
'cluster_new_delegation_token',
'cluster_renew_delegation_token',
'cluster_cancel_delegation_token',
'cluster_new_reservation',
'cluster_submit_reservation',
'cluster_update_reservation',
'cluster_delete_reservation',
'cluster_application_timeouts',
'cluster_application_timeout',
'cluster_update_application_timeout',
'cluster_scheduler_conf_mutation',
'cluster_modify_scheduler_conf_mutation',
'cluster_container_signal',
'scheduler_activities',
'application_activities'
]
create_parsers(rm_subparsers, ResourceManager, "Resource Manager", listing_of_apis)
def populate_node_manager_arguments(subparsers):
nm_parser = subparsers.add_parser(
'nm', help='NodeManager REST API\'s')
nm_parser.set_defaults(api_class=NodeManager)
nm_subparsers = nm_parser.add_subparsers()
listing_of_apis = [
'node_information',
'node_applications',
'node_application',
'node_containers',
'node_container',
'auxiliary_services',
'auxiliary_services_update'
]
create_parsers(nm_subparsers, NodeManager, "Node Manager", listing_of_apis)
def populate_application_master_arguments(subparsers):
am_parser = subparsers.add_parser(
'am', help='MapReduce Application Master REST API\'s')
am_parser.set_defaults(api_class=ApplicationMaster)
am_subparsers = am_parser.add_subparsers()
listing_of_apis = [
'application_information',
'jobs',
'job',
'job_attempts',
'job_counters',
'job_conf',
'job_tasks',
'job_task',
'task_counters',
'task_attempts',
'task_attempt',
'task_attempt_state',
'task_attempt_state_kill',
'task_attempt_counters'
]
create_parsers(am_subparsers, ApplicationMaster, "Application Master", listing_of_apis)
def populate_history_server_arguments(subparsers):
hs_parser = subparsers.add_parser(
'hs', help='History Server REST API\'s')
hs_parser.set_defaults(api_class=HistoryServer)
hs_subparsers = hs_parser.add_subparsers()
listing_of_apis = [
'application_information',
'jobs',
'job',
'job_attempts',
'job_counters',
'job_conf',
'job_tasks',
'job_task',
'task_counters',
'task_attempts',
'task_attempt',
'task_attempt_counters'
]
create_parsers(hs_subparsers, HistoryServer, "History Server", listing_of_apis)
def main():
parser = get_parser()
opts = parser.parse_args()
class_kwargs = {}
# Only ResourceManager supports HA
if opts.endpoint:
if opts.api_class == ResourceManager:
class_kwargs['service_endpoints'] = opts.endpoint.split(",")
else:
class_kwargs['service_endpoint'] = opts.endpoint
# CLI requires some special accommodation for Auth - custom class imports
if opts.auth:
# Currenly only hadoop's SimpleAuth and none are supported out of the box
if opts.auth == 'simple':
class_kwargs['auth'] = SimpleAuth()
else:
raise Exception(
"This auth mentod is not supported by CLI, please write your own python script if needed"
)
api = opts.api_class(**class_kwargs)
# Construct positional arguments for method
if 'method_args' in opts:
method_args = [getattr(opts, arg) for arg in opts.method_args]
else:
method_args = []
# Construct key arguments for method
if 'method_kwargs' in opts:
method_kwargs = dict((key, getattr(opts, key)) for key in opts.method_kwargs)
else:
method_kwargs = {}
response = getattr(api, opts.method)(*method_args, **method_kwargs)
pprint(response.data)