forked from wttech/gradle-aem-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.kt
139 lines (105 loc) · 3.36 KB
/
Environment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.cognifide.gradle.aem.environment
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.environment.docker.Docker
import com.cognifide.gradle.aem.environment.health.HealthChecker
import com.cognifide.gradle.aem.environment.health.HealthStatus
import com.cognifide.gradle.aem.environment.hosts.HostOptions
import com.fasterxml.jackson.annotation.JsonIgnore
import java.io.File
import java.io.Serializable
class Environment(@JsonIgnore val aem: AemExtension) : Serializable {
/**
* Path in which local AEM environment will be stored.
*/
var rootDir: File = aem.props.string("environment.rootDir")?.let { aem.project.file(it) }
?: aem.projectMain.file(".environment")
/**
* Convention directory for storing environment specific configuration files.
*/
val configDir
get() = File(aem.configCommonDir, ENVIRONMENT_DIR)
@JsonIgnore
val docker = Docker(this)
fun docker(options: Docker.() -> Unit) {
docker.apply(options)
}
@JsonIgnore
var healthChecker = HealthChecker(this)
val hosts = HostOptions(this)
val created: Boolean
get() = rootDir.exists()
@get:JsonIgnore
val running: Boolean
get() = docker.running
fun resolve() {
docker.containers.resolve()
}
fun up() {
if (running) {
aem.logger.info("Environment is already running!")
return
}
aem.logger.info("Turning on: $this")
docker.init()
docker.up()
aem.logger.info("Turned on: $this")
}
fun down() {
if (!running) {
aem.logger.info("Environment is not yet running!")
return
}
aem.logger.info("Turning off: $this")
docker.down()
aem.logger.info("Turned off: $this")
}
fun restart() {
down()
up()
}
fun destroy() {
aem.logger.info("Destroying: $this")
rootDir.deleteRecursively()
aem.logger.info("Destroyed: $this")
}
fun check(verbose: Boolean = true): List<HealthStatus> {
if (!running) {
throw EnvironmentException("Cannot check environment as it is not running!")
}
aem.logger.info("Checking $this")
return healthChecker.check(verbose)
}
fun reload() {
if (!running) {
throw EnvironmentException("Cannot reload environment as it is not running!")
}
aem.logger.info("Reloading $this")
docker.reload()
aem.logger.info("Reloaded $this")
}
fun hosts(options: HostOptions.() -> Unit) {
hosts.apply(options)
}
/**
* Defines hosts to be appended to system specific hosts file.
*/
fun hosts(vararg values: String) = hosts(values.toList())
/**
* Defines hosts to be appended to system specific hosts file.
*/
fun hosts(names: Iterable<String>) = hosts.other(names.map { url ->
if (!url.contains("://")) "http://$url" else url // backward compatibility
})
/**
* Configures environment service health checks.
*/
fun healthChecks(options: HealthChecker.() -> Unit) {
healthChecker.apply(options)
}
override fun toString(): String {
return "Environment(root=$rootDir,running=$running)"
}
companion object {
const val ENVIRONMENT_DIR = "environment"
}
}