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  try: 
 18    import json 
 19  except ImportError: 
 20    import simplejson as json 
 21   
 22  from cm_api.endpoints.types import * 
 23  from cm_api.endpoints import services, parcels, host_templates 
 24   
 25  __docformat__ = "epytext" 
 26   
 27  CLUSTERS_PATH = "/clusters" 
 28   
29 -def create_cluster(resource_root, name, version):
30 """ 31 Create a cluster 32 @param resource_root: The root Resource object. 33 @param name: Cluster name 34 @param version: Cluster CDH version 35 @return: An ApiCluster object 36 """ 37 apicluster = ApiCluster(resource_root, name, version) 38 apicluster_list = ApiList([apicluster]) 39 body = json.dumps(apicluster_list.to_json_dict()) 40 resp = resource_root.post(CLUSTERS_PATH, data=body) 41 # The server returns a list of created clusters (with size 1) 42 return ApiList.from_json_dict(ApiCluster, resp, resource_root)[0]
43
44 -def get_cluster(resource_root, name):
45 """ 46 Lookup a cluster by name 47 @param resource_root: The root Resource object. 48 @param name: Cluster name 49 @return: An ApiCluster object 50 """ 51 dic = resource_root.get("%s/%s" % (CLUSTERS_PATH, name)) 52 return ApiCluster.from_json_dict(dic, resource_root)
53
54 -def get_all_clusters(resource_root, view=None):
55 """ 56 Get all clusters 57 @param resource_root: The root Resource object. 58 @return: A list of ApiCluster objects. 59 """ 60 dic = resource_root.get(CLUSTERS_PATH, 61 params=view and dict(view=view) or None) 62 return ApiList.from_json_dict(ApiCluster, dic, resource_root)
63
64 -def delete_cluster(resource_root, name):
65 """ 66 Delete a cluster by name 67 @param resource_root: The root Resource object. 68 @param name: Cluster name 69 @return: The deleted ApiCluster object 70 """ 71 resp = resource_root.delete("%s/%s" % (CLUSTERS_PATH, name)) 72 return ApiCluster.from_json_dict(resp, resource_root)
73 74
75 -class ApiCluster(BaseApiObject):
76 _ATTRIBUTES = { 77 'name' : None, 78 'version' : None, 79 'maintenanceMode' : ROAttr(), 80 'maintenanceOwners' : ROAttr(), 81 } 82
83 - def __init__(self, resource_root, name=None, version=None):
84 BaseApiObject.init(self, resource_root, locals())
85
86 - def __str__(self):
87 return "<ApiCluster>: %s; version: %s" % (self.name, self.version)
88
89 - def _path(self):
90 return "%s/%s" % (CLUSTERS_PATH, self.name)
91
92 - def _cmd(self, cmd, data=None):
93 path = self._path() + '/commands/' + cmd 94 resp = self._get_resource_root().post(path, data=data) 95 return ApiCommand.from_json_dict(resp, self._get_resource_root())
96
97 - def _put(self, dic, params=None):
98 """Change cluster attributes""" 99 resp = self._get_resource_root().put( 100 self._path(), params=params, data=json.dumps(dic)) 101 cluster = ApiCluster.from_json_dict(resp, self._get_resource_root()) 102 103 self._update(cluster) 104 return self
105
106 - def get_commands(self, view=None):
107 """ 108 Retrieve a list of running commands for this cluster. 109 110 @param view: View to materialize ('full' or 'summary') 111 @return: A list of running commands. 112 """ 113 resp = self._get_resource_root().get( 114 self._path() + '/commands', 115 params = view and dict(view=view) or None) 116 return ApiList.from_json_dict(ApiCommand, resp, self._get_resource_root())
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 dic['name'] = newname 128 return self._put(dic)
129
130 - def create_service(self, name, service_type):
131 """ 132 Create a service. 133 134 @param name: Service name 135 @param service_type: Service type 136 @return: An ApiService object 137 """ 138 return services.create_service(self._get_resource_root(), name, 139 service_type, self.name)
140
141 - def delete_service(self, name):
142 """ 143 Delete a service by name. 144 145 @param name Service name 146 @return The deleted ApiService object 147 """ 148 return services.delete_service(self._get_resource_root(), name, self.name)
149
150 - def get_service(self, name):
151 """ 152 Lookup a service by name. 153 154 @param name: Service name 155 @return: An ApiService object 156 """ 157 return services.get_service(self._get_resource_root(), name, self.name)
158
159 - def get_all_services(self, view = None):
160 """ 161 Get all services in this cluster. 162 163 @return: A list of ApiService objects. 164 """ 165 return services.get_all_services(self._get_resource_root(), self.name, view)
166
167 - def get_parcel(self, product, version):
168 """ 169 Lookup a parcel by product and version. 170 171 @param product: the product name 172 @param version: the product version 173 @return: An ApiParcel object 174 """ 175 return parcels.get_parcel(self._get_resource_root(), product, version, self.name)
176
177 - def get_all_parcels(self, view = None):
178 """ 179 Get all parcels in this cluster. 180 181 @return: A list of ApiParcel objects. 182 """ 183 return parcels.get_all_parcels(self._get_resource_root(), self.name, view)
184
185 - def list_hosts(self):
186 """ 187 Lists all the hosts that are associated with this cluster. 188 189 @return: A list of ApiHostRef objects of the hosts in the cluster. 190 """ 191 resp = self._get_resource_root().get( 192 self._path() + '/hosts') 193 return ApiList.from_json_dict(ApiHostRef, resp, self._get_resource_root())
194
195 - def remove_host(self, hostId):
196 """ 197 Removes the association of the host with the cluster. 198 199 @return: A ApiHostRef of the host that was removed. 200 """ 201 resource_root = self._get_resource_root() 202 resp = resource_root.delete("%s/hosts/%s" % (self._path(), hostId)) 203 return ApiHostRef.from_json_dict(resp, resource_root)
204
205 - def remove_all_hosts(self):
206 """ 207 Removes the association of all the hosts with the cluster. 208 209 @return: A list of ApiHostRef objects of the hosts that were removed. 210 """ 211 resource_root = self._get_resource_root() 212 resp = resource_root.delete("%s/hosts" % (self._path(),)) 213 return ApiList.from_json_dict(ApiHostRef, resp, self._get_resource_root())
214
215 - def add_hosts(self, hostIds):
216 """ 217 Adds a host to the cluster. 218 219 @param hostIds: List of IDs of hosts to add to cluster. 220 @return: A list of ApiHostRef objects of the new 221 hosts that were added to the cluster 222 """ 223 resource_root = self._get_resource_root() 224 hostRefList = ApiList([ApiHostRef(resource_root, x) for x in hostIds]) 225 body = json.dumps(hostRefList.to_json_dict()) 226 resp = resource_root.post(self._path() + '/hosts', data=body) 227 return ApiList.from_json_dict(ApiHostRef, resp, resource_root)[0]
228
229 - def start(self):
230 """ 231 Start all services in a cluster, respecting dependencies. 232 233 @return: Reference to the submitted command. 234 """ 235 return self._cmd('start')
236
237 - def stop(self):
238 """ 239 Stop all services in a cluster, respecting dependencies. 240 241 @return: Reference to the submitted command. 242 """ 243 return self._cmd('stop')
244
245 - def deploy_client_config(self):
246 """ 247 Deploys client configuration to the hosts on the cluster. 248 249 @return: Reference to the submitted command. 250 @since: API v2 251 """ 252 return self._cmd('deployClientConfig')
253
254 - def enter_maintenance_mode(self):
255 """ 256 Put the cluster in maintenance mode. 257 258 @return: Reference to the completed command. 259 @since: API v2 260 """ 261 cmd = self._cmd('enterMaintenanceMode') 262 if cmd.success: 263 self._update(get_cluster(self._get_resource_root(), self.name)) 264 return cmd
265
266 - def exit_maintenance_mode(self):
267 """ 268 Take the cluster out of maintenance mode. 269 270 @return: Reference to the completed command. 271 @since: API v2 272 """ 273 cmd = self._cmd('exitMaintenanceMode') 274 if cmd.success: 275 self._update(get_cluster(self._get_resource_root(), self.name)) 276 return cmd
277
278 - def get_all_host_templates(self):
279 """ 280 Retrieves all host templates in the cluster. 281 @return ApiList of ApiHostTemplate objects. 282 """ 283 return host_templates.get_all_host_templates(self._get_resource_root(), self.name)
284
285 - def get_host_template(self, name):
286 """ 287 Retrieves a host templates by name. 288 @param name: Host template name. 289 @return An ApiHostTemplate object. 290 """ 291 return host_templates.get_host_template(self._get_resource_root(), name, self.name)
292
293 - def create_host_template(self, name):
294 """ 295 Creates a host template. 296 @param name: Name of the host template to create. 297 @return An ApiHostTemplate object. 298 """ 299 return host_templates.create_host_template(self._get_resource_root(), name, self.name)
300
301 - def delete_host_template(self, name):
302 """ 303 Deletes a host template. 304 @param name: Name of the host template to delete. 305 @return An ApiHostTemplate object. 306 """ 307 return host_templates.delete_host_template(self._get_resource_root(), name, self.name)
308
309 - def rolling_restart(self, slave_batch_size=None, 310 slave_fail_count_threshold=None, 311 sleep_seconds=None, 312 stale_configs_only=None, 313 unupgraded_only=None, 314 roles_to_include=None, 315 restart_service_names=None):
316 """ 317 Command to do a "best-effort" rolling restart of the given cluster, 318 i.e. it does plain restart of services that cannot be rolling restarted, 319 followed by first rolling restarting non-slaves and then rolling restarting 320 the slave roles of services that can be rolling restarted. The slave restarts 321 are done host-by-host. 322 @param: slave_batch_size Number of hosts with slave roles to restart at a time 323 Must be greater than 0. Default is 1. 324 @param: slave_fail_count_threshold The threshold for number of slave host batches that 325 are allowed to fail to restart before the entire command is considered failed. 326 Must be >= 0. Default is 0. 327 @param: sleep_seconds Number of seconds to sleep between restarts of slave host batches. 328 Must be >=0. Default is 0. 329 @param: stale_configs_only Restart roles with stale configs only. Default is false. 330 @param: unupgraded_only Restart roles that haven't been upgraded yet. Default is false. 331 @param: roles_to_include Role types to restart. Default is slave roles only. 332 @param: restart_service_names List of specific services to restart. 333 @return: Reference to the submitted command. 334 @since: API v4 335 """ 336 self._require_min_api_version(4) 337 args = dict() 338 if slave_batch_size: 339 args['slaveBatchSize'] = slave_batch_size 340 if slave_fail_count_threshold: 341 args['slaveFailCountThreshold'] = slave_fail_count_threshold 342 if sleep_seconds: 343 args['sleepSeconds'] = sleep_seconds 344 if stale_configs_only: 345 args['staleConfigsOnly'] = stale_configs_only 346 if unupgraded_only: 347 args['unUpgradedOnly'] = unupgraded_only 348 if roles_to_include: 349 args['rolesToInclude'] = roles_to_include 350 if restart_service_names: 351 args['restartServiceNames'] = restart_service_names 352 353 return self._cmd('rollingRestart', data = json.dumps(args))
354