This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.gradle
398 lines (344 loc) · 13.3 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.constructor.Constructor
import org.yaml.snakeyaml.representer.Representer
import org.yaml.snakeyaml.resolver.Resolver
import org.yaml.snakeyaml.nodes.Tag
import java.util.regex.Pattern
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.yaml:snakeyaml:1.18'
}
}
plugins {
id 'groovy'
id 'idea'
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.11'
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
compile 'com.amazonaws:aws-lambda-java-core:1.1.0'
compile 'com.amazonaws:aws-lambda-java-events:1.3.0'
compile 'com.amazonaws:aws-java-sdk-lambda:1.11.125'
compile 'com.amazonaws:aws-java-sdk-kms:1.11.125'
compile 'com.amazonaws:aws-java-sdk-s3:1.11.125'
compile 'com.google.zxing:core:3.2.0'
compile 'com.google.zxing:javase:3.2.0'
compile 'org.apache.xmlgraphics:batik-transcoder:1.9'
compile 'org.apache.xmlgraphics:batik-codec:1.9'
compile 'org.apache.xmlgraphics:xmlgraphics-commons:2.2'
compile 'org.apache.xmlgraphics:fop:2.2'
compile 'org.twitter4j:twitter4j-core:4.0.4'
compile 'org.twitter4j:twitter4j-stream:4.0.4'
compile 'org.twitter4j:twitter4j-async:4.0.4'
compile 'org.twitter4j:twitter4j-media-support:4.0.4'
compile 'org.twitter4j:twitter4j-httpclient-support:2.2.6'
compile 'log4j:log4j:1.2.17'
compile 'org.gebish:geb-core:1.1.1'
compile 'net.sourceforge.htmlunit:htmlunit:2.27'
compile 'org.seleniumhq.selenium:htmlunit-driver:2.27'
compile 'org.seleniumhq.selenium:selenium-java:2.53.1'
compile 'org.seleniumhq.selenium:selenium-support:2.53.1'
testCompile 'junit:junit:4.12'
}
test {
systemProperties = System.getProperties().findAll { key, value ->
key.startsWith('latcraft')
} + project.properties.findAll { key, value ->
key.startsWith('latcraft')
} + gradle.properties.findAll { key, value ->
key.startsWith('latcraft')
}
}
ext {
forEachLambdaTask = { Closure cl ->
file('src/main/groovy/lv/latcraft/event/tasks').eachFile { File taskFile ->
if (taskFile.file && taskFile.name.endsWith('.groovy') && !taskFile.name.startsWith('BaseTask')) {
String[] elements = taskFile.name.replaceAll(/\.groovy$/, '').split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")
String className = elements.join('')
String functionName = elements.collect{ it.toLowerCase() }.join('_')
String functionDescription = elements.join(' ')
cl(taskFile, functionName, functionDescription, className)
}
}
}
}
forEachLambdaTask { File taskFile, String functionName, String functionDescription, String className ->
String taskName = className.substring(0, 1).toLowerCase() + className.substring(1)
task(taskName, type: JavaExec, group: "event manager") {
main = "lv.latcraft.event.tasks.${className}"
classpath = sourceSets.main.runtimeClasspath
}
}
task generateFunctionDefinitions {
group = 'code generation'
doLast {
String functionsTerraformContent = ""
List<String> functions = []
forEachLambdaTask { File taskFile, String functionName, String functionDescription, String className ->
functions << functionName
functionsTerraformContent += """
resource "aws_lambda_function" "${functionName}_function" {
s3_bucket = "\${aws_s3_bucket.latcraft_code.bucket}"
s3_key = "event-manager.zip"
function_name = "${functionName}_function"
description = "${functionDescription}"
role = "\${aws_iam_role.latcraft_lambda_executor.arn}"
handler = "\${var.lambda_code_package_prefix}.${className}::\${var.lambda_code_default_method}"
runtime = "java8"
memory_size = "512"
timeout = "300"
kms_key_arn = "\${aws_kms_key.latcraft_kms_key.arn}"
environment {
variables = {
HOME = "/var/task"
JAVA_FONTS = "/var/task/fonts"
}
}
}
resource "aws_lambda_alias" "${functionName}_function_alias" {
name = "${functionName}_function_latest"
function_name = "\${aws_lambda_function.${functionName}_function.arn}"
function_version = "\$LATEST"
}
"""
}
String functionArnList = (functions + "craftbot").collect { "\"\${aws_lambda_function.${it}_function.arn}\"" }.join(', ')
String lambdaPolicyTerraformContent = """
resource "aws_iam_role_policy" "latcraft_lambda_executor_policy" {
name = "latcraft_lambda_executor_policy"
role = "\${aws_iam_role.latcraft_lambda_executor.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:FilterLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"\${aws_s3_bucket.latcraft_images.arn}",
"\${aws_s3_bucket.latcraft_code.arn}"
]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl",
"s3:GetObject",
"s3:GetObjectAcl"
],
"Resource": [
"\${aws_s3_bucket.latcraft_images.arn}/*",
"\${aws_s3_bucket.latcraft_code.arn}/*"
]
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": [
${functionArnList}
]
}
]
}
EOF
}
"""
file('30_functions.tf').text = functionsTerraformContent + lambdaPolicyTerraformContent
}
}
task generateApiMethodDefinitions {
group = 'code generation'
doLast {
String apiMethodsTerraformContent = ""
List<String> functions = []
forEachLambdaTask { File taskFile, String functionName, String functionDescription, String methodName ->
functions << functionName
apiMethodsTerraformContent += """
resource "aws_lambda_permission" "${functionName}_function_api_gatewaypermission" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = "\${aws_lambda_function.${functionName}_function.arn}"
qualifier = "\${aws_lambda_alias.${functionName}_function_alias.name}"
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:\${var.aws_region}:\${data.aws_caller_identity.current.account_id}:\${aws_api_gateway_rest_api.latcraft_api.id}/*/POST/\${aws_api_gateway_resource.LatCraftAPI${methodName}.path_part}"
}
resource "aws_api_gateway_resource" "LatCraftAPI${methodName}" {
rest_api_id = "\${aws_api_gateway_rest_api.latcraft_api.id}"
parent_id = "\${aws_api_gateway_rest_api.latcraft_api.root_resource_id}"
path_part = "${functionName}"
}
resource "aws_api_gateway_method" "LatCraftAPI${methodName}POST" {
api_key_required = true
rest_api_id = "\${aws_api_gateway_rest_api.latcraft_api.id}"
resource_id = "\${aws_api_gateway_resource.LatCraftAPI${methodName}.id}"
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "LatCraftAPI${methodName}POSTIntegration" {
rest_api_id = "\${aws_api_gateway_rest_api.latcraft_api.id}"
resource_id = "\${aws_api_gateway_resource.LatCraftAPI${methodName}.id}"
http_method = "\${aws_api_gateway_method.LatCraftAPI${methodName}POST.http_method}"
integration_http_method = "POST"
type = "AWS"
credentials = "\${aws_iam_role.latcraft_api_executor.arn}"
uri = "arn:aws:apigateway:\${var.aws_region}:lambda:path/2015-03-31/functions/arn:aws:lambda:\${var.aws_region}:\${data.aws_caller_identity.current.account_id}:function:\${aws_lambda_function.${functionName}_function.function_name}/invocations"
}
resource "aws_api_gateway_method_response" "LatCraftAPI${methodName}POSTResponse" {
rest_api_id = "\${aws_api_gateway_rest_api.latcraft_api.id}"
resource_id = "\${aws_api_gateway_resource.LatCraftAPI${methodName}.id}"
http_method = "\${aws_api_gateway_method.LatCraftAPI${methodName}POST.http_method}"
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_method_response" "LatCraftAPI${methodName}POSTError" {
rest_api_id = "\${aws_api_gateway_rest_api.latcraft_api.id}"
resource_id = "\${aws_api_gateway_resource.LatCraftAPI${methodName}.id}"
http_method = "\${aws_api_gateway_method.LatCraftAPI${methodName}POST.http_method}"
status_code = "500"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration_response" "LatCraftAPI${methodName}POSTIntegrationResponse" {
rest_api_id = "\${aws_api_gateway_rest_api.latcraft_api.id}"
resource_id = "\${aws_api_gateway_resource.LatCraftAPI${methodName}.id}"
http_method = "\${aws_api_gateway_method.LatCraftAPI${methodName}POST.http_method}"
status_code = "200"
depends_on = [
"aws_api_gateway_integration.LatCraftAPI${methodName}POSTIntegration"
]
}
"""
}
String functionArnList = (functions + "craftbot").collect { "\"\${aws_lambda_function.${it}_function.arn}\"" }.join(', ')
String apiPolicyTerraformContent = """
resource "aws_iam_role_policy" "latcraft_api_executor_policy" {
name = "latcraft_api_executor_policy"
role = "\${aws_iam_role.latcraft_api_executor.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:FilterLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": [
${functionArnList}
]
}
]
}
EOF
}
"""
file('50_api_methods.tf').text = apiPolicyTerraformContent + apiMethodsTerraformContent
}
}
task generateUpdateCalls {
group = 'code generation'
doLast {
file('upload_code.sh').text = '''#!/usr/bin/env bash
export AWS_DEFAULT_REGION=eu-west-1
./gradlew clean build
aws s3 cp ./build/distributions/*.zip s3://latcraft-code/event-manager.zip
'''
forEachLambdaTask { File taskFile, String functionName, String functionDescription, String className ->
file('upload_code.sh') << "aws lambda update-function-code --function-name ${functionName}_function --s3-bucket latcraft-code --s3-key event-manager.zip \n"
}
file('upload_code.sh') << "aws lambda update-function-code --function-name craftbot_function --s3-bucket latcraft-code --s3-key event-manager.zip \n\n"
def CR = String.valueOf(Character.toChars(0x0D))
def LF = String.valueOf(Character.toChars(0x0A))
file('upload_code.sh').text = file('upload_code.sh').text.replace(CR + LF, LF)
}
}
task generateTravisAfterDeploy {
group = 'code generation'
doLast {
LoaderOptions loaderOptions = new LoaderOptions()
loaderOptions.setAllowDuplicateKeys(false)
DumperOptions dumperOptions = new DumperOptions()
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN)
dumperOptions.setIndent(2)
dumperOptions.setWidth(500)
dumperOptions.setSplitLines(false)
Yaml yaml = new Yaml(new Constructor(), new Representer(), dumperOptions, loaderOptions, new Resolver())
def travis = yaml.load(file('.travis.yml').newInputStream())
List afterDeploy = []
project.forEachLambdaTask { File taskFile, String functionName, String functionDescription, String className ->
afterDeploy.add("aws lambda update-function-code --function-name ${functionName}_function --s3-bucket latcraft-code --s3-key event-manager.zip".toString())
}
afterDeploy.add("aws lambda update-function-code --function-name craftbot_function --s3-bucket latcraft-code --s3-key event-manager.zip".toString())
travis['after_deploy'] = afterDeploy
file('.travis.yml').text = yaml.dump(travis)
}
}
compileGroovy.dependsOn generateApiMethodDefinitions
compileGroovy.dependsOn generateFunctionDefinitions
compileGroovy.dependsOn generateTravisAfterDeploy
compileGroovy.dependsOn generateUpdateCalls
task buildZip(type: Zip, dependsOn: [classes, generateApiMethodDefinitions, generateFunctionDefinitions]) {
from compileGroovy
from processResources
into ('fonts') {
from file('src/main/resources/fonts')
}
into('lib') {
from configurations.runtime
}
}
build.dependsOn buildZip