-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.gradle
269 lines (227 loc) · 7.56 KB
/
build.gradle
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import groovyx.net.http.OkHttpEncoders
import groovyx.net.http.FromServer
import static groovyx.net.http.HttpBuilder.configure
import static groovyx.net.http.MultipartContent.multipart
import static groovyx.net.http.util.SslUtils.ignoreSslIssues
apply plugin: 'java'
def javaVersion = JavaVersion.VERSION_1_8
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
classpath 'io.github.http-builder-ng:http-builder-ng-okhttp:1.0.3'
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.2'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
subprojects {
apply from: "$rootDir/gradle/dependencies.gradle"
apply from: "$rootDir/gradle/integration.gradle"
configurations.all {
resolutionStrategy {
depLibs.each { k, v -> force(v) }
}
}
repositories {
jcenter()
maven {
name "Splunk Mirror - External"
url "https://splunk.jfrog.io/artifactory/maven-splunk-release/"
}
}
if (JavaVersion.current() != javaVersion) {
throw new GradleException("Wrong Java version: required is "
+ javaVersion + ", but found " + JavaVersion.current())
}
}
task getPlugins() {
doLast {
def token = null
if (project.properties.get('SCLOUD_TOKEN_FILE')?.trim()) {
token = readToken()
}
def http = getHttpConfig()
http.get {
request.uri.path = getUploadServicePath()
if (token != null) {
request.headers['Authorization'] = "Bearer ${token}"
}
response.success { fromServer, body ->
println body
}
response.failure { fromServer, body ->
def msg = httpErrorMsg("Error fetching plugins", fromServer, body)
throw new GradleException(msg)
}
}
}
}
task registerPlugin() {
doLast {
def token = null
if (project.properties.get('SCLOUD_TOKEN_FILE')?.trim()) {
token = readToken()
}
def http = getHttpConfig()
def name = project.properties.get('SDK_PLUGIN_NAME')
def desc = project.properties.get('SDK_PLUGIN_DESC') ?: ''
http.post {
request.uri.path = getUploadServicePath()
request.contentType = 'application/json'
if (token != null) {
request.headers['Authorization'] = "Bearer ${token}"
}
request.body = "{\"name\": \"${name}\", \"description\": \"${desc}\"}"
response.success { fromServer, body ->
println "Registered plugin: ${name}. Response: ${body}"
}
response.failure { fromServer, body ->
def msg = httpErrorMsg("Error registering plugin ${name}", fromServer, body)
throw new GradleException(msg)
}
}
}
}
task uploadPlugin() {
doLast {
def pluginIdProperty = 'PLUGIN_ID'
def pluginModule = 'PLUGIN_MODULE'
if (!project.hasProperty(pluginIdProperty)) {
throw new InvalidUserDataException("Must specify plugin id via gradle property: ${pluginIdProperty}")
}
def id = project.properties.get(pluginIdProperty)
def module = "dsp-plugin-functions"
if (project.hasProperty(pluginModule)) {
module = project.properties.get(pluginModule)
}
File plugin = new File("${module}/build/libs/${module}.jar")
def token = null
if (project.properties.get('SCLOUD_TOKEN_FILE')?.trim()) {
token = readToken()
}
def http = getHttpConfig()
http.post {
request.uri.path = "${getUploadServicePath()}/${id}/upload"
request.contentType = 'multipart/form-data'
if (token != null) {
request.headers['Authorization'] = "Bearer ${token}"
}
request.body = multipart {
part 'pluginJar', 'data-pipelines-plugin-template.jar', 'application/octet-stream', plugin
}
request.encoder 'multipart/form-data', OkHttpEncoders.&multipart
response.success { fromServer, body ->
println "Uploaded jar file for plugin ID: ${id}"
}
response.failure { fromServer, body ->
def msg = httpErrorMsg("Error uploading plugin ${name}", fromServer, body)
throw new GradleException(msg)
}
}
}
}
task deletePlugin() {
doLast {
def pluginIdProperty = 'PLUGIN_ID'
if (!project.hasProperty(pluginIdProperty)) {
throw new InvalidUserDataException("Must specify plugin id via gradle property: ${pluginIdProperty}")
}
def id = project.properties.get(pluginIdProperty)
def token = null
if (project.properties.get('SCLOUD_TOKEN_FILE')?.trim()) {
token = readToken()
}
def http = getHttpConfig()
http.delete {
request.uri.path = "${getUploadServicePath()}/${id}"
if (token != null) {
request.headers['Authorization'] = "Bearer ${token}"
}
response.success { fromServer, body ->
println "Deleted plugin with ID: ${id}"
}
response.failure { fromServer, body ->
def msg = httpErrorMsg("Error deleting plugin with ID: ${id}", fromServer, body)
throw new GradleException(msg)
}
}
}
}
task createPluginJar(dependsOn: shadowJar) {
group 'build'
description 'Create a plugin jar packaged with dependencies'
}
// Run this task to create initial project template.
task expandTemplates(type: Copy) {
group 'templates'
description 'Perform initial template expansion. Will overwrite any existing source.'
// expands files from the given path to the src directory
// now allows multiple functions in one plugin
from ("${project.rootDir}/${project.properties.get('SDK_FUNCTIONS_PATH')}/src") {
expand(project.properties)
}
into "${project.rootDir}/dsp-plugin-functions/src"
outputs.upToDateWhen { false }
}
ext.renameJavaFile = { String dir, String templateName, String newFilename ->
if (!dir.endsWith('/')) {
throw new InvalidUserDataException("directory should end in slash: " + dir)
}
file(dir+templateName).renameTo(file(dir+newFilename))
}
ext.addLine = { String filename, String newLine ->
def lineFound = false
def file = new File(filename)
file.createNewFile() // create file if doesn't exist
file.readLines().each { line ->
if (line.trim() == newLine) {
lineFound = true
}
}
if (!lineFound) {
file.append(newLine+"\n")
}
}
ext.readToken = {
String tokenFileProperty = "SCLOUD_TOKEN_FILE"
if (!project.hasProperty(tokenFileProperty)) {
throw new InvalidUserDataException("Missing required gradle property: ${tokenFileProperty}")
}
String fileName = project.properties.get(tokenFileProperty)
File tokenFile = new File(fileName)
if (tokenFile.text.isEmpty()) {
throw new InvalidUserDataException("Token file is empty. File: ${fileName}")
}
return tokenFile.text.trim()
}
ext.httpErrorMsg = { String msg, FromServer fs, Object body ->
return "${msg}. Status code: ${fs.getStatusCode().toString()}. Response: ${body}"
}
ext.getHttpConfig = {
def insecure = project.properties.get('PLUGIN_UPLOAD_INSECURE')
if (insecure.toString().toBoolean()) {
return configure {
ignoreSslIssues execution
request.uri = getUploadServiceURI()
}
} else {
return configure {
request.uri = getUploadServiceURI()
}
}
}
ext.getUploadServiceURI = {
def proto = project.properties.get('PLUGIN_UPLOAD_SERVICE_PROTOCOL')
def host = project.properties.get('PLUGIN_UPLOAD_SERVICE_HOST')
def port = project.properties.get('PLUGIN_UPLOAD_SERVICE_PORT')
return "${proto}://${host}:${port}/"
}
ext.getUploadServicePath = {
def tenant = project.properties.get('TENANT_NAME') ?: 'default'
def endpoint = project.properties.get('PLUGIN_UPLOAD_SERVICE_ENDPOINT')
return "/${tenant}/${endpoint}"
}