Created
August 23, 2017 02:06
-
-
Save seanf/5d8bd26516d27491ef78c801e11c1966 to your computer and use it in GitHub Desktop.
Idempotent jboss-cli with Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maven dependency: