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, 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: "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
47 -def get_cluster(resource_root, name):
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
56 -def get_all_clusters(resource_root, view=None):
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
65 -def delete_cluster(resource_root, name):
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
74 -class ApiCluster(BaseApiResource):
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):
85 BaseApiObject.init(self, resource_root, locals())
86
87 - def __str__(self):
88 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
89
90 - def _path(self):
91 return "%s/%s" % (CLUSTERS_PATH, self.name)
92
93 - def _put_cluster(self, dic, params=None):
94 """Change cluster attributes""" 95 cluster = self._put('', ApiCluster, data=dic, params=params) 96 self._update(cluster) 97 return self
98
99 - def get_service_types(self):
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
108 - def get_commands(self, view=None):
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
118 - def rename(self, newname):
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
133 - def update_cdh_version(self, new_cdh_version):
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
145 - def create_service(self, name, service_type):
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
156 - def delete_service(self, name):
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
165 - def get_service(self, name):
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
174 - def get_all_services(self, view = None):
175 """ 176 Get all services in this cluster. 177 178 @return: A list of ApiService objects. 179 """ 180 return services.get_all_services(self._get_resource_root(), self.name, view)
181
182 - def get_parcel(self, product, version):
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
192 - def get_all_parcels(self, view = None):
193 """ 194 Get all parcels in this cluster. 195 196 @return: A list of ApiParcel objects. 197 """ 198 return parcels.get_all_parcels(self._get_resource_root(), self.name, view)
199
200 - def list_hosts(self):
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
209 - def remove_host(self, hostId):
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
218 - def remove_all_hosts(self):
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
227 - def add_hosts(self, hostIds):
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
240 - def start(self):
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
248 - def stop(self):
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
256 - def restart(self):
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
265 - def deploy_client_config(self):
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
274 - def upgrade_services(self):
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
299 - def enter_maintenance_mode(self):
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
311 - def exit_maintenance_mode(self):
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
323 - def get_all_host_templates(self):
324 """ 325 Retrieves all host templates in the cluster. 326 @return: ApiList of ApiHostTemplate objects. 327 """ 328 return host_templates.get_all_host_templates(self._get_resource_root(), self.name)
329
330 - def get_host_template(self, name):
331 """ 332 Retrieves a host templates by name. 333 @param name: Host template name. 334 @return: An ApiHostTemplate object. 335 """ 336 return host_templates.get_host_template(self._get_resource_root(), name, self.name)
337
338 - def create_host_template(self, name):
339 """ 340 Creates a host template. 341 @param name: Name of the host template to create. 342 @return: An ApiHostTemplate object. 343 """ 344 return host_templates.create_host_template(self._get_resource_root(), name, self.name)
345
346 - def delete_host_template(self, name):
347 """ 348 Deletes a host template. 349 @param name: Name of the host template to delete. 350 @return: An ApiHostTemplate object. 351 """ 352 return host_templates.delete_host_template(self._get_resource_root(), name, self.name)
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
399 - def auto_assign_roles(self):
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
409 - def auto_configure(self):
410 """ 411 Automatically configures roles and services in a cluster. 412 413 Overwrites some existing configurations. Might create new role config 414 groups. Only default role config groups must exist before calling this 415 endpoint. Other role config groups must not exist. If they do, an exception 416 will be thrown preventing any configuration. Ignores the Cloudera 417 Management Service even if colocated with roles of this cluster. To avoid 418 over-committing the heap on hosts, assign hosts to this cluster that are 419 not being used by the Cloudera Management Service. 420 @since: API v6 421 """ 422 self._put("autoConfigure", None, api_version=6)
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