Example: Stop or start all services in a folder
This example reads through a specified GIS server folder and stops or starts all services contained therein based on a user-supplied parameter. If the user attempts to start a service that is already started, or stop a service that is already stopped, the script proceeds to the next service in the folder.
When running this script, you are asked to provide a user name and password that has administrative permission to ArcGIS Server. With this information, a token is retrieved that allows you to make web service calls to read the folder and stop and start the services.
You are also asked to provide the server name, as well as the folder whose services should be stopped or started. You can supply root for the root folder, although be aware that iterating through the root folder will affect the preconfigured geometry and search services.
A final parameter asks whether you want to stop or start all the services in the folder. The web service calls to stop and start a service using the ArcGIS REST API are very similar, so it's not difficult to allow both actions in this script.
When the initial request is made for the list of services in the folder, the response comes back in JavaScript Object Notation (JSON). Python's json.loads() function converts the JSON to a Python object through which you can iterate.
Although this script has some error checking and reporting, other checks have been left out for brevity.
# Demonstrates how to stop or start all services in a folder
# For Http calls
import httplib, urllib, json
# For system tools
import sys
# For reading passwords without echoing
import getpass
# Defines the entry point into the script
def main(argv=None):
# Print some info
print
print "This tool is a sample script that stops or starts all services in a folder."
print
# Ask for admin/publisher user name and password
username = raw_input("Enter user name: ")
password = getpass.getpass("Enter password: ")
# Ask for server name
serverName = raw_input("Enter server name: ")
serverPort = 6080
folder = raw_input("Enter the folder name or ROOT for the root location: ")
stopOrStart = raw_input("Enter whether you want to START or STOP all services: ")
# Check to make sure stop/start parameter is a valid value
if str.upper(stopOrStart) != "START" and str.upper(stopOrStart) != "STOP":
print "Invalid STOP/START parameter entered"
return
# Get a token
token = getToken(username, password, serverName, serverPort)
if token == "":
print "Could not generate a token with the username and password provided."
return
# Construct URL to read folder
if str.upper(folder) == "ROOT":
folder = ""
else:
folder += "/"
folderURL = "/arcgis/admin/services/" + folder
# This request only needs the token and the response formatting parameter
params = urllib.urlencode({'token': token, 'f': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request("POST", folderURL, params, headers)
# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print "Could not read folder information."
return
else:
data = response.read()
# Check that data returned is not an error object
if not assertJsonSuccess(data):
print "Error when reading folder information. " + str(data)
else:
print "Processed folder information successfully. Now processing services..."
# Deserialize response into Python object
dataObj = json.loads(data)
httpConn.close()
# Loop through each service in the folder and stop or start it
for item in dataObj['services']:
fullSvcName = item['serviceName'] + "." + item['type']
# Construct URL to stop or start service, then make the request
stopOrStartURL = "/arcgis/admin/services/" + folder + fullSvcName + "/" + stopOrStart
httpConn.request("POST", stopOrStartURL, params, headers)
# Read stop or start response
stopStartResponse = httpConn.getresponse()
if (stopStartResponse.status != 200):
httpConn.close()
print "Error while executing stop or start. Please check the URL and try again."
return
else:
stopStartData = stopStartResponse.read()
# Check that data returned is not an error object
if not assertJsonSuccess(stopStartData):
if str.upper(stopOrStart) == "START":
print "Error returned when starting service " + fullSvcName + "."
else:
print "Error returned when stopping service " + fullSvcName + "."
print str(stopStartData)
else:
print "Service " + fullSvcName + " processed successfully."
httpConn.close()
return
# A function to generate a token given username, password and the adminURL.
def getToken(username, password, serverName, serverPort):
# Token URL is typically http://server[:port]/arcgis/admin/generateToken
tokenURL = "/arcgis/admin/generateToken"
params = urllib.urlencode({'username': username, 'password': password, 'client': 'requestip', 'f': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
# Connect to URL and post parameters
httpConn = httplib.HTTPConnection(serverName, serverPort)
httpConn.request("POST", tokenURL, params, headers)
# Read response
response = httpConn.getresponse()
if (response.status != 200):
httpConn.close()
print "Error while fetching tokens from admin URL. Please check the URL and try again."
return
else:
data = response.read()
httpConn.close()
# Check that data returned is not an error object
if not assertJsonSuccess(data):
return
# Extract the token from it
token = json.loads(data)
return token['token']
# A function that checks that the input JSON object
# is not an error object.
def assertJsonSuccess(data):
obj = json.loads(data)
if 'status' in obj and obj['status'] == "error":
print "Error: JSON object returns an error. " + str(obj)
return False
else:
return True
# Script start
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))