def _set_ambari_credentials(self, cluster_spec, ambari_ip): services = cluster_spec.services for service in services: if service.name == 'AMBARI': is_admin_provided = False admin_user = self.ambari_user admin_password = self.ambari_password for u in service.users: if u.name == 'admin': self._update_ambari_admin_user(u.password, ambari_ip) is_admin_provided = True self.ambari_user = '******' self.ambari_password = u.password else: self._add_ambari_user(u, ambari_ip) if 'admin' in u.groups: admin_user = u.name admin_password = u.password if not is_admin_provided: if admin_user is None: raise ex.HadoopProvisionError("An Ambari user in the " "admin group must be " "configured.") self.ambari_user = admin_user self.ambari_password = admin_password self._delete_ambari_user('admin', ambari_ip) break
def _delete_ambari_user(self, user_name, ambari_ip): user_url = 'http://{0}:8080/api/v1/users/{1}'.\ format(ambari_ip, user_name) request = self._get_rest_request() result = request.delete(user_url, auth=(self.ambari_user, self.ambari_password)) if result.status_code != 200: raise ex.HadoopProvisionError('Unable to delete Ambari user: {0}' ' : {1}'.format( user_name, result.text))
def create_cluster(self, cluster, cluster_template): if cluster_template is None: raise ValueError('must supply cluster template') cluster_spec = clusterspec.ClusterSpec(cluster_template, cluster=cluster) hosts = self._get_servers(cluster) ambari_host = self._determine_host_for_server_component( 'AMBARI_SERVER', cluster_spec, hosts) self.cluster_name_to_ambari_host_mapping[cluster.name] = ambari_host rpm = self._get_rpm_uri(cluster_spec) servers = [] for host in hosts: host_role = utils.get_host_role(host) servers.append( h.HadoopServer(host, cluster_spec.node_groups[host_role], ambari_rpm=rpm)) provisioned = self._provision_cluster(cluster.name, cluster_spec, ambari_host, servers) if provisioned: installed = self._install_services(cluster.name, ambari_host) if installed: LOG.info("Install of Hadoop stack successful.") # add service urls self._set_cluster_info(cluster, cluster_spec, hosts) else: raise ex.HadoopProvisionError( 'Installation of Hadoop stack failed.') else: raise ex.HadoopProvisionError( 'Provisioning of Hadoop cluster failed.')
def _update_ambari_admin_user(self, password, ambari_ip): user_url = 'http://{0}:8080/api/v1/users/admin'.format(ambari_ip) update_body = '{{"Users":{{"roles":"admin,user","password":"******",'\ '"old_password":"******"}} }}'.format(password) request = self._get_rest_request() result = request.put(user_url, data=update_body, auth=(self.ambari_user, self.ambari_password)) if result.status_code != 200: raise ex.HadoopProvisionError('Unable to update Ambari admin user' ' credentials: {0}'.format( result.text))
def _add_ambari_user(self, user, ambari_info): user_url = 'http://{0}/api/v1/users/{1}'.format( ambari_info.get_address(), user.name) create_body = '{{"Users":{{"password":"******","roles":"{1}"}} }}'.\ format(user.password, '%s' % ','.join(map(str, user.groups))) request = self._get_rest_request() result = request.post(user_url, data=create_body, auth=( ambari_info.user, ambari_info.password)) if result.status_code != 201: raise ex.HadoopProvisionError('Unable to create Ambari user: {0}'. format(result.text))
def _finalize_ambari_state(self, ambari_info): LOG.info('Finalizing Ambari cluster state.') persist_state_uri = 'http://{0}/api/v1/persist'.format( ambari_info.get_address()) # this post data has non-standard format because persist # resource doesn't comply with Ambari API standards persist_data = '{ "CLUSTER_CURRENT_STATUS":' \ '"{\\"clusterState\\":\\"CLUSTER_STARTED_5\\"}" }' result = requests.post(persist_state_uri, data=persist_data, auth=(ambari_info.user, ambari_info.password)) if result.status_code != 201 and result.status_code != 202: LOG.warning('Finalizing of Ambari cluster state failed. {0}'. format(result.text)) raise ex.HadoopProvisionError('Unable to finalize Ambari state.')