AnsweredAssumed Answered

Publish Map Service with modified Properties

Question asked by ben_vk on Jul 6, 2016
Latest reply on Jan 11, 2018 by JQuinn-esristaff


Hi All,

 

I currently have a Python script which does 98% of what I require.

 

Currently, it picks up a local MXD file that has been modified in the last 3 days, then uses arcpy.mapping.CreateMapSDDraft() to create the Draft Service Definition, then stages the service using arcpy.StageService_server() and finally arcpy.UploadServiceDefinition_server()

 

Each time this file is uploaded, it is overwriting an existing map service, and these services all have Advanced Properties modified, for example the maxImageWidth and Height are modified, along with Max Number of Instanced modified to be 15.

 

The problem I have is that each time this script runs, it reverts those three settings back to the default, so then I have to manually set them to what we require, which in turn defeats the purpose of automating this process... I have pasted my working code below, its not pretty, but seems to do most of what I require....

 

Any ideas on how to set those additional parameters in python?

 

 

def publish_services(service):


    log.write_log(python_file, 'Updating Services (' + service + ') to GISServer')
    err = False
    # Define Local Variables
    wrkspc = r'\\gisserver\weave projects\Project_Files'
    doc = os.path.join(wrkspc, service + '.mxd')
    log.write_log(python_file, 'Publishing File to Service:')
    log.write_log(python_file, '    %s [doc]' % doc)
    is_edited = check_edit_date(service, doc)
    if is_edited is True:
        mapDoc = arcpy.mapping.MapDocument(doc)


        # Provide path to connection file
        # To create this file, right-click a folder in the Catalog window and
        #   click New > ArcGIS Server Connection
        con = 'GIS Servers\GisServer (Administration)'


        # Provide other service details
        sddraft = os.path.join(wrkspc, service + '.sddraft')
        sd = os.path.join(wrkspc, service + '.sd')
        summary = 'Map document displayed by the Weave Mapping System'
        tags = 'Weave, python, automated, MXD, Map Service Automation, No Man Hours Used'


        def del_sd():
            try:
                os.remove(sd)
                log.write_log(python_file, 'Removed the existing sd file - %s' % str(sd))
            except OSError:
                pass


        log.write_log(python_file, 'Creating .sddraft file')


        # Create service definition draft (.sddraft)
        arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER',
            con, True, None, summary, tags)


        log.write_log(python_file, 'Analyzing the .sddraft file for errors')
        # Analyze the service definition draft
        analysis = arcpy.mapping.AnalyzeForSD(sddraft)


        # Print errors, warnings, and messages returned from the analysis
        try:
            for key in 'errors':  # 'messages', 'warnings'):    # This is commented out for clarity
                log.write_log(python_file,  "--------" + key.upper() + "--------")
                vars = analysis[key]
                for ((message, code), layerlist) in vars.iteritems():
                    log_message = "    ", message, " (CODE %i)" % code
                    log.write_log(python_file, log_message)
                    log.write_log(python_file, "       applies to:", )
                    for layer in layerlist:
                        log.write_log(python_file, '\n             ' + str(layer.name), )
                    log.write_log(python_file, "")
        except:
            log.write_log(python_file, 'Ironically, there was an error printing the errors')
            print 'Ironically, there was an error printing the errors in the publish_services() module'
            err = True


        # remove file if it already exists,
        # need the folder to be cleared before creating again
        del_sd()


        # Stage and upload the service if the sddraft analysis did not contain errors
        if analysis['errors'] == {} or err is False:
            # Execute StageService
            arcpy.StageService_server(sddraft, sd)
            # Execute UploadServiceDefinition
            arcpy.UploadServiceDefinition_server(sd, con)
            log.write_log(python_file, "Service %s successfully published" % service)
        else:
            # if the sddraft analysis contained errors, display them
            log.write_log(python_file, str(analysis['errors']))
            log.write_log(python_file, "Service %s could not be published because errors were found during analysis."
                          % service
                          )


        # remove the temporary file once tasks complete
       del_sd()
    else:
        pass

Outcomes