def _reset_local_database(self): #force reset if silent option provided if get_silent(): default = "yes" else: default = "no" # Run automatic reset only for embedded DB okToRun = get_YN_input( "Confirm server reset [yes/no]({0})? ".format(default), get_silent()) if not okToRun: err = "Ambari Server 'reset' cancelled" raise FatalException(1, err) print "Resetting the Server database..." dbname = self.database_name filename = self.drop_tables_script_file username = self.database_username password = self.database_password command = PGConfig.SETUP_DB_CMD[:] command[-1] = command[-1].format(filename, username, password, dbname) drop_retcode, drop_outdata, drop_errdata = run_os_command(command) if not drop_retcode == 0: raise FatalException(1, drop_errdata) if drop_errdata and PGConfig.PG_ERROR_BLOCKED in drop_errdata: raise FatalException( 1, "Database is in use. Please, make sure all connections to the database are closed" ) if drop_errdata and get_verbose(): print_warning_msg(drop_errdata) print_info_msg("About to run database setup") retcode, outdata, errdata = self._setup_db() if errdata and get_verbose(): print_warning_msg(errdata) if (errdata and 'ERROR' in errdata.upper()) or ( drop_errdata and 'ERROR' in drop_errdata.upper()): err = "Non critical error in DDL" if not get_verbose(): err += ", use --verbose for more information" raise NonFatalException(err)
def set_current(options): logger.info("Set current cluster version.") server_status, pid = is_server_runing() if not server_status: err = 'Ambari Server is not running.' raise FatalException(1, err) finalize_options = SetCurrentVersionOptions(options) if finalize_options.no_finalize_options_set(): err = 'Must specify --cluster-name and --version-display-name. Please invoke ambari-server.py --help to print the options.' raise FatalException(1, err) admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Failed to read properties file.") base_url = get_ambari_server_api_base(properties) url = base_url + "clusters/{0}/stack_versions".format(finalize_options.cluster_name) admin_auth = base64.encodestring('%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') data = { "ClusterStackVersions": { "repository_version": finalize_options.desired_repo_version, "state": "CURRENT", "force": finalize_options.force_repo_version } } if get_verbose(): sys.stdout.write('\nCalling API ' + url + ' : ' + str(data) + '\n') request.add_data(json.dumps(data)) request.get_method = lambda: 'PUT' try: response = urllib2.urlopen(request) except urllib2.HTTPError, e: code = e.getcode() content = e.read() err = 'Error during setting current version. Http status code - {0}. \n {1}'.format( code, content) raise FatalException(1, err)
def set_current(options): server_status, pid = is_server_runing() if not server_status: err = 'Ambari Server is not running.' raise FatalException(1, err) finalize_options = SetCurrentVersionOptions(options) if finalize_options.no_finalize_options_set(): err = 'Must specify --cluster-name and --version-display-name. Please invoke ambari-server.py --help to print the options.' raise FatalException(1, err) admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Failed to read properties file.") base_url = get_ambari_server_api_base(properties) url = base_url + "clusters/{0}/stack_versions".format(finalize_options.cluster_name) admin_auth = base64.encodestring('%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') data = { "ClusterStackVersions": { "repository_version": finalize_options.desired_repo_version, "state": "CURRENT", "force": "true" if finalize_options.force_repo_version else "false" } } if get_verbose(): sys.stdout.write('\nCalling API ' + url + ' : ' + str(data) + '\n') request.add_data(json.dumps(data)) request.get_method = lambda: 'PUT' try: response = urllib2.urlopen(request) except urllib2.HTTPError, e: code = e.getcode() content = e.read() err = 'Error during setting current version. Http status code - {0}. \n {1}'.format( code, content) raise FatalException(1, err)
def refresh_stack_hash(properties): resources_location = get_resources_location(properties) stacks_location = get_stack_location(properties) resource_files_keeper = ResourceFilesKeeper(resources_location, stacks_location) try: print "Organizing resource files at {0}...".format(resources_location, verbose=get_verbose()) resource_files_keeper.perform_housekeeping() except KeeperException, ex: msg = "Can not organize resource files at {0}: {1}".format( resources_location, str(ex)) raise FatalException(-1, msg)
def refresh_stack_hash(properties): stack_location = get_stack_location(properties) # Hack: we determine resource dir as a parent dir for stack_location resources_location = os.path.dirname(stack_location) resource_files_keeper = ResourceFilesKeeper(resources_location) try: print "Organizing resource files at {0}...".format( resources_location, verbose=get_verbose()) resource_files_keeper.perform_housekeeping() except KeeperException, ex: msg = "Can not organize resource files at {0}: {1}".format( resources_location, str(ex)) raise FatalException(-1, msg)
def switch_addon_services(config_file): old_verbose_level = get_verbose() set_verbose(True) if not os.path.exists(config_file): print_error_msg( 'Configuration file {0} does not exist!'.format(config_file)) set_verbose(old_verbose_level) return 1 print_info_msg( "Switching addon services using config file {0}".format(config_file)) stack_location, extension_location, service_definitions_location, mpacks_staging_location, dashboard_location = get_mpack_properties( ) mpack_metadata = _named_dict(json.load(open(config_file, "r"))) if not mpack_metadata: print_error_msg('Malformed configuration file {0}'.format(config_file)) set_verbose(old_verbose_level) return 1 mpack_name = mpack_metadata.name mpack_version = mpack_metadata.version mpack_dirname = mpack_name + "-" + mpack_version mpack_staging_dir = os.path.join(mpacks_staging_location, mpack_dirname) options = _named_dict( ast.literal_eval("{'force' : True, 'verbose' : True}")) for artifact in mpack_metadata.artifacts: # Artifact name (Friendly name) artifact_name = artifact.name # Artifact type (stack-definitions, extension-definitions, service-definitions, etc) artifact_type = artifact.type # Artifact directory with contents of the artifact artifact_source_dir = os.path.join(mpack_staging_dir, artifact.source_dir) # Artifact directory with contents of the artifact artifact_source_dir = os.path.join(mpack_staging_dir, artifact.source_dir) print_info_msg("Processing artifact {0} of type {1} in {2}".format( artifact_name, artifact_type, artifact_source_dir)) if artifact.type == STACK_ADDON_SERVICE_DEFINITIONS_ARTIFACT_NAME: process_stack_addon_service_definitions_artifact( artifact, artifact_source_dir, options) print_info_msg( "Successfully switched addon services using config file {0}".format( config_file)) set_verbose(old_verbose_level) return 0
def _reset_local_database(self): #force reset if silent option provided if get_silent(): default = "yes" else: default = "no" # Run automatic reset only for embedded DB okToRun = get_YN_input("Confirm server reset [yes/no]({0})? ".format(default), get_silent()) if not okToRun: err = "Ambari Server 'reset' cancelled" raise FatalException(1, err) print "Resetting the Server database..." dbname = self.database_name filename = self.drop_tables_script_file username = self.database_username password = self.database_password command = PGConfig.SETUP_DB_CMD[:] command[-1] = command[-1].format(filename, username, password, dbname) drop_retcode, drop_outdata, drop_errdata = run_os_command(command) if not drop_retcode == 0: raise FatalException(1, drop_errdata) if drop_errdata and PGConfig.PG_ERROR_BLOCKED in drop_errdata: raise FatalException(1, "Database is in use. Please, make sure all connections to the database are closed") if drop_errdata and get_verbose(): print_warning_msg(drop_errdata) print_info_msg("About to run database setup") retcode, outdata, errdata = self._setup_db() if errdata and get_verbose(): print_warning_msg(errdata) if (errdata and 'ERROR' in errdata.upper()) or (drop_errdata and 'ERROR' in drop_errdata.upper()): err = "Non critical error in DDL" if not get_verbose(): err += ", use --verbose for more information" raise NonFatalException(err)
def _change_db_files_owner(self): database_name = self.database_name new_owner = self.database_username if '"' not in new_owner: #wrap to allow old username "ambari-server", postgres only new_owner = '\'"{0}"\''.format(new_owner) pass command = PGConfig.CHANGE_OWNER_COMMAND[:] command[-1] = command[-1].format(database_name, 'ambari', new_owner) retcode, stdout, stderr = run_os_command(command) if not retcode == 0: if get_verbose(): if stderr: print_error_msg("stderr:\n" + stderr.strip()) if stdout: print_error_msg("stdout:\n" + stdout.strip()) else: print_info_msg('Fixed database objects owner') return retcode
def sync_ldap(options): if not is_root(): err = 'Ambari-server sync-ldap should be run with ' \ 'root-level privileges' raise FatalException(4, err) server_status, pid = is_server_runing() if not server_status: err = 'Ambari Server is not running.' raise FatalException(1, err) properties = get_ambari_properties() if properties == -1: raise FatalException(1, "Failed to read properties file.") ldap_configured = properties.get_property(IS_LDAP_CONFIGURED) if ldap_configured != 'true': err = "LDAP is not configured. Run 'ambari-server setup-ldap' first." raise FatalException(1, err) # set ldap sync options ldap_sync_options = LdapSyncOptions(options) if ldap_sync_options.no_ldap_sync_options_set(): err = 'Must specify a sync option (all, existing, users or groups). Please invoke ambari-server.py --help to print the options.' raise FatalException(1, err) admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Event":{"specs":[{"principal_type":"users","sync_type":"all"},{"principal_type":"groups","sync_type":"all"}]}}] elif ldap_sync_options.ldap_sync_existing: sys.stdout.write('Syncing existing.') bodies = [{"Event":{"specs":[{"principal_type":"users","sync_type":"existing"},{"principal_type":"groups","sync_type":"existing"}]}}] else: sys.stdout.write('Syncing specified users and groups.') bodies = [{"Event":{"specs":[]}}] body = bodies[0] events = body['Event'] specs = events['specs'] if ldap_sync_options.ldap_sync_users is not None: new_specs = [{"principal_type":"users","sync_type":"specific","names":""}] get_ldap_event_spec_names(ldap_sync_options.ldap_sync_users, specs, new_specs) if ldap_sync_options.ldap_sync_groups is not None: new_specs = [{"principal_type":"groups","sync_type":"specific","names":""}] get_ldap_event_spec_names(ldap_sync_options.ldap_sync_groups, specs, new_specs) if get_verbose(): sys.stdout.write('\nCalling API ' + url + ' : ' + str(bodies) + '\n') request.add_data(json.dumps(bodies)) request.get_method = lambda: 'POST' try: response = urllib2.urlopen(request) except Exception as e: err = 'Sync event creation failed. Error details: %s' % e raise FatalException(1, err) response_status_code = response.getcode() if response_status_code != 201: err = 'Error during syncing. Http status code - ' + str(response_status_code) raise FatalException(1, err) response_body = json.loads(response.read()) url = response_body['resources'][0]['href'] request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') body = [{"LDAP":{"synced_groups":"*","synced_users":"*"}}] request.add_data(json.dumps(body)) request.get_method = lambda: 'GET' request_in_progress = True while request_in_progress: sys.stdout.write('.') sys.stdout.flush() try: response = urllib2.urlopen(request) except Exception as e: request_in_progress = False err = 'Sync event check failed. Error details: %s' % e raise FatalException(1, err) response_status_code = response.getcode() if response_status_code != 200: err = 'Error during syncing. Http status code - ' + str(response_status_code) raise FatalException(1, err) response_body = json.loads(response.read()) sync_info = response_body['Event'] if sync_info['status'] == 'ERROR': raise FatalException(1, str(sync_info['status_detail'])) elif sync_info['status'] == 'COMPLETE': print '\n\nCompleted LDAP Sync.' print 'Summary:' for principal_type, summary in sync_info['summary'].iteritems(): print ' {0}:'.format(principal_type) for action, amount in summary.iteritems(): print ' {0} = {1!s}'.format(action, amount) request_in_progress = False else: time.sleep(1) sys.stdout.write('\n') sys.stdout.flush()