1

We are trying to stop a particular app without manual intervention through a combination of AdminApp.list() and regex.

The end target is: Ease of USE. A user must be able to stop an application without logging-in to a WAS console or modifying the target application name values in the script. They must login into Jenkins and execute this stopapp.py script. It must be that simple.

According to this script, WAS returns a list of apps currently present in its DMGR. Then we append these items to an empty list, and then run a regex routine (re.match) to identify a particular app name. Then we substitute the extracted app name value in URI of the application which we are trying to stop. If successful, app will be stopped and status would be synced across nodes.

Here is my code:

    import re

    moduleName = "meap"
    ctxRootOld = "/meap_old"

    appList = []

    appObj = AdminApp.list("WebSphere:cell=myCell,node=myNode,server=myServer")
    print("These are the list of apps running in WAS DMGR: ")
    print(appObj)

    for apps in appObj:
        appList.append(apps)

    print("Now appending the apps to an empty list...Here are the values")
    print(appList)

    for app in appList:
        appMatch = re.match("^ABC\w+$", app)
        print(appMatch)
        result = appMatch.group(0)

    appStopName = result
    print (appStopName)

    appStopURI = ""+appStopName+".war,WEB-INF/web.xml"

    appmanager = AdminControl.queryNames('cell=myCell,node=myNode,type=ApplicationManager,*')

    AdminControl.invoke(appmanager, 'stopApplication', appStopName)

    AdminApp.edit(appStopName, ['-CtxRootForWebMod', [[moduleName, appStopURI, ctxRootOld]]])

    AdminConfig.save()

    AdminNodeManagement.syncActiveNodes()

The output is as following:

WASX7209I: Connected to process "dmgr" on node CellManager03 using SOAP connector;  The type of process is: DeploymentManager

These are the apps running in WAS DMGR:
DefaultApp
ABCPreProd

Now appending the apps to an empty list...Here are the values
['D','e','f','a','u','l','t'.....'P','r','o','d']

None

WASX7017E: Exception received while running file "/app/was_scripts/stopapp.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<string>", line 11, in ?
AttributeError: 'None' object has no attribute 'group'

Though it is printing a list (in a not-so proper format), but it is improperly appending to an empty list. It is taking each application name, then splitting them into individual letters, and then appending to an empty list.

As a result, the regex pattern matching is returning None instead of app name.

Please guide me so that I can rectify the error and automate this stuff. I feel a split method along with append will resolve the issue. But I don't know what kind of separator to put inside the split method. This is only my feeling, but everyone is free to send in their thoughts.

Thanks and Regards- KrisT :)

CK5
  • 1,055
  • 3
  • 16
  • 29

1 Answers1

0

According to your output, printing appObj results in apps being printed with newlines. If this is the case, try the following approach. This creates your appList by taking the string output and splitting it on the line breaks:

import re

moduleName = "meap"
ctxRootOld = "/meap_old"

appObj = AdminApp.list("WebSphere:cell=myCell,node=myNode,server=myServer")
print("These are the list of apps running in WAS DMGR: ")
print(appObj)

print("Now appending the apps to an empty list...Here are the values")
appList = str(appObj).splitlines()
print(appList)

for app in appList:
    appMatch = re.match("^ABC\w+$", app)
    print(appMatch)
    result = appMatch.group(0)

appStopName = result
print (appStopName)

appStopURI = ""+appStopName+".war,WEB-INF/web.xml"
appmanager = AdminControl.queryNames('cell=myCell,node=myNode,type=ApplicationManager,*')
AdminControl.invoke(appmanager, 'stopApplication', appStopName)
AdminApp.edit(appStopName, ['-CtxRootForWebMod', [[moduleName, appStopURI, ctxRootOld]]])
AdminConfig.save()

This should stop it appending each letter. Using .splitlines() can be a safer approach than using .split('\n'). If appObj is already a string, then you can also remove str().

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Hi @Martin Evans. I'm sorry for the late reply. I tried an alternative method and it worked. I added split('\n') to AdminApp.list() command right at the top. After applying split, things are working as intended. But I will try out splitlines() method as well and let you know about the output :-) – CK5 Apr 11 '19 at 05:39