-
Notifications
You must be signed in to change notification settings - Fork 32
/
InstanceManager.kt
209 lines (167 loc) · 7.73 KB
/
InstanceManager.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.cognifide.gradle.aem.common.instance
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.common.instance.action.*
import com.cognifide.gradle.aem.common.instance.provision.Provisioner
import com.cognifide.gradle.aem.common.instance.satisfy.Satisfier
import com.cognifide.gradle.aem.common.instance.tail.Tailer
import com.cognifide.gradle.aem.instance.InstancePlugin
import com.cognifide.gradle.common.pluginProject
import com.cognifide.gradle.common.utils.using
open class InstanceManager(val aem: AemExtension) {
private val project = aem.project
val local by lazy { aem.localInstanceManager }
/**
* Using remote AEM instances is acceptable in any project, so that lookup for project applying local instance plugin is required
* Needed to determine common directory storing instance related resources (tailer incident filter, Groovy scripts etc).
*/
val projectDir = aem.obj.dir {
convention(aem.obj.provider {
project.pluginProject(InstancePlugin.ID)?.layout?.projectDirectory ?: throw InstanceException(
"Using remote AEM instances requires having at least one project applying plugin '${InstancePlugin.ID}'" +
" or setting property 'instance.projectDir'!"
)
})
aem.prop.string("instance.projectDir")?.let { set(project.rootProject.file(it)) }
}
/**
* Directory storing instance wide configuration files.
*/
val configDir = aem.obj.dir {
convention(projectDir.dir(aem.prop.string("instance.configPath") ?: "src/aem/instance"))
aem.prop.file("instance.configDir")?.let { set(it) }
}
/**
* Directory storing outputs of instance tasks.
*/
val buildDir = aem.obj.dir {
convention(projectDir.dir("build/instance"))
aem.prop.file("instance.buildDir")?.let { set(it) }
}
val satisfier by lazy { Satisfier(this) }
fun satisfier(options: Satisfier.() -> Unit) = satisfier.using(options)
val provisioner by lazy { Provisioner(this) }
fun provisioner(options: Provisioner.() -> Unit) = provisioner.using(options)
val tailer by lazy { Tailer(this) }
fun tailer(options: Tailer.() -> Unit) = tailer.using(options)
fun resolveFiles() {
satisfier.resolve()
}
// ===== Definition API =====
/**
* List of AEM instances e.g on which packages could be deployed.
* Instance stored in map ensures name uniqueness and allows to be referenced in expanded properties.
*/
val defined = aem.obj.list<Instance> {
convention(aem.obj.provider {
val fromCmd = aem.prop.string("instance.list")?.let {
Instance.parse(aem, it) { env = Instance.ENV_CMD }
} ?: listOf()
val fromProperties = Instance.properties(aem)
(fromCmd + fromProperties).ifEmpty { Instance.defaultPair(aem) }
})
}
/**
* Map of AEM instances with names as a keys.
*/
val all = defined.map { p -> p.map { it.name to it }.toMap() }
/**
* Customize default options for instance services.
*/
fun sync(options: InstanceSync.() -> Unit) {
syncOptions = options
}
internal var syncOptions: InstanceSync.() -> Unit = {}
/**
* Define local instance (created on local file system).
*/
fun local(httpUrl: String) = local(httpUrl) {}
/**
* Define local instance (created on local file system).
*/
fun local(httpUrl: String, name: String) = local(httpUrl) { this.name = name }
/**
* Define local instance (created on local file system).
*/
fun local(httpUrl: String, options: LocalInstance.() -> Unit) {
defined.add(aem.obj.provider { LocalInstance.create(aem, httpUrl, options) })
}
/**
* Define remote instance (available on any host).
*/
fun remote(httpUrl: String) = remote(httpUrl) {}
/**
* Define remote instance (available on any host).
*/
fun remote(httpUrl: String, name: String) = remote(httpUrl) { this.name = name }
/**
* Define remote instance (available on any host).
*/
fun remote(httpUrl: String, options: Instance.() -> Unit) {
defined.add(aem.obj.provider { Instance.create(aem, httpUrl, options) })
}
fun named(name: String) = defined.get().firstOrNull()
?: throw InstanceException("Instance named '$name' is not defined!")
/**
* Get defined instance by name or create temporary definition if URL provided.
*/
fun parse(url: String): Instance = Instance.parse(aem, url).ifEmpty {
throw InstanceException("Instance URL cannot be parsed properly '$url'!")
}.single()
// ===== Actions API =====
fun awaitUp(instance: Instance, options: AwaitUpAction.() -> Unit = {}) = awaitUp(listOf(instance), options)
fun awaitUp(instances: Collection<Instance> = aem.instances, options: AwaitUpAction.() -> Unit = {}) {
AwaitUpAction(aem).apply(options).perform(instances)
}
fun awaitDown(instance: Instance, options: AwaitDownAction.() -> Unit = {}) = awaitDown(listOf(instance), options)
fun awaitDown(instances: Collection<Instance> = aem.instances, options: AwaitDownAction.() -> Unit = {}) {
AwaitDownAction(aem).apply(options).perform(instances)
}
fun awaitReloaded(instance: Instance, reloadOptions: ReloadAction.() -> Unit = {}, awaitUpOptions: AwaitUpAction.() -> Unit = {}) {
awaitReloaded(listOf(instance), reloadOptions, awaitUpOptions)
}
fun awaitReloaded(
instances: Collection<Instance> = aem.instances,
reloadOptions: ReloadAction.() -> Unit = {},
awaitUpOptions: AwaitUpAction.() -> Unit = {}
) {
reload(instances, reloadOptions)
awaitUp(instances, awaitUpOptions)
}
fun reload(instance: Instance, options: ReloadAction.() -> Unit = {}) = reload(listOf(instance), options)
fun reload(instances: Collection<Instance> = aem.instances, options: ReloadAction.() -> Unit = {}) = ReloadAction(aem).apply(options).perform(instances)
fun check(instance: Instance, options: CheckAction.() -> Unit) = check(listOf(instance), options)
fun check(instances: Collection<Instance> = aem.instances, options: CheckAction.() -> Unit) = CheckAction(aem).apply(options).perform(instances)
fun examine(instance: Instance) = examine(listOf(instance))
/**
* Checks as much as it can be despite type of instance before performing any other operations.
*
* Assumes that instances are already running.
*/
fun examine(instances: Collection<Instance> = aem.instances) {
examinePrerequisites(instances)
examineAvailable(instances)
}
/**
* Checks if local instances defined are meeting prerequisites before performing any other operations.
*
* Assumes that instances could not be running yet.
*/
fun examinePrerequisites(instances: Collection<Instance> = aem.instances) {
val localInstances = instances.filterIsInstance<LocalInstance>()
if (localInstances.isNotEmpty()) {
local.examinePrerequisites(localInstances)
}
}
/**
* Checks if instances are available before performing any other operations.
*/
fun examineAvailable(instances: Collection<Instance> = aem.instances) {
val unavailable = instances.filter { !it.available }
if (unavailable.isNotEmpty()) {
throw InstanceException("Some instances (${unavailable.size}) are unavailable:\n" +
unavailable.joinToString("\n") { "Instance '${it.name}' at URL '${it.httpUrl}'" } + "\n\n" +
"Ensure having correct URLs defined, credentials correctly encoded and networking in correct state (internet accessible, VPN on/off)"
)
}
}
}