server py api

2368
10
08-02-2011 11:19 AM
TedCronin
MVP Honored Contributor
WING on Win 7 Is beautiful...

So, how do I import _server_admin remotely, Esri python gods?  I want to rewrite all my AGSSOM subprocess code using the new server admin, but I want to write it locally and access the package.  Can I just add the server in my path using UNC to the server admin api?  Can this even be done, since I would be running 32 bit calling a module written in 64 bit?
Tags (2)
0 Kudos
10 Replies
TedCronin
MVP Honored Contributor
ok, so, it appears _server_admin is installed locally, cool.  You guys thought of everything.
0 Kudos
TedCronin
MVP Honored Contributor
Now if I could just figure out how to stop a service.  Like playing with .url and ._url, though.
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus
Hi Ted, I've messed with some code that I got from esri (thanks Kevin H.) and with the various sample in the 10.1 beta 2 help for the Rest API.  This is overkill for what I will need eventually, but it shows one way to 1) create a token on the fly, 2) list running services within a folder(s), 3) stop those services..<< insert your code here >>...and restarts them.  I'm still cleaning it up a bit, but thought it is worth sharing as is.  I really like the new api, and it eliminates those (mildly annoying) DOS windows from popping up as with the AGSSOC....although I am very greatful to those that worked on and published the AGSSOC (which I still use for 10.0, of course).

all: To use, replace the  server, port (if necessary), username, and password with your local info.  If you want to hardcode a token, you can modify it to do that.  Also, at a minimum, the "dfg_common" is one of my subfolders....you will need to replace that with one of your folders....or modify to eliminate or expand this option.  I hope this helps others.

'''  Sample of using REST API for ArcGIS Server 10.1 to list a service folder,
and stop and start the services...it also creates a token, if needed (can be hard coded). 
I have it set to work with MapServices but it in theory is can work with any.

This script basically lists the services in a folder (dfg_common in sample), stops those that are
currently running, then starts them again.  This is a snippet used for testing
(and not very pratical by itself).  For me, I will  incorporate it with updating 
data and caches.  Most likely it can be cleaned up a bit.

Modified for general sample distribution.  Replace the following with local info:
variable values for   server, port, username (AGS site Admin), and password.  
Also in the line "if folder == "dfg_common" should be modified and/or
replaced for your local site....or can be modified if you don't have
subfolder structure.

This was developed under 10.1 beta 2...not tested with pre-release yet, 
which is due out any day.  I will test in the next couple weeks and 
update/repost if needed.

Thanks to Kevin Hibma who got me on the right track, and additional info was from
samples in the 10.1 beta ArcGIS Rest API help....
Rebecca S.

debugged using Wing Pro IDE
'''

import json, urllib2, urllib
import arcpy

server = "<your server>"
port = "6080"
username = "<AGS site admin username>"
password = "<AGS site admin password>"
baseUrl = "http://{}:{}/arcgis/rest/services".format(server, port)

def gentoken(url, username, password, expiration=60):
    #code to get token from the server
    query_dict = {'username':   username,
                  'password':   password,
                  'expiration': str(expiration),
                  'client':     'requestip'}
    query_string = urllib.urlencode(query_dict)
    
    #arcpy.AddMessage("query_string: " + query_string)
    return json.loads(urllib.urlopen(url + "?f=json", query_string).read())['token']

def getCatalog(usermame, password):
    catalog = json.load(urllib2.urlopen(baseUrl + "/" + "?f=json"))
    print 'ROOT' 
    if "error" in catalog: return
    services = catalog['services']
    for service in services:
        response = json.load(urllib2.urlopen(baseUrl + '/' + service['name'] + '/' + service['type'] + "?f=json"))
        arcpy.AddMessage('  %s %s (%s)' % (service['name'], service['type'], 'ERROR' if "error" in response else 'SUCCESS'))
    folders = catalog['folders']
    for folderName in folders:
        if folderName == "dfg_common":    # I was filtering for a particular folder for testing
            catalog = json.load(urllib2.urlopen(baseUrl + "/" + folderName + "?f=json"))
            arcpy.AddMessage(folderName)
            if "error" in catalog: return
            services = catalog['services']
            for service in services:
                response = json.load(urllib2.urlopen(baseUrl + '/' + service['name'] + '/' + service['type'] + "?f=json"))
                #print '  %s %s (%s)' % (service['name'], service['type'], 'ERROR' if "error" in response else 'SUCCESS')
                #arcpy.AddMessage('  %s %s (%s)' % (service['name'], service['type'], 'ERROR' if "error" in response else 'SUCCESS'))
                serviceName = (service['name'] + "." + service['type'] )
                #arcpy.AddMessage(serviceName)
                msgStop = stopService(server, serviceName, username, password)
                if 'success' in msgStop:
                    arcpy.AddMessage("stopped " + (service['name']))
                else:
                    arcpy.AddMessage( msgStop)
                '''  
                -->  this won't work service if the service wasn't originally started
                --> to begin with....would never make the "services" list
                '''
                msgStart = startService(server, serviceName, username, password)
                if 'success' in msgStop:
                    arcpy.AddMessage("started " + (service['name']))
                else:
                    arcpy.AddMessage( msgStart)
                
def stopService(server, servicename, username, password, token=None, port=6080):
    #code to stop the service. token can be passed in, but code below will envoke the gettoken function
    if token is None:
        token_url = "http://{}:{}/arcgis/admin/generateToken".format(server, port)
        arcpy.AddMessage("token_url: " + token_url)
        token = gentoken(token_url, username, password)
        arcpy.AddMessage("token: " + token)
        
    stop_service_url = "http://{}:{}/arcgis/admin/services/{}/stop?token={}&f=json".format(server, port, servicename, token)
    #arcpy.AddMessage(stop_service_url)
    msg = urllib2.urlopen(stop_service_url, ' ').read() # The ' ' forces POST
    
    return msg

def startService(server, servicename, username, password, token=None, port=6080):
    #code to start the service. token can be passed in, but code below will envoke the gettoken function
    if token is None:
        token_url = "http://{}:{}/arcgis/admin/generateToken".format(server, port)
        token = gentoken(token_url, username, password)
        
    start_service_url = "http://{}:{}/arcgis/admin/services/{}/start?token={}&f=json".format(server, port, servicename, token)
    #arcpy.AddMessage("start_service_url: " + start_service_url)
    msg = urllib2.urlopen(start_service_url, ' ').read() # The ' ' forces POST
    
    return msg 

getCatalog(username, password)
0 Kudos
TedCronin
MVP Honored Contributor
Cool, thank you, very nice, indeed.
0 Kudos
KevinHibma
Esri Regular Contributor
Becky,

Your note in the code about:


                -->  this won't work service if the service wasn't originally started
                --> to begin with....would never make the "services" list


This is because I did the "list services" in the lazy way. I listed the services from the Services Directory, not from the ADMIN part. The Services Directory only shows services which are running, where the Admin shows stopped and started services. With Jason S. help I sometimes hack my little script a bit. If I get a more robust version, I'll post it up - otherwise what you have is nice.

And it should work fine with pre-release. There arent any (to my knowledge) changes to the URL or REST connections.
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus
Thanks for the additional info Kevin.  I figured there was a way to do it but hadn't looked into it yet.  Just happy that the api works so well!

.....so much to test....so little time
0 Kudos
KevinHibma
Esri Regular Contributor
Hey Becky,

I just uploaded the ArcGIS Server Administration Toolkit: http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340
Its a bunch of tools and scripts which you can use to do administrative tasks.

I also enhanced the listing of services- it now happens through the REST Admin, so even if a task is stopped, it'll report it back in the list.
Hope this could be of some use to you.
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus
Hey Becky,

I just uploaded the ArcGIS Server Administration Toolkit: http://www.arcgis.com/home/item.html?id=12dde73e0e784e47818162b4d41ee340
Its a bunch of tools and scripts which you can use to do administrative tasks.

I also enhanced the listing of services- it now happens through the REST Admin, so even if a task is stopped, it'll report it back in the list.
Hope this could be of some use to you.


Thanks Kevin.   I haven't had a chance to check them out yet, but even sight unseen, you get a +++1 from me!!   🙂
0 Kudos
TedCronin
MVP Honored Contributor
Yes, this looks pretty cool as well, so +1 as well.
0 Kudos