1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from cm_api.endpoints.types import *
18 from cm_api.endpoints import services, parcels, host_templates
19
20 __docformat__ = "epytext"
21
22 CLUSTERS_PATH = "/clusters"
23
25 """
26 Create a cluster
27 @param resource_root: The root Resource object.
28 @param name: Cluster name
29 @param version: Cluster CDH major version (eg: "4")
30 - The CDH minor version will be assumed to be the
31 latest released version, if 'fullVersion' is not
32 specified.
33 @param fullVersion: Cluster's full CDH version. (eg: "4.6.0")
34 - If specified, 'version' will be ignored.
35 - Since: v6
36 @return: An ApiCluster object
37 """
38 if fullVersion is not None:
39 api_version = 6
40 else:
41 api_version = 1
42
43 apicluster = ApiCluster(resource_root, name, version, fullVersion)
44 return call(resource_root.post, CLUSTERS_PATH, ApiCluster, True,
45 data=[apicluster], api_version=api_version)[0]
46
48 """
49 Lookup a cluster by name
50 @param resource_root: The root Resource object.
51 @param name: Cluster name
52 @return: An ApiCluster object
53 """
54 return call(resource_root.get, "%s/%s" % (CLUSTERS_PATH, name), ApiCluster)
55
57 """
58 Get all clusters
59 @param resource_root: The root Resource object.
60 @return: A list of ApiCluster objects.
61 """
62 return call(resource_root.get, CLUSTERS_PATH, ApiCluster, True,
63 params=view and dict(view=view) or None)
64
66 """
67 Delete a cluster by name
68 @param resource_root: The root Resource object.
69 @param name: Cluster name
70 @return: The deleted ApiCluster object
71 """
72 return call(resource_root.delete, "%s/%s" % (CLUSTERS_PATH, name), ApiCluster)
73
75 _ATTRIBUTES = {
76 'name' : None,
77 'displayName' : None,
78 'version' : None,
79 'fullVersion' : None,
80 'maintenanceMode' : ROAttr(),
81 'maintenanceOwners' : ROAttr(),
82 }
83
84 - def __init__(self, resource_root, name=None, version=None, fullVersion=None):
86
88 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
89
92
94 """Change cluster attributes"""
95 cluster = self._put('', ApiCluster, data=dic, params=params)
96 self._update(cluster)
97 return self
98
100 """
101 Get all service types supported by this cluster.
102
103 @return: A list of service types (strings)
104 """
105 resp = self._get_resource_root().get(self._path() + '/serviceTypes')
106 return resp[ApiList.LIST_KEY]
107
109 """
110 Retrieve a list of running commands for this cluster.
111
112 @param view: View to materialize ('full' or 'summary')
113 @return: A list of running commands.
114 """
115 return self._get("commands", ApiCommand, True,
116 params = view and dict(view=view) or None)
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 if self._get_resource_root().version < 6:
128 dic['name'] = newname
129 else:
130 dic['displayName'] = newname
131 return self._put_cluster(dic)
132
134 """
135 Manually set the CDH version.
136
137 @param new_cdh_version: New CDH version, e.g. 4.5.1
138 @return: An ApiCluster object
139 @since: API v6
140 """
141 dic = self.to_json_dict()
142 dic['fullVersion'] = new_cdh_version
143 return self._put_cluster(dic)
144
146 """
147 Create a service.
148
149 @param name: Service name
150 @param service_type: Service type
151 @return: An ApiService object
152 """
153 return services.create_service(self._get_resource_root(), name,
154 service_type, self.name)
155
157 """
158 Delete a service by name.
159
160 @param name: Service name
161 @return: The deleted ApiService object
162 """
163 return services.delete_service(self._get_resource_root(), name, self.name)
164
166 """
167 Lookup a service by name.
168
169 @param name: Service name
170 @return: An ApiService object
171 """
172 return services.get_service(self._get_resource_root(), name, self.name)
173
181
183 """
184 Lookup a parcel by product and version.
185
186 @param product: the product name
187 @param version: the product version
188 @return: An ApiParcel object
189 """
190 return parcels.get_parcel(self._get_resource_root(), product, version, self.name)
191
199
201 """
202 Lists all the hosts that are associated with this cluster.
203
204 @return: A list of ApiHostRef objects of the hosts in the cluster.
205 @since: API v3
206 """
207 return self._get("hosts", ApiHostRef, True, api_version=3)
208
210 """
211 Removes the association of the host with the cluster.
212
213 @return: A ApiHostRef of the host that was removed.
214 @since: API v3
215 """
216 return self._delete("hosts/" + hostId, ApiHostRef, api_version=3)
217
219 """
220 Removes the association of all the hosts with the cluster.
221
222 @return: A list of ApiHostRef objects of the hosts that were removed.
223 @since: API v3
224 """
225 return self._delete("hosts", ApiHostRef, True, api_version=3)
226
228 """
229 Adds a host to the cluster.
230
231 @param hostIds: List of IDs of hosts to add to cluster.
232 @return: A list of ApiHostRef objects of the new
233 hosts that were added to the cluster
234 @since: API v3
235 """
236 hostRefList = [ApiHostRef(self._get_resource_root(), x) for x in hostIds]
237 return self._post("hosts", ApiHostRef, True, data=hostRefList,
238 api_version=3)
239
241 """
242 Start all services in a cluster, respecting dependencies.
243
244 @return: Reference to the submitted command.
245 """
246 return self._cmd('start')
247
249 """
250 Stop all services in a cluster, respecting dependencies.
251
252 @return: Reference to the submitted command.
253 """
254 return self._cmd('stop')
255
257 """
258 Restart all services in the cluster.
259 Services are restarted in the appropriate order given their dependencies.
260
261 @return: Reference to the submitted command.
262 """
263 return self._cmd('restart')
264
266 """
267 Deploys client configuration to the hosts on the cluster.
268
269 @return: Reference to the submitted command.
270 @since: API v2
271 """
272 return self._cmd('deployClientConfig')
273
275 """
276 This command is no longer recommended with API v6 onwards. It simply does
277 not work when parcels are used, and even with packages it may fail due to
278 a race. Use upgrade_cdh instead.
279
280 Upgrades the services in the cluster to CDH5 version.
281 This command requires that the CDH packages in the hosts used by the
282 cluster be upgraded to CDH5 before this command is issued. Once issued,
283 this command will stop all running services before proceeding.
284
285 If parcels are used instead of CDH system packages then the following
286 steps need to happen in order:
287 1. Stop all services manually
288 2. Activate parcel
289 3. Run this upgrade command
290
291 The command will upgrade the services and their configuration to the
292 version available in the CDH5 distribution.
293
294 @return: Reference to the submitted command.
295 @deprecated: since API v6
296 """
297 return self._cmd('upgradeServices')
298
300 """
301 Put the cluster in maintenance mode.
302
303 @return: Reference to the completed command.
304 @since: API v2
305 """
306 cmd = self._cmd('enterMaintenanceMode')
307 if cmd.success:
308 self._update(get_cluster(self._get_resource_root(), self.name))
309 return cmd
310
312 """
313 Take the cluster out of maintenance mode.
314
315 @return: Reference to the completed command.
316 @since: API v2
317 """
318 cmd = self._cmd('exitMaintenanceMode')
319 if cmd.success:
320 self._update(get_cluster(self._get_resource_root(), self.name))
321 return cmd
322
329
337
345
353
354 - def rolling_restart(self, slave_batch_size=None,
355 slave_fail_count_threshold=None,
356 sleep_seconds=None,
357 stale_configs_only=None,
358 unupgraded_only=None,
359 roles_to_include=None,
360 restart_service_names=None):
361 """
362 Command to do a "best-effort" rolling restart of the given cluster,
363 i.e. it does plain restart of services that cannot be rolling restarted,
364 followed by first rolling restarting non-slaves and then rolling restarting
365 the slave roles of services that can be rolling restarted. The slave restarts
366 are done host-by-host.
367 @param slave_batch_size: Number of hosts with slave roles to restart at a time
368 Must be greater than 0. Default is 1.
369 @param slave_fail_count_threshold: The threshold for number of slave host batches that
370 are allowed to fail to restart before the entire command is considered failed.
371 Must be >= 0. Default is 0.
372 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches.
373 Must be >=0. Default is 0.
374 @param stale_configs_only: Restart roles with stale configs only. Default is false.
375 @param unupgraded_only: Restart roles that haven't been upgraded yet. Default is false.
376 @param roles_to_include: Role types to restart. Default is slave roles only.
377 @param restart_service_names: List of specific services to restart.
378 @return: Reference to the submitted command.
379 @since: API v4
380 """
381 args = dict()
382 if slave_batch_size:
383 args['slaveBatchSize'] = slave_batch_size
384 if slave_fail_count_threshold:
385 args['slaveFailCountThreshold'] = slave_fail_count_threshold
386 if sleep_seconds:
387 args['sleepSeconds'] = sleep_seconds
388 if stale_configs_only:
389 args['staleConfigsOnly'] = stale_configs_only
390 if unupgraded_only:
391 args['unUpgradedOnly'] = unupgraded_only
392 if roles_to_include:
393 args['rolesToInclude'] = roles_to_include
394 if restart_service_names:
395 args['restartServiceNames'] = restart_service_names
396
397 return self._cmd('rollingRestart', data=args, api_version=4)
398
400 """
401 Automatically assign roles to hosts and create the roles for all the services in a cluster.
402
403 Assignments are done based on services in the cluster and hardware specifications.
404 Existing roles will be taken into account and their assignments will be not be modified.
405 @since: API v6
406 """
407 self._put("autoAssignRoles", None, api_version=6)
408
423
424 - def upgrade_cdh(self, deploy_client_config=True, start_all_services=True, cdh_parcel_version=None):
425 """
426 Perform CDH upgrade to the next major version.
427
428 If using packages, CDH packages on all hosts of the cluster must be
429 manually upgraded before this command is issued.
430
431 The command will upgrade the services and their configuration to the
432 version available in the CDH5 distribution. All running services will
433 be stopped before proceeding.
434
435 @param deploy_client_config: Whether to deploy client configurations
436 after the upgrade. Default is True.
437 @param start_all_services: Whether to start all services after the upgrade.
438 Default is True.
439 @param cdh_parcel_version: If using parcels, the full version of an
440 already distributed parcel for the next major CDH version. Default
441 is None. Example versions are: '5.0.0-1.cdh5.0.0.p0.11' or
442 '5.0.2-1.cdh5.0.2.p0.32'
443 @return: Reference to the submitted command.
444 @since: API v6
445 """
446 args = dict()
447 args['deployClientConfig'] = deploy_client_config
448 args['startAllServices'] = start_all_services
449 if cdh_parcel_version:
450 args['cdhParcelVersion'] = cdh_parcel_version
451 return self._cmd('upgradeCdh', data=args, api_version=6)
452