-
Notifications
You must be signed in to change notification settings - Fork 32
/
ServiceComposer.kt
141 lines (122 loc) · 4.34 KB
/
ServiceComposer.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
package com.cognifide.gradle.aem.common.instance
import com.cognifide.gradle.aem.common.file.FileOperations
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils
import org.gradle.internal.os.OperatingSystem
import java.io.File
/**
* System service related options.
*/
class ServiceComposer(val manager: LocalInstanceManager) {
private val aem = manager.aem
private val logger = aem.logger
/**
* Path in which local service config files and scripts will be generated.
*/
val configDir = aem.obj.dir {
convention(manager.rootDir.dir("service"))
aem.prop.file("localInstance.service.configDir")?.let { set(it) }
}
val overrideDir = aem.obj.dir {
convention(manager.configDir.dir("service"))
aem.prop.file("localInstance.service.overrideDir")?.let { set(it) }
}
val executableFiles = aem.obj.strings {
set(listOf("*.sh"))
}
/**
* System user used to run instance.
*/
val user = aem.obj.string {
convention("aem")
aem.prop.string("localInstance.service.user")?.let { set(it) }
}
/**
* System group of user used to run instance.
*/
val group = aem.obj.string {
convention("aem")
aem.prop.string("localInstance.service.group")?.let { set(it) }
}
/**
* Controls number of file descriptors allowed.
*/
val limitNoFile = aem.obj.int {
convention(20000)
aem.prop.int("localInstance.service.limitNoFile")?.let { set(it) }
}
/**
* Environment variables loader command.
*/
val environmentCommand = aem.obj.string {
convention(". /etc/profile")
aem.prop.string("localInstance.service.environmentCommand")?.let { set(it) }
}
val projectPath get() = StringUtils.appendIfMissing(aem.project.path, ":")
/**
* Build command relative to root project used to start process which runs AEM instance as a system service.
*/
val startCommand = aem.obj.string {
convention("sh gradlew -i -s --console=plain ${projectPath}instanceUp")
aem.prop.string("localInstance.service.startCommand")?.let { set(it) }
}
/**
* Build command relative to root project used to stop process which runs AEM instance as a system service.
*/
val stopCommand = aem.obj.string {
convention("sh gradlew -i -s --console=plain ${projectPath}instanceDown")
aem.prop.string("localInstance.service.stopCommand")?.let { set(it) }
}
/**
* Build command relative to root project used to describe status of AEM instance running as a system service.
*/
val statusCommand = aem.obj.string {
convention("sh gradlew -q --console=plain ${projectPath}instanceStatus")
aem.prop.string("localInstance.service.statusCommand")?.let { set(it) }
}
/**
* Effective values (shorthand)
*/
val opts get() = mapOf(
"dir" to configDir.get().asFile,
"user" to user.orNull,
"group" to group.orNull,
"limitNoFile" to limitNoFile.orNull,
"environmentCommand" to environmentCommand.orNull,
"startCommand" to startCommand.orNull,
"stopCommand" to stopCommand.orNull,
"statusCommand" to statusCommand.orNull
)
fun compose() {
val dir = configDir.get().asFile.apply {
deleteRecursively()
mkdirs()
}
aem.assetManager.copyDir(LocalInstance.SERVICE_PATH, dir)
if (overrideDir.get().asFile.exists()) {
FileUtils.copyDirectory(overrideDir.get().asFile, dir)
}
val props = mapOf("service" to this)
aem.project.fileTree(dir).forEach { file ->
FileOperations.amendFile(file) { content ->
aem.prop.expand(content, props, file.absolutePath)
}
}
makeExecutable(dir)
}
@Suppress("TooGenericExceptionCaught")
private fun makeExecutable(dir: File) {
if (OperatingSystem.current().isWindows) {
return
}
aem.project.fileTree(dir)
.matching { it.include(executableFiles.get()) }
.forEach { file ->
try {
FileOperations.makeExecutable(file)
} catch (e: Exception) {
logger.warn("Cannot make file '$file' executable!", e)
}
}
}
}