[GIS] Specifying map service parameters when publishing with ArcPy

arcgis-10.2arcgis-serverarcpy

I publish map services on a ArcGIS server using the typical arcpy script below. I need to better specify some service parameters. For example, the service should be available in WMS instead of KML, with anti-aliasing and some specific pooling parameters.

Is there a way to specify these parameters using some arcpy functions?

connection6546543 = 'C:/blabla/arcgis on myserver.eu (admin).ags'
folder1116 = "Myfolder"

def publishService(mxdPath, service_name, summary, tags):
    sddraft = 'C:/services/' + service_name + '.sddraft'
    if os.path.isfile(sddraft): os.remove(sddraft)

    # Create service definition draft
    map = arcpy.mapping.MapDocument(mxdPath)
    arcpy.mapping.CreateMapSDDraft(map, sddraft, service_name, 'ARCGIS_SERVER', connection6546543, True, folder1116, summary, tags)

    # Analyze the service definition draft
    analysis = arcpy.mapping.AnalyzeForSD(sddraft)

    # Print errors, warnings, and messages returned from the analysis
    print "The following information was returned during analysis of the MXD:"
    for key in ('messages', 'warnings', 'errors'):
        print '----' + key.upper() + '---'
        vars = analysis[key]
        for ((message, code), layerlist) in vars.iteritems():
            print '    ', message, ' (CODE %i)' % code
            print '       applies to:',
            for layer in layerlist:
                print layer.name,
            print

    # Stage and upload the service if the sddraft analysis did not contain errors
    if analysis['errors'] == {}:
        print 'Create service definition...'
        sd = 'E:/services/' + service_name + '.sd'
        if os.path.isfile(sd): os.remove(sd)
        arcpy.StageService_server(sddraft, sd)

        print 'Publish service...'
        arcpy.UploadServiceDefinition_server(sd, connection6546543)
        print "Service successfully published"
        print 'Clean sd'
        if os.path.isfile(sd): os.remove(sd)
    else: 
        print "Service publication failed: Errors during analysis."

    print arcpy.GetMessages()

    print 'Clean sddraft'
    if os.path.isfile(sddraft): os.remove(sddraft)

Detailed solution

The detailed solution I developed with the answers is the code snippet below. It has to be inserted between the draft service creation and draft service analysis. It modifies some parameters (UsageTimeout, KML off and WMS on, etc.) but can easily be adapted to others.

import xml.dom.minidom as DOM 

(...)

# change service parameters
doc = DOM.parse(sddraft)
keys = doc.getElementsByTagName('Key')
for key in keys:
    if key.firstChild.data == 'UsageTimeout': key.nextSibling.firstChild.data = 6000
    if key.firstChild.data == 'WaitTimeout': key.nextSibling.firstChild.data = 60
    if key.firstChild.data == 'IdleTimeout': key.nextSibling.firstChild.data = 20000
    if key.firstChild.data == 'MinInstances': key.nextSibling.firstChild.data = 1
    if key.firstChild.data == 'MaxInstances': key.nextSibling.firstChild.data = 1
services___ = doc.getElementsByTagName('TypeName')
for service__ in services___:
    if service__.firstChild.data == 'KmlServer':
        service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'false'
    if service__.firstChild.data == 'WMSServer':
        service__.parentNode.getElementsByTagName('Enabled')[0].firstChild.data = 'true'

# save changes
if os.path.exists(sddraft): os.remove(sddraft)
f = open(sddraft,"w")
doc.writexml(f)
f.close()

Best Answer

In addition to modifying the values "after" you publish per Alex, you can modify the values before publishing by opening up the SDDraft and modifying the XML. (not using arcpy, but xml manipulation through built in Python libraries)

There are a ton of samples here at the bottom of the page. I dont think there is one that does exactly what you want, but its not a far leap to modify them to suit your needs.

Related Question