Hi Shafi,
The tool I use is the stop/start/delete tool in the ArcGIS Server Administration Toolbox http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340 by Kevin Hibma
I haven't finalized/generalize my scripts yet to incorporate this script into a Stop/Replace/Start routine, so I do it in three steps. Run the script to stop, then do my renaming/replacing to the new FGDB that I already did all the magic to, then run the script to start every thing up again. Downtime can be minimized this way.
For my use, I've updated the parameters so I could hard code a default server-name, port, admin account.
Once I enter the correct password, the Service List is populated so I can select what I services I would like to stop/start.
The script (with a little added comments):
'''
This script will stop or start all selected services selected after valid ServerName,
Port, AdminUser, AdminPassword, action (Stop/Start/Delete) are entered, and
services from populated service list are selected
Parameters required:
- ServerName
- Port
- AdminUser
- AdminPassword (SENT IN CLEAR TEXT! and will show in Results tab)
- Stop or Start (or delete....caution with this one...can leave option off ValueList if desired.)
----- Service(s) (multivalue list) ....this will be populated after above are input...
use check boxes to select those you want action to be applied to
Functions:
- gentoken (creates token required for Admin changes)
- stopStartServices
'''
import urllib, urllib2, json
import arcpy
# Functions:
def gentoken(server, port, adminUser, adminPass, expiration=60):
#Re-usable function to get a token required for Admin changes
query_dict = {'username': adminUser,
'password': adminPass,
'expiration': str(expiration),
'client': 'requestip'}
query_string = urllib.urlencode(query_dict)
url = "http://{}:{}/arcgis/admin/generateToken".format(server, port)
token = json.loads(urllib.urlopen(url + "?f=json", query_string).read())
if "token" not in token:
arcpy.AddError(token['messages'])
quit()
else:
return token['token']
def stopStartServices(server, port, adminUser, adminPass, stopStart, serviceList, token=None):
''' Function to stop, start or delete a service.
Requires Admin user/password, as well as server and port (necessary to construct token if one does not exist).
stopStart = Stop|Start|Delete
serviceList = List of services. A service must be in the <name>.<type> notation
If a token exists, you can pass one in for use.
'''
# Get and set the token
if token is None:
token = gentoken(server, port, adminUser, adminPass)
# Getting services from tool validation creates a semicolon delimited list that needs to be broken up
services = serviceList.split(';')
#modify the services(s)
for service in services:
service = urllib.quote(service.encode('utf8'))
op_service_url = "http://{}:{}/arcgis/admin/services/{}/{}?token={}&f=json".format(server, port, service, stopStart, token)
status = urllib2.urlopen(op_service_url, ' ').read()
if 'success' in status:
arcpy.AddMessage(str(service) + " === " + str(stopStart))
else:
arcpy.AddWarning(status)
return
if __name__ == "__main__":
# Gather inputs
server = arcpy.GetParameterAsText(0)
port = arcpy.GetParameterAsText(1)
adminUser = arcpy.GetParameterAsText(2)
adminPass = arcpy.GetParameterAsText(3)
stopStart = arcpy.GetParameter(4)
serviceList = arcpy.GetParameterAsText(5)
stopStartServices(server, port, adminUser, adminPass, stopStart, serviceList)
EDIT: I should add, that my scripts to create my new FGDB and feature datasets, and all the scripts to pre-process the data are so specific to my datasets, that they would not help you. But if you have specific things that you have questions about, I may be able to supply snippets...and if needed, you may need to break them off to new questions.