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