Created
October 17, 2013 18:22
-
-
Save brendancol/7029764 to your computer and use it in GitHub Desktop.
SOE Utils (taken from ArcGIS Server Admin Toolbox) requires requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This script will upload and register an SOE | |
==Inputs== | |
ServerName | |
Port | |
AdminUser | |
AdminPassword (sent in clear text) | |
SOE (file) | |
''' | |
import sys | |
import os | |
import json | |
import urllib | |
import urllib2 | |
import requests | |
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: | |
print token['messages'] | |
quit() | |
else: | |
return token['token'] | |
def upload(server, port, adminUser, adminPass, fileinput, token=None): | |
''' Function to upload a file to the REST Admin | |
Requires Admin user/password, as well as server and port (necessary to construct token if one does not exist). | |
fileinput = path to file to upload. (file upload will be done in binary) | |
NOTE: Dependency on 3rd party module "requests" for file upload | |
> http://docs.python-requests.org/en/latest/index.html | |
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) | |
# Properties used to upload a file using the request module | |
files = {"itemFile": open(fileinput, 'rb')} | |
files["f"] = "json" | |
URL='http://{}:{}/arcgis/admin/uploads/upload'.format(server, port) | |
response = requests.post(URL+"?f=json&token="+token, files=files); | |
print response.text | |
json_response = json.loads(response.text) | |
if "item" in json_response: | |
itemID = json_response["item"]["itemID"] | |
messages = registerSOE(server, port, adminUser, adminPass, itemID, token) | |
if messages and "is already registered" in messages[0]: | |
updateSOE(server, port, adminUser, adminPass, itemID, token) | |
else: | |
print json_response | |
return | |
def registerSOE(server, port, adminUser, adminPass, itemID, token=None): | |
''' Function to upload a file to the REST Admin | |
Requires Admin user/password, as well as server and port (necessary to construct token if one does not exist). | |
itemID = itemID of an uploaded SOE the server will register. | |
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) | |
# Registration of an SOE only requires an itemID. The single item dictionary is encoded in place | |
SOE_encode = urllib.urlencode({"id":itemID}) | |
register = "http://{}:{}/arcgis/admin/services/types/extensions/register?token={}&f=json".format(server, port, token) | |
status = urllib2.urlopen(register, SOE_encode).read() | |
resultingJson = json.loads(status) | |
if 'success' in status: | |
print "Succesfully registered SOE with {}".format(server) | |
elif 'messages' in resultingJson: | |
print resultingJson.get('messages') | |
return resultingJson.get('messages') | |
else: | |
'Failed to register SOE and not sure why? Charles anyone...' | |
return None | |
def updateSOE(server, port, adminUser, adminPass, itemID, token=None): | |
''' Function to update a file to the REST Admin | |
Requires Admin user/password, as well as server and port (necessary to construct token if one does not exist). | |
itemID = itemID of an uploaded SOE the server will register. | |
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) | |
# Registration of an SOE only requires an itemID. The single item dictionary is encoded in place | |
SOE_encode = urllib.urlencode({"id":itemID}) | |
register = "http://{}:{}/arcgis/admin/services/types/extensions/update?token={}&f=json".format(server, port, token) | |
status = urllib2.urlopen(register, SOE_encode).read() | |
resultingJson = json.loads(status) | |
if 'success' in status: | |
print "Succesfully updated SOE with {}".format(server) | |
elif 'messages' in resultingJson: | |
print resultingJson.get('messages') | |
return resultingJson.get('messages') | |
else: | |
'Failed to register SOE and not sure why? Charles anyone...' | |
return None | |
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) | |
#modify the services(s) | |
for service in serviceList: | |
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: | |
print str(service) + " === " + str(stopStart) | |
else: | |
print 'WARNING: {}'.format(status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment