I have script that publishes a service to Arc server that is federated with portal. I posted the two functions that do most of the work. I hope it helps.
def build_connection(locator_specs, working_dir):
"""
builds AGS connection
:param working_dir: script working path
:param locator_specs: instance of Specs class
:return: AGS connection
"""
out_folder_path = 'ags_connections'
clean_dir(out_folder_path, working_dir)
use_arcgis_desktop_staging_folder = False
staging_folder_path = out_folder_path
server_info = locator_specs.get_arc_gis_servers()
server = server_info.server
out_name = '{}.ags'.format(server)
server_url = 'https://{}:6443/arcgis/admin'.format(server)
print server_url
username = server_info.admin_user
password = xxxx.security.get_password(server, username)
logger.info(xxxx.indent('Making sever connection: {}'.format(out_name), 2))
arcpy.mapping.CreateGISServerConnectionFile("ADMINISTER_GIS_SERVICES",
out_folder_path,
out_name,
server_url,
"ARCGIS_SERVER",
use_arcgis_desktop_staging_folder,
staging_folder_path,
username,
password,
"SAVE_USERNAME")
return os.path.join(out_folder_path, out_name)
def publish_service(gis_server_connection_file, working_dir, location_dir):
"""
creates sd file and publishes it to server
:param gis_server_connection_file: AGS connection
:param working_dir: script directory
:return: None
"""
gis_server_connection_file = os.path.join(working_dir, gis_server_connection_file)
out_sd_folder = 'sd'
clean_dir(out_sd_folder, working_dir)
service_name = 'CW_Composite'
locator_path = os.path.join(location_dir, service_name)
sddraft_file = os.path.join(working_dir, out_sd_folder, 'cw_composite.sddraft')
sd_file = os.path.join(working_dir, out_sd_folder, 'cw_composite.sd')
summary = 'CEA address locator'
tags = 'address, locator, geocode'
folder_name = 'Locator'
logger.info(xxxx.indent('Building SDDraft: {}'.format(sddraft_file), 2))
analyze_messages = arcpy.CreateGeocodeSDDraft(locator_path, sddraft_file, service_name,
connection_file_path=gis_server_connection_file,
summary=summary,
tags=tags,
max_result_size=20,
max_batch_size=500,
suggested_batch_size=150,
folder_name=folder_name,
copy_data_to_server=True)
if analyze_messages['errors'] == {}:
logger.info(xxxx.indent('Staging service {}:'.format(sd_file), 2))
arcpy.server.StageService(sddraft_file, sd_file)
logger.info(xxxx.indent('Uploading sd: {}'.format(sd_file), 2))
arcpy.server.UploadServiceDefinition(sd_file, gis_server_connection_file)
logger.info(xxxx.indent(arcpy.GetMessages(), 2))
logger.info(xxxx.indent('Success: service published', 2))
else:
logger.error(xxxx.indent(analyze_messages['errors'], 2))
logger.error(xxxx.indent('Failure: service was not published'))