# # example: http://server.arcgis.com/en/server/latest/administer/linux/example-create-a-site.htm # Note the example is for ArcGIS Server not Portal but the concepts are the same. # from __future__ import print_function import sys import os, time import requests import json # HTTPS calls will generate warnings about the self-signed certificate if you delete this. from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) class arcgis(object): def __init__(self,arcgis_home,user,passwd): self.arcgis_home = arcgis_home self.user = user self.passwd = passwd self.hostname = "" self.java_home = "" def validateEnv(self): ''' Validate the needed environment variables ''' self.hostname = os.environ.get("ARCGIS_HOSTNAME","") if self.hostname == "": print("The Environment variable ARCGIS_HOSTNAME MUST be set, for example: gisportal.xxxxx.com") return False self.java_home = os.environ.get("JAVA_HOME","") if self.java_home == "": print("Warning : JAVA_HOME not be set, set to $ARCGIS_HOIME/portal/framwork/runtime/jre") os.environ['JAVA_HOME']="$ARCGIS_HOME/portal/framework/runtime/jre" return True def status_check(self): uri = "https://%s:7443/arcgis/portaladmin/Status" % self.hostname parameters = { "f" : "json" } try: response = requests.post( uri, data=parameters, timeout=10, verify=False ) rj = json.loads(response.text) status = rj["status"] print("portal is in servering with status %s"%(status)) except Exception as e: print("Failed to request the server %s with reason %s"%(uri,e)) return False return True def create_site(self): defaultdir = "%s/portal/usr" % self.arcgis_home content_store_path = os.path.join(defaultdir,"content") content_store = { "type" : "FileSystem", "provider" : "FileSystem", "connectionString" : content_store_path } content_storeJSON = json.dumps(content_store) parameters = { "username" : self.user, "password" : self.passwd, "fullname" : "APAAS CUSTOMER", "email" : "user@bd-apaas.com", "description" : "Administrator account created for APAAS", "securityQuestionIdx" : 1, "securityQuestionAns" : "Nothing", "contentStore" : content_storeJSON, "f" : "json" } uri = "https://%s:7443/arcgis/portaladmin/createNewSite" % self.hostname response = None timeout = False try: response = requests.post( uri, data=parameters, timeout=500, verify=False ) if response and response.status_code != 200: print("Server not available; code ", response.status_code) err = json.loads(response.text).get('error',None) if err is not None: print("Some error happended when create the new site: \n %s \n"%(err)) return False except Exception as e: print("Error when post the requrest with reason :%s"%e) timeout = True return True # ------------------------------------------------------------------------ if __name__ == "__main__": home = os.environ.get("ARCGIS_HOME","/arcgis") user = os.environ.get("PORTAL_USER","portaladmin") passwd = os.environ.get("PORTAL_PASSWORD","Aa123456") ag = arcgis(home,user,passwd) if ag.validateEnv(): if user == "portaladmin" or passwd == "Aa123456": print("Warning: default Parameter will be used when create new site user: %s and password: %s"%(user,passwd)) if ag.create_site(): print("Create the new site Successfully") sys.exit(0) print("Failed to Create a new site") sys.exit(1)