Skip to content

Commit

Permalink
Features, features...
Browse files Browse the repository at this point in the history
  • Loading branch information
Krystian Panek committed Aug 19, 2020
1 parent ec60231 commit 9d02f3c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,36 @@ class Container(val docker: Docker, val name: String) {
this.exitCodes = exitCode?.run { listOf(this) } ?: listOf()
}

fun ensureDir(vararg paths: String) = paths.forEach { path ->
execShell("Ensuring directory at path '$path'", "mkdir -p $path")
fun ensureFile(vararg paths: String) = ensureFile(paths.asIterable())

fun ensureFile(paths: Iterable<String>) {
ensureDir(paths.map { it.substringBeforeLast("/") })

val command = "touch ${paths.joinToString(" ")}"
when (paths.count()) {
1 -> execShell("Ensuring file '${paths.first()}'", command)
else -> execShell("Ensuring files (${paths.count()})", command)
}
}

fun cleanDir(vararg paths: String) = paths.forEach { path ->
execShell("Cleaning directory contents at path '$path'", "rm -fr $path/*")
fun ensureDir(vararg paths: String) = ensureDir(paths.toList())

fun ensureDir(paths: Iterable<String>) {
val command = "mkdir -p ${paths.joinToString(" ")}"
when (paths.count()) {
1 -> execShell("Ensuring directory '${paths.first()}'", command)
else -> execShell("Ensuring directories (${paths.count()})", command)
}
}

fun cleanDir(vararg paths: String) = cleanDir(paths.asIterable())

fun cleanDir(paths: Iterable<String>) {
val command = "rm -fr ${paths.joinToString(" ") { "$it/*" }}"
when (paths.count()) {
1 -> execShell("Cleaning contents of directory at path '${paths.first()}'", command)
else -> execShell("Cleaning contents of directories (${paths.count()})", command)
}
}

private fun exec(spec: ExecSpec): DockerResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class ContainerManager(private val docker: Docker) {

val defined = mutableListOf<Container>()

val dependent = common.obj.boolean {
convention(true)
common.prop.boolean("environment.docker.container.dependent")?.let { set(it) }
}

/**
* Define container.
*/
Expand Down Expand Up @@ -51,14 +56,22 @@ class ContainerManager(private val docker: Docker) {
fun up() {
common.progress {
message = "Configuring container(s): ${defined.names}"
common.parallel.each(defined) { it.up() }
if (dependent.get()) {
defined.forEach { it.up() }
} else {
common.parallel.each(defined) { it.up() }
}
}
}

fun reload() {
common.progress {
message = "Reloading container(s): ${defined.names}"
common.parallel.each(defined) { it.reload() }
if (dependent.get()) {
defined.forEach { it.reload() }
} else {
common.parallel.each(defined) { it.reload() }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Docker(val environment: EnvironmentExtension) {

val composeTemplateFile = common.obj.relativeFile(environment.sourceDir, "docker-compose.yml.peb")

val composeProperties = common.obj.map<String, Any?> { convention(mapOf()) }

// Shorthands useful to be used in template: 'docker-compose.yml.peb'

val configPath get() = runtime.determinePath(environment.sourceDir.get().asFile)
Expand All @@ -68,7 +70,10 @@ class Docker(val environment: EnvironmentExtension) {

targetFile.takeIf { it.exists() }?.delete()
templateFile.copyTo(targetFile)
common.prop.expand(targetFile, mapOf("docker" to this))
common.prop.expand(targetFile, composeProperties.get() + mapOf(
"docker" to this,
"project" to common.project
))
}

fun up() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,29 @@ class HostFileManager(val container: Container) {
return files
}

fun ensureFile(vararg paths: String, content: String = "") = paths.forEach { path ->
ensureDir(path.substringBeforeLast("/"))
file(path).apply {
if (!exists()) {
logger.info("Ensuring file '$this' for container '${container.name}'")
writeText(content)
}
}
}

fun ensureDir() {
rootDir.get().asFile.apply {
logger.info("Ensuring root directory '$this' for container '${container.name}'")
mkdirs()
}
}

fun ensureDir(vararg paths: String) = paths.forEach { path ->
file(path).apply {
logger.info("Ensuring directory '$this' for container '${container.name}'")
mkdirs()
fun ensureDir(vararg paths: String) {
paths.forEach { path ->
file(path).apply {
logger.info("Ensuring directory '$this' for container '${container.name}'")
mkdirs()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ class Host(val url: String) {
}

fun tag(vararg ids: String) = tag(ids.asIterable())

override fun toString(): String = "Host(url='$url', ip='$ip', tags=$tags)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class HostOptions(environment: EnvironmentExtension) : Serializable {

operator fun String.invoke(options: Host.() -> Unit = {}) = define(this, options)

operator fun String.invoke(vararg tags: String) = define(this) { tag(tags.asIterable()) }

fun define(url: String, options: Host.() -> Unit = {}) {
defined.add(common.obj.provider { Host(url).apply { ip = ipDefault.get(); options() } })
}
Expand Down

0 comments on commit 9d02f3c

Please sign in to comment.