Пример #1
0
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
Пример #2
0
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
Пример #3
0
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)
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)