X

WLST Scripting to Get WebLogic Libraries and Deployed Applications

Mark Piller
Senior Principal Consultant

Abstract

Often times you may need to be able to identify the libraries installed on a given WebLogic server. Perhaps you need to compare different installations in different environments. Maybe you want to know if you need to update the libraries such as the ADF Runtime libraries.

This article will walk through writing a Python script to run in the WebLogic Scripting Tool (WLST) command line. The script will create 2 files:

  1. Listing of Libraries installed on the server
  2. Listing of deployed applications on the server

Introduction

Two technologies that will be used in this effort are: WLST and Python.

WLST

The WebLogic Scripting Tool (WLST) is a powerful way to work within a WebLogic environment. This is available to you when you do a full installation of WebLogic. WLST is a command line environment that allows you to create, manage, and monitor WebLogic domains. You can do this from your own desktop with the proper credentials.  The WebLogic 12c documentation for WLST is Fusion Middleware Oracle WebLogic Scripting Tool. The command reference is located at Fusion Middleware WebLogic Scripting Tool Command Reference.

Python

Python is a scripting language and any file written in Python will end in the suffix "py". The python.org website is a great place to turn to for documentation and tutorials.

MBeans

The navigation that you will perform with this exercise is to navigate the System MBeans tree. A MBean is a managed bean (Java bean) that provides a Java Management Extensions (JMX) interface. WebLogic Server provides a set of MBeans that you can use to configure, monitor, and manage WebLogic Server resources through JMX. For more information please see The WebLogic Server® MBean Reference.

Construction

WLST Commands

The WLST commands used in this script are:

Command Description
cd Navigate the hierarchy of configuration or runtime beans.
ls List all child beans and/or attributes for the current configuration or runtime bean.
connect Connect WLST to a WebLogic Server instance.
disconnect Disconnect WLST from a WebLogic Server instance.
serverConfig
Navigate to the last MBean to which you navigated in the configuration MBean hierarchy or to the root of the hierarchy, DomainBean.
domainName
Name of the WebLogic domain to which WLST is connected.
serverName
Name of the server for which Node Manager property files are generated.
pwd
Display the current location in the configuration or runtime bean hierarchy.
execfile
After invoking WLST, use the execfile() command to run a script.

Details

The main function of WLST for this script is to sign into a targeted WebLogic domain and navigate the configuration MBean hierarchy. This navigation will target the Libraries and Applications installed on the domain.You can get a visual reference to this navigation by logging into Enterprise Manager Fusion Middleware Control (EM) and navigating the System MBean Browser in EM. Both Libraries and Applications are attributes in the Configuration MBean. Incidentally, when you navigate using EM you are using an application built on the Oracle Application Development Framework (ADF) that heavily uses MBeans to give you a visual reference to everything installed on WebLogic server.

EM System MBean Browser

The following instructions will walk you through the visual display found in the EM System MBean Browser.

  • Login to EM
  • Expand the WebLogic Domain node in the Navigation Tree Pane on the left
  • Click on the targeted domain node


  • Click on the WebLogic Domain drop down menu and select System MBean Browser


By default you are visiting the Configuration MBeans Node in the tree


  • Click on the Binoculars Find icon


  • Choose Attributes in the select one choice drop down


  • Enter "Libraries" and click on the Find icon


  • Slide the MBean Attributes view until you see the Libraries attribute. Then click on Libraries.

  • You should now be viewing the libraries installed on this domain. These will be what should be returned when completing the Python script.

  • Click on one of the libraries. The library you have selected will display a new set of attributes. One of those is the Targets attribute and this will report which of the servers in this domain have the selected library.


  •  Click on the Targets attribute. The elements and values within this display will define which servers have this specific library.


  • Click on the Return button until you return to the Domain view of the configuration MBeans (click on Return 3 times in this example). Slide the Attributes window to view the attributes toward the top of the window (they are alphabetical in order). Do this until you see the AppDeployments attribute. This is our other target value in the Python script. Click on AppDeployments and review what you have on your server domain.

  • You can also determine which servers the applications have been deployed to by clicking on each deployed application and examine the Targets attribute like you did for the libraries.

WLST Command Line

Before embarking on creating our Python script let's work out the WLST commands that need to be executed to accomplish our goal of identifying the installed libraries and deployed applications.

According to the WLST Scripting documentation there are 4 ways to use WLST interactively or as a script:

  • Execute the appropriate shell script for your environment.

  • Execute the java weblogic.WLST command.

  • Run a WLST script.

  • Execute the WebLogic Scripting Tool command from the Start menu (Windows only).

I am going to walk through the process of the 4th step above. The prerequisite is that you have WebLogic 10.3.6 or better installed on your Windows computer.

Perform the following steps:

  • Open a Windows command window
  • Navigate to your WebLogic WL_HOME/oracle_common/common/bin directory
  • Run the setWlstEnv.cmd script


  • Run the wlst.cmd script. You will see a wls:/offline> prompt.


  • Login to your target server using the connect command. Note: I do not have the SSL port enabled on my WebLogic server therefore I am using port 7001.

  • Navigate the MBean tree using the cd command to the Libraries attribute by entering cd('/Libraries'). The command line prompt conveniently changes as you navigate the MBean tree - letting you know where you are in the navigation. According to the Oracle documentation this is the configuration MBean hierarchy (serverConfig for the DomainBean) and for our example the Libraries attribute in that hierarchy.


  • Now execute the list command ls to view all the values (libraries) in the Libraries attribute

  • Now use the pwd command to confirm where you are in the MBean tree. This will match your current WLST prompt. However, you will not have a prompt when using a script therefore the pwd command adds value to your script if you need to confirm where you are in the navigation.
  • Also test the WLST variables that identify the domain and server you are connected to and navigating. Do this with a print command:  print domainName and print serverName.

  • Finally, let's test the ls command with parameters. According to the documentation you can create a map of the values returned by the ls command. The syntax we will use for this command example is: libraryListing = ls(returnMap='true', returnType='a'). Note that you need to save the returned value to a variable which in this case we will call libraryListing. When you do this on the command line you will still see the list of libraries displayed in the window. What is different now is that you have a map object of those values and can easily access these in a Python script.


  •  Review the size of the map return value with print libraryListing.size(). View a single returned value with a command like print libraryListing[0].


  • The next step is to navigate the MBean tree to determine which servers were targeted with a specific library. We will use the first library in the listing array we just created. Enter the change director command: cd(libraryListing[0]).


  • The next MBean attribute we want to find is Targets. We will do this with another list command but change the returnType to c which by definition only return child management objects of the specified management object (suppresses the display of other items). Enter the following command at the WLST prompt: childNodeListing = ls(returnMap='true',returnType='c'). Explore the array listing created by this command. You should find that the second position in the array is Targets (print childNodeListing[1]).


  • Now navigate to the Targets node using the cd command: cd('Targets'). Get another array list of the children of the Targets attribute: serverNames = ls(returnMap='true',returnType='c'). Examine the contents of that array and you will find the servers that the library has been deployed to.


  • Complete your testing in WLST with the disconnect command. Then exit the WLST command prompt with the exit command.

Python Script

All that remains is to write the Python script that will do the work we want. I am providing the source code for the Python script I created. The details of this script are as follows:

Global Variables and Initialization

There is an initialization section that creates some global variables and defines the output which are text files for the results. This also includes an import section where you can import additional Java libraries to support the work you want to accomplish.

Functions

Python supports creating custom functions. In my case I have created three functions: getLibraryListing, getAppListing, and getTargetServers. getLibraryListing and getAppListing are very similar and most likely could be replaced with a more common function. The getTargetSevers function navigates the MBean tree from either the application node or the libraries node to identify the targets (target servers).

Main Routine

getLibraryListing and getAppListing are executed in my main routine at the bottom of the Python script. This has also included some error checking. getTargetServers is called by getLibraryListing and getAppListing.

The logic for the script is as follows:

"""
---------------------------------------------------------------------------------------------------
Script  : ListLibraries.py
Author  : Mark Piller
Date    : 2015-01-06
Purpose : Create 2 separate files >
          1) list of libraries in the targeted WebLogic server
          2) List of applications in the targeted server

run this with > java weblogic.WLST ListLibraries.py
or by execfile('<drive>:<directory>/ListLibraries.py') when in the WLST command prompt
---------------------------------------------------------------------------------------------------
"""

import java.io as javaio



"""
---------------------------------------------------------------------
Global Variables
---------------------------------------------------------------------
"""
username = 'weblogic'
password = 'welcome1'
URL = 't3://<hostName>:7001'
outputDir = 'D:/temp'

# get the date to add to the file name
from datetime import date
today = date.today()
filedate = today.isoformat()

# libraries output saved to this file
libraryOutputFile = outputDir + "/" + "libraries-in-wls-" + filedate + ".txt"
libraryOutputFileWriter=javaio.FileWriter(libraryOutputFile)

# applications output saved to this file
appOutputFile = outputDir + "/" + "app-deployments-in-wls-" + filedate + ".txt"
appOutputFileWriter=javaio.FileWriter(appOutputFile)


"""
---------------------------------------------------------------------
Function to find the libraries
---------------------------------------------------------------------
"""
def getLibraryListing(mbeanPosition):
  cd(mbeanPosition)
  libraryListing = ls(returnMap='true', returnType='a')

  if libraryListing.size() > 0 :
    # print file headings
    libraryOutputFileWriter.write("Library Listing " + filedate)
    libraryOutputFileWriter.write(System.getProperty("line.separator"))
    libraryOutputFileWriter.write(System.getProperty("line.separator"))

    libraryOutputFileWriter.write("Domain    : " + domainName)
    libraryOutputFileWriter.write(System.getProperty("line.separator"))

    libraryOutputFileWriter.write("Server    : " + serverName)
    libraryOutputFileWriter.write(System.getProperty("line.separator"))

    libraryOutputFileWriter.write("MBean path: " + pwd())
    libraryOutputFileWriter.write(System.getProperty("line.separator"))
    libraryOutputFileWriter.write(System.getProperty("line.separator"))

    libraryOutputFileWriter.write("Libary\tVersion\tServer(s) Deployed To")
    libraryOutputFileWriter.write(System.getProperty("line.separator"))

    libraryOutputFileWriter.write("-------\t-------\t-----------------------------")
    libraryOutputFileWriter.write(System.getProperty("line.separator"))

    for library in libraryListing :
      # separate the library name from the version number
      libraryArray = library.split('#')
      myoutput = ""

      itemCount = 0
      for libraryItem in libraryArray :
        itemCount = itemCount + 1
        # item 1 is Library Name
        # item 2 - if exists - is Library Version
        if itemCount > 1:
          myoutput = myoutput + "\t"
        myoutput = myoutput + libraryItem

      # if a library version is not included then add a tab character
      if itemCount < 2:
        myoutput = myoutput + "\t"

      # identify the servers that the library is deployed to
      serverArray = getTargetServers(library)
      srvrCnt = 0
      myoutput = myoutput + "\t"
      for srvrNm in serverArray :
        srvrCnt = srvrCnt + 1
        myoutput = myoutput + srvrNm
        if srvrCnt < len(serverArray) :
          myoutput = myoutput + ', '

      # print to the file
      libraryOutputFileWriter.write(myoutput)
      libraryOutputFileWriter.write(System.getProperty("line.separator"))
  return


"""
---------------------------------------------------------------------
Function to find the applications
---------------------------------------------------------------------
"""
def getAppListing(mbeanPosition):
  cd(mbeanPosition)
  appListing = ls(returnMap='true', returnType='a')

  if appListing.size() > 0 :
    # print file headings
    appOutputFileWriter.write("Deployed Application Listing " + filedate)
    appOutputFileWriter.write(System.getProperty("line.separator"))
    appOutputFileWriter.write(System.getProperty("line.separator"))

    appOutputFileWriter.write("Domain    : " + domainName)
    appOutputFileWriter.write(System.getProperty("line.separator"))

    appOutputFileWriter.write("Server    : " + serverName)
    appOutputFileWriter.write(System.getProperty("line.separator"))

    appOutputFileWriter.write("MBean path: " + pwd())
    appOutputFileWriter.write(System.getProperty("line.separator"))
    appOutputFileWriter.write(System.getProperty("line.separator"))

    appOutputFileWriter.write("Application\tVersion\tServer(s) Deployed To")
    appOutputFileWriter.write(System.getProperty("line.separator"))

    appOutputFileWriter.write("-----------\t-------\t-----------------------------")
    appOutputFileWriter.write(System.getProperty("line.separator"))

    for app in appListing :
      # separate the app name from the version number
      appArray = app.split('#')
      myoutput = ""

      itemCount = 0
      for appItem in appArray :
        itemCount = itemCount + 1
        # item 1 is the application name
        # item 2 is the application version
        if itemCount > 1:
          myoutput = myoutput + "\t"
        myoutput = myoutput + appItem

      # if a application version is not included then add a tab character
      if itemCount < 2:
        myoutput = myoutput + "\t"       

      # identify the servers that the application is deployed to
      serverArray = getTargetServers(app)
      srvrCnt = 0
      myoutput = myoutput + "\t"
      for srvrNm in serverArray :
        srvrCnt = srvrCnt + 1
        myoutput = myoutput + srvrNm
        if srvrCnt < len(serverArray) :
          myoutput = myoutput + ', '

      # print to the file
      appOutputFileWriter.write(myoutput)
      appOutputFileWriter.write(System.getProperty("line.separator"))
  return


"""
---------------------------------------------------------------------
Function to find the servers deployed to
---------------------------------------------------------------------
"""
def getTargetServers(childNodeName):
  serverArray = []
  cd(childNodeName)
  childNodeListing = ls(returnMap='true',returnType='c')
  if childNodeListing.size() > 0 :
    for childNode in childNodeListing :
      if childNode == 'Targets' :
        cd('Targets')
        serverNames = ls(returnMap='true',returnType='c')
        if serverNames.size() > 0 :
          for srvrNm in serverNames :
            serverArray.append(srvrNm)
        # navigate to parent of Targets node
        cd('..')
  # navigate back to parent attribute
  cd('..')
  return serverArray



"""
---------------------------------------------------------------------
Main Routine
---------------------------------------------------------------------
"""
try:
  try:
    connect(username, password, URL)
    serverConfig()
    getLibraryListing('/Libraries')
    getAppListing('/AppDeployments')
  except Exception, e:
    print 'Unable to print libraries and applications'
    print e
finally:
  disconnect()
  libraryOutputFileWriter.close()
  appOutputFileWriter.close()

Save the above script as ListLibraries.py in a directory of your choice or download and rename the source here: ListLibraries.py.txt. As described in the header of this script you can run the script when in WLST with the command execfile('<yourDrive>:/<yourDir>/ListLibraries.py') or with the java command java weblogic.WLST ListLibraries.py.

The output is with a tab delimiter between the library (or application) and the version number. In this way it will be easy to import into Excel with the tab delimiter to separate into columns.

Below is a sample of the output:

Library Listing 2015-01-26

Domain    : dev_domain
Server    : AdminServer
MBean path: serverConfig:/Libraries

Libary    Version    Server(s) Deployed To
-------    -------    -----------------------------
UIX    11@11.1.1.1.0    AdfMngServer, AdminServer
adf.oracle.businesseditor    1.0@11.1.1.2.0    AdfMngServer, AdminServer
adf.oracle.domain    1.0@11.1.1.2.0    AdfMngServer, AdminServer
adf.oracle.domain.webapp    1.0@11.1.1.2.0    AdfMngServer, AdminServer
emai        AdfMngServer, AdminServer
emas        AdfMngServer, AdminServer
emcore        AdfMngServer, AdminServer
jsf    1.2@1.2.9.0    AdfMngServer, AdminServer
jstl    1.2@1.2.0.1    AdfMngServer, AdminServer
ohw-rcf    5@5.0    AdfMngServer, AdminServer
ohw-uix    5@5.0    AdfMngServer, AdminServer
oracle.adf.dconfigbeans    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.adf.desktopintegration    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.adf.desktopintegration.model    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.adf.management    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.bi.adf.model.slib    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.bi.adf.view.slib    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.bi.adf.webcenter.slib    1.0@11.1.1.2.0    AdfMngServer, AdminServer
oracle.bi.composer    11.1.1@0.1    AdfMngServer, AdminServer
oracle.bi.jbips    11.1.1@0.1    AdfMngServer, AdminServer
oracle.bpm.mgmt    11.1.1@11.1.1    AdfMngServer, AdminServer
oracle.dconfig-infra    11@11.1.1.1.0    AdfMngServer, AdminServer
oracle.jrf.system.filter        AdfMngServer, AdminServer
oracle.jsp.next    11.1.1@11.1.1    AdfMngServer, AdminServer
oracle.pwdgen    11.1.1@11.1.1.2.0    AdfMngServer, AdminServer
oracle.webcenter.composer    11.1.1@11.1.1    AdfMngServer, AdminServer
oracle.webcenter.skin    11.1.1@11.1.1    AdfMngServer, AdminServer
oracle.wsm.seedpolicies    11.1.1@11.1.1    AdfMngServer, AdminServer
orai18n-adf    11@11.1.1.1.0    AdfMngServer, AdminServer

View of Import to Excel Tab Delimited

Summary

WLST and Python scripts are very powerful tools for working in the
WebLogic domain. Many tasks can be automated. One powerful use case for
these scripts would be to include them in Jenkins of Hudson to automate
continuous integration processes. Once there is a successful compile and
test of the checked in code you can deploy it immediately to the
targeted environment using WLST and Python.


I suggest that you do a directory search of the Python scripts
supplied in your installation of WebLogic. These can propel you to more
sophisticated and complicated scripting capabilities. There were 749
Python scripts in the WebLogic 10.3.6 installation I performed - a lot
of examples that can be used to further explore.









Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.