首页 > 解决方案 > [WebSphere]:项目未正确附加到列表并且正则表达式失败

问题描述

AdminApp.list()我们正在尝试通过和的组合在没有人工干预的情况下停止特定应用程序regex

最终目标是易于使用。用户必须能够在不登录到 WAS 控制台或修改脚本中的目标应用程序名称值的情况下停止应用程序。他们必须登录 Jenkins 并执行此 stopapp.py 脚本。一定就是这么简单。

根据此脚本,WAS 返回其 DMGR 中当前存在的应用程序列表。然后我们将这些项目附加到一个空列表中,然后运行一个正则表达式例程 (re.match) 来识别特定的应用程序名称。然后我们将提取的应用程序名称值替换为我们试图停止的应用程序的 URI。如果成功,应用程序将停止,状态将跨节点同步。

这是我的代码

    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()

输出如下:

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'

虽然它正在打印一个列表(以不那么正确的格式),但它不正确地附加到一个空列表。它获取每个应用程序名称,然后将它们拆分为单独的字母,然后附加到一个空列表。

结果,正则表达式模式匹配返回None而不是应用程序名称。

请指导我,以便我可以纠正错误并自动化这些东西。我觉得 split 方法和 append 将解决这个问题。但我不知道在 split 方法中放置什么样的分隔符。这只是我的感觉,但每个人都可以自由地表达他们的想法。

谢谢和问候 - KrisT :)

标签: pythonregexjythonwebsphere-8wsadmin

解决方案


根据您的输出,打印appObj会导致应用程序使用换行符打印。如果是这种情况,请尝试以下方法。这appList通过获取字符串输出并将其拆分为换行符来创建您的:

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()

这应该停止它附加每个字母。使用.splitlines()可能是比使用更安全的方法.split('\n')。如果appObj已经是一个字符串,那么您也可以删除str().


推荐阅读