Select to view content in your preferred language

Editing a service definition to upload data connection information via arcpy

1230
5
07-17-2023 06:33 AM
Jay_Gregory
Regular Contributor

I'm trying to establish some devops patterns around publishing services in ArcGIS Server without a Windows machine with access to our production enterprise geodatabase from which to publish my Pro documents. 

Currently, from my development machine, I can configure my aprx with my layers pointing to dev tables.  I can then get those aprx folders over to my production (Linux) Server via source control, and run a script with arcpy.mp.updateConnectionProperties to swap all my dev tables with production tables. Then i can go through the documented arcpy process to script map service publishing.  

However, those arpx folders get large, and storing them in source control seems unnecessary.  I'm curious how I might instead save an offline service definition file, pass that through source control, and use arcpy to swap out the enterprise geodatabase connection information, and publish that.  

Any tips or documented workflows someone can point me to?

5 Replies
A_Wyn_Jones
Esri Contributor

@Jay_Gregory That's a nice workflow!

I think you'd be able to move the .aprx file itself as you're repointing the data sources once it's been moved. I'm assuming the large size of the Arpx folders is due to the MyProject.gdb folder? 

Here's the documentation regarding ArcGIS Pro Project folder structure:

https://pro.arcgis.com/en/pro-app/latest/help/projects/what-is-a-project.htm

 

"We've boosted the Anti-Mass Spectrometer to 105 percent. Bit of a gamble, but we need the extra resolution."
0 Kudos
TonyContreras_Frisco_TX
Occasional Contributor III

Instead of doing this, would your workflow support registering the dev database with your production GIS Server, but specify the production database connection to be used on the GIS Server side? I'm not sure if the sd method bypasses this setting, but it is worth a try.

More information here

0 Kudos
rhys-kdrm
New Contributor II

@Jay_Gregory  - I realise this post is a bit old now, but yes you can do what you want.  A Service Definition file is just a zip file.  If you rename it to .zip, you can open it with 7-Zip or similar software.  You won't be able to open the zip file with Winzip though.  I have done what you are asking a few times for similar projects, e.g. when I need to remap datasources when publishing services to a different environment (e.g. DEV to PROD) without actually needing to go through the publishing process, and also when migrating mobile map packages from one environment to another.  You can do all of this via Python.  In your code, you would first stop the service by making a request to the REST endpoint that stops the service.  Then you can unzip the SD file using the py7zr Python package, and locate the .mapx file in the unzipped files, then remap the datasources in the .mapx file.  Then you can zip the files back up into an SD file, copy the files over to the server (using Python), then start the service again by making a request to the REST endpoint that starts the service.  

Jay_Gregory
Regular Contributor

@rhys-kdrm This is a really great option, and I had no idea you could do that with the sd files.  

What I ended up doing was just adding most of an aprx folder structure to my .gitignore file, so just the aprx files are sent through source control, which felt a little cleaner.  But I'll definitely keep this in my back pocket - thanks for the tip.  

0 Kudos
rhys-kdrm
New Contributor II

@Jay_Gregory - You can also change .aprx to .zip and unzip and add the files to source control (same with any ArcGIS file).  With older versions of ArcGIS Pro, my APRXs would occasionally become corrupt, and I didn't want to recreate them, so I put the unzipped files in source control, then when the APRX became corrupt, I was able to unzip the APRX and find what caused it to become corrupt.  Fortunately, ArcGIS Pro is a lot more stable now, so I don't generally unzip the APRXs now.  I do often modify .lyrx files though via code.  For example, if I need to download all of the data from a public feature layer (i.e. a layer within a feature service), I have written Python code that will download the data, import it into a file geodabase, and create a layer file from the REST endpoint for the layer definition, then edit that .lyrx file via code to point to the new file geodatabase.  You could of course do this by writing Python code to create an empty APRX, add the feature layer to the APRX, then share as a layer file.  I sometimes do that, however, in my case, I needed to add some other steps to my script, and I also need to create layer files from secure feature layers, so I am doing this without creating an APRX manually or via code.

0 Kudos