Select to view content in your preferred language

Overwriting a Service to Server through Arcpy

275
0
05-11-2022 07:30 AM
CameronCole
New Contributor III

Hey All,

I know there are some threads out there that seems to already answer part of the my question. 

I am trying to overwrite a service to ArcServer through ArcPy but when I am using this "esriServiceDefinitionType_Replacement" service its rewriting the service definition. For example, the existing service is a feature service and Linear referencing service  with certain pool and so forth. After the overwrite is done its just a map service. 

Here is the code that I am using. (its a little messy since I am setting it up to iterate through 39 folders and overwrite 39 different service)

import arcpy
import os
import xml.dom.minidom as DOM

arcpy.env.overwriteoutput = True
arcpy.env.overwrite_existing_service = True

target_root = r'\\Server\Cloud Share\IT_SHARE\Development\GIS-Mo\GIS Data Dev\Spokane'
infile = r'\\Server\Cloud Share\IT_SHARE\Development\GIS-Mo\GIS Data Dev\Spokane\Spokane County Inv.mxd'

# define local variables
wrkspc = target_root                                                    # folder directory header
mapDoc = arcpy.mapping.MapDocument(infile)                              # actual name of the mxd
con = r'User\AppData\Roaming\ESRI\Desktop10.5\ArcCatalog\serverstaging on www.apps.crab.wa.gov (publisher).ags'             # string to the ArcGIS Server connection file
service = 'Spokane_County_Inv'
print ('Service name = ' + service)                                     # service name we will want to iterate name
sddraft = os.path.join(wrkspc, service) + '.sddraft'                    # service draft
sd = os.path.join(wrkspc, service) + '.sd'                              # service definition
summary = 'Test service for doing the overwrite'                        # summary that can be anything
tags = 'test'                                                           # tags that can be anything

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

newType = 'esriServiceDefinitionType_Replacement'
xml = os.path.join(wrkspc, sddraft)
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
    if desc.parentNode.tagName == 'SVCManifest':
        if desc.hasChildNodes():
            desc.firstChild.data = newType
outXml = xml
f = open(outXml, 'w')    
doc.writexml( f )    
f.close()

# stage and upload the service if the sddraft analysis did not contain errors
if analysis['errors'] == {}:
    # Execute StageService
    print ('Stage Service')
    arcpy.StageService_server(sddraft, sd)
    # Execute UploadServiceDefinition
    print ('Upload Service Def')
    arcpy.UploadServiceDefinition_server(sd,
        con,
        in_service_name="",
        in_cluster="",
        in_folder_type="FROM_SERVICE_DEFINITION",
        in_folder="", in_startupType="STARTED",
        in_override="USE_DEFINITION",
        in_my_contents="NO_SHARE_ONLINE",
        in_public="PRIVATE",
        in_organization="NO_SHARE_ORGANIZATION",
        in_groups="")

else:
    # if the sddraft analysis contained errors, display them
    print analysis['errors']

print ('Published Successfully: ' + service)

 

  

0 Kudos
0 Replies