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   
 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
49 -def get_cluster(resource_root, name):
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
58 -def get_all_clusters(resource_root, view=None):
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
67 -def delete_cluster(resource_root, name):
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
76 -class ApiCluster(BaseApiResource):
77 _ATTRIBUTES = { 78 'name' : None, 79 'displayName' : None, 80 'clusterUrl' : None, 81 'version' : None, 82 'fullVersion' : None, 83 'maintenanceMode' : ROAttr(), 84 'maintenanceOwners' : ROAttr(), 85 } 86
87 - def __init__(self, resource_root, name=None, version=None, fullVersion=None):
88 BaseApiObject.init(self, resource_root, locals())
89
90 - def __str__(self):
91 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
92
93 - def _path(self):
94 return "%s/%s" % (CLUSTERS_PATH, self.name)
95
96 - def _put_cluster(self, dic, params=None):
97 """Change cluster attributes""" 98 cluster = self._put('', ApiCluster, data=dic, params=params) 99 self._update(cluster) 100 return self
101
102 - def get_service_types(self):
103 """ 104 Get all service types supported by this cluster. 105 106 @return: A list of service types (strings) 107 """ 108 resp = self._get_resource_root().get(self._path() + '/serviceTypes') 109 return resp[ApiList.LIST_KEY]
110
111 - def get_commands(self, view=None):
112 """ 113 Retrieve a list of running commands for this cluster. 114 115 @param view: View to materialize ('full' or 'summary') 116 @return: A list of running commands. 117 """ 118 return self._get("commands", ApiCommand, True, 119 params = view and dict(view=view) or None)
120
121 - def rename(self, newname):
122 """ 123 Rename a cluster. 124 125 @param newname: New cluster name 126 @return: An ApiCluster object 127 @since: API v2 128 """ 129 dic = self.to_json_dict() 130 if self._get_resource_root().version < 6: 131 dic['name'] = newname 132 else: 133 dic['displayName'] = newname 134 return self._put_cluster(dic)
135
136 - def update_cdh_version(self, new_cdh_version):
137 """ 138 Manually set the CDH version. 139 140 @param new_cdh_version: New CDH version, e.g. 4.5.1 141 @return: An ApiCluster object 142 @since: API v6 143 """ 144 dic = self.to_json_dict() 145 dic['fullVersion'] = new_cdh_version 146 return self._put_cluster(dic)
147
148 - def create_service(self, name, service_type):
149 """ 150 Create a service. 151 152 @param name: Service name 153 @param service_type: Service type 154 @return: An ApiService object 155 """ 156 return services.create_service(self._get_resource_root(), name, 157 service_type, self.name)
158
159 - def delete_service(self, name):
160 """ 161 Delete a service by name. 162 163 @param name: Service name 164 @return: The deleted ApiService object 165 """ 166 return services.delete_service(self._get_resource_root(), name, self.name)
167
168 - def get_service(self, name):
169 """ 170 Lookup a service by name. 171 172 @param name: Service name 173 @return: An ApiService object 174 """ 175 return services.get_service(self._get_resource_root(), name, self.name)
176
177 - def get_all_services(self, view = None):
178 """ 179 Get all services in this cluster. 180 181 @return: A list of ApiService objects. 182 """ 183 return services.get_all_services(self._get_resource_root(), self.name, view)
184
185 - def get_parcel(self, product, version):
186 """ 187 Lookup a parcel by product and version. 188 189 @param product: the product name 190 @param version: the product version 191 @return: An ApiParcel object 192 """ 193 return parcels.get_parcel(self._get_resource_root(), product, version, self.name)
194
195 - def get_all_parcels(self, view = None):
196 """ 197 Get all parcels in this cluster. 198 199 @return: A list of ApiParcel objects. 200 """ 201 return parcels.get_all_parcels(self._get_resource_root(), self.name, view)
202
203 - def list_hosts(self):
204 """ 205 Lists all the hosts that are associated with this cluster. 206 207 @return: A list of ApiHostRef objects of the hosts in the cluster. 208 @since: API v3 209 """ 210 return self._get("hosts", ApiHostRef, True, api_version=3)
211
212 - def remove_host(self, hostId):
213 """ 214 Removes the association of the host with the cluster. 215 216 @return: A ApiHostRef of the host that was removed. 217 @since: API v3 218 """ 219 return self._delete("hosts/" + hostId, ApiHostRef, api_version=3)
220
221 - def remove_all_hosts(self):
222 """ 223 Removes the association of all the hosts with the cluster. 224 225 @return: A list of ApiHostRef objects of the hosts that were removed. 226 @since: API v3 227 """ 228 return self._delete("hosts", ApiHostRef, True, api_version=3)
229
230 - def add_hosts(self, hostIds):
231 """ 232 Adds a host to the cluster. 233 234 @param hostIds: List of IDs of hosts to add to cluster. 235 @return: A list of ApiHostRef objects of the new 236 hosts that were added to the cluster 237 @since: API v3 238 """ 239 hostRefList = [ApiHostRef(self._get_resource_root(), x) for x in hostIds] 240 return self._post("hosts", ApiHostRef, True, data=hostRefList, 241 api_version=3)
242
243 - def start(self):
244 """ 245 Start all services in a cluster, respecting dependencies. 246 247 @return: Reference to the submitted command. 248 """ 249 return self._cmd('start')
250
251 - def stop(self):
252 """ 253 Stop all services in a cluster, respecting dependencies. 254 255 @return: Reference to the submitted command. 256 """ 257 return self._cmd('stop')
258
259 - def restart(self, restart_only_stale_services=None, 260 redeploy_client_configuration=None):
261 """ 262 Restart all services in the cluster. 263 Services are restarted in the appropriate order given their dependencies. 264 265 @param restart_only_stale_services: Only restart services that have stale 266 configuration and their dependent 267 services. Default is False. 268 @param redeploy_client_configuration: Re-deploy client configuration for 269 all services in the cluster. Default 270 is False. 271 @since API v6 272 273 @return: Reference to the submitted command. 274 """ 275 if self._get_resource_root().version < 6: 276 return self._cmd('restart') 277 else: 278 args = dict() 279 args['restartOnlyStaleServices'] = restart_only_stale_services 280 args['redeployClientConfiguration'] = redeploy_client_configuration 281 return self._cmd('restart', data=args, api_version=6)
282
283 - def deploy_client_config(self):
284 """ 285 Deploys Service client configuration to the hosts on the cluster. 286 287 @return: Reference to the submitted command. 288 @since: API v2 289 """ 290 return self._cmd('deployClientConfig')
291
292 - def deploy_cluster_client_config(self, hostIds=[]):
293 """ 294 Deploys Cluster client configuration (Kerberos configuration) to the 295 hosts on the cluster. Any hosts that are decommissioned or have running 296 roles will be skipped. 297 298 @param hostIds: hostIds of hosts to deploy to. If empty, deploys to all 299 hosts in the cluster. 300 @return: Reference to the submitted command. 301 @since: API v7 302 """ 303 return self._cmd('deployClusterClientConfig', data=hostIds, 304 api_version=7)
305
306 - def upgrade_services(self):
307 """ 308 This command is no longer recommended with API v6 onwards. It simply does 309 not work when parcels are used, and even with packages it may fail due to 310 a race. Use upgrade_cdh instead. 311 312 Upgrades the services in the cluster to CDH5 version. 313 This command requires that the CDH packages in the hosts used by the 314 cluster be upgraded to CDH5 before this command is issued. Once issued, 315 this command will stop all running services before proceeding. 316 317 If parcels are used instead of CDH system packages then the following 318 steps need to happen in order: 319 1. Stop all services manually 320 2. Activate parcel 321 3. Run this upgrade command 322 323 The command will upgrade the services and their configuration to the 324 version available in the CDH5 distribution. 325 326 @return: Reference to the submitted command. 327 @deprecated: since API v6 328 """ 329 return self._cmd('upgradeServices')
330
331 - def enter_maintenance_mode(self):
332 """ 333 Put the cluster in maintenance mode. 334 335 @return: Reference to the completed command. 336 @since: API v2 337 """ 338 cmd = self._cmd('enterMaintenanceMode') 339 if cmd.success: 340 self._update(get_cluster(self._get_resource_root(), self.name)) 341 return cmd
342
343 - def exit_maintenance_mode(self):
344 """ 345 Take the cluster out of maintenance mode. 346 347 @return: Reference to the completed command. 348 @since: API v2 349 """ 350 cmd = self._cmd('exitMaintenanceMode') 351 if cmd.success: 352 self._update(get_cluster(self._get_resource_root(), self.name)) 353 return cmd
354
355 - def get_all_host_templates(self):
356 """ 357 Retrieves all host templates in the cluster. 358 @return: ApiList of ApiHostTemplate objects. 359 """ 360 return host_templates.get_all_host_templates(self._get_resource_root(), self.name)
361
362 - def get_host_template(self, name):
363 """ 364 Retrieves a host templates by name. 365 @param name: Host template name. 366 @return: An ApiHostTemplate object. 367 """ 368 return host_templates.get_host_template(self._get_resource_root(), name, self.name)
369
370 - def create_host_template(self, name):
371 """ 372 Creates a host template. 373 @param name: Name of the host template to create. 374 @return: An ApiHostTemplate object. 375 """ 376 return host_templates.create_host_template(self._get_resource_root(), name, self.name)
377
378 - def delete_host_template(self, name):
379 """ 380 Deletes a host template. 381 @param name: Name of the host template to delete. 382 @return: An ApiHostTemplate object. 383 """ 384 return host_templates.delete_host_template(self._get_resource_root(), name, self.name)
385
386 - def rolling_restart(self, slave_batch_size=None, 387 slave_fail_count_threshold=None, 388 sleep_seconds=None, 389 stale_configs_only=None, 390 unupgraded_only=None, 391 roles_to_include=None, 392 restart_service_names=None):
393 """ 394 Command to do a "best-effort" rolling restart of the given cluster, 395 i.e. it does plain restart of services that cannot be rolling restarted, 396 followed by first rolling restarting non-slaves and then rolling restarting 397 the slave roles of services that can be rolling restarted. The slave restarts 398 are done host-by-host. 399 @param slave_batch_size: Number of hosts with slave roles to restart at a time 400 Must be greater than 0. Default is 1. 401 @param slave_fail_count_threshold: The threshold for number of slave host batches that 402 are allowed to fail to restart before the entire command is considered failed. 403 Must be >= 0. Default is 0. 404 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches. 405 Must be >=0. Default is 0. 406 @param stale_configs_only: Restart roles with stale configs only. Default is false. 407 @param unupgraded_only: Restart roles that haven't been upgraded yet. Default is false. 408 @param roles_to_include: Role types to restart. Default is slave roles only. 409 @param restart_service_names: List of specific services to restart. 410 @return: Reference to the submitted command. 411 @since: API v4 412 """ 413 args = dict() 414 if slave_batch_size: 415 args['slaveBatchSize'] = slave_batch_size 416 if slave_fail_count_threshold: 417 args['slaveFailCountThreshold'] = slave_fail_count_threshold 418 if sleep_seconds: 419 args['sleepSeconds'] = sleep_seconds 420 if stale_configs_only: 421 args['staleConfigsOnly'] = stale_configs_only 422 if unupgraded_only: 423 args['unUpgradedOnly'] = unupgraded_only 424 if roles_to_include: 425 args['rolesToInclude'] = roles_to_include 426 if restart_service_names: 427 args['restartServiceNames'] = restart_service_names 428 429 return self._cmd('rollingRestart', data=args, api_version=4)
430
431 - def rolling_upgrade(self, upgrade_from_cdh_version, 432 upgrade_to_cdh_version, 433 upgrade_service_names, 434 slave_batch_size=None, 435 slave_fail_count_threshold=None, 436 sleep_seconds=None):
437 """ 438 Command to do a rolling upgrade of services in the given cluster 439 440 This command does not handle any services that don't support rolling 441 upgrades. The command will throw an error and not start if upgrade of 442 any such service is requested. 443 444 This command does not upgrade the full CDH Cluster. You should normally 445 use the upgradeCDH Command for upgrading the cluster. This is primarily 446 helpful if you need to need to recover from an upgrade failure or for 447 advanced users to script an alternative to the upgradeCdhCommand. 448 449 This command expects the binaries to be available on hosts and activated. 450 It does not change any binaries on the hosts. 451 452 @param upgrade_from_cdh_version: Current CDH Version of the services. 453 Example versions are: "5.1.0", "5.2.2" or "5.4.0" 454 @param upgrade_to_cdh_version: Target CDH Version for the services. 455 The CDH version should already be present and activated on the nodes. 456 Example versions are: "5.1.0", "5.2.2" or "5.4.0" 457 @param upgrade_service_names: List of specific services to be upgraded and restarted. 458 @param slave_batch_size: Number of hosts with slave roles to restart at a time 459 Must be greater than 0. Default is 1. 460 @param slave_fail_count_threshold: The threshold for number of slave host batches that 461 are allowed to fail to restart before the entire command is considered failed. 462 Must be >= 0. Default is 0. 463 @param sleep_seconds: Number of seconds to sleep between restarts of slave host batches. 464 Must be >=0. Default is 0. 465 466 @return: Reference to the submitted command. 467 @since: API v10 468 """ 469 args = dict() 470 args['upgradeFromCdhVersion'] = upgrade_from_cdh_version 471 args['upgradeToCdhVersion'] = upgrade_to_cdh_version 472 args['upgradeServiceNames'] = upgrade_service_names 473 474 if slave_batch_size: 475 args['slaveBatchSize'] = slave_batch_size 476 if slave_fail_count_threshold: 477 args['slaveFailCountThreshold'] = slave_fail_count_threshold 478 if sleep_seconds: 479 args['sleepSeconds'] = sleep_seconds 480 481 return self._cmd('rollingUpgrade', data=args, api_version=10)
482
483 - def auto_assign_roles(self):
484 """ 485 Automatically assign roles to hosts and create the roles for all the services in a cluster. 486 487 Assignments are done based on services in the cluster and hardware specifications. 488 Existing roles will be taken into account and their assignments will be not be modified. 489 @since: API v6 490 """ 491 self._put("autoAssignRoles", None, api_version=6)
492
493 - def auto_configure(self):
494 """ 495 Automatically configures roles and services in a cluster. 496 497 Overwrites some existing configurations. Might create new role config 498 groups. Only default role config groups must exist before calling this 499 endpoint. Other role config groups must not exist. If they do, an exception 500 will be thrown preventing any configuration. Ignores the Cloudera 501 Management Service even if colocated with roles of this cluster. To avoid 502 over-committing the heap on hosts, assign hosts to this cluster that are 503 not being used by the Cloudera Management Service. 504 @since: API v6 505 """ 506 self._put("autoConfigure", None, api_version=6)
507
508 - def first_run(self):
509 """ 510 Prepare and start services in a cluster. 511 Perform all the steps needed to prepare each service in a 512 cluster and start the services in order. 513 514 @return: Reference to the submitted command. 515 @since: API v7 516 """ 517 return self._cmd('firstRun', None, api_version=7)
518
519 - def upgrade_cdh(self, deploy_client_config=True, start_all_services=True, 520 cdh_parcel_version=None, cdh_package_version=None, 521 rolling_restart=False, slave_batch_size=None, sleep_seconds=None, 522 slave_fail_count_threshold=None):
523 """ 524 Perform CDH upgrade to the next major version. In v9+, also supports 525 minor CDH 5 upgrades (5.a.b to 5.x.y where x > a) and supports maintenance 526 release changes (a.b.x to a.b.y). 527 528 If using packages, CDH packages on all hosts of the cluster must be 529 manually upgraded before this command is issued. 530 531 The command will upgrade the services and their configuration to the 532 requested version. All running services will be stopped before proceeding, 533 unless rolling restart is requested and is available. 534 535 @param deploy_client_config: Whether to deploy client configurations 536 after the upgrade. Default is True. Has no effect in v9+; 537 client configurations are always deployed. 538 @param start_all_services: Whether to start all services after the upgrade. 539 Default is True. Has no effect in v9+; services are always 540 restarted. 541 @param cdh_parcel_version: If upgrading to parcels, the full version of an 542 already distributed parcel for the next CDH version. Default 543 is None. Example versions are: '5.0.0-1.cdh5.0.0.p0.11' or 544 '5.0.2-1.cdh5.0.2.p0.32'. 545 @param cdh_package_version: If upgrading to packages, the full version of an 546 already installed package for the next CDH version. Default 547 is None. Example versions are: '5.2.0' or '4.5.0'. Only available 548 since v9. 549 @param rolling_restart: If you'd like to do a rolling restart, set this to 550 True. Default is False. Only available since v9. 551 @param slave_batch_size: Controls the rolling restart slave batch size. 552 Only applicable when rolling_restart is True. 553 @param sleep_seconds: Controls how many seconds to sleep betweein rolling 554 restart batches. Only applicable when rolling_restart is True. 555 @param slave_fail_count_threshold: Controls how many slave restart failures 556 are tolerated in a rolling restart. Only applicable when 557 rolling_restart is True. 558 @return: Reference to the submitted command. 559 @since: API v6 for major upgrades only, v9 for maintenance and CDH 5 minor 560 releases. 561 """ 562 args = dict() 563 args['deployClientConfig'] = deploy_client_config 564 args['startAllServices'] = start_all_services 565 if cdh_parcel_version: 566 args['cdhParcelVersion'] = cdh_parcel_version 567 if cdh_package_version: 568 args['cdhPackageVersion'] = cdh_package_version 569 if rolling_restart: 570 args['rollingRestartArgs'] = { 571 'slaveBatchSize' : slave_batch_size, 572 'sleepSeconds' : sleep_seconds, 573 'slaveFailCountThreshold' : slave_fail_count_threshold 574 } 575 return self._cmd('upgradeCdh', data=args, api_version=6)
576