Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
changed printlns to slf4j logger (#16); rethrowing exceptions so the …
Browse files Browse the repository at this point in the history
…task will fail (#17)
  • Loading branch information
Ingwersaft committed Feb 20, 2019
1 parent 109be41 commit 18ddb06
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
15 changes: 9 additions & 6 deletions src/main/kotlin/com/mkring/wildlydeplyplugin/CliExecutioner.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mkring.wildlydeplyplugin

import org.jboss.`as`.cli.scriptsupport.CLI
import org.slf4j.LoggerFactory

private val log = LoggerFactory.getLogger(CliExecutioner::class.java)

object CliExecutioner {
fun execute(
Expand All @@ -10,23 +13,23 @@ object CliExecutioner {
password: String?,
commands: List<String>
) {
println("deploy(): " + this)
log.debug("deploy(): " + this)
checkHostDns(host)
checkSocket(host, port)
CLI.newInstance().let { cli ->
println("wildfly connect with $user on $host:$port")
log.debug("wildfly connect with $user on $host:$port")
connect(cli, host, port, user, password)

commands.forEach {
println("going to execute `$it`")
log.debug("going to execute `$it`")
val result: CLI.Result? = cli.cmd(it)
println("result: ${result?.isSuccess}")
log.debug("result: ${result?.isSuccess}")
try {
result?.response?.get("result")?.asString()?.let {
println("result string:\n$it")
log.debug("result string:\n$it")
} ?: run { println("result or response null") }
} catch (e: Exception) {
println("cmd might have failed: ${e.message}")
log.debug("cmd might have failed: ${e.message}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.slf4j.LoggerFactory

open class DeployWildflyPlugin : Plugin<Project> {
val log = LoggerFactory.getLogger(DeployWildflyPlugin::class.java)
override fun apply(project: Project) {
println("DeployWildflyPlugin applied")
log.debug("DeployWildflyPlugin applied")
}
}

open class DeployWildflyTask : DefaultTask() {
val log = LoggerFactory.getLogger(DeployWildflyTask::class.java)

@Input
var file: String? = null
Expand Down Expand Up @@ -57,24 +60,24 @@ open class DeployWildflyTask : DefaultTask() {
@TaskAction
fun deployWildfly() {
if (file == null || user == null || password == null) {
println("DeployWildflyTask: missing configuration")
log.error("DeployWildflyTask: missing configuration")
return
}
if (reload && restart) {
println("reload && restart are mutually exclusive!")
log.error("reload && restart are mutually exclusive!")
return
}
if (awaitReload && awaitRestart) {
println("awaitReload && awaitRestart are mutually exclusive!")
log.error("awaitReload && awaitRestart are mutually exclusive!")
return
}
if (awaitReload && reload.not()) {
println("awaitReload is pointless if no reload is set")
log.warn("awaitReload is pointless if no reload is set")
}
if (awaitRestart && restart.not()) {
println("awaitRestart is pointless if no restart is set")
log.warn("awaitRestart is pointless if no restart is set")
}
println("deployWildfly: going to deploy $file to $host:$port")
log.info("deployWildfly: going to deploy $file to $host:$port")
try {
FileDeployer(
file,
Expand All @@ -92,8 +95,8 @@ open class DeployWildflyTask : DefaultTask() {
awaitRestart
).deploy()
} catch (e: Exception) {
println("deployWildfly task failed: ${e.message}")
e.printStackTrace()
log.error("deployWildfly task failed: ${e.message}", e)
throw e
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.mkring.wildlydeplyplugin
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.slf4j.LoggerFactory

open class ExecuteWildflyTask : DefaultTask() {
val log = LoggerFactory.getLogger(ExecuteWildflyTask::class.java)
@Input
var host: String = "localhost"
@Input
Expand All @@ -26,18 +28,18 @@ open class ExecuteWildflyTask : DefaultTask() {
@TaskAction
fun executeWildfly() {
if (user == null || password == null || commands == null || commands?.isEmpty() == true) {
println("ExecuteWildflyTask: missing configuration")
log.error("ExecuteWildflyTask: missing configuration")
return
}
println("ExecuteWildflyTask: on $host:$port going to execute:")
log.info("ExecuteWildflyTask: on $host:$port going to execute:")
commands?.forEach {
println("`$it`")
log.debug("command: `$it`")
}
try {
CliExecutioner.execute(host, port, user, password, commands ?: emptyList())
} catch (e: Exception) {
println("ExecuteWildflyTask task failed: ${e.message}")
e.printStackTrace()
log.error("ExecuteWildflyTask task failed: ${e.message}", e)
throw e
}
}
}
53 changes: 28 additions & 25 deletions src/main/kotlin/com/mkring/wildlydeplyplugin/FileDeployer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package com.mkring.wildlydeplyplugin
import org.jboss.`as`.cli.CommandLineException
import org.jboss.`as`.cli.scriptsupport.CLI
import org.jboss.dmr.ModelNode
import org.slf4j.LoggerFactory
import java.io.File
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Socket
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

private val log = LoggerFactory.getLogger(FileDeployer::class.java)

class FileDeployer(
private val file: String?,
private val host: String,
Expand All @@ -25,12 +28,13 @@ class FileDeployer(
private val restart: Boolean,
private val awaitRestart: Boolean
) {

fun deploy() {
println("deploy(): " + this)
log.debug("deploy(): " + this)
checkHostDns(host)
checkSocket(host, port)
CLI.newInstance().let { cli ->
println("wildfly connect with $user on $host:$port")
log.debug("wildfly connect with $user on $host:$port")
connect(cli, host, port, user, password)
val force = if (force) {
"--force"
Expand All @@ -47,17 +51,17 @@ class FileDeployer(
} else {
""
}
println("connected successfully")
log.debug("connected successfully")
val deploymentExists = File(file).isFile
println("given $file existent: $deploymentExists")
log.debug("given $file existent: $deploymentExists")
if (deploymentExists.not()) throw IllegalStateException("couldn't find given deployment")

if (undeployBeforehand) {
println("\nundeploying existing deployment with same name if preset...")
log.debug("\nundeploying existing deployment with same name if preset...")
val shouldUndeploy =
blockingCmd("deployment-info", 2, ChronoUnit.MINUTES).response.get("result").asList()
.any { it.asProperty().name == name.removePrefix("--name=") }
println("shouldUndeploy=$shouldUndeploy")
log.debug("shouldUndeploy=$shouldUndeploy")
if (shouldUndeploy) {
blockingCmd("undeploy $name", 2, ChronoUnit.MINUTES).response.also {
println("undeploy response: $it\n")
Expand All @@ -67,26 +71,26 @@ class FileDeployer(

// deploy
val deploySuccess = cli.cmd("deploy $force $name $runtimeName $file").isSuccess
println("deploy success: $deploySuccess")
log.debug("deploy success: $deploySuccess")

enableDeploymentIfNecessary(name)

if (reload) {
try {
println("going to reload wildfly")
log.debug("going to reload wildfly")
val reloadSuccess = cli.cmd("reload").isSuccess
println("reload success: $reloadSuccess")
log.debug("reload success: $reloadSuccess")
} catch (e: CommandLineException) {
println("looks like reload timed out: ${e.message}")
log.debug("looks like reload timed out: ${e.message}")
}
}
if (restart) {
try {
println("going to restart wildfly")
log.debug("going to restart wildfly")
val restartSuccess = cli.cmd("shutdown --restart=true").isSuccess
println("restart success: $restartSuccess")
log.debug("restart success: $restartSuccess")
} catch (e: CommandLineException) {
println("looks like restart timed out: ${e.message}")
log.debug("looks like restart timed out: ${e.message}")
}
}
cli.disconnect()
Expand All @@ -101,15 +105,15 @@ class FileDeployer(
}

fun blockTillCliIsBack() {
println("going to block until the reload/restart finished...\n")
log.debug("going to block until the reload/restart finished...\n")
Thread.sleep(1000)
val postReloadDeploymentInfoPrettyPrint =
blockingCmd("deployment-info", 1, ChronoUnit.MINUTES).response.responsePrettyPrint()
println("\n\nPOST reload/restart deployment info:\n$postReloadDeploymentInfoPrettyPrint")
log.debug("\n\nPOST reload/restart deployment info:\n$postReloadDeploymentInfoPrettyPrint")
}

private fun enableDeploymentIfNecessary(name: String) {
println("\nchecking if deployment is enabled...")
log.debug("\nchecking if deployment is enabled...")
val deploymentEnabled =
blockingCmd("deployment-info", 2, ChronoUnit.MINUTES).response.get("result").asList().map {
it.asProperty().name to it.getParam("enabled").removePrefix("enabled: ")
Expand All @@ -118,9 +122,9 @@ class FileDeployer(
}?.second?.toBoolean()

if (deploymentEnabled == false) {
println("not enabled! going to enable now!")
log.debug("not enabled! going to enable now!")
blockingCmd("deploy $name", 2, ChronoUnit.MINUTES).response.also {
println("enable response: $it\n")
log.debug("enable response: $it\n")
}
}
}
Expand All @@ -140,14 +144,14 @@ class FileDeployer(
}
return cmd
} catch (e: Exception) {
println("connect + cmd exception: ${e::class.java.simpleName}:${e.message}")
log.debug("connect + cmd exception: ${e::class.java.simpleName}:${e.message}")
Thread.sleep(500)
continue
} finally {
try {
cli.disconnect()
} catch (e: Exception) {
println("disconnect exception: ${e::class.java.simpleName}:${e.message}")
log.debug("disconnect exception: ${e::class.java.simpleName}:${e.message}")
throw e
}
}
Expand Down Expand Up @@ -181,19 +185,18 @@ fun connect(
}

fun checkHostDns(host: String) {
println("$host DNS: ${InetAddress.getAllByName(host).joinToString(";")}")
log.debug("$host DNS: ${InetAddress.getAllByName(host).joinToString(";")}")
}

fun checkSocket(host: String, port: Int) {
Socket().use {
try {
it.connect(InetSocketAddress(host, port), 2000)
it.close()
println("socket connect worked")
log.debug("socket connect worked")
} catch (e: Exception) {
println("looks like we can't connect?!")
println("${e.message}")
e.printStackTrace()
log.debug("looks like we can't connect?!")
log.debug("${e.message}")
}
}
}

0 comments on commit 18ddb06

Please sign in to comment.