Skip to content

Commit

Permalink
Merge pull request #444 from Cognifide/environment-hosts-conventions
Browse files Browse the repository at this point in the history
Environment hosts conventions
  • Loading branch information
Krystian Panek authored Sep 2, 2019
2 parents ce98a98 + faf1718 commit 84ead38
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.cognifide.gradle.aem.AemTask
import com.cognifide.gradle.aem.common.file.FileOperations
import com.cognifide.gradle.aem.common.file.resolver.FileResolver
import com.cognifide.gradle.aem.common.utils.Formats
import com.cognifide.gradle.aem.common.utils.Patterns
import com.cognifide.gradle.aem.environment.docker.base.CygPath
import com.cognifide.gradle.aem.environment.docker.base.DockerRuntime
import com.cognifide.gradle.aem.environment.docker.base.runtime.Toolbox
Expand Down Expand Up @@ -84,7 +85,7 @@ class Environment(@JsonIgnore val aem: AemExtension) : Serializable {
@JsonIgnore
var healthChecker = HealthChecker(this)

val hosts = HostOptions()
val hosts = HostOptions(this)

val created: Boolean
get() = rootDir.exists()
Expand Down Expand Up @@ -226,7 +227,9 @@ class Environment(@JsonIgnore val aem: AemExtension) : Serializable {
/**
* Defines hosts to be appended to system specific hosts file.
*/
fun hosts(names: Iterable<String>) = hosts.define(dockerRuntime.hostIp, names)
fun hosts(names: Iterable<String>) = hosts.other(names.map { url ->
if (!url.contains("://")) "http://$url" else url // backward compatibility
})

/**
* Configures environment service health checks.
Expand Down
32 changes: 19 additions & 13 deletions src/main/kotlin/com/cognifide/gradle/aem/environment/hosts/Host.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@ package com.cognifide.gradle.aem.environment.hosts
import com.cognifide.gradle.aem.environment.EnvironmentException
import com.fasterxml.jackson.annotation.JsonIgnore
import java.io.Serializable
import java.net.URL

class Host(val ip: String, val name: String) : Serializable {
class Host(val url: String) : Serializable {

var ip = "127.0.0.1"

var tags = mutableSetOf<String>()

@get:JsonIgnore
val config = URL(url)

init {
if (ip.isBlank() || name.isBlank()) {
throw EnvironmentException("Invalid hosts configuration (empty value), ip: $ip, name: $name.")
if (url.isBlank()) {
throw EnvironmentException("Host URL cannot be blank!")
}
}

@get:JsonIgnore
val text: String
get() = "$ip\t$name"
get() = "$ip\t${config.host}"

companion object {
fun of(value: String): Host {
val parts = value.trim().split(" ").map { it.trim() }
if (parts.size != 2) {
throw EnvironmentException("Invalid hosts value: '$value'")
}
val (ip, name) = parts
fun tag(id: String) {
tags.add(id)
}

return Host(ip, name)
}
fun tag(ids: Iterable<String>) {
tags.addAll(ids)
}

fun tag(vararg ids: String) = tag(ids.asIterable())
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
package com.cognifide.gradle.aem.environment.hosts

class HostOptions {
import com.cognifide.gradle.aem.environment.Environment
import com.cognifide.gradle.aem.environment.EnvironmentException
import net.minidev.json.annotate.JsonIgnore
import java.io.Serializable

/**
* Manages host definitions in case of different purposes indicated by tags.
*/
class HostOptions(environment: Environment) : Serializable {

var defined = mutableListOf<Host>()

fun define(ip: String, name: String) {
defined.add(Host(ip, name))
}
@JsonIgnore
var ipDefault = environment.dockerRuntime.hostIp

fun define(ip: String, vararg names: String) {
define(ip, names.asIterable())
fun define(url: String, options: Host.() -> Unit = {}) {
defined.add(Host(url).apply { ip = ipDefault; options() })
}

fun define(ip: String, names: Iterable<String>) {
names.forEach { define(ip, it) }
}
fun define(vararg urls: String, options: Host.() -> Unit = {}) = define(urls.asIterable(), options)

fun define(urls: Iterable<String>, options: Host.() -> Unit = {}) = urls.forEach { define(it, options) }

fun find(vararg tags: String) = find(tags.asIterable())

fun find(tags: Iterable<String>) = all(tags).first()

fun define(vararg values: String) {
define(values.asIterable())
fun all(vararg tags: String) = all(tags.asIterable())

fun all(tags: Iterable<String>) = defined.filter { h -> tags.all { t -> h.tags.contains(t) } }.ifEmpty {
throw EnvironmentException("Environment has no hosts tagged with '$tags'!")
}

fun define(values: Iterable<String>) {
defined.addAll(values.map { Host.of(it) })
// ----- DSL shorthands / conventions -----

/**
* Get host responsible for accessing AEM author instance.
*/
@get:JsonIgnore
val author: Host
get() = find(TAG_AUTHOR)

/**
* Get hosts responsible for accessing AEM author instances.
*/
@get:JsonIgnore
val authors: List<Host>
get() = all(TAG_AUTHOR)

fun author(vararg urls: String, options: Host.() -> Unit = {}) = author(urls.asIterable(), options)

fun author(urls: Iterable<String>, options: Host.() -> Unit = {}) = define(urls) { tag(TAG_AUTHOR); options() }

/**
* Get host responsible for accessing AEM author instance.
*/
@get:JsonIgnore
val publish: Host
get() = find(TAG_PUBLISH)

/**
* Get hosts responsible for accessing AEM publish instances.
*/
@get:JsonIgnore
val publishes: List<Host>
get() = all(TAG_PUBLISH)

fun publish(vararg urls: String, options: Host.() -> Unit = {}) = publish(urls.asIterable(), options)

fun publish(urls: Iterable<String>, options: Host.() -> Unit = {}) = define(urls) { tag(TAG_PUBLISH); options() }

@get:JsonIgnore
val others: List<Host>
get() = all(TAG_OTHER)

fun other(vararg urls: String, options: Host.() -> Unit = {}) = other(urls.asIterable(), options)

fun other(urls: Iterable<String>, options: Host.() -> Unit = {}) = define(urls) { tag(TAG_OTHER); options() }

companion object {

val TAG_AUTHOR = "author"

val TAG_PUBLISH = "publish"

val TAG_OTHER = "other"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ open class EnvironmentHosts : AemDefaultTask() {

@TaskAction
fun appendHosts() {
Hosts.of(aem.environment.hosts.defined).append()
val hosts = Hosts.of(aem.environment.hosts.defined)
hosts.append()

aem.notifier.notify("Environment hosts", "Appended with success")
}
Expand Down

0 comments on commit 84ead38

Please sign in to comment.