def get_service_portal_ids(server, port, user, password): service_portal_ids = None # Get list of services on specified ArcGIS Server service_list = getServiceList(server, port, user, password) if len(service_list) > 0: service_portal_ids = {} # Get ids for all portal items assocatied with each AGS service for service in service_list: parsed_service = service.split('//') folder = None if len(parsed_service) == 1: service_name_type = parsed_service[0] else: folder = parsed_service[0] service_name_type = parsed_service[1] service_portal_prop = getServicePortalProps(server, port, user, password, folder, service_name_type) service_portal_items = service_portal_prop['portalItems'] root_service = service.split('.')[0].replace('//', '/') for service_portal_item in service_portal_items: service_portal_ids['{}/{}'.format( root_service, service_portal_item['type'] )] = service_portal_item['itemID'].encode('ascii') return service_portal_ids
def get_service_portal_ids(server, port, user, password): service_portal_ids = None # Get list of services on specified ArcGIS Server service_list = getServiceList(server, port, user, password) if len(service_list) > 0: service_portal_ids = {} # Get ids for all portal items assocatied with each AGS service for service in service_list: parsed_service = service.split('//') folder = None if len(parsed_service) == 1: service_name_type = parsed_service[0] else: folder = parsed_service[0] service_name_type = parsed_service[1] service_portal_prop = getServicePortalProps(server, port, user, password, folder, service_name_type) service_portal_items = service_portal_prop['portalItems'] root_service = service.split('.')[0].replace('//', '/') for service_portal_item in service_portal_items: service_portal_ids['{}/{}'.format(root_service, service_portal_item['type'])] = service_portal_item['itemID'].encode('ascii') return service_portal_ids
def get_ags_services(server, port, adminuser, password): ''' Return collection of ArcGIS Server services and service info''' agsServices = {} # Get all services that exist on server allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [service for service in allServices if service not in excludeServices] # Create dictionary from list of services; values are None. for service in services: parsedService = service.split('//') folder = None if len(parsedService) == 1: serviceNameType = parsedService[0] else: folder = parsedService[0] serviceNameType = parsedService[1] info = getServiceInfo(server, port, adminuser, password, folder, serviceNameType) agsServices[service] = info if debug: print "\nwithin get_ags_servides function:" print "agsServices:" print agsServices return agsServices
def get_ags_services(server, port, adminuser, password): ''' Return collection of ArcGIS Server services and service info''' agsServices = {} # Get all services that exist on server allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [ service for service in allServices if service not in excludeServices ] # Create dictionary from list of services; values are None. for service in services: parsedService = service.split('//') folder = None if len(parsedService) == 1: serviceNameType = parsedService[0] else: folder = parsedService[0] serviceNameType = parsedService[1] info = getServiceInfo(server, port, adminuser, password, folder, serviceNameType) agsServices[service] = info if debug: print "\nwithin get_ags_servides function:" print "agsServices:" print agsServices return agsServices
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL try: # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [ service for service in allServices if service not in excludeServices ] if len(services) == 0: raise Exception( 'ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?' ) # ------------------------------------------------- # List service workspaces # ------------------------------------------------- numServices = len(services) i = 0 # Print header print 'ArcGIS Server|Service|On Server Connection String/or Path' for service in services: onServerStr = '' folder, serviceNameType = parseService(service) serviceManifest = getServiceManifest(server, port, adminuser, password, folder, serviceNameType) databases = serviceManifest.get('databases') if databases: onServerConnStr = databases[0].get('onServerConnectionString') if onServerConnStr: if onServerConnStr.find('DB_CONNECTION_PROPERTIES') > 0: # It's an enterprise geodatabase onServerStr = onServerConnStr.split( 'DB_CONNECTION_PROPERTIES=')[1] else: onServerStr = onServerConnStr.replace('DATABASE=', '') else: if serviceNameType.find( '.GeocodeServer') > 0 or serviceNameType.find( '.GPServer') > 0: onServerStr = serviceManifest.get('resources')[0].get( 'serverPath') if len(onServerStr) == 0: onServerStr = str(serviceManifest) print '{}|{}|{}'.format(server, service, onServerStr) except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL print print '=' * 100 print ' Remap portal ids stored within ArcGIS Server services' print '=' * 100 print try: # ------------------------------------------------- # Get portal items with URLs # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' # Create portal object portal_address = '{}://{}:7443/arcgis'.format(protocol, server) portal = Portal(portal_address, adminuser, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') print '\n- Retrieving portal item information from portal...' portal_url_items = getPortalURLItems(portal) if not portal_url_items: raise Exception( 'ERROR: There are no URL portal items. Have you published the portal content?' ) # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- print '\n- Retrieving list of ArcGIS Server services...' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [ service for service in allServices if service not in excludeServices ] if len(services) == 0: raise Exception( 'ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?' ) # ------------------------------------------------- # Update portal item ids with service portal properties json # ------------------------------------------------- portalItemIDsToDelete = [] print '\n- Remap portal ids on each ArcGIS Server service...\n' totalNumIDsNotFound = 0 for service in services: time.sleep(0.5) print '\t' + ('-' * 75) print '\tService: ' + service folder, serviceNameType = parseService(service) numIDsFoundForService = 0 # Get the service info info = getServiceInfo(server, port, adminuser, password, folder, serviceNameType) # Get the service portal properties json and update the item ids print '\n\t- Retrieving information about associated portal items stored in the server JSON...' servicePortalPropsOrig = info.get('portalProperties') if not servicePortalPropsOrig: raise Exception( 'ERROR: The service ' + service + ' does not ' + 'have any portal properties ("portalProperties" JSON key/value). ' + 'Did you federate the server?') if servicePortalPropsOrig: servicePortalProps = copy.deepcopy(servicePortalPropsOrig) servicePortalItemsOrig = servicePortalProps.get('portalItems') servicePortalItems = copy.deepcopy(servicePortalItemsOrig) if not servicePortalItems: totalSuccess = False print '\n\t**** ERROR: this service does not have any associated portal items.' continue if servicePortalItems: print '\n\t- Associated portal items...' for servicePortalItem in servicePortalItems: orig_id = servicePortalItem['itemID'] # Get service search string serviceSearchStr = getServiceSearchString( service, servicePortalItem) print '\n\t - ' + serviceSearchStr + ': original item id = ' + orig_id # Get new portal item id new_id = findPortalItemID(server, serviceSearchStr, portal_url_items) if new_id: if (new_id <> orig_id): numIDsFoundForService = numIDsFoundForService + 1 servicePortalItem['itemID'] = new_id portalItemIDsToDelete.append(orig_id) print '\t\tFound new item id - ' + new_id else: print '\t\tItem IDs match, not processing.' else: print '\n\t**** WARNING: new item id not found.' servicePortalProps['portalItems'] = servicePortalItems info['portalProperties'] = servicePortalProps if doUpdateService: print '\n\n\t- Updating portal item information stored within service JSON (service will be restarted automatically)...' if numIDsFoundForService == 0: print '\n\t**** WARNING: there were no new ids found for this service so there is no need to update the service JSON info.' continue success, status = editServiceInfo( server, port, adminuser, password, folder, serviceNameType, info) if success: print '\t\tDone.' else: totalSuccess = False print '**** ERROR: Update of service was not successful.' print 'status: ' + str(status) if doDeleteItems: print #print '=' * 100 print '\n\n-Deleting portal items that were remapped to original portal item...' if len(portalItemIDsToDelete) == 0: print '\n**** ERROR: No portal items to delete; which means there were no portal items ' print '\t owned by ' + portal.logged_in_user( )['username'] + ' that were remapped to original portal item.\n' # Get list of all portal ids so we can verify that the portal item exists before we delete portal_items = portal.search(['id']) time.sleep(5) for portalItemID in portalItemIDsToDelete: time.sleep(2) itemFound = False print ' -Deleting id ' + portalItemID + '...' # Delete if item exists for portal_item in portal_items: if portal_item['id'] == portalItemID: itemFound = True results = portal.delete_item( portalItemID, portal.logged_in_user()['username']) if results: print '\tDone.' else: totalSuccess = False print '**** ERROR: Deletion of service was not successful.' if not itemFound: print '\tItem ' + portalItemID + ' does not exist. Skipping...' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: print print if totalSuccess: print "Remap of portal item ids on services was completed successfully." sys.exit(0) else: print "ERROR: Remap of portal item ids on services was _NOT_ completed successfully." sys.exit(1)
if validAction.lower() == serviceAction.lower(): isActionValid = True break if not isActionValid: print "User specified action '" + serviceAction + " is not valid." print "Valid actions are:" + str(validActionList) + "\n" sys.exit(1) try: startTime = datetime.now() # --------------------------------------------------------------------- # Get list of all services/or user specified list # --------------------------------------------------------------------- if not serviceList: serviceList = getServiceList(serverName, serverPort, userName, passWord, useSSL) if len(serviceList) == 0: print "\t*ERROR: No services to " + serviceAction.title() + "." # --------------------------------------------------------------------- # Filter the service list and remove services that are already at the requested state # --------------------------------------------------------------------- if len(serviceList) > 0: actionStatusMap = {"STOP": "STOPPED", "START": "STARTED"} modServiceList = [] print "\n{}".format("-" * 110) print "- Check status of specified services...\n"
if validAction.lower() == serviceAction.lower(): isActionValid = True break if not isActionValid: print "User specified action '" + serviceAction + " is not valid." print "Valid actions are:" + str(validActionList) + "\n" sys.exit(1) try: startTime = datetime.now() # --------------------------------------------------------------------- # Get list of all services/or user specified list # --------------------------------------------------------------------- if not serviceList: serviceList = getServiceList(serverName, serverPort, userName, passWord, useSSL) # Remove hosted services from list since these can't be started/stopped print '\nRemoving "Hosted" services from service list; these services can not be started/stopped.' serviceList = [x for x in serviceList if x.find('Hosted/') == -1] if len(serviceList) == 0: print "\t*ERROR: No services to " + serviceAction.title() + "." # --------------------------------------------------------------------- # Filter the service list and remove services that are already at the requested state # --------------------------------------------------------------------- if len(serviceList) > 0: actionStatusMap = {"STOP":"STOPPED", "START":"STARTED"}
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, use_ssl, output_file, owners = results if debug: print server, port, adminuser, password, use_ssl, output_file, owners if use_ssl: protocol = 'https' else: protocol = 'http' try: # Create connection to portal; need to get service item owner information portal = Portal('https://' + server + ':7443/arcgis', adminuser, password) # Verify if user specify owners exist invalid_owners = get_invalid_owners(portal, owners) if len(invalid_owners) > 0: print '\nThe following owners do not exist. Exiting script: \n{}'.format(', '.join(invalid_owners)) totalSuccess = False else: # Get all services that exist on server all_services = getServiceList(server, port, adminuser, password) all_services = [service.replace('//', '/') for service in all_services] # Create collection of only hosted services print '\n{}'.format('=' * 80) print 'Extracting service definition information (editing, sync, export, tracking)' print 'for hosted feature services that meet filter criteria.' print '{}\n'.format('=' * 80) definitions = {} for service in all_services: folder, serviceNameType = parseService(service) if serviceNameType.endswith('.FeatureServer'): portal_props = getServicePortalProps(server, port, adminuser, password, folder, serviceNameType) if portal_props: if portal_props['isHosted']: portal_items = portal_props['portalItems'] for portal_item in portal_items: #NOTE: for hosted services there should only be one portal item do_write = True item_id = portal_item['itemID'] item = portal.item(item_id) if item is None: print '\tPortal item {} for service does not exist.'.format(item_id) item_owner = None else: item_owner = item['owner'] if owners: if item_owner not in owners: do_write = False if do_write: print '{} ({})...'.format(service, item_owner) definition = getHostedServiceDefinition(server, port, adminuser, password, folder, serviceNameType) definitions[service] = {'owner': item_owner, 'updateDefinition': definition} # Write info to json file print '\nWriting "definition" information for {} services to file {}'.format(len(definitions), output_file) json.dump(definitions, open(output_file,'w')) print '\nDone.\n' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL try: # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [service for service in allServices if service not in excludeServices] if len(services) == 0: raise Exception('ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?') # ------------------------------------------------- # List service workspaces # ------------------------------------------------- numServices = len(services) i = 0 # Print header print 'ArcGIS Server|Service|On Server Connection String/or Path' for service in services: onServerStr = '' folder, serviceNameType = parseService(service) serviceManifest = getServiceManifest(server, port, adminuser, password, folder, serviceNameType) databases = serviceManifest.get('databases') if databases: onServerConnStr = databases[0].get('onServerConnectionString') if onServerConnStr: if onServerConnStr.find('DB_CONNECTION_PROPERTIES') > 0: # It's an enterprise geodatabase onServerStr = onServerConnStr.split('DB_CONNECTION_PROPERTIES=')[1] else: onServerStr = onServerConnStr.replace('DATABASE=','') else: if serviceNameType.find('.GeocodeServer') > 0 or serviceNameType.find('.GPServer') > 0: onServerStr = serviceManifest.get('resources')[0].get('serverPath') if len(onServerStr) == 0: onServerStr = str(serviceManifest) print '{}|{}|{}'.format(server, service, onServerStr) except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, use_ssl, output_file, owners = results if debug: print server, port, adminuser, password, use_ssl, output_file, owners if use_ssl: protocol = 'https' else: protocol = 'http' try: # Create connection to portal; need to get service item owner information portal = Portal('https://' + server + ':7443/arcgis', adminuser, password) # Verify if user specify owners exist invalid_owners = get_invalid_owners(portal, owners) if len(invalid_owners) > 0: print '\nThe following owners do not exist. Exiting script: \n{}'.format( ', '.join(invalid_owners)) totalSuccess = False else: # Get all services that exist on server all_services = getServiceList(server, port, adminuser, password) all_services = [ service.replace('//', '/') for service in all_services ] # Create collection of only hosted services print '\n{}'.format('=' * 80) print 'Extracting service definition information (editing, sync, export, tracking)' print 'for hosted feature services that meet filter criteria.' print '{}\n'.format('=' * 80) definitions = {} for service in all_services: folder, serviceNameType = parseService(service) if serviceNameType.endswith('.FeatureServer'): portal_props = getServicePortalProps( server, port, adminuser, password, folder, serviceNameType) if portal_props: if portal_props['isHosted']: portal_items = portal_props['portalItems'] for portal_item in portal_items: #NOTE: for hosted services there should only be one portal item do_write = True item_id = portal_item['itemID'] item = portal.item(item_id) if item is None: print '\tPortal item {} for service does not exist.'.format( item_id) item_owner = None else: item_owner = item['owner'] if owners: if item_owner not in owners: do_write = False if do_write: print '{} ({})...'.format( service, item_owner) definition = getHostedServiceDefinition( server, port, adminuser, password, folder, serviceNameType) definitions[service] = { 'owner': item_owner, 'updateDefinition': definition } # Write info to json file print '\nWriting "definition" information for {} services to file {}'.format( len(definitions), output_file) json.dump(definitions, open(output_file, 'w')) print '\nDone.\n' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL try: # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection #excludeServices = ['SampleWorldCities.MapServer'] excludeServices = [] services = [service for service in allServices if service not in excludeServices] services = [service.replace('//', '/') for service in allServices] if len(services) == 0: raise Exception('ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?') # ------------------------------------------------- # List service workspaces # ------------------------------------------------- numServices = len(services) i = 0 for service in services: i += 1 print service #print 'Total: {}'.format(i) except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, use_ssl, output_folder, owners = results if debug: print server, port, adminuser, password, use_ssl, output_folder, owners if use_ssl: protocol = 'https' else: protocol = 'http' try: # Create connection to portal; need to get service item owner information portal = Portal('https://' + server + ':7443/arcgis', adminuser, password) # Verify if user specify owners exist invalid_owners = get_invalid_owners(portal, owners) if len(invalid_owners) > 0: print '\nThe following owners do not exist. Exiting script: \n{}'.format(', '.join(invalid_owners)) totalSuccess = False else: # Get all services that exist on server all_services = getServiceList(server, port, adminuser, password) all_services = [service.replace('//', '/') for service in all_services] # Create collection of only hosted services print '\n{}'.format('=' * 80) print 'Extracting scene service definition information for hosted scene services' print 'that meet criteria.' print '{}\n'.format('=' * 80) total_scene_services = 0 total_files_created = 0 for service in all_services: folder, serviceNameType = parseService(service) if serviceNameType.endswith('.SceneServer'): total_scene_services += 1 print '-' * 80 print 'Service: {}'.format(service) portal_props = getServicePortalProps(server, port, adminuser, password, folder, serviceNameType) if portal_props: if portal_props['isHosted']: portal_items = portal_props['portalItems'] for portal_item in portal_items: #NOTE: for hosted services there should only be one portal item do_write = True item_id = portal_item['itemID'] item = portal.item(item_id) if item is None: print '\tPortal item {} for service does not exist.'.format(item_id) item_owner = None else: item_owner = item['owner'] item_title = item['title'] print 'Item id: {}, Owner: {}, Title: {}'.format(item_id, item_owner, item_title) if owners: if item_owner not in owners: do_write = False if do_write: service_json = getServiceJSON(server, port, adminuser, password, folder, serviceNameType) definition = {} definition['name'] = serviceNameType.split('.')[0] definition['serviceDescription'] = service_json output_file = os.path.join(output_folder, serviceNameType.replace('.SceneServer', '') + '_sceneserver.json') # Write info to json file print '\nWriting "definition" to file {}'.format(output_file) json.dump(definition, open(output_file,'w')) total_files_created += 1 else: print 'Does not meet criteria. Will not write "definition" file.' print '-' * 80 print '\nTotal number of scene services: {}'.format(total_scene_services) print 'Total number of scene service definition files created: {}'.format(total_files_created) print '\nDone.\n' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, use_ssl, output_folder, owners = results if debug: print server, port, adminuser, password, use_ssl, output_folder, owners if use_ssl: protocol = 'https' else: protocol = 'http' try: # Create connection to portal; need to get service item owner information portal = Portal('https://' + server + ':7443/arcgis', adminuser, password) # Verify if user specify owners exist invalid_owners = get_invalid_owners(portal, owners) if len(invalid_owners) > 0: print '\nThe following owners do not exist. Exiting script: \n{}'.format( ', '.join(invalid_owners)) totalSuccess = False else: # Get all services that exist on server all_services = getServiceList(server, port, adminuser, password) all_services = [ service.replace('//', '/') for service in all_services ] # Create collection of only hosted services print '\n{}'.format('=' * 80) print 'Extracting scene service definition information for hosted scene services' print 'that meet criteria.' print '{}\n'.format('=' * 80) total_scene_services = 0 total_files_created = 0 for service in all_services: folder, serviceNameType = parseService(service) if serviceNameType.endswith('.SceneServer'): total_scene_services += 1 print '-' * 80 print 'Service: {}'.format(service) portal_props = getServicePortalProps( server, port, adminuser, password, folder, serviceNameType) if portal_props: if portal_props['isHosted']: portal_items = portal_props['portalItems'] for portal_item in portal_items: #NOTE: for hosted services there should only be one portal item do_write = True item_id = portal_item['itemID'] item = portal.item(item_id) if item is None: print '\tPortal item {} for service does not exist.'.format( item_id) item_owner = None else: item_owner = item['owner'] item_title = item['title'] print 'Item id: {}, Owner: {}, Title: {}'.format( item_id, item_owner, item_title) if owners: if item_owner not in owners: do_write = False if do_write: service_json = getServiceJSON( server, port, adminuser, password, folder, serviceNameType) definition = {} definition['name'] = serviceNameType.split( '.')[0] definition[ 'serviceDescription'] = service_json output_file = os.path.join( output_folder, serviceNameType.replace( '.SceneServer', '') + '_sceneserver.json') # Write info to json file print '\nWriting "definition" to file {}'.format( output_file) json.dump(definition, open(output_file, 'w')) total_files_created += 1 else: print 'Does not meet criteria. Will not write "definition" file.' print '-' * 80 print '\nTotal number of scene services: {}'.format( total_scene_services) print 'Total number of scene service definition files created: {}'.format( total_files_created) print '\nDone.\n' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str( sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: if totalSuccess: sys.exit(0) else: sys.exit(1)
def main(): exit_err_code = 1 # Print/get script arguments results = print_args() if not results: sys.exit(exit_err_code) server, port, adminuser, password, use_ssl, file_path, option = results total_success = True title_break_count = 100 section_break_count = 75 search_query = None print '=' * title_break_count print 'ManageServiceProperties ({})'.format(option) print '=' * title_break_count clusters = getClusters(server, port, adminuser, password, use_ssl) cluster_names = [cluster['clusterName'] for cluster in clusters] try: services = getServiceList(server, port, adminuser, password, use_ssl) services = [service.replace('//', '/') for service in services] # Properties of hosted services should not be altered; remove hosted # services from the services list services = [x for x in services if x.find('Hosted/') == -1] # --------------------------------------------------------------------- # Report service properties # --------------------------------------------------------------------- if option == "REPORT": f = open(file_path, 'w') print 'Writing service property information to file (excluding hosted services)...\n' for service in services: folder, servicename_type = parseService(service) service_info = getServiceInfo(server, port, adminuser, password, folder, servicename_type, use_ssl) # Don't write service info if service is associated # with gp service. Service is edited through gp service. parent_name = service_info['properties'].get('parentName') if parent_name: if parent_name.find('.GPServer') > -1: continue write_str = '{{"service": "{}", "properties": {{"clusterName": "{}", "minInstancesPerNode": {}, "maxInstancesPerNode": {}}}}}\n' write_str = write_str.format( service, service_info['clusterName'], service_info['minInstancesPerNode'], service_info['maxInstancesPerNode'] ) f.write(write_str) f.close # --------------------------------------------------------------------- # Update/Edit service properties # --------------------------------------------------------------------- if option == "UPDATE": file_contents = read_file(file_path) if not validate_file_contents(file_contents): raise Exception('File {} is not valid. Exiting script.'.format(file_path)) for line in file_contents: print '\n{}'.format('-' * 90) line_json = json.loads(line) mod_service = line_json['service'] mod_service_props = line_json['properties'] print mod_service if mod_service.find('Hosted/') > -1: print '\n\tWARNING: Skipping update. Hosted service properties should not be updated.' continue if mod_service not in services: print '\n\tWARNING: Can not find specified service: {}. Skipping update.\n'.format(mod_service) continue # Get the current service properties folder, servicename_type = parseService(mod_service) service_info = getServiceInfo(server, port, adminuser, password, folder, servicename_type, use_ssl) # Don't write service info if service is associated # with gp service. Service is edited through gp service. parent_name = service_info['properties'].get('parentName') if parent_name: if parent_name.find('.GPServer') > -1: '\n\tWARNING: Can not edit service associated with GP service. Skipping update.\n' continue # Print the original and new service property values. # Also check if properties have actually changed. print '\t{:<25}: {:<20}{:<5}{:<20}'.format('', 'Orig. Value', '', 'New Value') has_changes = False is_valid = True for prop in mod_service_props.keys(): if mod_service_props[prop] != service_info[prop]: has_changes = True print '\t{:<25}: {:<20}{:<5}{:<20}'.format(prop, service_info[prop], '', mod_service_props[prop]) # Validate 'clusterName' if prop == 'clusterName': if mod_service_props[prop] not in cluster_names: is_valid = False print '***ERROR: Value for property "{}": "{}" is NOT valid. Skipping update.'.format(prop, mod_service_props[prop]) # Validate that specific properties are numeric if prop in ['minInstancesPerNode', 'minInstancesPerNode']: if not isinstance(mod_service_props[prop], (int)): is_valid = False print '***ERROR: Value for property "{}": {} is NOT integer. Skipping update.'.format(prop, mod_service_props[prop]) if not is_valid: continue if not has_changes: print '\n\tWARNING: no changes to service properties. Skipping update.\n' continue # Update the service properties from the contents of the file for prop in mod_service_props.keys(): service_info[prop] = mod_service_props[prop] # Save the service properties to the service print '\n\tSaving updates to service.' success, status = editServiceInfo(server, port, adminuser, password, folder, servicename_type, service_info) if success: print '\tDone.' else: total_success = False print '**** ERROR: Update of service was not successful.' print 'status: ' + str(status) time.sleep(15) except: total_success = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error # into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \ "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: print '\nDone.' if total_success: sys.exit(0) else: sys.exit(exit_err_code)
def main(): totalSuccess = True # ------------------------------------------------- # Check arguments # ------------------------------------------------- results = check_args() if not results: sys.exit(exitErrCode) server, port, adminuser, password, useSSL = results if debug: print server, port, adminuser, password, useSSL print print '=' * 100 print ' Remap portal ids stored within ArcGIS Server services' print '=' * 100 print try: # ------------------------------------------------- # Get portal items with URLs # ------------------------------------------------- if useSSL: protocol = 'https' else: protocol = 'http' # Create portal object portal_address = '{}://{}:7443/arcgis'.format(protocol, server) portal = Portal(portal_address, adminuser, password) if not portal: raise Exception('ERROR: Could not create "portal" object.') print '\n- Retrieving portal item information from portal...' portal_url_items = getPortalURLItems(portal) if not portal_url_items: raise Exception('ERROR: There are no URL portal items. Have you published the portal content?') # ------------------------------------------------- # Get all services that exist on server # ------------------------------------------------- print '\n- Retrieving list of ArcGIS Server services...' allServices = getServiceList(server, port, adminuser, password) # Remove certain services from collection excludeServices = ['SampleWorldCities.MapServer'] services = [service for service in allServices if service not in excludeServices] if len(services) == 0: raise Exception('ERROR: There are no user published ArcGIS Server services. Have you published the ArcGIS Server services?') # ------------------------------------------------- # Update portal item ids with service portal properties json # ------------------------------------------------- portalItemIDsToDelete = [] print '\n- Remap portal ids on each ArcGIS Server service...\n' totalNumIDsNotFound = 0 for service in services: time.sleep(0.5) print '\t' + ('-' * 75) print '\tService: ' + service folder, serviceNameType = parseService(service) numIDsFoundForService = 0 # Get the service info info = getServiceInfo(server, port, adminuser, password, folder, serviceNameType) # Get the service portal properties json and update the item ids print '\n\t- Retrieving information about associated portal items stored in the server JSON...' servicePortalPropsOrig = info.get('portalProperties') if not servicePortalPropsOrig: raise Exception('ERROR: The service ' + service + ' does not ' + 'have any portal properties ("portalProperties" JSON key/value). ' + 'Did you federate the server?') if servicePortalPropsOrig: servicePortalProps = copy.deepcopy(servicePortalPropsOrig) servicePortalItemsOrig = servicePortalProps.get('portalItems') servicePortalItems = copy.deepcopy(servicePortalItemsOrig) if not servicePortalItems: totalSuccess = False print '\n\t**** ERROR: this service does not have any associated portal items.' continue if servicePortalItems: print '\n\t- Associated portal items...' for servicePortalItem in servicePortalItems: orig_id = servicePortalItem['itemID'] # Get service search string serviceSearchStr = getServiceSearchString(service, servicePortalItem) print '\n\t - ' + serviceSearchStr + ': original item id = ' + orig_id # Get new portal item id new_id = findPortalItemID(server, serviceSearchStr, portal_url_items) if new_id: if (new_id <> orig_id): numIDsFoundForService = numIDsFoundForService + 1 servicePortalItem['itemID'] = new_id portalItemIDsToDelete.append(orig_id) print '\t\tFound new item id - ' + new_id else: print '\t\tItem IDs match, not processing.' else: print '\n\t**** WARNING: new item id not found.' servicePortalProps['portalItems'] = servicePortalItems info['portalProperties'] = servicePortalProps if doUpdateService: print '\n\n\t- Updating portal item information stored within service JSON (service will be restarted automatically)...' if numIDsFoundForService == 0: print '\n\t**** WARNING: there were no new ids found for this service so there is no need to update the service JSON info.' continue success, status = editServiceInfo(server, port, adminuser, password, folder, serviceNameType, info) if success: print '\t\tDone.' else: totalSuccess = False print '**** ERROR: Update of service was not successful.' print 'status: ' + str(status) if doDeleteItems: print #print '=' * 100 print '\n\n-Deleting portal items that were remapped to original portal item...' if len(portalItemIDsToDelete) == 0: print '\n**** ERROR: No portal items to delete; which means there were no portal items ' print '\t owned by ' + portal.logged_in_user()['username'] + ' that were remapped to original portal item.\n' # Get list of all portal ids so we can verify that the portal item exists before we delete portal_items = portal.search(['id']) time.sleep(5) for portalItemID in portalItemIDsToDelete: time.sleep(2) itemFound = False print ' -Deleting id ' + portalItemID + '...' # Delete if item exists for portal_item in portal_items: if portal_item['id'] == portalItemID: itemFound = True results = portal.delete_item(portalItemID, portal.logged_in_user()['username']) if results: print '\tDone.' else: totalSuccess = False print '**** ERROR: Deletion of service was not successful.' if not itemFound: print '\tItem ' + portalItemID + ' does not exist. Skipping...' except: totalSuccess = False # Get the traceback object tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) # Print Python error messages for use in Python / Python Window print print "***** ERROR ENCOUNTERED *****" print pymsg + "\n" finally: print print if totalSuccess: print "Remap of portal item ids on services was completed successfully." sys.exit(0) else: print "ERROR: Remap of portal item ids on services was _NOT_ completed successfully." sys.exit(1)