def repairMosaicDatasets(dataDrive):
    
    try:
        
        totalSuccess = True
        
        newPath = OpsServerConfig.getEnvDataRootPath(dataDrive)

        # "Create" RepairMosaicDatasetPaths GP tool Original Path/New Path
        # parameter string
        repairOrigNewPath = ""
        for regName in installOnlyPublishingFolders.keys():
            repairOrigNewPath = repairOrigNewPath + "; " + \
                    installOnlyPublishingFolders[regName] + " " + newPath
        # Remove leading semicolon
        repairOrigNewPath = repairOrigNewPath.replace("; ", "", 1)

        rootSearchPath = newPath
        
        # Get list of all folders ending in ".gdb"
        print
        print "Searching " + rootSearchPath + " looking for file geodatabases..."
        gdbList = findFolderPath(rootSearchPath, "*.gdb", False)
        
        # Ensure that list only contains entries that are local geodatabase
        # (file geodatabase); just in case there happens to be a folder with ".gdb"
        # that is not a geodatabase
        gdbList[:] = [gdb for gdb in gdbList if
                      arcpy.Describe(gdb).workspaceType.upper() == "LOCALDATABASE"]
        
        # Loop through all file geodatabases and repair paths for any non-referenced
        # mosaic datasets
        for gdb in gdbList:
            print
            print "Found file geodatabase: " + gdb
            print "\tChecking for existence of non-referenced mosaic datasets..."
            
            # Get any mosaic datasets in geodatabase
            arcpy.env.workspace = gdb
            mdList = arcpy.ListDatasets("*", "Mosaic")
            
            # Modify list to contain only non-reference mosaic datasets
            mdList[:] = [md for md in mdList if not arcpy.Describe(md).referenced]
            
            if len(mdList) == 0:
                print "\tNone found."
            else:
                print "\tFound " + str(len(mdList)) + " non-referenced mosaic dataset(s)."
            
            for md in mdList:
                print
                print "\tRepairing paths in mosaic dataset '" + md + "'..."
                results = repairMosaicDatasetPaths(md, repairOrigNewPath)
                success = checkResults(results, printMsg)
                if not success:
                    totalSuccess = success
    
    except:
        
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
     
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
     
        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        
    finally:
        print
        print
        if totalSuccess:
            print "Done repairing mosaic dataset paths."
        else:
            print "ERROR occurred during mosaic dataset path repair."
        print
        
        return totalSuccess
def registerDataStores(sharedDataStoreConfig, forInstallPurposeOnly, username,
                       password, dataDrive):

    successRegister = True

    sharedDBConfig = sharedDataStoreConfig
    sharedFolderConfig = sharedDataStoreConfig

    try:
        opsServer = OpsServerConfig.getOpsServerRootPath(dataDrive)
        environmentData = OpsServerConfig.getEnvDataRootPath(dataDrive)
        clientServerName = OpsServerConfig.publishingDBServer
        clientFolderPath = OpsServerConfig.publishingFolder
        clientFolderDSName = OpsServerConfig.dataFolderDStoreName
        installOnlyClientFolders = OpsServerConfig.installOnlyPublishingFolders
        dbsToRegister = OpsServerConfig.databasesToCreate

        print "\n\t-Register Data Stores with Site...\n"

        # ---------------------------------------------------------------------
        # Register Folders by just using paths
        # ---------------------------------------------------------------------
        if regFolders:

            if forInstallPurposeOnly:
                clientFolderPaths = installOnlyClientFolders
            else:
                clientFolderPaths = {clientFolderDSName: clientFolderPath}

            for registrationName, clientFolderPath in clientFolderPaths.items(
            ):

                # Create folder data store item
                if sharedFolderConfig:
                    sharedStr = "shared"
                    dsPath, dsItem = create_shared_folder_item(
                        registrationName, environmentData, serverName)

                else:
                    sharedStr = "replicated"
                    dsPath, dsItem = create_replicated_folder_item(
                        registrationName, clientFolderPath, environmentData)

# Register the data store item
                print "\t\t-Registering folder " + environmentData + " with site as " + registrationName + " (" + sharedStr + ")..."
                success, response = register(serverName,
                                             serverPort,
                                             username,
                                             password,
                                             dsItem,
                                             useSSL=False)
                if success:
                    print "\t\tDone.\n"
                else:
                    successRegister = False
                    print "ERROR:" + str(response)

        ## ---------------------------------------------------------------------
        ## Register databases
        ## (NOTE: Assumes database password is the same as that used to install
        ## arcgis server)
        ## ---------------------------------------------------------------------
        if regDatabases:

            for db in dbsToRegister:

                isManaged = dbsToRegister[db][0]
                registrationName = dbsToRegister[db][1]

                # Do not register managed database if it is for
                # install purposes only
                if isManaged and forInstallPurposeOnly:
                    continueRegister = False
                else:
                    continueRegister = True

                if continueRegister:

                    success, server_db_conn = create_postgresql_db_connection_str(
                        serverName,
                        serverPort,
                        username,
                        password,
                        serverName,
                        db,
                        'sde',
                        password,
                        useSSL=False,
                        token=None,
                        encrypt_dbpassword=False)
                    if not success:
                        successRegister = False
                        print "ERROR: error encountered while creating server database connection string -"
                        print str(server_db_conn)

                    if forInstallPurposeOnly:
                        registrationName = registrationName + "_InstallOnly"

                    managedStr = ""
                    if isManaged:
                        managedStr = "Managed"
                        dsPath, dsItem = create_managed_entdb_item(
                            registrationName, server_db_conn)

                    else:
                        managedStr = "Non-managed"
                        if sharedDBConfig:
                            managedStr = managedStr + " - shared"
                            dsPath, dsItem = create_shared_entdb_item(
                                registrationName, server_db_conn)

                        else:
                            managedStr = managedStr + " - replicated"

                            success, publisher_db_conn = create_postgresql_db_connection_str(
                                serverName,
                                serverPort,
                                username,
                                password,
                                clientServerName,
                                db,
                                'sde',
                                password,
                                useSSL=False,
                                token=None,
                                encrypt_dbpassword=False)
                            if not success:
                                successRegister = False
                                print "ERROR: error encountered while creating publisher database connection string -"
                                print str(publisher_db_conn)

                            dsPath, dsItem = create_replicated_entdb_item(
                                registrationName, publisher_db_conn,
                                server_db_conn)

                    print "\t\t-Registering " + db + " database with site as " + registrationName + " (" + managedStr + ")..."
                    success, response = register(serverName,
                                                 serverPort,
                                                 username,
                                                 password,
                                                 dsItem,
                                                 useSSL=False)
                    if success:
                        print "\t\tDone.\n"
                    else:
                        successRegister = False
                        print "ERROR:" + str(response)

    except:
        successRegister = False

        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]

        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(
            sys.exc_info()[1])

        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"

    finally:
        # Return success flag
        return successRegister
def registerDataStores(sharedDataStoreConfig, forInstallPurposeOnly, username, password, dataDrive):
    
    successRegister = True
    
    sharedDBConfig = sharedDataStoreConfig
    sharedFolderConfig = sharedDataStoreConfig

    try:
        opsServer = OpsServerConfig.getOpsServerRootPath(dataDrive)
	environmentData = OpsServerConfig.getEnvDataRootPath(dataDrive)
        clientServerName = OpsServerConfig.publishingDBServer
        clientFolderPath = OpsServerConfig.publishingFolder
	clientFolderDSName = OpsServerConfig.dataFolderDStoreName
        installOnlyClientFolders = OpsServerConfig.installOnlyPublishingFolders
        dbsToRegister = OpsServerConfig.databasesToCreate
        
        print "\n\t-Register Data Stores with Site...\n"
        
        # ---------------------------------------------------------------------
        # Register Folders by just using paths
        # ---------------------------------------------------------------------
        if regFolders:
            
            if forInstallPurposeOnly:
                clientFolderPaths = installOnlyClientFolders
            else:
                clientFolderPaths = {clientFolderDSName: clientFolderPath}
            
            for registrationName, clientFolderPath in clientFolderPaths.items():
                
		# Create folder data store item
                if sharedFolderConfig:
                    sharedStr = "shared"
		    dsPath, dsItem = create_shared_folder_item(registrationName, environmentData, serverName)
		    
                else:
                    sharedStr = "replicated"
                    dsPath, dsItem = create_replicated_folder_item(registrationName, clientFolderPath, environmentData)

		# Register the data store item
		print "\t\t-Registering folder " + environmentData + " with site as " + registrationName + " (" + sharedStr + ")..."
		success, response = register(serverName, serverPort, username, password, dsItem, useSSL=False)
		if success:
		    print "\t\tDone.\n"
		else:
		    successRegister = False
		    print "ERROR:" + str(response)

        ## ---------------------------------------------------------------------
        ## Register databases
        ## (NOTE: Assumes database password is the same as that used to install
        ## arcgis server)
        ## ---------------------------------------------------------------------
	if regDatabases:
        
            for db in dbsToRegister:
                
                isManaged = dbsToRegister[db][0]
                registrationName = dbsToRegister[db][1]
                
                # Do not register managed database if it is for
                # install purposes only
                if isManaged and forInstallPurposeOnly:
                    continueRegister = False
                else:
                    continueRegister = True
                
                if continueRegister:
                    
		    success, server_db_conn = create_postgresql_db_connection_str(
                                        serverName, serverPort, username, password,
                                        serverName, db, 'sde', password, useSSL=False, token=None, encrypt_dbpassword=False)
		    if not success:
			successRegister = False
			print "ERROR: error encountered while creating server database connection string -"
			print str(server_db_conn)
		    
                    if forInstallPurposeOnly:
                        registrationName = registrationName + "_InstallOnly"
                    
                    managedStr = ""
                    if isManaged:
                        managedStr = "Managed"
			dsPath, dsItem = create_managed_entdb_item(registrationName, server_db_conn)
			
                    else:
                        managedStr = "Non-managed"
                        if sharedDBConfig:
                            managedStr = managedStr + " - shared"
			    dsPath, dsItem = create_shared_entdb_item(registrationName, server_db_conn)
			    
                        else:
                            managedStr = managedStr + " - replicated"
			    
			    success, publisher_db_conn = create_postgresql_db_connection_str(
                                        serverName, serverPort, username, password,
                                        clientServerName, db, 'sde', password, useSSL=False, token=None, encrypt_dbpassword=False)
			    if not success:
				successRegister = False
				print "ERROR: error encountered while creating publisher database connection string -"
				print str(publisher_db_conn)
			
			    dsPath, dsItem = create_replicated_entdb_item(registrationName, publisher_db_conn, server_db_conn)
			    
                    print "\t\t-Registering " + db + " database with site as " + registrationName + " (" + managedStr + ")..."
		    success, response = register(serverName, serverPort, username, password, dsItem, useSSL=False)
		    if success:
			print "\t\tDone.\n"
		    else:
			successRegister = False
			print "ERROR:" + str(response)
		    

    except:
        successRegister = False
        
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
     
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        
        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        
    finally:
        # Return success flag
        return successRegister
Example #4
0
if not doesDriveExist(dataDrive):
    print "\nThe drive specified for parameter <DataDriveLetter>" + " (" + dataDrive + ") is an invalid drive."
    goodParameters = False

if not doesDriveExist(cacheDrive):
    print "\nThe drive specified for parameter <CacheDriveLetter>" + " (" + cacheDrive + ") is an invalid drive."
    goodParameters = False

# Exit script if parameters are not valid.
if not goodParameters:
    print "\nInvalid script parameters. Exiting " + scriptName + "."
    sys.exit(1)


DestinationDBFolder = OpsServerConfig.getDBConnFileRootPath(dataDrive)
DestinationCacheFolder = OpsServerConfig.getCacheRootPath(cacheDrive)
DestinationFolder = OpsServerConfig.getEnvDataRootPath(dataDrive)

modifyOwnershipList = []
printMsg = True
totalCopySuccess = True
totalRepairMDSuccess = True


def copyDataFolders(srcRootPath, destRootPath, ownerAccount):
    copySuccess = True

    # Check if there is available space on destination drive
    # to copy folders.
    freeSpace = getFreeSpace(destRootPath, "GB")
Example #5
0
    "SupportFiles")

sys.path.append(supportFilesPath)

import httplib, urllib, json
import arcpy
import OpsServerConfig
import subprocess
from Utilities import makePath
from Utilities import findFilePath
from Utilities import editFiles

serverName = OpsServerConfig.serverName
serverPort = OpsServerConfig.serverPort
dbsForOpsServer = OpsServerConfig.databasesToCreate
rootPostgreSQLPath = OpsServerConfig.getPostgreSQLRootPath()

siteExistsCheck = True
dbExistsCheck = True


def PreConfigChecks(agsSiteAdminUserName, password):
    try:

        success = True
        msgPrefix = "Failed: "
        print
        print "--Running pre-configuration checks..."
        print

        # ---------------------------------------------------------------------
Example #6
0
if not doesDriveExist(dataDrive):
    print "\nThe drive specified for parameter <DataDriveLetter>" + \
                " (" + dataDrive + ") is an invalid drive."
    goodParameters = False

if not doesDriveExist(cacheDrive):
    print "\nThe drive specified for parameter <CacheDriveLetter>" + \
                " (" + cacheDrive + ") is an invalid drive."
    goodParameters = False

# Exit script if parameters are not valid.
if not goodParameters:
    print "\nInvalid script parameters. Exiting " + scriptName + "."
    sys.exit(1)

DestinationDBFolder = OpsServerConfig.getDBConnFileRootPath(dataDrive)
DestinationCacheFolder = OpsServerConfig.getCacheRootPath(cacheDrive)
DestinationFolder = OpsServerConfig.getEnvDataRootPath(dataDrive)

modifyOwnershipList = []
printMsg = True
totalCopySuccess = True
totalRepairMDSuccess = True


def copyDataFolders(srcRootPath, destRootPath, ownerAccount):
    copySuccess = True

    # Check if there is available space on destination drive
    # to copy folders.
    freeSpace = getFreeSpace(destRootPath, "GB")
    os.path.dirname(os.path.dirname(sys.argv[0])))), "SupportFiles")

sys.path.append(supportFilesPath)

import httplib, urllib, json
import arcpy
import OpsServerConfig
import subprocess
from Utilities import makePath
from Utilities import findFilePath
from Utilities import editFiles

serverName = OpsServerConfig.serverName
serverPort = OpsServerConfig.serverPort
dbsForOpsServer = OpsServerConfig.databasesToCreate
rootPostgreSQLPath = OpsServerConfig.getPostgreSQLRootPath()

siteExistsCheck = True
dbExistsCheck = True

def PreConfigChecks(agsSiteAdminUserName, password):
    try:
    
        success = True
        msgPrefix = "Failed: "
        print
        print "--Running pre-configuration checks..."
        print
        
        # ---------------------------------------------------------------------
        # Check if site already exists
Example #8
0
def createSite(username, password, dataDrive, cacheDrive):

    success = True
    
    try:
        print
        print "--Create ArcGIS Server Site..."
        print
        
        agsCache = OpsServerConfig.getCacheRootPath(cacheDrive)
        
        pathList = ["arcgisserver", "directories"]
        agsData = makePath(serverDrive, pathList)
        
        pathList = ["arcgisserver", "config-store"]
        agsConfig = makePath(serverDrive, pathList)
    
        # Set up required properties for config store
        print "\t-Setting up required properties for config store..."
        configStoreConnection={"connectionString": agsConfig, "type": "FILESYSTEM"}
        print "\tDone."
        print
     
        # Set up paths for server directories             
        jobsDirPath = os.path.join(agsData, "arcgisjobs")
        outputDirPath = os.path.join(agsData, "arcgisoutput")
        systemDirPath = os.path.join(agsData, "arcgissystem")
       
        # Create Python dictionaries representing server directories
        print "\t-Creating Python dictionaries representing server directories"
        print "\t\t(arcgiscache, arcgisjobs, arcgisoutput, arcgissystem)..."
        cacheDir = dict(name = "arcgiscache",physicalPath = agsCache,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 = "")
        print "\tDone."
        print
    
        # Serialize directory information to JSON
        print "\t-Serializing server directory information to JSON..."
        directoriesJSON = json.dumps(dict(directories = [cacheDir, jobsDir, outputDir, systemDir]))      
        print "\tDone."
        print
        
        # Construct URL to create a new site
        print "\t-Constructing URL to create a new site..."
        createNewSiteURL = "/arcgis/admin/createNewSite"
        print "\tDone."
        print
        
        # Set up parameters for the request
        print "\t-Setting up parameters for the request to create new site..."
        params = urllib.urlencode({'username': username, 'password': password, 'configStoreConnection': configStoreConnection, 'directories':directoriesJSON, 'f': 'json'})
        
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        print "\tDone."
        print
        
        # Connect to URL and post parameters
        print "\t-Making request to create new site..."
        httpConn = httplib.HTTPConnection(servername, serverPort)
        httpConn.request("POST", createNewSiteURL, params, headers)
        
        # Read response
        response = httpConn.getresponse()
        if (response.status != 200):
            httpConn.close()
            print "\tERROR: Error while creating the site."
            print
            success = False
        else:
            data = response.read()
            httpConn.close()
            
            # Check that data returned is not an error object
            if not assertJsonSuccess(data):          
                print "\tERROR: Error returned by operation. " + str(data)
                print
            else:
                print "\tSite created successfully."
                print "\tDone."
                print
         
    except:
        success = False
        
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
     
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        
        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        
    finally:
        # Return success flag
        return success    
def createDataStores(agsServerAccount, password, dataDrive):
    success = True
    
    try:
	
	opsServer = OpsServerConfig.getOpsServerRootPath(dataDrive)
	environmentData = OpsServerConfig.getEnvDataRootPath(dataDrive)
	dbConnFileRootPath = OpsServerConfig.getDBConnFileRootPath(dataDrive)
	
        print
        print "--Create Local Data Stores..."
	
        # Create local variables to use for both creating enterprise
        # geodatabases and creating connection files.
        dbPlatform = "POSTGRESQL"
        accountAuthentication = "DATABASE_AUTH"
        dbAdmin = "postgres"    #For PostgreSQL this is the postgres superuser
        gdbAdminName = "sde"    #For PostreSQL this must be 'sde'
	
	# 2/26/2013: Modified to dynamically search for keycodes file instead of
	# hardcoding to specific version of the software.
        #pathList = ["Program Files", "ESRI", "License10.1", "sysgen", "keycodes"]
	#authorizationFile = makePath("C", pathList)
	pathList = ["Program Files", "ESRI"]
	authorizationFile = findFilePath(makePath("C", pathList), "keycodes")
        

        # Create list of database names to create connection file.
	dbsToConnect = []
	for db in dbsToCreate:
	    dbsToConnect.append(db)
		
        # ---------------------------------------------------------------------
        # Create local environment data folders
        # ---------------------------------------------------------------------
        
        if createLocalEnvFolders:
            
            print "\n\t-Creating local environment data folders..."
            
            foldersToCreate = []
            foldersToCreate.append(environmentData)
	    foldersToCreate.append(dbConnFileRootPath)
		
            if not os.path.exists(environmentData):
                for folder in foldersToCreate:
                    print "\t\tCreating folder: " + folder
                    os.makedirs(folder)
                print "\t\tDone."
                print
                
		changeOwnership(opsServer, agsServerAccount)

        # ---------------------------------------------------------------------
        # Create local enterprise databases
        # ---------------------------------------------------------------------
        
        if createEnterpriseGDBs:
            
            print "\n\t-Creating local enterprise geodatabases" + \
                    " (this will take a few minutes)...\n"
	    
            for db in dbsToCreate:
                print "\t\tCreating geodatabase '" + db + "'..."
                arcpy.CreateEnterpriseGeodatabase_management(dbPlatform,
                                                            "localhost",
                                                            db,
                                                            accountAuthentication,
                                                            dbAdmin,
                                                            password,
                                                            "",
                                                            gdbAdminName,
                                                            password,
                                                            "",
                                                            authorizationFile)
		print "\t\tDone.\n"

        # ---------------------------------------------------------------------
        # Update PostgreSQL connection file to allow remote connections
        #   to environment databases
        # ---------------------------------------------------------------------
        
        if updatePostgreSQLConnectionFile:
	    connectionFilePath = findFilePath(rootPostgreSQLPath, "pg_hba.conf")
            
            print "\n\t-Updating PostgreSQL connection file to allow remote" + \
                    " connections to databases..."
            print "\t\tFile: " + connectionFilePath 
            	    
	    # Create list of PostgreSQL connection entries
	    postgreSQLConnEntries = []
	    postgreSQLConnEntries.append("host all all 0.0.0.0/0 md5")	#IPv4
	    postgreSQLConnEntries.append("host all all ::/0 md5")  	#IPv6
		
	    # Determine if connection entry already exists in file
	    for postgreSQLConnEntry in postgreSQLConnEntries:
		if findInFile(connectionFilePath, postgreSQLConnEntry):
		    #Entry already exists in file so remove entry from list
		    postgreSQLConnEntries.remove(postgreSQLConnEntry)	    

	    # Add connection entries
	    if len(postgreSQLConnEntries) > 0:
                hbaFile = open(connectionFilePath, 'a')
                for postgreSQLConnEntry in postgreSQLConnEntries:
                    hbaFile.write("{}\n".format(postgreSQLConnEntry))
                hbaFile.close()

                # Reload config file
                print "\n\t-Reloading connection file..."
		exeFile = findFilePath(rootPostgreSQLPath, "pg_ctl.exe")
		confFolder = os.path.dirname(connectionFilePath)
		exeCommand = "”" + exeFile + "” -D “" + confFolder + "” reload"
		os.popen(exeCommand)		
		
            print "\t\tDone."

        # ---------------------------------------------------------------------
        # Create SDE connection files to environment geodatabases
        # ---------------------------------------------------------------------
        
        if createSDEConnectionFiles:
            
            print "\n\t-Creating SDE connection files..."
            
            for db in dbsToCreate:
		outFile = dbsToCreate[db][1] + ".sde"
		
		# Set output folder location
		outFolder = dbConnFileRootPath
		sdeFilePath = os.path.join(outFolder, outFile)
		
		# If SDE connection file already exists, delete it.
		if os.path.exists(sdeFilePath):
		    print "\t\t* Deleting existing file " + sdeFilePath
		    os.remove(sdeFilePath)
		    print "\t\tRe-creating connection file " + sdeFilePath
		else:
		    print "\t\tCreating connection file " + sdeFilePath
		
		arcpy.CreateDatabaseConnection_management(outFolder,
							  outFile,
							  dbPlatform,
							  servername,
							  accountAuthentication,
							  gdbAdminName,
							  password,
							  "SAVE_USERNAME",
							  db.lower(),
							  "#",
							  "TRANSACTIONAL",
							  "sde.DEFAULT",
							  "#")
		    
		print "\t\tDone.\n"
		# Change ownership of sde file
		changeOwnership(sdeFilePath, agsServerAccount)

    except:
        success = False
        
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
     
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages() + "\n"
        
        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        print msgs
        
    finally:
        # Return success flag
        return success
def repairMosaicDatasets(dataDrive):

    try:

        totalSuccess = True

        newPath = OpsServerConfig.getEnvDataRootPath(dataDrive)

        # "Create" RepairMosaicDatasetPaths GP tool Original Path/New Path
        # parameter string
        repairOrigNewPath = ""
        for regName in installOnlyPublishingFolders.keys():
            repairOrigNewPath = repairOrigNewPath + "; " + \
                    installOnlyPublishingFolders[regName] + " " + newPath
        # Remove leading semicolon
        repairOrigNewPath = repairOrigNewPath.replace("; ", "", 1)

        rootSearchPath = newPath

        # Get list of all folders ending in ".gdb"
        print
        print "Searching " + rootSearchPath + " looking for file geodatabases..."
        gdbList = findFolderPath(rootSearchPath, "*.gdb", False)

        # Ensure that list only contains entries that are local geodatabase
        # (file geodatabase); just in case there happens to be a folder with ".gdb"
        # that is not a geodatabase
        gdbList[:] = [
            gdb for gdb in gdbList
            if arcpy.Describe(gdb).workspaceType.upper() == "LOCALDATABASE"
        ]

        # Loop through all file geodatabases and repair paths for any non-referenced
        # mosaic datasets
        for gdb in gdbList:
            print
            print "Found file geodatabase: " + gdb
            print "\tChecking for existence of non-referenced mosaic datasets..."

            # Get any mosaic datasets in geodatabase
            arcpy.env.workspace = gdb
            mdList = arcpy.ListDatasets("*", "Mosaic")

            # Modify list to contain only non-reference mosaic datasets
            mdList[:] = [
                md for md in mdList if not arcpy.Describe(md).referenced
            ]

            if len(mdList) == 0:
                print "\tNone found."
            else:
                print "\tFound " + str(
                    len(mdList)) + " non-referenced mosaic dataset(s)."

            for md in mdList:
                print
                print "\tRepairing paths in mosaic dataset '" + md + "'..."
                results = repairMosaicDatasetPaths(md, repairOrigNewPath)
                success = checkResults(results, printMsg)
                if not success:
                    totalSuccess = success

    except:

        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]

        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(
            sys.exc_info()[1])

        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"

    finally:
        print
        print
        if totalSuccess:
            print "Done repairing mosaic dataset paths."
        else:
            print "ERROR occurred during mosaic dataset path repair."
        print

        return totalSuccess
def createDataStores(agsServerAccount, password, dataDrive):
    success = True

    try:

        opsServer = OpsServerConfig.getOpsServerRootPath(dataDrive)
        environmentData = OpsServerConfig.getEnvDataRootPath(dataDrive)
        dbConnFileRootPath = OpsServerConfig.getDBConnFileRootPath(dataDrive)

        print
        print "--Create Local Data Stores..."

        # Create local variables to use for both creating enterprise
        # geodatabases and creating connection files.
        dbPlatform = "POSTGRESQL"
        accountAuthentication = "DATABASE_AUTH"
        dbAdmin = "postgres"  #For PostgreSQL this is the postgres superuser
        gdbAdminName = "sde"  #For PostreSQL this must be 'sde'

        # 2/26/2013: Modified to dynamically search for keycodes file instead of
        # hardcoding to specific version of the software.
        #pathList = ["Program Files", "ESRI", "License10.1", "sysgen", "keycodes"]
        #authorizationFile = makePath("C", pathList)
        pathList = ["Program Files", "ESRI"]
        authorizationFile = findFilePath(makePath("C", pathList), "keycodes")

        # Create list of database names to create connection file.
        dbsToConnect = []
        for db in dbsToCreate:
            dbsToConnect.append(db)

    # ---------------------------------------------------------------------
    # Create local environment data folders
    # ---------------------------------------------------------------------

        if createLocalEnvFolders:

            print "\n\t-Creating local environment data folders..."

            foldersToCreate = []
            foldersToCreate.append(environmentData)
            foldersToCreate.append(dbConnFileRootPath)

            if not os.path.exists(environmentData):
                for folder in foldersToCreate:
                    print "\t\tCreating folder: " + folder
                    os.makedirs(folder)
                print "\t\tDone."
                print

                changeOwnership(opsServer, agsServerAccount)

    # ---------------------------------------------------------------------
    # Create local enterprise databases
    # ---------------------------------------------------------------------

        if createEnterpriseGDBs:

            print "\n\t-Creating local enterprise geodatabases" + \
                    " (this will take a few minutes)...\n"

            for db in dbsToCreate:
                print "\t\tCreating geodatabase '" + db + "'..."
                arcpy.CreateEnterpriseGeodatabase_management(
                    dbPlatform, "localhost", db, accountAuthentication,
                    dbAdmin, password, "", gdbAdminName, password, "",
                    authorizationFile)
                print "\t\tDone.\n"

    # ---------------------------------------------------------------------
    # Update PostgreSQL connection file to allow remote connections
    #   to environment databases
    # ---------------------------------------------------------------------

        if updatePostgreSQLConnectionFile:
            connectionFilePath = findFilePath(rootPostgreSQLPath,
                                              "pg_hba.conf")

            print "\n\t-Updating PostgreSQL connection file to allow remote" + \
                    " connections to databases..."
            print "\t\tFile: " + connectionFilePath

            # Create list of PostgreSQL connection entries
            postgreSQLConnEntries = []
            postgreSQLConnEntries.append("host all all 0.0.0.0/0 md5")  #IPv4
            postgreSQLConnEntries.append("host all all ::/0 md5")  #IPv6

            # Determine if connection entry already exists in file
            for postgreSQLConnEntry in postgreSQLConnEntries:
                if findInFile(connectionFilePath, postgreSQLConnEntry):
                    #Entry already exists in file so remove entry from list
                    postgreSQLConnEntries.remove(postgreSQLConnEntry)

            # Add connection entries
            if len(postgreSQLConnEntries) > 0:
                hbaFile = open(connectionFilePath, 'a')
                for postgreSQLConnEntry in postgreSQLConnEntries:
                    hbaFile.write("{}\n".format(postgreSQLConnEntry))
                hbaFile.close()

                # Reload config file
                print "\n\t-Reloading connection file..."
                exeFile = findFilePath(rootPostgreSQLPath, "pg_ctl.exe")
                confFolder = os.path.dirname(connectionFilePath)
                exeCommand = "”" + exeFile + "” -D “" + confFolder + "” reload"
                os.popen(exeCommand)

            print "\t\tDone."

    # ---------------------------------------------------------------------
    # Create SDE connection files to environment geodatabases
    # ---------------------------------------------------------------------

        if createSDEConnectionFiles:

            print "\n\t-Creating SDE connection files..."

            for db in dbsToCreate:
                outFile = dbsToCreate[db][1] + ".sde"

                # Set output folder location
                outFolder = dbConnFileRootPath
                sdeFilePath = os.path.join(outFolder, outFile)

                # If SDE connection file already exists, delete it.
                if os.path.exists(sdeFilePath):
                    print "\t\t* Deleting existing file " + sdeFilePath
                    os.remove(sdeFilePath)
                    print "\t\tRe-creating connection file " + sdeFilePath
                else:
                    print "\t\tCreating connection file " + sdeFilePath

                arcpy.CreateDatabaseConnection_management(
                    outFolder, outFile, dbPlatform, servername,
                    accountAuthentication, gdbAdminName, password,
                    "SAVE_USERNAME", db.lower(), "#", "TRANSACTIONAL",
                    "sde.DEFAULT", "#")

                print "\t\tDone.\n"
                # Change ownership of sde file
                changeOwnership(sdeFilePath, agsServerAccount)

    except:
        success = False

        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]

        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(
            sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages() + "\n"

        # Print Python error messages for use in Python / Python Window
        print
        print "***** ERROR ENCOUNTERED *****"
        print pymsg + "\n"
        print msgs

    finally:
        # Return success flag
        return success