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
Solved! Go to Solution.
Finally I see some discussion on this topic. I am going through a similar problem.
I have automated the overwrite of the service definition of a feature service, but every time it is overwritten it resets the 'maxRecordCount' back to the default value of 1000.
As you say, this then defeats the purpose of automating since I have to manually upload the service so it allows 50,000 records to be queried at REST.
I am doing this through AGOL -- any idea how to tailor this part:
portalAdminName = 'gisadmin' # Gisserver login
portalAdminPassword = 'xxxxxxx' # gisserver password
serverName = 'gisserver' # Server Name
serverPort = 6080 # Port number of the server
...to allow for log in and edit of a service on an AGOL organizational account?
I have tried:
but I am getting an error on the httpConn request saying "getaddrinfo failed".
Any help or direction would be greatly appreciated, otherwise it looks like a call to Esri is in store. Thanks.
The code to generate a token is for an on-premises ArcGIS Server, not AGOL. This link has a code example for generating a token with in AGOL:
try:
print('Generating Token')
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']
except:
token = ''
I'll add that you'll likely want to capture the exception:
try:
print('Generating Token')
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']
except Exception as e:
print(e)
token = ''
And also note that the token is generated using a referer, so any request that uses the token should have the same referer header.