This WLST script "VASDomainTemplateFinalV1.py" reads domain information from "VASDomainInfoV1.txt" and creates weblogic server domain template in the directory as mentioned in "VASDomainInfoV1.txt".
配置过程:
1)This python script reads the domain related information from "VASDomainInfoV1.txt" and creates the "Weblogic Domain" template(jar file) as mentioned in the "VASDomainInfoV1.txt".
2) Note this script assumes WL_HOME is set "c:\bea\wlserver_10.0" and location of default weblogic domain template is "C:\bea\wlserver_10.0\common\templates\domains\wls.jar"
执行过程:
1) set the classpath by executing "WL_HOME/server/bin/setWLSEnv.cmd" or "WL_HOME/server/bin/setWLSEnv.sh" for Unix.
2)Save the "VASDomainInfoV1.txt" file in any directory.
3)Modify the parameter values in "VASDomainInfoV1.txt" as per requirement. Please check out the sample info mentioned in the same file.
4)Change the directory location of "VASDomainInfoV1.txt" in line 72 of "VASDomainTemplateFinalV1.py" as per the location you saved in above step.
5)execute the script as below
java weblogic.WLST VASDomainTemplateFinalV1.py
VASDomainInfoV1.txt:
#VASVASVAS
UserPassword::<Weblogic User Password>
AdminServerName::<Admin Server Name>
AdminServerListenAddress::<Admin Server IP Address>
AdminServerPort::<Admin Server Port>
TemplateDestinationDirectory::<Location where you want to store template>
TemplateName::<Domain Template Name>
Clusters::<Cluster Name 1>,<Cluster Name 2>
Machines::<Machine Name 1>,<Machine Name 2>
ManagedServers::"<Cluster Name1>,<Machine Name 1>,<Managed Server Name>,<ManagedServerIP:Port>;<Cluster Name1>,<Machine Name 1>,<Managed Server Name>,<ManagedServerIP:Port>"
#Sample DomainInfo File
#UserPassword::weblogic
#AdminServerName::VASAdminServer
#AdminServerListenAddress::127.0.0.1
#AdminServerPort::9999
#TemplateDestinationDirectory::C:\student\course_wlp_admin\work\templates\
#TemplateName::VASWLSTCluster
#Clusters::VASCluster1,VASCluster2
#Machines::VASMachine1,VASMachine2
#ManagedServers::"VASCluster1,VASMachine1,VASM1,127.0.0.1:9998;VASCluster2,VASMachine2,VASM2,127.0.0.1:9997"
#Please don't forget to mention double quotes for managed servers"
VASDomainTemplateFinalV1.py:
#=======================================================================================
# VASVASVAS
# Author : Vijay Bheemineni
# Creation Date : 11/27/2007
# This is an example of WLST offline domain template creation script. This example uses the Basic WebLogic
# Domain template to create a clustered domain using WLST offline configuration.
# All the values( Example : password,admin server name etc) are mentioned in separate note pad file. This
# "python" script read values from the file and creates the weblogic resources. Below is the format of the
# file(VASDomainInfo.txt).
# Note : This script assumes "WL_HOME" is set to "c:\bea\wlserver_10.0" and location of default weblogic
# template is "C:\bea\wlserver_10.0\common\templates\domains\wls.jar"
#----------------------------------------------------------------------------------------------------------------------------------
#Userpassword::weblogic
#AdminServerName::VASAdminServer
#AdminServerListenAddress::127.0.0.1
#AdminServerPort::9999
#templateDestinationDirectory::C:\student\course_wlp_admin\work\templates\
#templateName::VASWLSTCluster
#clusters::VASCluster1,VASCluster2
#machines::VASMachine1,VASMachine2
#managedServers::"VASCluster1,VASMachine1,VASM1,127.0.0.1:9998;VASCluster2,VASMachine2,VASM2,127.0.0.1:9997"
#----------------------------------------------------------------------------------------------------------------------------------
# Please store the above lines in notepad file as "VASDomainInfo.txt" and domain template is created
# using values mentioned in this file.
#
#
# Usage:
# 1) first set the path. This can be done by executing "WL_HOME\common\bin\setWLSEnv" script
# 2)
# <Windows> = java weblogic.WLST VASDomainTemplate.py
# <Unix> = java weblogic.WLST VASDomainTemplate.py>
#=======================================================================================
from java.util import HashMap
import os
import re
#===============================================================================================================
# defining Global Variables
#===============================================================================================================
templateLocation = ""
password=""
adminName=""
adminListenAddress=""
adminPort=""
templateDestination=""
templateName=""
adminServerPath="/Server/"
clusters=""
machines=""
managedServersList=[]
managedServers=""
#===============================================================================================================
# Using map to store the variables
#===============================================================================================================
hm = HashMap()
#===============================================================================================================
# This function reads the values defined in the "VASDomainInfo.txt" and stores them in a map.
# In this program "VASDomainInfo.txt" is located in "C:\VASVijay\VASPersonalTechnical\VASWeblogic\VASWLST".
# Change the directory and file name as per your directory structure and choice.
#===============================================================================================================
def readParameters():
print "------------------Entering readParameters function-----------------"
print
try :
# read the "VASDomainInfo" file, this file consists of all the information required to create the template
readFile = open(r'C:\VASVijay\VASPersonalTechnical\VASWeblogic\VASWLST\VASDomainInfo.txt','rb')
doublecolon = re.compile(r'::')
comment=re.compile(r'#')
for line in readFile.readlines():
# This if condition helps to read only lines which contains the character "::", this if condition avoids the trap of reading empty lines.
if not comment.search(line) and doublecolon.search(line) :
hm.put(str(line.rstrip().split('::')[0]),str(line.rstrip().split('::')[1]))
# IOError exception is caught here
except IOError :
print "Not able to read the information in the VASDomainInfo file"
# We get IndexError when the logic reads empty lines or it reads lines which don't contain the character '::'
except IndexError :
print "Array Index Error"
else :
print "Sucesfully read domain info file"
print
print "------------------Leaving readParameters function-----------------"
print
return
#===============================================================================================================
# This function reads fetches the values of resources defined in "VASDomainFile" from the map and assign them to global variables
# defined in the section above.
#===============================================================================================================
def assignParameters():
print "------------------Entering assignParameters function-----------------"
print
# defining global parameters
global password
password = hm.get("UserPassword")
global adminName
adminName = hm.get("AdminServerName")
global adminListenAddress
adminListenAddress = hm.get("AdminServerListenAddress")
global adminPort
adminPort = hm.get("AdminServerPort")
global templateDestination
templateDestination = hm.get("TemplateDestinationDirectory")
global templateName
templateName = hm.get("TemplateName")
global clusters
clusters = hm.get("Clusters")
global machines
machines = hm.get("Machines")
global managedServers
managedServers=hm.get("ManagedServers").replace('"','')
print "password :",password
print "adminName :",adminName
print "adminListenAddress :",adminListenAddress
print "adminPort :",adminPort
print "templateDestination :",templateDestination
print "templateName :",templateName
print "clusters :",clusters
print "machines :",machines
print "managedServers :",managedServers
print
print "------------------Leaving readParameters function-----------------"
print
return
#===============================================================================================================
# This function read the basic domain template "wls.jar". This function assumes that WL_HOME parameter is set to "C:\bea\
# wlserver_10.0". Change the path as per location of WL_HOME.
#===============================================================================================================
def readBasicTemplate() :
print "------------------Entering readBasicTemplate function-----------------"
print
# Reading the basic weblogic domain template
try:
readTemplate(r'C:\bea\wlserver_10.0\common\templates\domains\wls.jar')
print "Sucessfully read the basic template"
except WLSTException:
print " There is issue reading the basic template, please check the location"
print
print "------------------Leaving readBasicTemplate function-----------------"
print
return
#===============================================================================================================
# This function changes the name of "AdminServer". By default name of the Admin Server in "wls.jar" is "AdminServer". If
# different admin server name is mentioned in "VASDomainInfo.txt" then this function will be called to change the Admin Server name
#===============================================================================================================
def needToChangeAdminServerName():
print "------------------Entering needToChangeAdminServerName function-----------------"
print
# Changing the admin server name if its not "AdminServer".
if not adminName == "AdminServer" :
print "AdminServer name needs to be changed"
cd('/')
cd('/Server/AdminServer')
cmo.setName(adminName)
print " New Admin Server Name : " + str(cmo.getName())
print
print "------------------Leaving needToChangeAdminServerName function-----------------"
print
return
#===============================================================================================================
# This function reads the password mentioned for the default user "weblogic" in "VASDomainInfo.txt" and sets the password for the
# user "weblogic" as mentioned in the the file.
#===============================================================================================================
def setUserPassword():
print "------------------Entering setUserPassword function-----------------"
print
# Setting up of new password for the default user "weblogic".
try :
cd('/')
cd('/Security/base_domain/User/weblogic')
print "password :",password
cmo.setUserPassword(password)
except WLSTException :
print " Exception raise while setting the password for the user"
print
print "------------------Leaving setUserPassword function-----------------"
print
return
#===============================================================================================================
# This function sets properties of the Admin Server such as "Admin Server Listen Port" and "Admin Server Listen Address.
#===============================================================================================================
def adminServerConfiguration():
print "------------------Entering adminServerConfiguration function-----------------"
print
try :
cd('/')
global adminServerPath
adminServerPath = adminServerPath + adminName
cd(adminServerPath)
print "adminName :",adminName
# Setting the admin server properties such as listen address and listen port
print "adminListenAddress :",adminListenAddress
print "adminPort :",adminPort
set('ListenAddress',adminListenAddress)
set('ListenPort',int(adminPort))
except WLSTException :
print "Exception caught while settings the properties of the Admin Server"
print
print "------------------Leaving adminServerConfiguration function-----------------"
print
return
#===============================================================================================================
# This function checks if template "jar" file already exists in the directory as mentioned in the variable "TemplateDestinationDirectory"
# in the "VASDomainInfo.txt", if yes returns "Yes" else return "No"
#===============================================================================================================
def validateTemplatePath(fname):
print "------------------Entering validateTemplatePath function-----------------"
print
FileExists="No"
# if already teamplate exists with the same name in the directory mentioned this function return "yes" and asks for user input.
if os.path.exists(fname) :
FileExists="Yes"
print
print "------------------Leaving validateTemplatePath function-----------------"
print
return FileExists
#===============================================================================================================
# This function creates the clusters as mentioned in "VASDomainInfo.txt.
#===============================================================================================================
def createclusters() :
print "------------------Entering createClusters function-----------------"
print
cd('/')
for cluster in clusters.split(','):
# creating clusters
create(cluster,'Cluster')
print "Cluster : " , cluster
print "Cluster had been created sucessfully"
print
print "------------------Leaving createClusters function-----------------"
print
return
#===============================================================================================================
# This function creates the machines as mentioned in "VASDomainInfo.txt.
#===============================================================================================================
def createmachines() :
print "------------------Entering createMachines function-----------------"
print
cd('/')
for machine in machines.split(','):
# creating Machines
create(machine,'Machine')
print "Machine : ", machine
print "Machines have been created sucessfully"
print
print "------------------Leaving createMachines function-----------------"
print
return
#===============================================================================================================
# This function creates managed servers and assigns them to respective clusters and machines as mentioned in "VASDomainInfo.txt".
# This function also set properties of the managed servers such as "Managed Server IP"and "Managed Server Port" as mentioned in
# "VASDomainInfo.txt".
#===============================================================================================================
def createmanagedServers():
print "------------------Entering createManagedServers function-----------------"
print
# Appending the managed server information to the list
for managedServer in managedServers.split(';') :
managedServersList.append(managedServer)
hm.put("managedServersList",managedServersList)
for managedServer in managedServersList:
managedServerInfo=managedServer.split(',')
cd('/')
clusterName=managedServerInfo[0]
print "Cluster Name : ", clusterName
machineName=managedServerInfo[1]
print "Machine Name : ", machineName
managedServerName=managedServerInfo[2]
print "Managed Server Name : ", managedServerName
managedServerIP=managedServerInfo[3].split(':')[0]
print "Managed Server IP : ", managedServerIP
managedServerPort=managedServerInfo[3].split(':')[1]
print "Managed Server Port : ", managedServerPort
# creating managed server
create(managedServerName,'Server')
# assigning the managed server to the cluster
assign('Server', managedServerName,'Cluster',clusterName)
# assiging the managed server to the machine
assign('Server',managedServerName,'Machine',machineName)
ManagedServerLocation="/Server/" + managedServerName
cd(ManagedServerLocation)
# setting the properties of the managed server
set('ListenPort', int(managedServerPort))
set('ListenAddress', managedServerIP)
print
print "------------------Leaving createManagedServer function-----------------"
print
return
#===============================================================================================================
# This function first checks if the template exists in the directory mentioned in "VASDomainInfo.txt", if it exists it asks you
# whether you want to over write template file, if answered "Yes" then it overwrites the template file else it asks you to change the
# template name. Finally it closes the template.
#===============================================================================================================
def writeBasicDomainTemplate():
print "------------------Entering writeBasicDomainTemplate function-----------------"
print
try :
global templateLocation
# storing the template location in "templateLocation" variable
templateLocation = str(templateDestination) + str(templateName) + ".jar"
print " Template Location : " + templateLocation
print
# This while loop prompts user to new template name if template already exists in the directory and user doesn't want to override the old template
while 1:
FileExists = validateTemplatePath(templateLocation)
if FileExists == "Yes" :
print "File : ", templateLocation,"already exists !"
UserInput = raw_input('Do you want to overwrite the above file? enter "Yes" or "No" : ')
# Ignoring the case of what ever user enters, now user can enter "Yes","YEs","YES","No","NO","no"
keyword = re.compile(UserInput,re.I)
if keyword.search("yes") :
print "Overwriting the existing domain template ...."
break
else :
newTemplate = raw_input("Please enter the new template name : ")
templateLocation = templateDestination + newTemplate + '.jar'
else :
break
# Writing the template
writeTemplate(templateLocation)
# Closing the template
closeTemplate()
except WLSTException :
print " Exception caught in writeBasicDomainTemplate function"
print
print "------------------Leaving writeBasicDomainTemplate function-----------------"
print
return
#===============================================================================================================
# This function just displays the properties of the objects which we have created.
#===============================================================================================================
def displayProperties():
print "------------------Entering displayProperties function-----------------"
print
# reading the template
readTemplate(templateLocation)
cd('/')
print "AdminServerPath : ",adminServerPath
cd(adminServerPath)
# displaying few of the properties
print " Admin Server Name : " + cmo.getName()
print " Admin Server Listen Address : " + cmo.getListenAddress()
print " Admin Server Listen Port : " + str(cmo.getListenPort())
print " Template Location : " + templateLocation
print
print "------------------Leaving displayProperties function-----------------"
print
return
#===============================================================================================================
# main function which calls other functions
#===============================================================================================================
if __name__=="main":
readParameters()
assignParameters()
readBasicTemplate()
needToChangeAdminServerName()
setUserPassword()
adminServerConfiguration()
createclusters()
createmachines()
createmanagedServers()
writeBasicDomainTemplate()
displayProperties()