Skip To Content

Ejemplo: Crear un sitio

En este ejemplo se crea un sitio de ArcGIS Server basándose en valores provistos por el usuario para la cuenta del administrador principal del sitio, la ubicación del almacenamiento de configuración y la ubicación de la raíz de los directorios del servidor. Estos son los mismos valores que un usuario de forma manual introduce cuando se solicita la creación de un sitio en ArcGIS Server Manager.

La operación createNewSite en la API REST de ArcGIS se puede invocar con menos código, introduciendo simplemente el nombre de usuario y la contraseña deseados para el administrador del sitio principal, así como el nombre del servidor y el número de puerto. El ejemplo a continuación agrega la flexibilidad para definir la ubicación del almacenamiento de configuración deseado y la ubicación del directorio del servidor. La mayor parte del código está relacionado con la creación de directorios.

Al definir los directorios del servidor mediante programación, se debe pasar una gran cantidad de notación de objetos de JavaScript (JSON) que detalla el directorio propiedades a la operación createNewSite. En el siguiente ejemplo, el directorio propiedades están configurados mediante los diccionarios de Python. A continuación, estos diccionarios se serializan en JSON usando la función json.dumps() de Python. Esto ayuda a mantener la legibilidad del ejemplo.

El directorio propiedades utilizadas a continuación son los mismos que el valor predeterminado aplicado por el Administrador cuando se crea un sitio. Puede modificarlos a su gusto.

# Demonstrates how to create a new site

# For Http calls
import httplib, urllib, json

# For system tools
import sys, os

# For reading passwords without echoing
import getpass

# Defines the entry point into the script
def main(argv=None):
  
    # Ask for admin/publisher user name and password
    username = raw_input("Enter desired primary site administrator name: ")
    password = getpass.getpass("Enter desired primary site administrator password: ")

    # Ask for server name
    serverName = raw_input("Enter server name: ")
    serverPort = 6080 

    # Ask for config store and root server directory paths
    configStorePath = raw_input("Enter config store path: ")
    rootDirPath = raw_input("Enter root server directory path: ")
    
    # Set up required properties for config store
    configStoreConnection={"connectionString": configStorePath, "type": "FILESYSTEM"}
 
    # Set up paths for server directories             
    cacheDirPath = os.path.join(rootDirPath, "arcgiscache")
    jobsDirPath = os.path.join(rootDirPath, "arcgisjobs")
    outputDirPath = os.path.join(rootDirPath, "arcgisoutput")
    systemDirPath = os.path.join(rootDirPath, "arcgissystem")
   
    # Create Python dictionaries representing server directories
    cacheDir = dict(name = "arcgiscache",physicalPath = cacheDirPath,directoryType = "CACHE",cleanupMode = "NONE",maxFileAge = 0,description = "Stores tile caches used by map, globe, and image services for rapid performance.", virtualPath = "")    
    jobsDir = dict(name = "arcgisjobs",physicalPath = jobsDirPath, directoryType = "JOBS",cleanupMode = "TIME_ELAPSED_SINCE_LAST_MODIFIED",maxFileAge = 360,description = "Stores results and other information from geoprocessing services.", virtualPath = "")
    outputDir = dict(name = "arcgisoutput",physicalPath = outputDirPath,directoryType = "OUTPUT",cleanupMode = "TIME_ELAPSED_SINCE_LAST_MODIFIED",maxFileAge = 10,description = "Stores various information generated by services, such as map images.", virtualPath = "")
    systemDir = dict(name = "arcgissystem",physicalPath = systemDirPath,directoryType = "SYSTEM",cleanupMode = "NONE",maxFileAge = 0,description = "Stores files that are used internally by the GIS server.", virtualPath = "")
 
    # Serialize directory information to JSON    
    directoriesJSON = json.dumps(dict(directories = [cacheDir, jobsDir, outputDir, systemDir]))      
  
    # Construct URL to create a new site
    createNewSiteURL = "/arcgis/admin/createNewSite"
    
    # Set up parameters for the request
    params = urllib.urlencode({'username': username, 'password': password, 'configStoreConnection': 

configStoreConnection, 'directories':directoriesJSON, '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", createNewSiteURL, params, headers)
    
    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        print "Error while creating the site."
        return
    else:
        data = response.read()
        httpConn.close()
        
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):          
            print "Error returned by operation. " + str(data)
        else:
            print "Site created successfully"
     
        return
    

# 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:]))