Skip to content

Instantly share code, notes, and snippets.

@pauldzy
Created August 3, 2019 14:41
Show Gist options
  • Save pauldzy/a98b8b30ce65e671491c639f824d5220 to your computer and use it in GitHub Desktop.
Save pauldzy/a98b8b30ce65e671491c639f824d5220 to your computer and use it in GitHub Desktop.
Python 3 arcpy script to automate the deployment of ArcGIS Pro defined map services to standalone ArcGIS Servers
import os,sys,arcpy;
import xml.dom.minidom as DOM;
###############################################################################
# Alter the following constants as required
###############################################################################
project_name = "HydrologicUnits";
ags_service = "HydrologicUnits";
ags_folder = "Support";
ags_credits = None;
ags_description = None;
ags_summary = None;
ags_tags = None;
ags_use_limits = None;
ags_properties = {
'schemaLockingEnabled': False
,'MinInstances': 1
,'MaxInstances': 4
,'enableDynamicLayers': True
}
ags_services = {
'WMSServer': True
,'WFSServer': False
,'KmlServer': False
}
ags_service_props = {}
ags_credits = None;
ags_description = None;
ags_summary = None;
ags_tags = None;
ags_use_limits = None;
ags_default_connection_file = "inlandwaters.ags";
map_filter = "*";
###############################################################################
dir_path = os.path.dirname(os.path.realpath(__file__));
if len(sys.argv) > 1:
ags_file = sys.argv[1]
else:
ags_file = ags_default_connection_file;
ags_conn = dir_path + os.sep + ags_file;
if not arcpy.Exists(ags_conn):
arcpy.RaiseError("AGS connection file not found");
arcpy.AddMessage("Using AGS Connection " + ags_file);
aprx_file = dir_path + os.sep + project_name + ".aprx";
if not arcpy.Exists(ags_conn):
arcpy.RaiseError("Aprx project file not found");
arcpy.AddMessage("Publishing map service for " + project_name)
arcpy.AddMessage("Loading project map.");
aprx = arcpy.mp.ArcGISProject(aprx_file);
map = aprx.listMaps(map_filter)[0];
arcpy.AddMessage("Generating service draft.");
sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER","MAP_SERVICE",ags_service,map);
sddraft.targetServer = ags_conn;
sddraft.copyDataToServer = False;
sddraft.serverFolder = ags_folder;
sddraft.credits = ags_credits;
sddraft.description = ags_description;
sddraft.summary = ags_summary;
sddraft.tags = ags_tags;
sddraft.useLimitations = ags_use_limits;
sddraft_filename = dir_path + os.sep + ags_service + ".sddraft";
if arcpy.Exists(sddraft_filename):
arcpy.Delete_management(sddraft_filename);
sddraft.exportToSDDraft(sddraft_filename);
def srv_property(doc,property,value):
keys = doc.getElementsByTagName('Key')
for key in keys:
if key.hasChildNodes():
if key.firstChild.data == property:
if value is True:
key.nextSibling.firstChild.data = 'true';
elif value is False:
key.nextSibling.firstChild.data = 'false';
else:
key.nextSibling.firstChild.data = value
return doc;
def soe_enable(doc,soe,value):
typeNames = doc.getElementsByTagName('TypeName');
for typeName in typeNames:
if typeName.firstChild.data == soe:
extension = typeName.parentNode
for extElement in extension.childNodes:
if extElement.tagName == 'Enabled':
if value is True:
extElement.firstChild.data = 'true';
else:
extElement.firstChild.data = 'false';
return doc;
def soe_property(doc,soe,soeProperty,soePropertyValue):
typeNames = doc.getElementsByTagName('TypeName');
for typeName in typeNames:
if typeName.firstChild.data == soe:
extension = typeName.parentNode
for extElement in extension.childNodes:
if extElement.tagName in ['Props','Info']:
for propArray in extElement.childNodes:
for propSet in propArray.childNodes:
for prop in propSet.childNodes:
if prop.tagName == "Key":
if prop.firstChild.data == soeProperty:
if prop.nextSibling.hasChildNodes():
prop.nextSibling.firstChild.data = soePropertyValue
else:
txt = doc.createTextNode(soePropertyValue)
prop.nextSibling.appendChild(txt)
return doc;
doc = DOM.parse(sddraft_filename);
for k, v in ags_properties.items():
doc = srv_property(doc,k,v);
for k, v in ags_services.items():
doc = soe_enable(doc,k,v);
for k, v in ags_service_props.items():
doc = soe_property(doc,k,v.keys()[0],v.values()[0]);
descriptions = doc.getElementsByTagName('Type');
for desc in descriptions:
if desc.parentNode.tagName == 'SVCManifest':
if desc.hasChildNodes():
desc.firstChild.data = 'esriServiceDefinitionType_Replacement';
f = open(sddraft_filename,'w');
doc.writexml(f);
f.close();
arcpy.AddMessage("Staging service.");
sd_filename = dir_path + os.sep + ags_service + ".sd";
if arcpy.Exists(sd_filename):
arcpy.Delete_management(sd_filename);
arcpy.StageService_server(sddraft_filename,sd_filename);
arcpy.AddMessage("Deploying service to AGS.");
arcpy.UploadServiceDefinition_server(sd_filename,ags_conn);
arcpy.AddMessage("Deployment complete.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment