Skip to content

Instantly share code, notes, and snippets.

@seanf
Created August 23, 2017 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanf/5d8bd26516d27491ef78c801e11c1966 to your computer and use it in GitHub Desktop.
Save seanf/5d8bd26516d27491ef78c801e11c1966 to your computer and use it in GitHub Desktop.
Idempotent jboss-cli with Kotlin
import org.jboss.`as`.cli.scriptsupport.CLI
import org.slf4j.LoggerFactory
/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
// Refs:
// https://developer.jboss.org/wiki/AdvancedCLIScriptingWithGroovyRhinoJythonEtc
// https://github.com/wildfly/wildfly-core/pull/1501/files?diff=unified#diff-25ae58578a2f30dd07236a549dc2f8d9R62
// Note that running CLI/embed-server seems to have side effects which cause problems if you later launch wildfly
// so it may be best to do this in a separate subprocess first.
object Setup {
private val log = LoggerFactory.getLogger(Setup::class.java)
@JvmStatic
@JvmSuppressWildcards
fun main(args: Array<String>) {
val jbossHome = System.getProperty("jboss.home") ?: throw RuntimeException(
"System property jboss.home needs to be set")
// NB may need to set jboss.home.dir if adding modules
// System.setProperty("jboss.home.dir", jbossHome)
val cli = CLI.newInstance()
cli.execute("embed-server --std-out=echo --server-config=standalone-full.xml --jboss-home=$jbossHome")
try {
// this config came from zanata-config-arq-test.cli
cli.executeQuietly("data-source remove --name=zanataDatasource")
cli.execute("""data-source add --name=zanataDatasource \
--jndi-name=java:jboss/datasources/zanataDatasource --driver-name=h2 \
--connection-url=jdbc:h2:mem:zanata;DB_CLOSE_DELAY=-1 \
--user-name=sa --password=sa \
--validate-on-match=false --background-validation=false \
--valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.novendor.JDBC4ValidConnectionChecker \
--exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.novendor.NullExceptionSorter \
--use-ccm=true""")
} finally {
cli.execute("stop-embedded-server")
}
}
private fun CLI.execute(command: String) {
log.info("Executing: $command")
val result = cmd(command)
if (!result.isSuccess) throw Exception("Command failed: $command")
}
private fun CLI.executeQuietly(command: String) {
try {
log.info("Executing: $command")
cmd(command)
} catch (_: Exception) {
// ignore failure
log.info("Ignoring failed command: $command")
}
}
}
@seanf
Copy link
Author

seanf commented Aug 23, 2017

Maven dependency:

    <dependency>
      <groupId>org.wildfly.core</groupId>
      <artifactId>wildfly-cli</artifactId>
      <version>2.2.0.Final</version>
    </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment