wildfly groovy.png

Groovy connects with WFLY cli

Starting groovysh with jboss cli jar on classpath could be done in the following way

groovysh -cp $JBOSS_HOME/bin/client/jboss-cli-client.jar

To connect with CLI you need

  1. import scriptsupport package

  2. disable security manager as there is some flaw in handling security policy through client jar up to WildFly. You can define a security manager policy file where permission for everythin is set. Anoth option is to use call System.setSecurityManager(null) which seems to me being easier.

  3. instantiate cli and connect to a running WildFly instance

import org.jboss.as.cli.scriptsupport.*
cli = CLI.newInstance()
cli.connect("remote", "localhost", 9999, null, null)

Utility methods

def getResponseAsStringList(def result) {
  list = []
  result.getResponse().get("result").asList().each {list << it.asString()}
  return list

The script

And here is a script which loads data about servers in domain. If you print the content of the variables hostServer, profileServer or groupProfile you will get listing you could be interested in.

groupProfile = [:]
groups = getResponseAsStringList(cli.cmd(':read-children-names(child-type=server-group)'))
profiles = getResponseAsStringList(cli.cmd(':read-children-names(child-type=profile)'))

groups.each { group ->
  profile = cli.cmd("/server-group=$group:read-attribute(name=profile, include-defaults=true)")
  groupProfile[group] = profile

hostServer = [:]
profileServer = [:]
profiles.each {profile -> profileServer[profile] = []}
hosts = getResponseAsStringList(cli.cmd(':read-children-names(child-type=host)'))

hosts.each { host ->
 resultServers = cli.cmd("/host=$host:read-children-names(child-type=server)")
 servers = getResponseAsStringList(resultServers)
 serverProfile = [:]
 servers.each { server ->
   status = cli.cmd("/host=$host/server-config=$server:read-attribute(name=status, include-defaults=true)")
   group = cli.cmd("/host=$host/server-config=$server:read-attribute(name=group, include-defaults=true)")
   // when disabled profile is not filled
   // profile = cli.cmd("/host=$host/server=$server:read-attribute(name=profile-name, include-defaults=true)")
   profile = groupProfile[group]
   serverProfile[server] = [status, group, profile]
   // if (profileServer[profile] == null)  profileServer[profile] = []
   profileServer[profile] << "${host}:${server}"
 hostServer[host] = serverProfile
comments powered by Disqus