python - [WebSphere] : Items are not properly appending to a list and regex is failing

标签 python regex jython websphere-8 wsadmin

我们正在尝试通过 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 方法和附加方法可以解决这个问题。但我不知道在 split 方法中放置什么样的分隔符。这只是我个人的感受,大家可以发表自己的想法。

感谢和问候 - KrisT :)

最佳答案

根据您的输出,打印 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()

关于python - [WebSphere] : Items are not properly appending to a list and regex is failing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587896/

相关文章:

python - 在哪里可以找到有关Django ORM返回类型/可能引发的异常的文档

python - 根据pygame中的点击更改图像

Python 列表解析

regex - 使用 awk 只包含以特定值开头的列?

c# - 为什么 Regex.Match 只返回 1 个结果?

python - 在 Jython 中使用 PyCrypto 导入问题

python - Django - Django > 1.9 中的 'sqlclear' 等价物

正则表达式 - 将单词与共享空间匹配

python - 无法让 Jython 在 Mac 上工作

python - 在 ImageJ 中安装 python 脚本