WAS 8.5 Administration using Jython

C:\WASClient> wsadmin -lang jython -host 192.168.1.5 -port 8879
WASX7209I: Connected to process "dmgr" on node serverCellManager01 using SOAP connector;
The type of process is: DeploymentManager
WASX7031I: For help, enter: "print Help.help()"
wsadmin> execfile("C:\WASClient\scripts\was_scripts.py")
wsadmin> addJVMProperty(‘server1’, ‘myproperty’, ‘value’)
wsadmin> syncNodes()
wsadmin> restartServer(‘server1’)

  1. How to create Application Server?
    wsadmin> createApplicationServer(‘Node01’, ‘MyServer’)
    ‘MyServer(cells/Cell01/nodes/Node01/servers/MyServer|server.xml#Server_1402570244015)’
    wsadmin> AdminConfig.save()

    wsadmin> removeServer(‘Node01’, ‘MyServer’)

    def createApplicationServer (nodeName, serverName, templateName = None):
    if templateName == None:
    templateName = 'default'
    options = '[-name %s -templateName %s -genUniquePorts true]' % (serverName, templateName)
    return AdminTask.createApplicationServer(nodeName, options)
    def removeServer (nodeName, serverName):
    AdminTask.deleteServer('[-serverName %s -nodeName %s]' % (serverName, nodeName))
  2. How to retrieve all Ports used by a Server?

    def getServerPorts (nodeName, serverName): 
    ports = {} 
    serverIndex = AdminConfig.list("ServerIndex", AdminConfig.getid("/Node:" + nodeName + "/")) 
    for serverEntry in AdminConfig.list("ServerEntry", serverIndex).splitlines(): 
      if AdminConfig.showAttribute(serverEntry, "serverName") == serverName: 
         for nep in AdminConfig.list("NamedEndPoint", serverEntry).splitlines(): 
            ports[AdminConfig.showAttribute(nep, "endPointName")] = AdminConfig.showAttribute(AdminConfig.showAttribute(nep, "endPoint"), "port")
    return ports
    def printServerPorts(nodeName, serverName):
    for (name, port) in getServerPorts(nodeName, serverName).items():
      print "%40s - %s" %(name, port) 
  3. How to set Server JVM Classpath
    wsadmin> setClasspath(‘server1’, [‘/app/lib/slf4j.jar’,’/app/lib/logback.jar’])

    def printClasspath(serverName):
    jpd = AdminConfig.list("JavaProcessDef", AdminConfig.getid('/Server:' + serverName))
    jvm = toList(AdminConfig.showAttribute(jpd, "jvmEntries"))[0]
    print "%s" % AdminConfig.showAttribute(jvm, "classpath")
    def setClasspath (serverName, classPaths):
    jpd = AdminConfig.list("JavaProcessDef", AdminConfig.getid('/Server:' + serverName))
    jvm = toList(AdminConfig.showAttribute(jpd, "jvmEntries"))[0]
    for cp in classPaths:
    setObjectProperty(jvm, "classpath", cp)
    def setObjectProperty (objectId, propertyName, propertyValue):
    AdminConfig.modify(objectId, [[propertyName, propertyValue]])
    def removeClasspath (serverName):
    jpd = AdminConfig.list("JavaProcessDef", AdminConfig.getid('/Server:' + serverName))
    jvm = toList(AdminConfig.showAttribute(jpd, "jvmEntries"))[0]
    AdminConfig.unsetAttributes(jvm, "classpath")
  4. How to install Application?
    wsadmin> installApp(‘C:\Sample.war’, ‘node01’, ‘server1’, ‘sample_app’, ‘/sample’, ‘mydomain_vh’)
    wsadmin> installApp(‘C:\Sample.ear’, ‘node01’, ‘server1’, ‘sample_app’, vhName=’mydomain_vh’)

    def installApp(location, nodeName, serverName, appName, ctxRoot=None, vhName=None):
    cellName = AdminConfig.list('Cell')
    options = [ 
        '-nopreCompileJSPs',
        '-distributeApp',
        '-nouseMetaDataFromBinary',
        '-deployejb',
        '-createMBeansForResources',
        '-noreloadEnabled',
        '-deployws',
        '-validateinstall warn',
        '-noprocessEmbeddedConfig',
        '-filepermission .*\.dll=755#.*\.so=755#.*\.a=755#.*\.sl=755',
        '-noallowDispatchRemoteInclude',
        '-noallowServiceRemoteInclude',
        '-appname', appInfo["appName"],
        '-MapModulesToServers', [
                 ['.*', '.*', 'WebSphere:cell='+cellName+',node='+nodeName+',server='+serverName]
                                ]
              ]
    if vhName != None:
        options.append('-MapWebModToVH')
        options.append([['.*', '.*', vhName]])
    if ctxRoot != None:
        options.append('-contextroot')
        options.append(ctxRoot)
    AdminApp.install(location, options)
  5. How to update JVM logs location?
    wsadmin> configureJVMLogs(‘server1’, ‘/logs/Websphere/server1/’)

    def configureJVMLogs (serverName, logDir):
    logDir = logDir.strip()
    if (logDir[len(logDir) - 1] != '/'):
    logDir = logDir + "/"
    server = AdminConfig.getid('/Server:' + serverName)
    setFile(AdminConfig.showAttribute(server, 'errorStreamRedirect'), logDir, 'SystemErr.log')
    setFile(AdminConfig.showAttribute(server, 'outputStreamRedirect'), logDir, 'SystemOut.log')
    def setFile(streamId, logDir, fileName):
    AdminConfig.modify(streamId, [['fileName', logDir + fileName]])
  6. How to Start/Stop Server
    wsadmin> stopServer(‘Node01’, ‘MyServer’)

    def startServer(nodeName, serverName):
    AdminControl.startServer(serverName, nodeName)
    def stopServer(nodeName, serverName):
    server = AdminControl.completeObjectName('WebSphere:type=Server,name=%s,*' % serverName)
    if len(server) > 0:
    AdminControl.stopServer(serverName, nodeName)
    else:
    print 'Server %s already stopped!' % serverName
    def restartServer(nodeName, serverName):
    stopServer(nodeName, serverName)
    startServer(nodeName, serverName)
  7. How to synchronize the Node?
    wsadmin> syncNodes(‘node01’)

    def syncNodes(nodeName):
    nodeSync = AdminControl.completeObjectName('type=NodeSync,node=' + nodeName + ',*')
    AdminControl.invoke(nodeSync, 'sync')
    def syncAllNodes():
    for node in AdminConfig.list('Node').splitlines():
      syncNodes(AdminConfig.showAttribute(node, 'name'))
  8. How to get the Server State?
    wsadmin> showServerStatus(‘server1’)

    def showServerStatus(serverName):
    serverObj = AdminControl.completeObjectName('WebSphere:type=Server,name=' + serverName + ',*')
    if len(serverObj) > 0:
    serverStatus = AdminControl.getAttribute(serverObj, 'state')
    else:
    serverStatus = 'STOPPED'
    print "%15s %s" %(serverName, serverStatus)
    def showAllServerStatus():
    for server in AdminConfig.list('Server').splitlines():
    showServerStatus(AdminConfig.showAttribute(server, 'name'))
  9. Managing VirtualHost Configuration
    wsadmin> createVirtualHostWithAliases(‘myhost’, [[‘host1.com’, 80], [‘secure.host.com’, 443]])
    wsadmin> AdminConfig.save()
    wsadmin> addVirtualHostAlias(‘myhost’, [[‘host2.com’, 80], [‘secure.host2.com’, 443]])
    wsadmin> AdminConfig.save()
    wsadmin> printVirtualHostAliases(‘myhost’)
    host1.com:80
    secure.host.com:443
    host2.com:80
    secure.host2.com:443
    wsadmin> removeVirtualHostAlias(‘myhost’, ‘host1.com’)
    wsadmin> AdminConfig.save()
    wsadmin> removeVirtualHost(‘myhost’)
    wsadmin> AdminConfig.save()

    def printAllVirtualHosts():
    for vh in toList(AdminConfig.list("VirtualHost")):
    print "%s" %(AdminConfig.showAttribute(vh, "name"))
    def printAllVirtualHostWithAliases():
    for vh in toList(AdminConfig.list("VirtualHost")):
    print "%s" %(AdminConfig.showAttribute(vh, "name"))
    for al in toList(AdminConfig.showAttribute(vh, 'aliases')):
      print "\t%s:%s" %(AdminConfig.showAttribute(al, 'hostname'), AdminConfig.showAttribute(al, 'port'))
    def printVirtualHostAliases(virtualHost):
    vh = AdminConfig.getid("/VirtualHost:" + virtualHost)
    if len(vh) > 0:
    for al in toList(AdminConfig.showAttribute(vh, 'aliases')):
      print "%s:%s" % (AdminConfig.showAttribute(al, 'hostname'), AdminConfig.showAttribute(al, 'port'))
    else:
    print "VirtualHost '%s' not found" % (virtualHost)
    def createVirtualHost (virtualHostName): 
    AdminConfig.create("VirtualHost", AdminConfig.list("Cell"), [["name", virtualHostName]]) 
    def createVirtualHostWithAliases (virtualHostName, aliases): 
    virtualHost = AdminConfig.create("VirtualHost", AdminConfig.list("Cell"), [["name", virtualHostName]]) 
    for alias in aliases:
    AdminConfig.create("HostAlias", virtualHost, [["hostname", alias[0]], ["port", alias[1]]])
    def addVirtualHostAlias(virtualHostName, aliases):
      virtualHost = AdminConfig.getid("/VirtualHost:" + virtualHostName)
      for alias in aliases:
        AdminConfig.create("HostAlias", virtualHost, [["hostname", alias[0]], ["port", alias[1]]])
    def removeVirtualHostAlias(virtualHostName, hostName):
    virtualHost = AdminConfig.getid("/VirtualHost:" + virtualHostName)
    for alias in toList(AdminConfig.showAttribute(vh, 'aliases')):
    if AdminConfig.showAttribute(alias, 'hostname') == hostName:
      AdminConfig.remove(alias)
    def removeVirtualHost(virtualHostName):
    virtualHost = AdminConfig.getid("/VirtualHost:" + virtualHostName)
    if len(virtualHost) > 0:
    AdminConfig.remove(virtualHost)
    else:
    print "VirtualHost '%s' not found" % (virtualHost)    

Leave a Reply

Your email address will not be published. Required fields are marked *