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
24 -def create_cluster(resource_root, name, version=None, fullVersion=None):
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: "CDH4")
30 - The CDH minor version will be assumed to be the
31 latest released version for CDH4, or 5.0 for CDH5.
32 @param fullVersion: Cluster's full CDH version. (eg: "5.1.1")
33 - If specified, 'version' will be ignored.
34 - Since: v6
35 @return: An ApiCluster object
36 """
37 if version is None and fullVersion is None:
38 raise Exception("Either 'version' or 'fullVersion' must be specified")
39 if fullVersion is not None:
40 api_version = 6
41 version = None
42 else:
43 api_version = 1
44
45 apicluster = ApiCluster(resource_root, name, version, fullVersion)
46 return call(resource_root.post, CLUSTERS_PATH, ApiCluster, True,
47 data=[apicluster], api_version=api_version)[0]
48
50 """
51 Lookup a cluster by name
52 @param resource_root: The root Resource object.
53 @param name: Cluster name
54 @return: An ApiCluster object
55 """
56 return call(resource_root.get, "%s/%s" % (CLUSTERS_PATH, name), ApiCluster)
57
59 """
60 Get all clusters
61 @param resource_root: The root Resource object.
62 @return: A list of ApiCluster objects.
63 """
64 return call(resource_root.get, CLUSTERS_PATH, ApiCluster, True,
65 params=view and dict(view=view) or None)
66
68 """
69 Delete a cluster by name
70 @param resource_root: The root Resource object.
71 @param name: Cluster name
72 @return: The deleted ApiCluster object
73 """
74 return call(resource_root.delete, "%s/%s" % (CLUSTERS_PATH, name), ApiCluster)
75
77 _ATTRIBUTES = {
78 'name' : None,
79 'displayName' : None,
80 'clusterUrl' : None,
81 'version' : None,
82 'fullVersion' : None,
83 'hostsUrl' : ROAttr(),
84 'maintenanceMode' : ROAttr(),
85 'maintenanceOwners' : ROAttr(),
86 'entityStatus' : ROAttr(),
87 }
88
89 - def __init__(self, resource_root, name=None, version=None, fullVersion=None):
91
93 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
94
97
99 """Change cluster attributes"""
100 cluster = self._put('', ApiCluster, data=dic, params=params)
101 self._update(cluster)
102 return self
103
105 """
106 Get all service types supported by this cluster.
107
108 @return: A list of service types (strings)
109 """
110 resp = self._get_resource_root().get(self._path() + '/serviceTypes')
111 return resp[ApiList.LIST_KEY]
112
114 """
115 Retrieve a list of running commands for this cluster.
116
117 @param view: View to materialize ('full' or 'summary')
118 @return: A list of running commands.
119 """
120 return self._get("commands", ApiCommand, True,
121 params = view and dict(view=view) or None)
122
124 """
125 Rename a cluster.
126
127 @param newname: New cluster name
128 @return: An ApiCluster object
129 @since: API v2
130 """
131 dic = self.to_json_dict()
132 if self._get_resource_root().version < 6:
133 dic['name'] = newname
134 else:
135 dic['displayName'] = newname
136 return self._put_cluster(dic)
137
139 """
140 Manually set the CDH version.
141
142 @param new_cdh_version: New CDH version, e.g. 4.5.1
143 @return: An ApiCluster object
144 @since: API v6
145 """
146 dic = self.to_json_dict()
147 dic['fullVersion'] = new_cdh_version
148 return self._put_cluster(dic)
149
151 """
152 Create a service.
153
154 @param name: Service name
155 @param service_type: Service type
156 @return: An ApiService object
157 """
158 return services.create_service(self._get_resource_root(), name,
159 service_type, self.name)
160
162 """
163 Delete a service by name.
164
165 @param name: Service name
166 @return: The deleted ApiService object
167 """
168 return services.delete_service(self._get_resource_root(), name, self.name)
169
171 """
172 Lookup a service by name.
173
174 @param name: Service name
175 @return: An ApiService object
176 """
177 return services.get_service(self._get_resource_root(), name, self.name)
178
186
188 """
189 Lookup a parcel by product and version.
190
191 @param product: the product name
192 @param version: the product version
193 @return: An ApiParcel object
194 """
195 return parcels.get_parcel(self._get_resource_root(), product, version, self.name)
196
204
206 """
207 Lists all the hosts that are associated with this cluster.
208
209 @return: A list of ApiHostRef objects of the hosts in the cluster.
210 @since: API v3
211 """
212 return self._get("hosts", ApiHostRef, True, api_version=3)
213
215 """
216 Removes the association of the host with the cluster.
217
218 @return: A ApiHostRef of the host that was removed.
219 @since: API v3
220 """
221 return self._delete("hosts/" + hostId, ApiHostRef, api_version=3)
222
224 """
225 Removes the association of all the hosts with the cluster.
226
227 @return: A list of ApiHostRef objects of the hosts that were removed.
228 @since: API v3
229 """
230 return self._delete("hosts", ApiHostRef, True, api_version=3)
231
233 """
234 Adds a host to the cluster.
235
236 @param hostIds: List of IDs of hosts to add to cluster.
237 @return: A list of ApiHostRef objects of the new
238 hosts that were added to the cluster
239 @since: API v3
240 """
241 hostRefList = [ApiHostRef(self._get_resource_root(), x) for x in hostIds]
242 return self._post("hosts", ApiHostRef, True, data=hostRefList,
243 api_version=3)
244
246 """
247 Start all services in a cluster, respecting dependencies.
248
249 @return: Reference to the submitted command.
250 """
251 return self._cmd('start')
252
254 """
255 Stop all services in a cluster, respecting dependencies.
256
257 @return: Reference to the submitted command.
258 """
259 return self._cmd('stop')
260
261 - def restart(self, restart_only_stale_services=None,
262 redeploy_client_configuration=None,
263 restart_service_names=None):
264 """
265 Restart all services in the cluster.
266 Services are restarted in the appropriate order given their dependencies.
267
268 @param restart_only_stale_services: Only restart services that have stale
269 configuration and their dependent
270 services. Default is False.
271 @param redeploy_client_configuration: Re-deploy client configuration for
272 all services in the cluster. Default
273 is False.
274 @param restart_service_names: Only restart services that are specified and their dependent services.
275 Available since API v11.
276 @since API v6
277
278 @return: Reference to the submitted command.
279 """
280 if self._get_resource_root().version < 6:
281 return self._cmd('restart')
282 else:
283 args = dict()
284 args['restartOnlyStaleServices'] = restart_only_stale_services
285 args['redeployClientConfiguration'] = redeploy_client_configuration
286 if self._get_resource_root().version >= 11:
287 args['restartServiceNames'] = restart_service_names
288 return self._cmd('restart', data=args, api_version=6)
289
291 """
292 Deploys Service client configuration to the hosts on the cluster.
293
294 @return: Reference to the submitted command.
295 @since: API v2
296 """
297 return self._cmd('deployClientConfig')
298
300 """
301 Deploys Cluster client configuration (Kerberos configuration) to the
302 hosts on the cluster. Any hosts that are decommissioned or have running
303 roles will be skipped.
304
305 @param hostIds: hostIds of hosts to deploy to. If empty, deploys to all
306 hosts in the cluster.
307 @return: Reference to the submitted command.
308 @since: API v7
309 """
310 return self._cmd('deployClusterClientConfig', data=hostIds,
311 api_version=7)
312
314 """
315 This command is no longer recommended with API v6 onwards. It simply does
316 not work when parcels are used, and even with packages it may fail due to
317 a race. Use upgrade_cdh instead.
318
319 Upgrades the services in the cluster to CDH5 version.
320 This command requires that the CDH packages in the hosts used by the
321 cluster be upgraded to CDH5 before this command is issued. Once issued,
322 this command will stop all running services before proceeding.
323
324 If parcels are used instead of CDH system packages then the following
325 steps need to happen in order:
326 1. Stop all services manually
327 2. Activate parcel
328 3. Run this upgrade command
329
330 The command will upgrade the services and their configuration to the
331 version available in the CDH5 distribution.
332
333 @return: Reference to the submitted command.
334 @deprecated: since API v6
335 """
336 return self._cmd('upgradeServices')
337
339 """
340 Put the cluster in maintenance mode.
341
342 @return: Reference to the completed command.
343 @since: API v2
344 """
345 cmd = self._cmd('enterMaintenanceMode')
346 if cmd.success:
347 self._update(get_cluster(self._get_resource_root(), self.name))
348 return cmd
349
351 """
352 Take the cluster out of maintenance mode.
353
354 @return: Reference to the completed command.
355 @since: API v2
356 """
357 cmd = self._cmd('exitMaintenanceMode')
358 if cmd.success:
359 self._update(get_cluster(self._get_resource_root(), self.name))
360 return cmd
361
368
376
384
392
393 - def rolling_restart(self, slave_batch_size=None,
394 slave_fail_count_threshold=None,
395 sleep_seconds=None,
396 stale_configs_only=None,
397 unupgraded_only=None,
398 roles_to_include=None,
399 restart_service_names=None):
400 """
401 Command to do a "best-effort" rolling restart of the given cluster,
402 i.e. it does plain restart of services that cannot be rolling restarted,
403 followed by first rolling restarting non-slaves and then rolling restarting
404 the slave roles of services that can be rolling restarted. The slave restarts
405 are done host-by-host.
406 @param slave_batch_size: Number of hosts with slave roles to restart at a time
407 Must be greater than 0. Default is 1.
408 @param slave_fail_count_threshold: The threshold for number of slave host batches that
409 are allowed to fail to restart before the entire command is considered failed.
410 Must be >= 0. Default is 0.
411 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches.
412 Must be >=0. Default is 0.
413 @param stale_configs_only: Restart roles with stale configs only. Default is false.
414 @param unupgraded_only: Restart roles that haven't been upgraded yet. Default is false.
415 @param roles_to_include: Role types to restart. Default is slave roles only.
416 @param restart_service_names: List of specific services to restart.
417 @return: Reference to the submitted command.
418 @since: API v4
419 """
420 args = dict()
421 if slave_batch_size:
422 args['slaveBatchSize'] = slave_batch_size
423 if slave_fail_count_threshold:
424 args['slaveFailCountThreshold'] = slave_fail_count_threshold
425 if sleep_seconds:
426 args['sleepSeconds'] = sleep_seconds
427 if stale_configs_only:
428 args['staleConfigsOnly'] = stale_configs_only
429 if unupgraded_only:
430 args['unUpgradedOnly'] = unupgraded_only
431 if roles_to_include:
432 args['rolesToInclude'] = roles_to_include
433 if restart_service_names:
434 args['restartServiceNames'] = restart_service_names
435
436 return self._cmd('rollingRestart', data=args, api_version=4)
437
438 - def rolling_upgrade(self, upgrade_from_cdh_version,
439 upgrade_to_cdh_version,
440 upgrade_service_names,
441 slave_batch_size=None,
442 slave_fail_count_threshold=None,
443 sleep_seconds=None):
444 """
445 Command to do a rolling upgrade of services in the given cluster
446
447 This command does not handle any services that don't support rolling
448 upgrades. The command will throw an error and not start if upgrade of
449 any such service is requested.
450
451 This command does not upgrade the full CDH Cluster. You should normally
452 use the upgradeCDH Command for upgrading the cluster. This is primarily
453 helpful if you need to need to recover from an upgrade failure or for
454 advanced users to script an alternative to the upgradeCdhCommand.
455
456 This command expects the binaries to be available on hosts and activated.
457 It does not change any binaries on the hosts.
458
459 @param upgrade_from_cdh_version: Current CDH Version of the services.
460 Example versions are: "5.1.0", "5.2.2" or "5.4.0"
461 @param upgrade_to_cdh_version: Target CDH Version for the services.
462 The CDH version should already be present and activated on the nodes.
463 Example versions are: "5.1.0", "5.2.2" or "5.4.0"
464 @param upgrade_service_names: List of specific services to be upgraded and restarted.
465 @param slave_batch_size: Number of hosts with slave roles to restart at a time
466 Must be greater than 0. Default is 1.
467 @param slave_fail_count_threshold: The threshold for number of slave host batches that
468 are allowed to fail to restart before the entire command is considered failed.
469 Must be >= 0. Default is 0.
470 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches.
471 Must be >=0. Default is 0.
472
473 @return: Reference to the submitted command.
474 @since: API v10
475 """
476 args = dict()
477 args['upgradeFromCdhVersion'] = upgrade_from_cdh_version
478 args['upgradeToCdhVersion'] = upgrade_to_cdh_version
479 args['upgradeServiceNames'] = upgrade_service_names
480
481 if slave_batch_size:
482 args['slaveBatchSize'] = slave_batch_size
483 if slave_fail_count_threshold:
484 args['slaveFailCountThreshold'] = slave_fail_count_threshold
485 if sleep_seconds:
486 args['sleepSeconds'] = sleep_seconds
487
488 return self._cmd('rollingUpgrade', data=args, api_version=10)
489
491 """
492 Automatically assign roles to hosts and create the roles for all the services in a cluster.
493
494 Assignments are done based on services in the cluster and hardware specifications.
495 Existing roles will be taken into account and their assignments will be not be modified.
496 @since: API v6
497 """
498 self._put("autoAssignRoles", None, api_version=6)
499
514
516 """
517 Prepare and start services in a cluster.
518 Perform all the steps needed to prepare each service in a
519 cluster and start the services in order.
520
521 @return: Reference to the submitted command.
522 @since: API v7
523 """
524 return self._cmd('firstRun', None, api_version=7)
525
526 - def upgrade_cdh(self, deploy_client_config=True, start_all_services=True,
527 cdh_parcel_version=None, cdh_package_version=None,
528 rolling_restart=False, slave_batch_size=None, sleep_seconds=None,
529 slave_fail_count_threshold=None):
530 """
531 Perform CDH upgrade to the next major version. In v9+, also supports
532 minor CDH 5 upgrades (5.a.b to 5.x.y where x > a) and supports maintenance
533 release changes (a.b.x to a.b.y).
534
535 If using packages, CDH packages on all hosts of the cluster must be
536 manually upgraded before this command is issued.
537
538 The command will upgrade the services and their configuration to the
539 requested version. All running services will be stopped before proceeding,
540 unless rolling restart is requested and is available.
541
542 @param deploy_client_config: Whether to deploy client configurations
543 after the upgrade. Default is True. Has no effect in v9+;
544 client configurations are always deployed.
545 @param start_all_services: Whether to start all services after the upgrade.
546 Default is True. Has no effect in v9+; services are always
547 restarted.
548 @param cdh_parcel_version: If upgrading to parcels, the full version of an
549 already distributed parcel for the next CDH version. Default
550 is None. Example versions are: '5.0.0-1.cdh5.0.0.p0.11' or
551 '5.0.2-1.cdh5.0.2.p0.32'.
552 @param cdh_package_version: If upgrading to packages, the full version of an
553 already installed package for the next CDH version. Default
554 is None. Example versions are: '5.2.0' or '4.5.0'. Only available
555 since v9.
556 @param rolling_restart: If you'd like to do a rolling restart, set this to
557 True. Default is False. Only available since v9.
558 @param slave_batch_size: Controls the rolling restart slave batch size.
559 Only applicable when rolling_restart is True.
560 @param sleep_seconds: Controls how many seconds to sleep betweein rolling
561 restart batches. Only applicable when rolling_restart is True.
562 @param slave_fail_count_threshold: Controls how many slave restart failures
563 are tolerated in a rolling restart. Only applicable when
564 rolling_restart is True.
565 @return: Reference to the submitted command.
566 @since: API v6 for major upgrades only, v9 for maintenance and CDH 5 minor
567 releases.
568 """
569 args = dict()
570 args['deployClientConfig'] = deploy_client_config
571 args['startAllServices'] = start_all_services
572 if cdh_parcel_version:
573 args['cdhParcelVersion'] = cdh_parcel_version
574 if cdh_package_version:
575 args['cdhPackageVersion'] = cdh_package_version
576 if rolling_restart:
577 args['rollingRestartArgs'] = {
578 'slaveBatchSize' : slave_batch_size,
579 'sleepSeconds' : sleep_seconds,
580 'slaveFailCountThreshold' : slave_fail_count_threshold
581 }
582 return self._cmd('upgradeCdh', data=args, api_version=6)
583
608