1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 try:
18 import json
19 except ImportError:
20 import simplejson as json
21
22 from cm_api.endpoints.types import *
23 from cm_api.endpoints import services, parcels, host_templates
24
25 __docformat__ = "epytext"
26
27 CLUSTERS_PATH = "/clusters"
28
30 """
31 Create a cluster
32 @param resource_root: The root Resource object.
33 @param name: Cluster name
34 @param version: Cluster CDH version
35 @return: An ApiCluster object
36 """
37 apicluster = ApiCluster(resource_root, name, version)
38 apicluster_list = ApiList([apicluster])
39 body = json.dumps(apicluster_list.to_json_dict())
40 resp = resource_root.post(CLUSTERS_PATH, data=body)
41
42 return ApiList.from_json_dict(ApiCluster, resp, resource_root)[0]
43
45 """
46 Lookup a cluster by name
47 @param resource_root: The root Resource object.
48 @param name: Cluster name
49 @return: An ApiCluster object
50 """
51 dic = resource_root.get("%s/%s" % (CLUSTERS_PATH, name))
52 return ApiCluster.from_json_dict(dic, resource_root)
53
55 """
56 Get all clusters
57 @param resource_root: The root Resource object.
58 @return: A list of ApiCluster objects.
59 """
60 dic = resource_root.get(CLUSTERS_PATH,
61 params=view and dict(view=view) or None)
62 return ApiList.from_json_dict(ApiCluster, dic, resource_root)
63
65 """
66 Delete a cluster by name
67 @param resource_root: The root Resource object.
68 @param name: Cluster name
69 @return: The deleted ApiCluster object
70 """
71 resp = resource_root.delete("%s/%s" % (CLUSTERS_PATH, name))
72 return ApiCluster.from_json_dict(resp, resource_root)
73
74
76 _ATTRIBUTES = {
77 'name' : None,
78 'version' : None,
79 'maintenanceMode' : ROAttr(),
80 'maintenanceOwners' : ROAttr(),
81 }
82
83 - def __init__(self, resource_root, name=None, version=None):
85
87 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
88
91
92 - def _cmd(self, cmd, data=None):
96
97 - def _put(self, dic, params=None):
105
107 """
108 Retrieve a list of running commands for this cluster.
109
110 @param view: View to materialize ('full' or 'summary')
111 @return: A list of running commands.
112 """
113 resp = self._get_resource_root().get(
114 self._path() + '/commands',
115 params = view and dict(view=view) or None)
116 return ApiList.from_json_dict(ApiCommand, resp, self._get_resource_root())
117
119 """
120 Rename a cluster.
121
122 @param newname: New cluster name
123 @return: An ApiCluster object
124 @since: API v2
125 """
126 dic = self.to_json_dict()
127 dic['name'] = newname
128 return self._put(dic)
129
131 """
132 Create a service.
133
134 @param name: Service name
135 @param service_type: Service type
136 @return: An ApiService object
137 """
138 return services.create_service(self._get_resource_root(), name,
139 service_type, self.name)
140
149
151 """
152 Lookup a service by name.
153
154 @param name: Service name
155 @return: An ApiService object
156 """
157 return services.get_service(self._get_resource_root(), name, self.name)
158
166
168 """
169 Lookup a parcel by product and version.
170
171 @param product: the product name
172 @param version: the product version
173 @return: An ApiParcel object
174 """
175 return parcels.get_parcel(self._get_resource_root(), product, version, self.name)
176
184
194
196 """
197 Removes the association of the host with the cluster.
198
199 @return: A ApiHostRef of the host that was removed.
200 """
201 resource_root = self._get_resource_root()
202 resp = resource_root.delete("%s/hosts/%s" % (self._path(), hostId))
203 return ApiHostRef.from_json_dict(resp, resource_root)
204
214
216 """
217 Adds a host to the cluster.
218
219 @param hostIds: List of IDs of hosts to add to cluster.
220 @return: A list of ApiHostRef objects of the new
221 hosts that were added to the cluster
222 """
223 resource_root = self._get_resource_root()
224 hostRefList = ApiList([ApiHostRef(resource_root, x) for x in hostIds])
225 body = json.dumps(hostRefList.to_json_dict())
226 resp = resource_root.post(self._path() + '/hosts', data=body)
227 return ApiList.from_json_dict(ApiHostRef, resp, resource_root)[0]
228
230 """
231 Start all services in a cluster, respecting dependencies.
232
233 @return: Reference to the submitted command.
234 """
235 return self._cmd('start')
236
238 """
239 Stop all services in a cluster, respecting dependencies.
240
241 @return: Reference to the submitted command.
242 """
243 return self._cmd('stop')
244
246 """
247 Deploys client configuration to the hosts on the cluster.
248
249 @return: Reference to the submitted command.
250 @since: API v2
251 """
252 return self._cmd('deployClientConfig')
253
255 """
256 Put the cluster in maintenance mode.
257
258 @return: Reference to the completed command.
259 @since: API v2
260 """
261 cmd = self._cmd('enterMaintenanceMode')
262 if cmd.success:
263 self._update(get_cluster(self._get_resource_root(), self.name))
264 return cmd
265
267 """
268 Take the cluster out of maintenance mode.
269
270 @return: Reference to the completed command.
271 @since: API v2
272 """
273 cmd = self._cmd('exitMaintenanceMode')
274 if cmd.success:
275 self._update(get_cluster(self._get_resource_root(), self.name))
276 return cmd
277
284
292
300
308
309 - def rolling_restart(self, slave_batch_size=None,
310 slave_fail_count_threshold=None,
311 sleep_seconds=None,
312 stale_configs_only=None,
313 unupgraded_only=None,
314 roles_to_include=None,
315 restart_service_names=None):
316 """
317 Command to do a "best-effort" rolling restart of the given cluster,
318 i.e. it does plain restart of services that cannot be rolling restarted,
319 followed by first rolling restarting non-slaves and then rolling restarting
320 the slave roles of services that can be rolling restarted. The slave restarts
321 are done host-by-host.
322 @param: slave_batch_size Number of hosts with slave roles to restart at a time
323 Must be greater than 0. Default is 1.
324 @param: slave_fail_count_threshold The threshold for number of slave host batches that
325 are allowed to fail to restart before the entire command is considered failed.
326 Must be >= 0. Default is 0.
327 @param: sleep_seconds Number of seconds to sleep between restarts of slave host batches.
328 Must be >=0. Default is 0.
329 @param: stale_configs_only Restart roles with stale configs only. Default is false.
330 @param: unupgraded_only Restart roles that haven't been upgraded yet. Default is false.
331 @param: roles_to_include Role types to restart. Default is slave roles only.
332 @param: restart_service_names List of specific services to restart.
333 @return: Reference to the submitted command.
334 @since: API v4
335 """
336 self._require_min_api_version(4)
337 args = dict()
338 if slave_batch_size:
339 args['slaveBatchSize'] = slave_batch_size
340 if slave_fail_count_threshold:
341 args['slaveFailCountThreshold'] = slave_fail_count_threshold
342 if sleep_seconds:
343 args['sleepSeconds'] = sleep_seconds
344 if stale_configs_only:
345 args['staleConfigsOnly'] = stale_configs_only
346 if unupgraded_only:
347 args['unUpgradedOnly'] = unupgraded_only
348 if roles_to_include:
349 args['rolesToInclude'] = roles_to_include
350 if restart_service_names:
351 args['restartServiceNames'] = restart_service_names
352
353 return self._cmd('rollingRestart', data = json.dumps(args))
354