Websphere maintain automated deployment scripts

1 Introduction

Websphere wsadmin supports command-line operation of the automatic deployment and maintenance, the procedure can be simplified to reduce manual processing, increase efficiency and reduce the possibility of error.

The following provides a method Websphere execute wsadmin command line and provides Websphere application deployment and automated maintenance scripts.

2. Use the wsadmin command line configuration was modified

By wsadmin can use to configure was the command line, call jython script, you do not need to log into the console.

Execute the following code into the wsadmin command line console (with Linux systems, base version was an example):

/was/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/wsadmin.sh -lang jython

Executes the specified parameters -f wsadmin jython script executable script file specified.

/was/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/wsadmin.sh -lang jython -f xxx.py

When open wsadmin, bin directories that need to enter the corresponding opening was server wsadmin.

When you open wsadmin, may need to enter a user name and password was (was generally will not need to enter a password).

When was deployed in a Windows system, jython script file encoding should be ANSI; when was deployed in the Linux system, jython script file was encoded should be the same user code. When coding inconsistencies, perform jython script may be wrong.

2.1 jython Public script

When jython public function was deployed unified in a common script commonfunc.py, the deployment was using scripts, using shell command to invoke wsadmin, then call jython script, the script calls the public commonfunc.py of public functions.

When the script calls jython script commonfunc.py public, jython script and commonfunc.py need to be saved in the same directory, or call the import sys; sys.path.append (r'commonfunc.py directory ').

Sample call script follows the public.

from commonfunc import func

func (parameter 1, parameter 2 ...)

Newline Windows system is '\ r \ n', newline Linux system is '\ n'. When Windows system calls jython script, use newline '\ r \ n'; in the Linux system call jython script, use newline '\ n'.

2.2 jython script shows public

Production environment, the database password "Creating a J2C authentication data" entered in step to a string of random input, the real password manually input the data center.

2.2.1 depending on the operating system selection newline

import os

 

def getlinesep():

    if(os.path.exists('c:/windows')):

        return '\r\n'

    else:

        return '\n'

2.2.2 uninstall applications

Application Name Example # appname parameter to be unloaded: 'csr_war'

 

def cf_uninstallapp(appname):

    funcname = 'uninstalling'

    if(AdminApp.list().find(appname) == -1):

        print appname + '- the application is not installed "

        return

    AdminApp.uninstall(appname)

    AdminConfig.save()

    print funcname + '- completed'

    return

2.2.3  创建oracleJDBC提供程序

#参数 无

 

def cf_createjdbcprovider():

    funcname='创建oracleJDBC提供程序'

    AdminTask.createJDBCProvider('[-scope Cell=' + AdminControl.getCell() +' -databaseType Oracle -providerType "Oracle JDBC Driver" -implementationType "Connection pool data source" -name "Oracle JDBC Driver" -description "Oracle JDBC Driver" -classpath [${WAS_INSTALL_ROOT}/lib/ojdbc6.jar ] -nativePath "" ]')

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.4  创建J2C认证数据(存储数据库用户密码)

#参数 name J2C认证数据名字 示例 'newcc'

#参数 user 数据库用户名 示例 'newcc'

#参数 pwd 数据库密码 示例 'newcc'

 

def cf_createJ2Cdata(name, user, pwd):

    funcname='创建J2C认证数据'

    arg='[-alias ' + name + ' -user ' + user + ' -password ' + pwd + ' -description  ]'

 

    AdminTask.createAuthDataEntry(arg)

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.5  创建数据源

#参数 datasourcename 数据源名称 示例 'kb'

#参数 jndiname jndi名字 示例 'jdbc/kb'

#参数 authalias 使用的J2C认证数据名 示例 'newcc'

#参数 jdbcurl JDBC连接数据库的url 示例 'jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=sz-qzgbkdb1)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=sz-qzgbkdb2)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=szgbkqz)))'

 

def cf_createdatasource(datasourcename, jndiname, authalias, jdbcurl):

    funcname='创建数据源'

    jdbcCFname=datasourcename + '_CF'

    authalias_use=AdminControl.getNode() + '/' + authalias

 

    #获取oracle jdbc提供程序ID

    oracle_jdbc='"Oracle JDBC Driver('

    jdbc_list_1=AdminConfig.list('JDBCProvider', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    jdbc_list_2=jdbc_list_1.split(getlinesep())

    jdbc_index=0

    jdbcproviderid=''

 

    for i in range(len(jdbc_list_2)):

        if jdbc_list_2[i].find(oracle_jdbc) == 0:

            jdbc_index=i

            break

 

    jdbcproviderid=jdbc_list_2[jdbc_index].split('|resources.xml#')[1].split(')')[0]

    print jdbcproviderid

 

 

    #创建数据源

    AdminTask.createDatasource('"Oracle JDBC Driver(cells/' + AdminControl.getCell() + '|resources.xml#' + jdbcproviderid + ')"', '[-name ' + datasourcename + ' -jndiName ' + jndiname + ' -dataStoreHelperClassName com.ibm.websphere.rsadapter.Oracle11gDataStoreHelper -containerManagedPersistence true -componentManagedAuthenticationAlias ' + authalias_use + ' -configureResourceProperties [[URL java.lang.String ' + jdbcurl + ']]]')

 

    #获取数据源ID

    datasource_list_1=AdminConfig.list('DataSource', AdminConfig.getid( '/Cell:' + AdminControl.getCell()+ '/'))

    datasource_list_2=datasource_list_1.split(getlinesep())

    datasource_index=0

    datasourceid=''

 

    for i in range(len(datasource_list_2)):

        if datasource_list_2[i].find(datasourcename+ '(') == 0:

            datasource_index=i

            break

 

    datasourceid=datasource_list_2[datasource_index].split('|resources.xml#')[1].split(')')[0]

    print datasourceid

 

 

    #获取CMPConnectorFactoryID

    jdbcCF_list_1=AdminConfig.list('CMPConnectorFactory', AdminConfig.getid( '/Cell:' + AdminControl.getCell()+ '/'))

    jdbcCF_list_2=jdbcCF_list_1.split(getlinesep())

    jdbcCF_index=0

    jdbcCFid=''

 

    for i in range(len(jdbcCF_list_2)):

        if jdbcCF_list_2[i].find(jdbcCFname + '(') == 0:

            jdbcCF_index=i

            break

 

    jdbcCFid=jdbcCF_list_2[jdbcCF_index].split('|resources.xml#')[1].split(')')[0]

    print jdbcCFid

 

    #设置认证别名

    arg1='(cells/' + AdminControl.getCell() + '|resources.xml#' + datasourceid + ')'

    arg2='[[authDataAlias ' + authalias_use + '] [mappingConfigAlias ""]]'

    AdminConfig.create('MappingModule', arg1 , arg2)

 

    arg1='(cells/' + AdminControl.getCell() + '|resources.xml#' + jdbcCFid + ')'

    arg2='[[name "' + jdbcCFname + '"] [authDataAlias "' + authalias_use + '"] [xaRecoveryAuthAlias ""]]'

    AdminConfig.modify(arg1, arg2)

 

    arg2='[[authDataAlias ' + authalias_use + '] [mappingConfigAlias ""]]'

    AdminConfig.create('MappingModule', arg1, arg2)

 

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.6  创建虚拟主机

#参数 hostname 虚拟主机名 示例 'kb_host'

 

def cf_createvirtualhost(hostname):

    funcname='创建虚拟主机'

 

    AdminConfig.create('VirtualHost', AdminConfig.getid('/Cell:' + AdminControl.getCell()+ '/'), '[[name "' + hostname + '"]]')

    AdminConfig.save()

 

    print funcname + '-完毕'

    return

2.2.7  设置虚拟主机端口

#参数 hostname 虚拟主机名 示例 'kb_host'

#参数 portnum  示例 '7001'

 

def cf_setvirtualhostport(hostname, portnum):

    funcname='设置虚拟主机端口'

 

    AdminConfig.create('HostAlias', AdminConfig.getid('/Cell:' + AdminControl.getCell() + '/VirtualHost:' + hostname + '/'), '[[hostname "*"] [port "' + portnum + '"]]')

   

    AdminConfig.save()

    print funcname + '-完毕'

    return 

2.2.8  设置服务端口

#参数 portname 端口名 示例 'kb_port'

#参数 portnum  示例 '7001'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_createserverport(portname, portnum, servername='server1'):

    funcname='设置服务端口'

 

    #获取ServerEntryID

    serverentry_list_1=AdminConfig.list('ServerEntry', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    serverentry_list_2=serverentry_list_1.split(getlinesep())

    serverentry_index=0

    serverentryid=''

 

    for i in range(len(serverentry_list_2)):

        if serverentry_list_2[i].find(servername+ '(') == 0:

            serverentry_index=i

            break

 

    serverentryid=serverentry_list_2[serverentry_index].split('|serverindex.xml#')[1].split(')')[0]

    print serverentryid

 

 

    AdminConfig.create('NamedEndPoint', '(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '|serverindex.xml#' + serverentryid +')', '[[endPointName "' + portname + '"]]')

 

 

    #获取NamedEndPointID

    nameendpoint_list_1=AdminConfig.list('NamedEndPoint', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    nameendpoint_list_2=nameendpoint_list_1.split(getlinesep())

 

    for i in range(len(nameendpoint_list_2)):

        if AdminConfig.show(nameendpoint_list_2[i]).find('[endPointName ' + portname + ']') != -1:

            nameendpoint_index=i

    nameendpointid=nameendpoint_list_2[nameendpoint_index].split('|serverindex.xml#')[1].split(')')[0]

 

    print nameendpointid

 

    AdminConfig.create('EndPoint', '(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '|serverindex.xml#' + nameendpointid + ')', '[[port "' + portnum + '"] [host "*"]]')

 

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.9  设置服务Web容器传输链(HTTP/HTTPS)

#参数 chainname Web容器传输链名字 示例 'kb_chain'

#参数 portname 使用的端口名字 示例 'kb_port'

#参数 type 标记创建HTTP或HTTPS传输链,默认HTTP 示例 'HTTPS'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_createchain(chainname, portname, type='HTTP', servername='server1'):

    funcname='设置Web容器传输链设置'

 

    #获取TransportChannelServiceID

    tcs_list_1=AdminConfig.list('TransportChannelService', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    tcs_list_2=tcs_list_1.split(getlinesep())

 

    tcs_index=0

    tcsid=''

 

    for i in range(len(tcs_list_2)):

        if tcs_list_2[i].find('/' + servername + '|') != -1:

            tcs_index=i

            break

 

    tcsid=tcs_list_2[tcs_index].split('|server.xml#')[1].split(')')[0]

    print tcsid

 

    #获取NamedEndPointID

    nameendpoint_list_1=AdminConfig.list('NamedEndPoint', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    nameendpoint_list_2=nameendpoint_list_1.split(getlinesep())

 

    for i in range(len(nameendpoint_list_2)):

        if AdminConfig.show(nameendpoint_list_2[i]).find('[endPointName ' + portname + ']') != -1:

            nameendpoint_index=i

    nameendpointid=nameendpoint_list_2[nameendpoint_index].split('|serverindex.xml#')[1].split(')')[0]

 

    print nameendpointid

 

    #判断创建HTTP/HTTPS传输链

    if(type == 'HTTPS'):

        chaintemplate='WebContainer-Secure(templates/chains|webcontainer-chains.xml#Chain_2'

    else:

        chaintemplate='WebContainer(templates/chains|webcontainer-chains.xml#Chain_1'

   

    AdminTask.createChain('(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '/servers/' + servername + '|server.xml#' + tcsid + ')', '[-template ' + chaintemplate + ') -name ' + chainname + ' -endPoint (cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '|serverindex.xml#' + nameendpointid + ')]')

 

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.10     设置JAVA虚拟机堆大小

#参数 initialsize 初始堆大小 示例 '1024'

#参数 maxsize 最大堆大小 示例 '2048'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_setjavaheapsize(initialsize, maxsize, servername='server1'):

    funcname='设置JAVA虚拟机堆大小'

 

    AdminTask.setJVMProperties('[-nodeName ' + AdminControl.getNode() + ' -serverName ' + servername + ' -initialHeapSize ' + initialsize + ' -maximumHeapSize ' + maxsize + ']')

 

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.11     设置Web容器线程池

#参数 minsize 最小大小 示例 '10'

#参数 maxsize 最大大小 示例 '50'

#参数 threadname 默认'WebContainer' 示例 'WebContainer'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_setwebcontainerthread(minsize, maxsize, threadname='WebContainer', servername='server1'):

    funcname='设置Web容器线程池'

 

    #获取WebContainerID

    thread_list_1=AdminConfig.list('ThreadPool', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    thread_list_2=thread_list_1.split(getlinesep())

 

    thread_index=0

    threadid=''

 

    for i in range(len(thread_list_2)):

        if thread_list_2[i].find(threadname + '(') != -1:

            thread_index=i

            break

 

    threadid=thread_list_2[thread_index].split('|server.xml#')[1].split(')')[0]

    print threadid

 

    AdminConfig.modify('(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '/servers/' + servername + '|server.xml#' + threadid + ')', '[[maximumSize "' + maxsize + '"] [name "' + threadname + '"] [minimumSize "' + minsize + '"]]')

 

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.12     设置数据源连接池

#参数 datasourcename 数据源名字 示例 'kb'

#参数 mincon 最小连接数 示例 '5'

#参数 maxcon 最大连接数 示例 '50'

 

def cf_setconnectionpool(datasourcename, mincon, maxcon):

    funcname='设置数据源连接池'

 

    #获取数据源信息

    datasource_list_1=AdminConfig.list('DataSource', AdminConfig.getid( '/Cell:' + AdminControl.getCell()+ '/'))

    datasource_list_2=datasource_list_1.split(getlinesep())

    datasource_index=0

 

    for i in range(len(datasource_list_2)):

        if datasource_list_2[i].find(datasourcename+ '(') == 0:

            datasource_index=i

            break

       

    datasourceinfo=AdminConfig.show(datasource_list_2[datasource_index])

 

    #获取连接池ID

    connectionpoolid=datasourceinfo.split('[connectionPool')[1].split('|resources.xml#')[1].split(')')[0]

    print connectionpoolid

 

 

    AdminConfig.modify('(cells/' + AdminControl.getCell() + '|resources.xml#' + connectionpoolid + ')', '[[maxConnections "' + maxcon + '"] [minConnections "' + mincon + '"]]')

    AdminConfig.save()

 

    print funcname + '-完毕'

    return

2.2.13     设置JVM日志参数

#参数 streamredirectname 需要设置的输出名称 示例 'SystemErr.log'或'SystemOut.log'

#参数 logsize 日志文件大小,单位M 示例 '20'

#参数 lognum 日志文件数量 示例 '10'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_setJVMlog(streamredirectname, logsize, lognum, servername='server1'):

    funcname='设置JVM日志'

 

    streamredirect_list_1=AdminConfig.list('StreamRedirect', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    streamredirect_list_2=streamredirect_list_1.split(getlinesep())

 

    #遍历StreamRedirect

    for i in range(len(streamredirect_list_2)):

        streamredirectinfo=AdminConfig.show(streamredirect_list_2[i])

        if streamredirectinfo.find(streamredirectname) != -1:

            streamredirectid=streamredirect_list_2[i].split('server.xml#')[1].split(')')[0]

            AdminConfig.modify('(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '/servers/' + servername + '|server.xml#' + streamredirectid + ')', '[[rolloverType "SIZE"] [maxNumberOfBackupFiles "' + lognum + '"] [rolloverSize "' + logsize + '"] [baseHour "24"] [rolloverPeriod "24"] [formatWrites "true"]  [messageFormatKind "BASIC"] [suppressWrites "false"] [suppressStackTrace "false"]]')

 

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.14     创建SSL证书库和密钥

#参数 keyname SSL证书名字 示例 'csr_key'

#参数 keypath 证书文件路径 示例 '/was/key/keystore.jks'

#参数 pwd 证书密码 示例 'password'

#参数 keytype 证书类型,默认'JKS' 示例 'JKS'

 

def cf_createsslkey(keyname, keypath, pwd, keytype='JKS'):

    funcname='创建SSL证书库和密钥'

 

    AdminTask.createKeyStore('[-keyStoreName ' + keyname + ' -scopeName (cell):' + AdminControl.getCell() + ':(node):' + AdminControl.getNode() + ' -keyStoreDescription  -keyStoreLocation ' + keypath + ' -keyStorePassword ' + pwd + ' -keyStorePasswordVerify ' + pwd + ' -keyStoreType ' + keytype + ' -keyStoreInitAtStartup false -keyStoreReadOnly false -keyStoreStashFile false -keyStoreUsage SSLKeys ]')

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.15     创建SSL配置

#参数 sslname SSL配置名字 示例 'csr_ssl'

#参数 keyname SSL证书名字 示例 'csr_key'

#参数 keyalias SSL证书别名 示例 'server'

 

def cf_createsslconfig(sslname, keyname, keyalias):

    funcname='创建SSL配置'

 

    AdminTask.createSSLConfig('[-alias ' + sslname + ' -type JSSE -scopeName (cell):' + AdminControl.getCell() + ':(node):' + AdminControl.getNode() + ' -keyStoreName ' + keyname + ' -keyStoreScopeName (cell):' + AdminControl.getCell() + ':(node):' + AdminControl.getNode() + ' -trustStoreName ' + keyname + ' -trustStoreScopeName (cell):' + AdminControl.getCell() + ':(node):' + AdminControl.getNode() + ' -serverKeyAlias ' + keyalias + ' -clientKeyAlias ' + keyalias + ' ]')

    AdminConfig.save()

    print funcname + '-完毕'

    return

2.2.16     设置HTTPS传输链SSL配置

#参数 chainname Web容器传输链名字 示例 'csr_chain_ssl'

#参数 sslname 使用的SSL配置名字 示例 'csr_ssl'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_sethttpschain(chainname, sslname, servername='server1'):

    funcname='设置HTTPS传输链SSL配置'

 

    #获取传输链信息

    transportchannel_list1=AdminConfig.list('TransportChannelService', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    transportchannel_list2=transportchannel_list1.split(getlinesep())

 

    chaininfo=''

 

    #遍历传输链信息

    for i in range(len(transportchannel_list2)):

        #获取全部传输链信息

        transportchannel_all=AdminConfig.show(transportchannel_list2[i])

        #判断待修改的传输链是否在当前的信息中

        if(transportchannel_all.find(chainname + '(') != -1):

            chaininfo=transportchannel_all.split(chainname)[1].split(']')[0]

            break

           

 

    #获得待修改HTTPS传输链信息

    chaininfo_list1=AdminConfig.show(chaininfo)

    chaininfo_list2=chaininfo_list1.split(getlinesep())

 

    sslinboundchannelid=''

    #查找对应的SSLInboundChannelID

    for i in range(len(chaininfo_list2)):

        if(chaininfo_list2[i].find('[transportChannels') == 0):

            chaininfo_list3=chaininfo_list2[i].split(' ')

            for i in range(len(chaininfo_list3)):

                if(chaininfo_list3[i].find('SSL_') == 0):

                    sslinboundchannelid=chaininfo_list3[i].split('|server.xml#')[1].split(')')[0]

                    break

 

    AdminConfig.modify('(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '/servers/' + servername + '|server.xml#' + sslinboundchannelid + ')', '[[sslConfigAlias "' + sslname + '"]]')

    AdminConfig.save()

 

 

    print funcname + '-完毕'

    return

2.2.17     获取主机名信息(前三个字节与最后一个字节)

import socket

 

def cf_gethostname_short():

    hostname=socket.gethostname()

    len_hostname=len(hostname)

    hn=hostname[0:3]+hostname[len_hostname-1:len_hostname]

    return hn

2.2.18     修改访问was时返回的HTTP头中的Server字段为主机名信息

#参数 chainname Web容器传输链名字 示例 'csr_chain_ssl'

#参数 servername 默认'server1' 示例 'server1'

 

def cf_setservervalue(chainname, servername='server1'):

    funcname='修改访问was时返回的HTTP头中的Server字段为主机名信息'

 

    #获取传输链信息    

    transportchannel_list1=AdminConfig.list('TransportChannelService', AdminConfig.getid( '/Cell:' + AdminControl.getCell() + '/'))

    transportchannel_list2=transportchannel_list1.split(getlinesep())

 

    chaininfo=''

 

    #遍历传输链信息

    for i in range(len(transportchannel_list2)):

        #获取全部传输链信息

        transportchannel_all=AdminConfig.show(transportchannel_list2[i])

        #判断待修改的传输链是否在当前的信息中

        if(transportchannel_all.find(chainname + '(') != -1):

            chaininfo=transportchannel_all.split(chainname)[1].split(']')[0]

            break

           

 

    #获得待修改HTTPS传输链信息

    chaininfo_list1=AdminConfig.show(chaininfo)

    chaininfo_list2=chaininfo_list1.split(getlinesep())

 

    httpinboundchannelid=''

    #查找对应的httpinboundchannelid

    for i in range(len(chaininfo_list2)):

        if(chaininfo_list2[i].find('[transportChannels') == 0):

            chaininfo_list3=chaininfo_list2[i].split(' ')

            for i in range(len(chaininfo_list3)):

                if(chaininfo_list3[i].find('HTTP_') == 0):

                    httpinboundchannelid=chaininfo_list3[i].split('|server.xml#')[1].split(')')[0]

                    break

                   

    AdminConfig.create('Property', '(cells/' + AdminControl.getCell() + '/nodes/' + AdminControl.getNode() + '/servers/' + servername + '|server.xml#' + httpinboundchannelid + ')', '[[validationExpression ""] [name "ServerHeaderValue"] [description ""] [value "' + cf_gethostname_short() + '"] [required "false"]]')

    AdminConfig.save()

 

 

    print funcname + '-完毕'

    return

 

发布了37 篇原创文章 · 获赞 0 · 访问量 2311

Guess you like

Origin blog.csdn.net/a82514921/article/details/104621116