将Wildfly-CLI脚本中的属性用于if / else逻辑

时间:2018-11-07 10:41:11

标签: jboss wildfly

在Wildfly CLI脚本中,可以测试资源的存在并执行一些条件逻辑:

if (outcome != success) of /subsystem=datasources/xa-data-source=MY_DATASOURCE:read-resource  
// now do something  

运行cli时,您可以提供脚本中可用的属性,如下所示:

$JBOSS_HOME/bin/jboss-cli.sh --file=my.cli --properties=my.properties  

我想基于这些属性介绍一些条件逻辑,以进行一些条件配置。

一个示例是配置邮件服务器,有时邮件服务器需要用户名和密码,而在其他情况下,它允许匿名访问。设置邮件服务器配置时,我希望能够具有这样的条件逻辑

if MAILSERVER_USERNAME is defined //confiure mailserver with username password else // configure mailserver without attributes username/password

我唯一可以看到的是,如果我已经在我的standalone-full.xml中添加了系统属性设置,我可以这样查询它:

if (outcome != success) of /system-property=foo:read-resource  
 // now do something  

我想根据从my.properties传入的属性做一些类似的事情。

这可能吗?

谢谢

丹尼尔

1 个答案:

答案 0 :(得分:2)

您无法通过--properties传递的属性可以在每种情况下使用,这使得测试它们的存在变得很麻烦:

[standalone@localhost] :resolve-expression(expression=$myProperty)
Unrecognized variable myProperty
[standalone@localhost] :resolve-expression(expression=${myProperty})
{
    "outcome" => "failed",
    "failure-description" => "WFLYCTL0211: Cannot resolve expression '${myProperty}'",
    "rolled-back" => true
}

不过,您可以通过在cli脚本中使用set命令来解决该问题:

[standalone@localhost] set myProperty=${myProperty}
[standalone@localhost] :resolve-expression(expression=$myProperty)
{
    "outcome" => "success",
    "result" => "myValue"
}

然后您可以使用resolve-expression命令的结果来测试属性的存在:

[standalone@localhost] if (outcome == success) of :resolve-expression(expression=$myProperty)
[standalone@localhost] echo success ! myProperty is set ( $myProperty )
[standalone@localhost] end-if
success ! myProperty is set ( myValue )

如果未定义您的媒体资源,尝试使用resolve-expression来解决它会产生错误:

[standalone@localhost] set myProperty=${notMyProperty}
[standalone@localhost] :resolve-expression(expression=$myProperty)
{
    "outcome" => "failed",
    "failure-description" => "WFLYCTL0211: Cannot resolve expression '${notMyProperty}'",
    "rolled-back" => true
}