Package cm_api :: Package endpoints :: Module clusters
[hide private]
[frames] | no frames]

Source Code for Module cm_api.endpoints.clusters

  1  # Licensed to Cloudera, Inc. under one 
  2  # or more contributor license agreements.  See the NOTICE file 
  3  # distributed with this work for additional information 
  4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
  5  # to you under the Apache License, Version 2.0 (the 
  6  # "License"); you may not use this file except in compliance 
  7  # with the License.  You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 16   
 17  from cm_api.endpoints.types import * 
 18  from cm_api.endpoints import services, parcels, host_templates 
 19  from sys import api_version 
 20   
 21  __docformat__ = "epytext" 
 22   
 23  CLUSTERS_PATH = "/clusters" 
 24   
25 -def create_cluster(resource_root, name, version=None, fullVersion=None):
26 """ 27 Create a cluster 28 @param resource_root: The root Resource object. 29 @param name: Cluster name 30 @param version: Cluster CDH major version (eg: "CDH4") 31 - The CDH minor version will be assumed to be the 32 latest released version for CDH4, or 5.0 for CDH5. 33 @param fullVersion: Cluster's full CDH version. (eg: "5.1.1") 34 - If specified, 'version' will be ignored. 35 - Since: v6 36 @return: An ApiCluster object 37 """ 38 if version is None and fullVersion is None: 39 raise Exception("Either 'version' or 'fullVersion' must be specified") 40 if fullVersion is not None: 41 api_version = 6 42 version = None 43 else: 44 api_version = 1 45 46 apicluster = ApiCluster(resource_root, name, version, fullVersion) 47 return call(resource_root.post, CLUSTERS_PATH, ApiCluster, True, 48 data=[apicluster], api_version=api_version)[0]
49
50 -def get_cluster(resource_root, name):
51 """ 52 Lookup a cluster by name 53 @param resource_root: The root Resource object. 54 @param name: Cluster name 55 @return: An ApiCluster object 56 """ 57 return call(resource_root.get, "%s/%s" % (CLUSTERS_PATH, name), ApiCluster)
58
59 -def get_all_clusters(resource_root, view=None):
60 """ 61 Get all clusters 62 @param resource_root: The root Resource object. 63 @return: A list of ApiCluster objects. 64 """ 65 return call(resource_root.get, CLUSTERS_PATH, ApiCluster, True, 66 params=view and dict(view=view) or None)
67
68 -def delete_cluster(resource_root, name):
69 """ 70 Delete a cluster by name 71 @param resource_root: The root Resource object. 72 @param name: Cluster name 73 @return: The deleted ApiCluster object 74 """ 75 return call(resource_root.delete, "%s/%s" % (CLUSTERS_PATH, name), ApiCluster)
76
77 -class ApiCluster(BaseApiResource):
78 _ATTRIBUTES = { 79 'name' : None, 80 'displayName' : None, 81 'clusterUrl' : None, 82 'version' : None, 83 'fullVersion' : None, 84 'hostsUrl' : ROAttr(), 85 'maintenanceMode' : ROAttr(), 86 'maintenanceOwners' : ROAttr(), 87 'entityStatus' : ROAttr(), 88 } 89
90 - def __init__(self, resource_root, name=None, version=None, fullVersion=None):
91 BaseApiObject.init(self, resource_root, locals())
92
93 - def __str__(self):
94 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
95
96 - def _path(self):
97 return "%s/%s" % (CLUSTERS_PATH, self.name)
98
99 - def _put_cluster(self, dic, params=None):
100 """Change cluster attributes""" 101 cluster = self._put('', ApiCluster, data=dic, params=params) 102 self._update(cluster) 103 return self
104
105 - def get_service_types(self):
106 """ 107 Get all service types supported by this cluster. 108 109 @return: A list of service types (strings) 110 """ 111 resp = self._get_resource_root().get(self._path() + '/serviceTypes') 112 return resp[ApiList.LIST_KEY]
113
114 - def get_commands(self, view=None):
115 """ 116 Retrieve a list of running commands for this cluster. 117 118 @param view: View to materialize ('full' or 'summary') 119 @return: A list of running commands. 120 """ 121 return self._get("commands", ApiCommand, True, 122 params = view and dict(view=view) or None)
123
124 - def rename(self, newname):
125 """ 126 Rename a cluster. 127 128 @param newname: New cluster name 129 @return: An ApiCluster object 130 @since: API v2 131 """ 132 dic = self.to_json_dict() 133 if self._get_resource_root().version < 6: 134 dic['name'] = newname 135 else: 136 dic['displayName'] = newname 137 return self._put_cluster(dic)
138
139 - def update_cdh_version(self, new_cdh_version):
140 """ 141 Manually set the CDH version. 142 143 @param new_cdh_version: New CDH version, e.g. 4.5.1 144 @return: An ApiCluster object 145 @since: API v6 146 """ 147 dic = self.to_json_dict() 148 dic['fullVersion'] = new_cdh_version 149 return self._put_cluster(dic)
150
151 - def create_service(self, name, service_type):
152 """ 153 Create a service. 154 155 @param name: Service name 156 @param service_type: Service type 157 @return: An ApiService object 158 """ 159 return services.create_service(self._get_resource_root(), name, 160 service_type, self.name)
161
162 - def delete_service(self, name):
163 """ 164 Delete a service by name. 165 166 @param name: Service name 167 @return: The deleted ApiService object 168 """ 169 return services.delete_service(self._get_resource_root(), name, self.name)
170
171 - def get_service(self, name):
172 """ 173 Lookup a service by name. 174 175 @param name: Service name 176 @return: An ApiService object 177 """ 178 return services.get_service(self._get_resource_root(), name, self.name)
179
180 - def get_all_services(self, view = None):
181 """ 182 Get all services in this cluster. 183 184 @return: A list of ApiService objects. 185 """ 186 return services.get_all_services(self._get_resource_root(), self.name, view)
187
188 - def get_parcel(self, product, version):
189 """ 190 Lookup a parcel by product and version. 191 192 @param product: the product name 193 @param version: the product version 194 @return: An ApiParcel object 195 """ 196 return parcels.get_parcel(self._get_resource_root(), product, version, self.name)
197
198 - def get_all_parcels(self, view = None):
199 """ 200 Get all parcels in this cluster. 201 202 @return: A list of ApiParcel objects. 203 """ 204 return parcels.get_all_parcels(self._get_resource_root(), self.name, view)
205
206 - def list_hosts(self):
207 """ 208 Lists all the hosts that are associated with this cluster. 209 210 @return: A list of ApiHostRef objects of the hosts in the cluster. 211 @since: API v3 212 """ 213 return self._get("hosts", ApiHostRef, True, api_version=3)
214
215 - def remove_host(self, hostId):
216 """ 217 Removes the association of the host with the cluster. 218 219 @return: A ApiHostRef of the host that was removed. 220 @since: API v3 221 """ 222 return self._delete("hosts/" + hostId, ApiHostRef, api_version=3)
223
224 - def remove_all_hosts(self):
225 """ 226 Removes the association of all the hosts with the cluster. 227 228 @return: A list of ApiHostRef objects of the hosts that were removed. 229 @since: API v3 230 """ 231 return self._delete("hosts", ApiHostRef, True, api_version=3)
232
233 - def add_hosts(self, hostIds):
234 """ 235 Adds a host to the cluster. 236 237 @param hostIds: List of IDs of hosts to add to cluster. 238 @return: A list of ApiHostRef objects of the new 239 hosts that were added to the cluster 240 @since: API v3 241 """ 242 hostRefList = [ApiHostRef(self._get_resource_root(), x) for x in hostIds] 243 return self._post("hosts", ApiHostRef, True, data=hostRefList, 244 api_version=3)
245
246 - def start(self):
247 """ 248 Start all services in a cluster, respecting dependencies. 249 250 @return: Reference to the submitted command. 251 """ 252 return self._cmd('start')
253
254 - def stop(self):
255 """ 256 Stop all services in a cluster, respecting dependencies. 257 258 @return: Reference to the submitted command. 259 """ 260 return self._cmd('stop')
261
262 - def restart(self, restart_only_stale_services=None, 263 redeploy_client_configuration=None, 264 restart_service_names=None):
265 """ 266 Restart all services in the cluster. 267 Services are restarted in the appropriate order given their dependencies. 268 269 @param restart_only_stale_services: Only restart services that have stale 270 configuration and their dependent 271 services. Default is False. 272 @param redeploy_client_configuration: Re-deploy client configuration for 273 all services in the cluster. Default 274 is False. 275 @param restart_service_names: Only restart services that are specified and their dependent services. 276 Available since API v11. 277 @since API v6 278 279 @return: Reference to the submitted command. 280 """ 281 if self._get_resource_root().version < 6: 282 return self._cmd('restart') 283 else: 284 args = dict() 285 args['restartOnlyStaleServices'] = restart_only_stale_services 286 args['redeployClientConfiguration'] = redeploy_client_configuration 287 if self._get_resource_root().version >= 11: 288 args['restartServiceNames'] = restart_service_names 289 return self._cmd('restart', data=args, api_version=6)
290
291 - def deploy_client_config(self):
292 """ 293 Deploys Service client configuration to the hosts on the cluster. 294 295 @return: Reference to the submitted command. 296 @since: API v2 297 """ 298 return self._cmd('deployClientConfig')
299
300 - def deploy_cluster_client_config(self, hostIds=[]):
301 """ 302 Deploys Cluster client configuration (Kerberos configuration) to the 303 hosts on the cluster. Any hosts that are decommissioned or have running 304 roles will be skipped. 305 306 @param hostIds: hostIds of hosts to deploy to. If empty, deploys to all 307 hosts in the cluster. 308 @return: Reference to the submitted command. 309 @since: API v7 310 """ 311 return self._cmd('deployClusterClientConfig', data=hostIds, 312 api_version=7)
313
314 - def upgrade_services(self):
315 """ 316 This command is no longer recommended with API v6 onwards. It simply does 317 not work when parcels are used, and even with packages it may fail due to 318 a race. Use upgrade_cdh instead. 319 320 Upgrades the services in the cluster to CDH5 version. 321 This command requires that the CDH packages in the hosts used by the 322 cluster be upgraded to CDH5 before this command is issued. Once issued, 323 this command will stop all running services before proceeding. 324 325 If parcels are used instead of CDH system packages then the following 326 steps need to happen in order: 327 1. Stop all services manually 328 2. Activate parcel 329 3. Run this upgrade command 330 331 The command will upgrade the services and their configuration to the 332 version available in the CDH5 distribution. 333 334 @return: Reference to the submitted command. 335 @deprecated: since API v6 336 """ 337 return self._cmd('upgradeServices')
338
339 - def enter_maintenance_mode(self):
340 """ 341 Put the cluster in maintenance mode. 342 343 @return: Reference to the completed command. 344 @since: API v2 345 """ 346 cmd = self._cmd('enterMaintenanceMode') 347 if cmd.success: 348 self._update(get_cluster(self._get_resource_root(), self.name)) 349 return cmd
350
351 - def exit_maintenance_mode(self):
352 """ 353 Take the cluster out of maintenance mode. 354 355 @return: Reference to the completed command. 356 @since: API v2 357 """ 358 cmd = self._cmd('exitMaintenanceMode') 359 if cmd.success: 360 self._update(get_cluster(self._get_resource_root(), self.name)) 361 return cmd
362
363 - def get_all_host_templates(self):
364 """ 365 Retrieves all host templates in the cluster. 366 @return: ApiList of ApiHostTemplate objects. 367 """ 368 return host_templates.get_all_host_templates(self._get_resource_root(), self.name)
369
370 - def get_host_template(self, name):
371 """ 372 Retrieves a host templates by name. 373 @param name: Host template name. 374 @return: An ApiHostTemplate object. 375 """ 376 return host_templates.get_host_template(self._get_resource_root(), name, self.name)
377
378 - def create_host_template(self, name):
379 """ 380 Creates a host template. 381 @param name: Name of the host template to create. 382 @return: An ApiHostTemplate object. 383 """ 384 return host_templates.create_host_template(self._get_resource_root(), name, self.name)
385
386 - def delete_host_template(self, name):
387 """ 388 Deletes a host template. 389 @param name: Name of the host template to delete. 390 @return: An ApiHostTemplate object. 391 """ 392 return host_templates.delete_host_template(self._get_resource_root(), name, self.name)
393
394 - def rolling_restart(self, slave_batch_size=None, 395 slave_fail_count_threshold=None, 396 sleep_seconds=None, 397 stale_configs_only=None, 398 unupgraded_only=None, 399 roles_to_include=None, 400 restart_service_names=None):
401 """ 402 Command to do a "best-effort" rolling restart of the given cluster, 403 i.e. it does plain restart of services that cannot be rolling restarted, 404 followed by first rolling restarting non-slaves and then rolling restarting 405 the slave roles of services that can be rolling restarted. The slave restarts 406 are done host-by-host. 407 @param slave_batch_size: Number of hosts with slave roles to restart at a time 408 Must be greater than 0. Default is 1. 409 @param slave_fail_count_threshold: The threshold for number of slave host batches that 410 are allowed to fail to restart before the entire command is considered failed. 411 Must be >= 0. Default is 0. 412 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches. 413 Must be >=0. Default is 0. 414 @param stale_configs_only: Restart roles with stale configs only. Default is false. 415 @param unupgraded_only: Restart roles that haven't been upgraded yet. Default is false. 416 @param roles_to_include: Role types to restart. Default is slave roles only. 417 @param restart_service_names: List of specific services to restart. 418 @return: Reference to the submitted command. 419 @since: API v4 420 """ 421 args = dict() 422 if slave_batch_size: 423 args['slaveBatchSize'] = slave_batch_size 424 if slave_fail_count_threshold: 425 args['slaveFailCountThreshold'] = slave_fail_count_threshold 426 if sleep_seconds: 427 args['sleepSeconds'] = sleep_seconds 428 if stale_configs_only: 429 args['staleConfigsOnly'] = stale_configs_only 430 if unupgraded_only: 431 args['unUpgradedOnly'] = unupgraded_only 432 if roles_to_include: 433 args['rolesToInclude'] = roles_to_include 434 if restart_service_names: 435 args['restartServiceNames'] = restart_service_names 436 437 return self._cmd('rollingRestart', data=args, api_version=4)
438
439 - def rolling_upgrade(self, upgrade_from_cdh_version, 440 upgrade_to_cdh_version, 441 upgrade_service_names, 442 slave_batch_size=None, 443 slave_fail_count_threshold=None, 444 sleep_seconds=None):
445 """ 446 Command to do a rolling upgrade of services in the given cluster 447 448 This command does not handle any services that don't support rolling 449 upgrades. The command will throw an error and not start if upgrade of 450 any such service is requested. 451 452 This command does not upgrade the full CDH Cluster. You should normally 453 use the upgradeCDH Command for upgrading the cluster. This is primarily 454 helpful if you need to need to recover from an upgrade failure or for 455 advanced users to script an alternative to the upgradeCdhCommand. 456 457 This command expects the binaries to be available on hosts and activated. 458 It does not change any binaries on the hosts. 459 460 @param upgrade_from_cdh_version: Current CDH Version of the services. 461 Example versions are: "5.1.0", "5.2.2" or "5.4.0" 462 @param upgrade_to_cdh_version: Target CDH Version for the services. 463 The CDH version should already be present and activated on the nodes. 464 Example versions are: "5.1.0", "5.2.2" or "5.4.0" 465 @param upgrade_service_names: List of specific services to be upgraded and restarted. 466 @param slave_batch_size: Number of hosts with slave roles to restart at a time 467 Must be greater than 0. Default is 1. 468 @param slave_fail_count_threshold: The threshold for number of slave host batches that 469 are allowed to fail to restart before the entire command is considered failed. 470 Must be >= 0. Default is 0. 471 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches. 472 Must be >=0. Default is 0. 473 474 @return: Reference to the submitted command. 475 @since: API v10 476 """ 477 args = dict() 478 args['upgradeFromCdhVersion'] = upgrade_from_cdh_version 479 args['upgradeToCdhVersion'] = upgrade_to_cdh_version 480 args['upgradeServiceNames'] = upgrade_service_names 481 482 if slave_batch_size: 483 args['slaveBatchSize'] = slave_batch_size 484 if slave_fail_count_threshold: 485 args['slaveFailCountThreshold'] = slave_fail_count_threshold 486 if sleep_seconds: 487 args['sleepSeconds'] = sleep_seconds 488 489 return self._cmd('rollingUpgrade', data=args, api_version=10)
490
491 - def auto_assign_roles(self):
492 """ 493 Automatically assign roles to hosts and create the roles for all the services in a cluster. 494 495 Assignments are done based on services in the cluster and hardware specifications. 496 Existing roles will be taken into account and their assignments will be not be modified. 497 @since: API v6 498 """ 499 self._put("autoAssignRoles", None, api_version=6)
500
501 - def auto_configure(self):
502 """ 503 Automatically configures roles and services in a cluster. 504 505 Overwrites some existing configurations. Might create new role config 506 groups. Only default role config groups must exist before calling this 507 endpoint. Other role config groups must not exist. If they do, an exception 508 will be thrown preventing any configuration. Ignores the Cloudera 509 Management Service even if colocated with roles of this cluster. To avoid 510 over-committing the heap on hosts, assign hosts to this cluster that are 511 not being used by the Cloudera Management Service. 512 @since: API v6 513 """ 514 self._put("autoConfigure", None, api_version=6)
515
516 - def first_run(self):
517 """ 518 Prepare and start services in a cluster. 519 Perform all the steps needed to prepare each service in a 520 cluster and start the services in order. 521 522 @return: Reference to the submitted command. 523 @since: API v7 524 """ 525 return self._cmd('firstRun', None, api_version=7)
526
527 - def upgrade_cdh(self, deploy_client_config=True, start_all_services=True, 528 cdh_parcel_version=None, cdh_package_version=None, 529 rolling_restart=False, slave_batch_size=None, sleep_seconds=None, 530 slave_fail_count_threshold=None):
531 """ 532 Perform CDH upgrade to the next major version. In v9+, also supports 533 minor CDH 5 upgrades (5.a.b to 5.x.y where x > a) and supports maintenance 534 release changes (a.b.x to a.b.y). 535 536 If using packages, CDH packages on all hosts of the cluster must be 537 manually upgraded before this command is issued. 538 539 The command will upgrade the services and their configuration to the 540 requested version. All running services will be stopped before proceeding, 541 unless rolling restart is requested and is available. 542 543 @param deploy_client_config: Whether to deploy client configurations 544 after the upgrade. Default is True. Has no effect in v9+; 545 client configurations are always deployed. 546 @param start_all_services: Whether to start all services after the upgrade. 547 Default is True. Has no effect in v9+; services are always 548 restarted. 549 @param cdh_parcel_version: If upgrading to parcels, the full version of an 550 already distributed parcel for the next CDH version. Default 551 is None. Example versions are: '5.0.0-1.cdh5.0.0.p0.11' or 552 '5.0.2-1.cdh5.0.2.p0.32'. 553 @param cdh_package_version: If upgrading to packages, the full version of an 554 already installed package for the next CDH version. Default 555 is None. Example versions are: '5.2.0' or '4.5.0'. Only available 556 since v9. 557 @param rolling_restart: If you'd like to do a rolling restart, set this to 558 True. Default is False. Only available since v9. 559 @param slave_batch_size: Controls the rolling restart slave batch size. 560 Only applicable when rolling_restart is True. 561 @param sleep_seconds: Controls how many seconds to sleep betweein rolling 562 restart batches. Only applicable when rolling_restart is True. 563 @param slave_fail_count_threshold: Controls how many slave restart failures 564 are tolerated in a rolling restart. Only applicable when 565 rolling_restart is True. 566 @return: Reference to the submitted command. 567 @since: API v6 for major upgrades only, v9 for maintenance and CDH 5 minor 568 releases. 569 """ 570 args = dict() 571 args['deployClientConfig'] = deploy_client_config 572 args['startAllServices'] = start_all_services 573 if cdh_parcel_version: 574 args['cdhParcelVersion'] = cdh_parcel_version 575 if cdh_package_version: 576 args['cdhPackageVersion'] = cdh_package_version 577 if rolling_restart: 578 args['rollingRestartArgs'] = { 579 'slaveBatchSize' : slave_batch_size, 580 'sleepSeconds' : sleep_seconds, 581 'slaveFailCountThreshold' : slave_fail_count_threshold 582 } 583 return self._cmd('upgradeCdh', data=args, api_version=6)
584
585 - def configure_for_kerberos(self, datanode_transceiver_port=None, 586 datanode_web_port=None):
587 """ 588 Command to configure the cluster to use Kerberos for authentication. 589 590 This command will configure all relevant services on a cluster for 591 Kerberos usage. This command will trigger a GenerateCredentials command 592 to create Kerberos keytabs for all roles in the cluster. 593 594 @param datanode_transceiver_port: The HDFS DataNode transceiver port to use. 595 This will be applied to all DataNode role configuration groups. If 596 not specified, this will default to 1004. 597 @param datanode_web_port: The HDFS DataNode web port to use. This will be 598 applied to all DataNode role configuration groups. If not specified, 599 this will default to 1006. 600 @return: Reference to the submitted command. 601 @since: API v11 602 """ 603 args = dict() 604 if datanode_transceiver_port: 605 args['datanodeTransceiverPort'] = datanode_transceiver_port 606 if datanode_web_port: 607 args['datanodeWebPort'] = datanode_web_port 608 return self._cmd('configureForKerberos', data=args, api_version=11)
609
610 - def export(self, export_auto_config=False):
611 """ 612 Export the cluster template for the given cluster. ccluster must have host 613 templates defined. It cluster does not have host templates defined it will 614 export host templates based on roles assignment. 615 616 @param export_auto_config: Also export auto configured configs 617 @return: Return cluster template 618 @since: API v12 619 """ 620 621 return self._get("export", ApiClusterTemplate, False, 622 params=dict(exportAutoConfig=export_auto_config), api_version=12)
623
624 - def pools_refresh(self):
625 """ 626 Refresh Dynamic Pools configurations for relevant services.. 627 628 @return: Reference to the submitted command. 629 @since: API v6 630 """ 631 return self._cmd('poolsRefresh', api_version=6)
632
633 - def list_dfs_services(self, view=None):
634 """ 635 List available DFS (distributed file system) services in a cluster. 636 @param view: View to materialize 637 @return: List of available distributed file system services in the cluster. 638 @since: API v12 639 """ 640 if view: 641 return self._get_resource_root().get("%s/%s?view=%s" % (self._path(), 'dfsServices', view)) 642 else: 643 return self._get_resource_root().get("%s/%s" % (self._path(), 'dfsServices'))
644